18 March 2012

CodeEval FizzBuzz

CodeEval is a site where programmers can compete for jobs or in my case hopefully learn something more. I have small experience in Java and Visual Basic. I have slightly more experience in HTML, JavaScript and PHP. So I use this site for practise.
The 1st problem they put on there is FizzBuzz, the problem is below:

Description

Players generally sit in a circle. The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by 'A' e.g. three is replaced by the word fizz and any divisible by 'B' e.g. five by the word buzz. Numbers divisible by both become fizz buzz. A player who hesitates or makes a mistake is either eliminated.

Write a program that prints out the the pattern generated by such a scenario given the values of 'A'/'B' and 'N' which are read from an input text file. The input text file contains three space delimited numbers i.e. A, B, N. The program should then print out the final series of numbers using 'F' for fizz, 'B' for 'buzz' and 'FB' for fizz buzz.

Input sample:

Your program should read an input file (provided on the command line) which contains multiple newline separated lines. Each line will contain 3 numbers which are space delimited. The first number is first number to divide by ('A' in this example), the second number is the second number to divide by ('B' in this example) and the third number is where you should count till ('N' in this example). You may assume that the input file is formatted correctly and is the numbers are valid positive integers.e.g.

3 5 10
2 7 15

Output sample:

Print out the series 1 through N replacing numbers divisible by 'A' by F, numbers divisible by 'B' by B and numbers divisible by both as 'FB'. Since the input file contains multiple sets of values, your output will print out one line per set. Ensure that there are no trailing empty spaces on each line you print.e.g.

1 2 F 4 B F 7 8 F B
1 F 3 F 5 F B F 9 F 11 F 13 FB 15

Unfortunately when I did the Java solution I couldn't do it, so I searched for the solution online. DO NOT CLICK LOOK UNLESS YOU HAVE TRIED IT FIRST
/*Sample code to read in test cases:
public class fizzbuzz {
    public static void main (String[] args) {
    ...
    File file = new File(args[0]);
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line;
    while ((line = in.readLine()) != null) {
        String[] lineArray = line.split("\s");
        if (lineArray.length > 0) {
            //Process line of input Here
        }
    }
  }
}
*/
import java.io.*;

import java.util.*;

public class fizzbuzz {

    public static void main(String[] args) throws IOException{

        Scanner console = new Scanner(new FileReader(args[0]));
        while(console.hasNext()){
            int first=console.nextInt();
            int second =console.nextInt();
            int third =console.nextInt();

            for(int i=1;i<=third;i++){
                if(i%first==0 && i%second==0 ){
                    System.out.printf("FB ");
                }
                else if(i%first==0)
                    System.out.printf("F ");
                else if(i%second==0)
                    System.out.printf("B " );
                else
                    System.out.printf("%d ",i);
            }
            System.out.println();
        }
        console.close();
    }
}
The last bit of the code I can do, it's just I couldn't read a file.
I also decided to see if I could do it in PHP as well
<?php
 /*Sample code to read in test cases:
 $fh = fopen($argv[1], "r");
 while (true) {
 $test = fgets($fh);
 # break loop if $test is an empty line
 # $test represents the test case, do something with it
 }
 */
    $file_get_contents=fopen($argv[1],"r");
     while ( ($line = fgets($file_get_contents)) !== false) {
     $k=0;
           foreach(preg_split("/[\s]/", $line) as $space){
              if($k==0){
                  $f=$space;
               }
                if($k==1){
                  $b=$space;
                }
               if($k==2){
                   $n=$space;
                }
                $k=$k+1;
            }
           for($i=1;$i<=$n;$i++){
                if(($i % $f==0)&&($i % $b==0)){
                 echo "FB ";
                }
                elseif($i % $f==0){
              echo "F ";
             }
                elseif($i % $b==0){
                 echo "B ";
                }
                else{
                 echo $i." ";
                }
            }
            echo "\n";
        }
 ?>
Good tip they do provide comments to help you through it, which is rather handy. Anyway I also tried to do it in JavaScript:
/*Sample code to read in test cases:
function codeEvalExecute(line) 
{ //your code goes here. line is your test case 
//return 
}*/
function codeEvalExecute(line){
 //an array with 3 values
 var Byline=line.split(" ");
 //fizz
 var f=Byline[0];
 //buzz
 var b=Byline[1];
 //number run to
 var n=Byline[2];
 var string="";
 
 for(var i=1;i<=n;i++){
  if((i % f==0)&&(i % b==0)){
          string=string+"FB ";
        }
        else if(i % f==0){
           string=string+"F ";
       }
        else if(i % b==0){
            string=string+"B ";
        }
        else{
           string=string+i+" ";
       }
 }
 return string;
}
Update 18/03/2012 11.59am
I should point out if you want to learn JavaScript or PHP I suggest w3 school as for Java I don't know any sites so put some in the comments if you have a suggestion

1 comment:

  1. Please don't suggest W3 Schools for learning. The site is rife with errors and overall is fairly dated. CodeAcademy is a much better alternative. Nice work on CodeEval!

    ReplyDelete