Académique Documents
Professionnel Documents
Culture Documents
return a*b
>>> generate_n_chars(5,"y")
'yyyyy'
2-The function max() from exercise 1) and the function max_of_three()
from exercise 2) will only work for two and three numbers,
respectively. But suppose we have a much larger number of numbers,
or suppose we cannot tell in advance how many they are? Write a
function max_in_list() that takes a list of numbers and returns the
largest one.
>>> def max_in_list( list ):
max=list[0]
for i in list:
if i > max:
max = i
return max
>>> print(max_in_list([5,3,1,-6]))
5
3- Write a program that maps a list of words intoa list of integers representing the lengths of the
correponding words.
>>> def len_words(arr):
temp=[]
for i in arr:
temp.append(len(i))
return temp
Riya
Aparna
Isha
>>> print(arr)
>>> print(len_words(arr))
[4, 6, 4]
>>>
4-Write a function find_longest_word() that takes a list of words and
returns the length of the longest one. Modify the same to do with
lambda expression.
max=len(arr[0])
temp=arr[0]
for i in arr:
if (len(i)>max):
max=len(i)
temp=i
print(temp+",",max)
Riya
Aparna
Isha
>>> find_longest_word(arr)
Aparna, 6
5- Write a function filter_long_words() that takes a list of words and an integer n and returns the
list of words that are longer than n. Modify the same to do with lambda expression.
>>> def filter_long_words(arr,a):
for i in range(0,len(arr)):
if (len(arr[i])>a):
print(arr[i])
Riya
Riya Aparna
>>> filter_long_words(arr,a)
Riya Aparna
>>>
6- 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'mmad!". Note that
punctuation, capitalization, and spacing are usually ignored.
>>> def palindrome(str):
l=0
h=len(str)-1
s=str.lower()
while(l<=h):
l += 1
h -= 1
elif (s[l]==s[h]):
l += 1
h -= 1
else:
return False
return True
>>> if(palindrome(str)==True):
print("It is palindrome")
It is palindrome
>>>
7- A pangram is a sentence that contains all the letters of the English alphabet at least once, for
example: The quick brown fox jumps over the lazy dog. Your task here is to write a function to
check a sentence to see if it is a pangram or not.
>>> import string
alpha="abcdefghijklmnopqrstuvwxyz"
for i in alpha:
if i not in str.lower():
return False
return True
Enter the string: The quick brown fox jumps over the lazy dog
>>> if(pangram(str)==True):
print("Yes,it is pangram")
Yes,it is pangram
>>>
"merry":"god",
"christmas":"jul",
"and":"och",
"and":"och",
"happy":"gott",
"new":"nytt",
"year":"ar"
}
if i in str:
str=str.replace(i,my_dict[i])
>>> print(str)
>>>
9- Write a function char_freq() that takes a string and builds a frequency listing of thecharacters
contained in it. Represent the frequency listing as a Python dictionary. Try it with something
likechar_freq("abbabcbdbabdbdbabababcbcbab").
>>> def frequency(str):
freq={}
for i in str:
keys=freq.keys()
if i in keys:
freq[i] += 1
else:
freq[i] = 1
return freq
>>> print(frequency(str))
>>>
12- Create a Date class, which represents the Date with its attributes. Write a UseDate class,
which makes use of the Date class to instantiate, and call methods on the object.
>>> class Date:
def __init__(self,hours,mins,sec):
self.hours=hours
self.mins=mins
self.sec=sec
def fulltime(self):
print(f"{self.hours}:{self.mins}:{self.sec}")
pass
>>> d1=UseDate(4,32,22)
>>> d2=UseDate(8,27,44)
>>> print(d1.fulltime())
4:32:22
None
>>> print(d2.fulltime())
8:27:44
None
>>>
13- WAP to read data from one file and writes in second file.
>>> file1=open("riya.txt","r")
>>> print(file1.read())
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
for i in f:
f1.write(i)
95
73
19
>>> file2=open("priya.txt","r")
>>> print(file2.read())
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
>>>
14- WAP which will display diffrenent function of mathand numpy library.
Numpy-
>>> import numpy
No. of dimensions: 2
Size of array: 6
>>>
MATH-
>>> import math
>>> print(math.pow(2,4))
16.0
>>> print(math.sqrt(100))
10.0
>>> print(math.ceil(4.5876))
>>> print(math.floor(4.5876))