Old version
This is the CS 111 site as it appeared on May 10, 2018.
Midterm I Practice Problems
Solutions will be posted under Other Content on Blackboard as we get closer to the exam.
-
Given the string
s = 'Columbus'
, evaluate the following:s[0]
s[0:1]
s[1:]
s[-1]
s[3: :-1]
s[2: :2]
-
Given the list
myList = [3, 2, -1, [4, 7], 5]
, evaluate the following:myList[0]
myList[0:1]
myList[-1]
myList[3]
myList[:2]
myList[2: :2]
-
Evaluate the following:
'a' in 'backache'
[1, 2, 3] + [[11, 13, 12][1]] + [22, 33, 44, 55][1:]
[3 for x in range(6)]
[2*y + 1 for y in [1, 3, 5, 7]]
[x for x in range(3, 10) if x % 2 == 0]
[len(w) for w in ['Go', 'Terriers']]
-
Write a function
count_ones(s)
that takes in a strings
of'0'
s and'1'
s and returns the number of'1'
s in the input. -
Write a function
swap_bits(s)
that takes in a strings
of'0'
s and'1'
s and returns a string in which each bit ins
has been swapped/replaced with the other bit. For example,swap_bits('101011')
should return'010100'
. -
Write a function
num_divisors(n)
that returns the number of integers from 1 ton
(inclusive) that dividen
evenly. For example,num_divisors(42)
should return8
, because 1, 2, 3, 6, 7, 14, 21, and 42 are all divisors of 42. -
Use the above
num_divisors(n)
function in order to write a functionmost_divisors(lst)
that takes in a list of integerslst
and returns the integer from that list with the most divisors. For instance,most_divisors([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
should return12
. -
Write a function
count_transitions(s)
that takes in a strings
of'0'
s and'1'
s and returns the number of times there is a transition from a'0'
to a'1'
or vice-versa in that input string. For example,count_transitions('1110110000')
should return3
. -
Write a function
longest_string(lst)
that takes in a list of stringslst
as input and returns the longest string from that list. For example,longest_string(['short', 'longer', 'sesquipedalian'])
should return'sesquipedalian'
. -
Write a function
cycle(s, n)
that takes in a strings
of'0'
s and'1'
s and an integern
and returns the string in whichs
has shifted its last character to the initial positionn
times. For example,cycle('1110110000', 2)
should return'0011101100'
. -
What is printed by the following working Python program?
def dog(x): print('in dog, x is', x) y = cat(x - 1) + cat(x + 2) print('in dog, y is', y) return y def cat(y): print('in cat, y is', y) x = rat(y * 2) + 3 print('in cat, x is', x) return x def rat(x): print('in rat, x is', x) return 2 * x y = dog(3) print('at this level, y is', y)
-
What is printed by the following working Python program?
def mystery(x): print('x is', x) if x < 1: return 2 else: p = 6 - mystery(x - 1) print('p is', p) return p y = mystery(3) print('y is', y)