Skip to content

Instantly share code, notes, and snippets.

@qmmr
Last active June 29, 2021 05:01
Show Gist options
  • Save qmmr/6b57cde20d37319f79ff5bc10d59c84b to your computer and use it in GitHub Desktop.
Save qmmr/6b57cde20d37319f79ff5bc10d59c84b to your computer and use it in GitHub Desktop.
Python Home Assignment #5: Basic Loops
"""
pirple.com/python
Python Home Assignment #5: Basic Loops
You're about to do an assignment called "Fizz Buzz", which is one of the classic programming challenges.
It is a favorite for interviewers, and a shocking number of job-applicants can't get it right.
But you won't be one of those people. Here are the rules for the assignment (as specified by Imran Gory):
- Write a program that prints the numbers from 1 to 100.
- But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
- For numbers which are multiples of both three and five print "FizzBuzz".
The one hint you'll likely need is:
There is a Python operator called "modulo", represented by the percentage sign (%) that gives you the remainder left over after division. So for example:
6 % 3 Equals zero. Because after dividing 6 by 3, there is zero leftover. Whereas:
6 % 4 Equals 2. Because after dividing 6 by 4, there are 2 left over from the six.
If that was confusing, don't worry. It will make more sense as you use it. The point is: the modulo operator is useful for finding out if X is a multiple of Y. If it is, then X % Y will yield zero. Knowing this should help you complete this assignment without any issue.
Extra Credit:
Instead of only printing "fizz", "buzz", and "fizzbuzz", add a fourth print statement: "prime".
You should print this whenever you encounter a number that is prime (divisible only by itself and one).
As you implement this, don't worry about the efficiency of the algorithm you use to check for primes.
It's okay for it to be slow.
"""
# Instead of checking if the number is prime we may cheat here a liitle :)
# and simply store the first 25 prime numbers (all the prime numbers less than 100)
# However, if needed, we may swap this function with one from the answers in
# https://stackoverflow.com/questions/1801391/what-is-the-best-algorithm-for-checking-if-a-number-is-prime
def isPrime(n):
return n in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
# FizzBuzz loop
for counter in range(1, 101):
output = ""
if isPrime(counter):
output += "Prime"
else:
if counter % 3 == 0:
output += "Fizz"
if counter % 5 == 0:
output += "Buzz"
print(output if len(output) else counter)
@Galiciacd2010
Copy link

Thank You for valuable information

@kimogele
Copy link

Thank you

@anonymousincognito
Copy link

def FizzBuzz(number): primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] if number in primeNumbers: print("prime") if number % 3 == 0 and number % 5 == 0: print("FizzBuzz") if number % 3 == 0: print("Fizz") if number % 5 == 0: print("Buzz") else: print(number) FizzBuzz(3)
What's wrong with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment