Can you write algorithm and comment for the fllowing python code. the form of al
ID: 3719051 • Letter: C
Question
Can you write algorithm and comment for the fllowing python code. the form of algorithm and comment show in the picture. Thanks!
Code:
def deslang(slang, wordDictionary):
s=''
for i in slang.split(' '):
if i in wordDictionary:
i=wordDictionary[i]
s+=i+' '
return s[:-1]
def update_dictionary(file,dict):
try:
for i in open(file):
s=i.split(',')
dict[s[0]]=s[1]
print(file+" loaded successfully.")
except:
print(file+" does not exist.")
print("The dictionary has "+str(len(dict))+" entries.")
return dict
abgorithm: Prints Message based on iteration n 1. Take value n passed to the function 2. Iterate from 0 to n (inclusive) 3. For Each iteration n 4. if i is divisible by both 3 and 5, print FizzBuzz 5. if i is only divisible by 3, print Fizz 6. if i is only divisible by 5, print Buzz 7. else print the number Parameters n Returns: Nothing def fizz buzz(n): for i in range (e, n+1): # iterate from 0 to n inclusive # if i is 0, print 0 elíf í % 3 0 and ¡ % 5-0: # if 1 1s divisible by both 3 and 5 print FizzBuzz elif ? % 3-0: # if i is only divisible by 3, print Fizz elif í % 5- 0: # if I ls only divisible by 5, print Buzz print (0) print ('FizzBuzz')#output message print ('Fizz') print( 'Buzz') print(i) #output message #output message # else print number itself #output message #test case #test case else: fizz_buzz(5) fizz_buzz (15)Explanation / Answer
def fizzbuzz(n):
if n % 3 == 0 and n % 5 == 0:
return 'FizzBuzz'
elif n % 3 == 0:
return 'Fizz'
elif n % 5 == 0:
return 'Buzz'
else:
return str(n)
print " ".join(fizzbuzz(n) for n in xrange(1, 21))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.