Py
Py
sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
5)Iterative
if( n == 0):
return 0
else:
x=0
y=1
for i in range(1,n):
z = (x + y)
x=y
y=z
return y
for i in range(10):
print (fib(i))
6) Write a program that prompts the user
to enter a list of words and stores in a
list only those words whose first letter
occurs again within the word (for
example, 'Baboon'). The program should
display the resulting list.
req_list =[]
i=0
n = int(input(“Enter the number of words to be
entered: “))
while i<n:
input_string = input(“Enter the word: “)
lowercase_input_string = input_string.lower()
input_first_letter = lowercase_input_string[0]
rem_string = lowercase_input_string[1:]
res = rem_string.find(input_first_letter)
if res!= -1:
req_list.append(input_string)
i += 1
7)Write a
version of
a
palindrome
recognizer
that also
accepts
phrase palindromes such as
"Go hang a salami I'm a
lasagna hog.",
"Was it a rat I saw?", "Step
on no pets", "Sit on a
potato pan, Otis",
"Lisa Bonet ate no basil",
"Satan, oscillate my
metallic sonatas",
"I roamed under it as a
tired nude Maori", "Rise to
vote sir", or
the exclamation "Dammit, I'm
mad!". Note that
punctuation, capitalization,
and spacing are usually
ignored."""
import string
ignored = string.punctuation
+ " "
def is_palindrome(str):
cleanstr = ""
for i in str:
cleanstr += "" if i in
ignored else i # I love
Python ternary operator
return cleanstr.lower() ==
cleanstr[::-1].lower()
print(make_ing_form('lie'))
print(make_ing_form('see'))
print(make_ing_form('move'))
print(make_ing_form('hug'))
9) Define a procedure histogram()
that takes a list of integers and prints
a histogram to the screen. For
example, histogram([4, 9, 7]) should
print the following:
def histogram( items ):
for n in items:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)
histogram([2, 3, 6, 5])
return True
if x:
print("Not a Valid Password")
def
map_to_leng
ths_for(wor
ds):
lengths = []
for word in words:
lengths.append(len(word))
return lengths
def
map_to_lengths_map(words):
return map(len, words)
def
map_to_lengths_lists(words)
:
return [len(word) for
word in words]
if __name__ == "__main__":
words = ['abv', 'try
me', 'test']
print
map_to_lengths_for(words)
print
map_to_lengths_map(words)
print
map_to_lengths_lists(words)
people_info = []
while True:
individual_info =
input()
if individual_info
== "":
break
else:
people_info.append(tupl
e((individual_info.spli
t(","))))
people_info.sort(key =
itemgetter(0, 1, 2))
print(people_info)