2017f Exam
2017f Exam
li = [1,2,3,4]
Ex. 1. for i in range(2,4):
li[i] = 2*li[i]
print(li)
x = 5
Ex. 2. for n in range(10,6,-1):
if (x < i):
x = x + i
print(x)
li = []
Ex. 3. for i in range(2,7):
li.append(i)
li[i] = i
print(li)
1
My code (for anonymous examination):
li = [0,1,2,3,4,5,6,7]
Ex. 4. for i in range(2,8):
li[i] = li[i-1] + li[i-2]
print(li)
print("immutable"[-5:][:-1])
Ex. 5.
word = ''
Ex. 6. for letter in 'animal':
if letter in 'ai':
word = word + letter
else:
word = word + letter + 'o' + letter
print(word)
a = []
Ex. 7. b = [a]
b[0].append(5)
b.append(6)
print(b)
li1 = []
Ex. 8. li2 = [1,2,3]
for n in li2:
li1.append(n*n)
print(li1)
li = []
Ex. 9. for n in range(3,6):
li.append(str(10 ** n))
print(li)
2
My code (for anonymous examination):
a = (3,2,1)
Ex. 10. b = [0,1,1,2,2,3]
for i in range(0,4):
b[i] = a[i]
print(b)
li = [7,9,5,7,2,3]
Ex. 11. n = 0
for i in range(0, len(li)-1):
if (li[i] > li[i+1]):
n = n + li[i]
print(n)
w1 = 'Sweden'
Ex. 12. w2 =''
for i in range(3):
w2 = w2 + w1[i:]
print(w2)
def funE1(a,b):
Ex. 13. li = []
for x in range(a):
li.append(x+b)
return li
print(funE1(5,8))
def funE2(s):
Ex. 14. li = []
while len(li) < len(s):
li[len(s)] = s
return li
print(funE2('abc'))
3
My code (for anonymous examination):
def funE3(s,t,u):
Ex. 15. for i in range(0,len(s)):
if s[i] == t:
s[i] = u
else:
s[i] = s[0]
s = [1,2,3,4]
funE3(s,3,5)
print(s)
def funE4(s):
Ex. 16. li = []
for i in range(0,len(s)):
li.append(s[i])
li.append(i)
if (i == 3):
li.append(s[i])
return li
print(funE4('apple'))
def funE5(s,t):
Ex. 17. for i in t:
s[i] = s[i] + 1
li = [1,2,3,4]
tu = (0,1,1,2,2)
funE5(li,tu)
print(li)
def funE6(n):
Ex. 18. t = 'a'
li = []
while len(t) < n:
t = t + 'a'
li.append('b' + t + 'b')
return li
print(funE6(5))
4
My code (for anonymous examination):
def funE7(s):
Ex. 19. li = []
for i in range(len(s)):
if s[i]!='a':
li.append(s[i])
return li
print(funE7('uppsala'))
def funE8(n):
Ex. 20. li = [0]
while len(li) < n:
li.append(len(li)+li[-1])
return li
print(funE8(7))
def funE9(n):
Ex. 21. li = []
for w1 in range(0,n):
for w2 in range(0,n):
if w1*w2==n:
li.append((w1,w2))
return li
print(funE9((12)))
def funE10(s):
Ex. 22. for i in range(len(s)-1):
if s[i] > s[i+1]:
t = s[i]
s[i] = s[i+1]
s[i+1] = t
li = [5,9,3,7,4]
funE10(li)
print(li)
5
My code (for anonymous examination):
Write a function – let’s call it list_from_tuple – that takes any tuple as its
Ex. 23. input and returns the corresponding list, i.e. the list with the same values in
the same order. Don’t make use of the existing python method list! So, for
instance:
list_from_tuple(()) returns []
list_from_tuple((1,2,3)) returns [1,2,3]
list_from_tuple((1,(2,3),4)) returns [1,(2,3),4]
Write a function (ordered) that takes a list of integers as its input parameter
Ex. 24. and returns True if it is ordered smaller to larger values. Duplicates are allowed.
Otherwise (if some value is preceded by a larger one), it should return False.
Sorting should not be performed. The given list should not be modified and no
other list should be created. So, for instance:
ordered([]) returns True
ordered([5]) returns True
ordered([3, 5, 5, 12, 55, 55, 77]) returns True
ordered([8, 32, 8, 12, 8, 55, 8, 78]) returns False
When the the function returns False it should also, as a side effect, print a
message like “Value preceding smaller value: 32”, telling us which was
the first value being larger than the next one.
Write a function (ngram) that takes a string and an integer n as input and returns
Ex. 25. the list of all character n-grams (i.e. substrings of length n) ordered according to
their appearance in the input string. So, for instance,
ngram('uppsala',3) returns ['upp', 'pps', 'psa', 'sal', 'ala']
ngram('uppsala',5) returns ['uppsa', 'ppsal', 'psala']
ngram('uppsala',7) returns ['uppsala']
ngram(,2) returns []
ngram('uppsala',8) returns []
Write a function (min_max_num) which takes a list of integers as its input pa-
Ex. 26. rameter and returns a tuple (with two places) whose first component is the small-
est number in the list and the second one the largest number. The solution you
propose should loop through the numbers only once, and sorting should not be
performed. So, for instance:
min_max_num([7,1,7,9,3,5,9,0]) returns (0, 9)
min_max_num([5,5,5,5,5]) returns (5, 5)
min_max_num([5]) returns (5, 5)
Calling min_max_num([]) should result in an error.