patternpythonMinor
FizzBuzz for CodeEval
Viewed 0 times
codeevalforfizzbuzz
Problem
I'm a beginner programmer and decided to try some coding challenges. I found CodeEval and attempted the first challenge, FizzBuzz. However upon submitting my code I found that my submission only partially solved the problem.
Link to the challenge
Challenge Description:
Players generally sit in a circle. The first player says the number
“1”, and each player says next number in turn. However, any number
divisible by X (for example, three) is replaced by the word fizz, and
any divisible by Y (for example, five) by the word buzz. Numbers
divisible by both become fizz buzz. A player who hesitates, or makes a
mistake is eliminated from the game.
Write a program that prints out the final series of numbers where
those divisible by X, Y and both are replaced by “F” for fizz, “B” for
buzz and “FB” for fizz buzz.
Input Sample:
Your program should accept a file as its first argument. The file
contains multiple separated lines; each line contains 3 numbers that
are space delimited. The first number is the first divider (X), the
second number is the second divider (Y), and the third number is how
far you should count (N). You may assume that the input file is
formatted correctly and the numbers are valid positive integers.
For Example:
1) 3 5 10
2) 2 7 15
Output Sample:
Print out the series 1 through N replacing numbers divisible by X with
“F”, numbers divisible by Y with “B” and numbers divisible by both
with “FB”. Since the input file contains multiple sets of values, your
output should print out one line per set. Ensure that there are no
trailing empty spaces in each line you print.
1) 1 2 F 4 B F 7 8 F B
2) 1 F 3 F 5 F B F 9 F 11 F 13 FB 15
How can I improve my code speed-wise and memory-wise so that I can fully complete this challenge?
The code is working for my test cases.
```
import sys
def main(name_file):
_test_cases = open(name_file, 'r')
for test in _test
Link to the challenge
Challenge Description:
Players generally sit in a circle. The first player says the number
“1”, and each player says next number in turn. However, any number
divisible by X (for example, three) is replaced by the word fizz, and
any divisible by Y (for example, five) by the word buzz. Numbers
divisible by both become fizz buzz. A player who hesitates, or makes a
mistake is eliminated from the game.
Write a program that prints out the final series of numbers where
those divisible by X, Y and both are replaced by “F” for fizz, “B” for
buzz and “FB” for fizz buzz.
Input Sample:
Your program should accept a file as its first argument. The file
contains multiple separated lines; each line contains 3 numbers that
are space delimited. The first number is the first divider (X), the
second number is the second divider (Y), and the third number is how
far you should count (N). You may assume that the input file is
formatted correctly and the numbers are valid positive integers.
For Example:
1) 3 5 10
2) 2 7 15
Output Sample:
Print out the series 1 through N replacing numbers divisible by X with
“F”, numbers divisible by Y with “B” and numbers divisible by both
with “FB”. Since the input file contains multiple sets of values, your
output should print out one line per set. Ensure that there are no
trailing empty spaces in each line you print.
1) 1 2 F 4 B F 7 8 F B
2) 1 F 3 F 5 F B F 9 F 11 F 13 FB 15
How can I improve my code speed-wise and memory-wise so that I can fully complete this challenge?
The code is working for my test cases.
```
import sys
def main(name_file):
_test_cases = open(name_file, 'r')
for test in _test
Solution
There is a bug in your code for those times when the two input values are not prime.
Consider an instance when the input values are 4 and 6. In this case, your code will output F for multiples of 4, and B for multiples of 6, and FB for multiples of 24.... great... but, is it? No, 12 is a multiple of both, but will only print "F ".
If you want to use the optimized cascading-if-else version of FizzBuzz, then you need to ensure the inputs are both prime, or have no common factors.
It would often still be faster to compute the common factors and then after that do the system you have.
Consider an instance when the input values are 4 and 6. In this case, your code will output F for multiples of 4, and B for multiples of 6, and FB for multiples of 24.... great... but, is it? No, 12 is a multiple of both, but will only print "F ".
If you want to use the optimized cascading-if-else version of FizzBuzz, then you need to ensure the inputs are both prime, or have no common factors.
It would often still be faster to compute the common factors and then after that do the system you have.
Context
StackExchange Code Review Q#105845, answer score: 8
Revisions (0)
No revisions yet.