Final Sample Web
Final Sample Web
Q10-(9p) Text file "data.txt" contains words and Q12- ( 5p) Given W= 120.1684, write a formatted
integer numbers. print statement to print W within ten spaces and
a) Complete the program to build and display the list right adjusted form as "W=120.17".
of all alphabetic words, The symbol denotes the space character .
b) Complete the last print line to get the list of words
without repetition by converting it to a set and then
to a list.
Answers:
1 rfile = open("data.txt") print("Weight =%10.2f %s"% (W, B))
3 wordlist=[]
4 for s in rfile:
5 for splt in s.split():
6 if splt.isalpha():
Q13- (9p) The following program is prepared to read
7 ______________________________ two 4-digit integer numbers from keyboard, N1
8 print( "list of words:", _____________ ) and N2, and check if the sum of digits of N1 is
9 print( "words nonrepeating:", _________) equal to sum of digits of N2. For example: N1 =
4109, N2 = 6107. Sum of digits of N1=14, sum of
digits of N2 = 14.
7 Answer:
___________________________________ Some program lines are left empty, fill in them
7 wordlist.append(splt) with appropriate Python statements and write your
8 8 wordlist
___________________________________ answers within the given box.
9 list(set(wordlist))
9 ___________________________________
___________________________________
Q11- ( 15p)
a) Write a function "listsqr" that squares all integer
___________________________________
elements of a list (specified as argument). You
may use method "is_integer()" to test an
___________________________________
element is an integer or not.
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
Answer:
___________________________________
___________________________________
1 def listsqr( L):
2 for k in L:
___________________________________
3 if k.is_integer():
4 k=k*k
___________________________________
5 5 return L
___________________________________
Answer:
Sum1=Sum1+int(i)
b) Call your new function to create the sqrL list that
contains the squares of the integers in the list Sum2=Sum2+int(j)
L=[1,3,8,21,55].
Sum1 == Sum2
Answer:
sqrL=listsqr(L)
CMPE-CMSE 107 Final Exam Page 3
Q14- (5p c1a) Q15- (12p) In the code given below, the numbers in
The following Python code fragment finds the number the text file "fruits.txt" shall be found and written
of each word in a given string. But there is a to the text file "numbers.txt". Complete the missing
semantic error in the code. When the code runs, it parts accordingly.
finds the number of each word as one. Find and
write the line number and the correction of the
error in the code.