Skip to content

Commit 0f9eed1

Browse files
Update 100+ Python challenging programming exercises.txt
FizzBuzz is a very common interview question, however, it is not difficult and could be solved by a beginner learner.
1 parent 603eea8 commit 0f9eed1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

β€Ž100+ Python challenging programming exercises.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,7 @@ solutions=solve(numheads,numlegs)
23712371
print solutions
23722372

23732373
#----------------------------------------#
2374+
Level: Easy
23742375
Question:
23752376
Write a Person class with an instance variable, age, and a constructor that takes an integer, initialAge, as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as initialAge, the constructor should set age to 0 and print Age is not valid, setting age to 0. In addition, you must write the following instance methods:
23762377

@@ -2412,5 +2413,29 @@ for i in range(0, t):
24122413
p.amIOld()
24132414
print("")
24142415
#----------------------------------------#
2416+
Level: Easy
2417+
Question:
2418+
Suppose we have a number n. We have to display a string representation of all numbers from 1 to n, but there are some constraints.
2419+
2420+
1. If the number is divisible by 3, write "Fizz" instead of the number
2421+
2. If the number is divisible by 5, write "Buzz" instead of the number
2422+
3. If the number is divisible by 3 and 5 both, write "FizzBuzz" instead of the number
24152423

2424+
(Credit: Common interview question)
24162425

2426+
Hints: Use if-elif-else statements to split the problem into sections.
2427+
2428+
Answer:
2429+
2430+
def fizzbuzz(n):
2431+
for a in range(1,n+1):
2432+
if a % 3 == 0 and a % 5 == 0:
2433+
print('FizzBuzz')
2434+
elif a % 3 == 0:
2435+
print('Fizz')
2436+
elif a % 5 == 0:
2437+
print('Buzz')
2438+
else:
2439+
print(a)
2440+
fizzbuzz(n)
2441+
#----------------------------------------#

0 commit comments

Comments
 (0)