From 603eea83323a6f54a567d2a9b9825b40b130271b Mon Sep 17 00:00:00 2001 From: Rohit Barua <83600150+Rohit-beep-droid@users.noreply.github.com> Date: Sun, 23 May 2021 13:22:13 -0400 Subject: [PATCH 1/2] Update 100+ Python challenging programming exercises.txt Added an easy HackerRank challenge from its 30 days of Code challenges. --- ...thon challenging programming exercises.txt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 97af5aaf..e1bde956 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -2371,5 +2371,46 @@ solutions=solve(numheads,numlegs) print solutions #----------------------------------------# +Question: +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: + +1. yearPasses() should increase the instance variable by 1. +2. amIOld() should perform the following conditional actions: + * If age < 13, print "You are young." + * If age > or equal to 13 and age < or equal to 18, print "You are a teenager." + * Otherwise, print "You are old." + +Hints: Use if-elif-else, class Object, and def __init__(self,attribute). + +(Credit: HackerRank (30 days of Code)) + +Answer: + +class Person: + def __init__(self,initialAge): + self.age = initialAge + def amIOld(self): + if self.age<13: + if self.age<0: + self.age=0 + print("Age is not valid, setting age to 0.") + print("You are young.") + elif 13<=self.age<=18: + print("You are a teenager.") + else: + print("You are old.") + def yearPasses(self): + self.age+=1 + +t = int(input()) +for i in range(0, t): + age = int(input()) + p = Person(age) + p.amIOld() + for j in range(0, 3): + p.yearPasses() + p.amIOld() + print("") +#----------------------------------------# From 0f9eed1e02ebc4c65a8c69de060de6f0feabd117 Mon Sep 17 00:00:00 2001 From: Rohit Barua <83600150+Rohit-beep-droid@users.noreply.github.com> Date: Sun, 23 May 2021 17:50:37 -0400 Subject: [PATCH 2/2] 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. --- ...thon challenging programming exercises.txt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index e1bde956..896cf196 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -2371,6 +2371,7 @@ solutions=solve(numheads,numlegs) print solutions #----------------------------------------# +Level: Easy Question: 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: @@ -2412,5 +2413,29 @@ for i in range(0, t): p.amIOld() print("") #----------------------------------------# +Level: Easy +Question: +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. + +1. If the number is divisible by 3, write "Fizz" instead of the number +2. If the number is divisible by 5, write "Buzz" instead of the number +3. If the number is divisible by 3 and 5 both, write "FizzBuzz" instead of the number +(Credit: Common interview question) +Hints: Use if-elif-else statements to split the problem into sections. + +Answer: + +def fizzbuzz(n): + for a in range(1,n+1): + if a % 3 == 0 and a % 5 == 0: + print('FizzBuzz') + elif a % 3 == 0: + print('Fizz') + elif a % 5 == 0: + print('Buzz') + else: + print(a) +fizzbuzz(n) +#----------------------------------------#