def mystery(n):
x = 0
while(x != 1 and x != 4):
x = 0
while (n != 0):
x = x + (n % 10) ** 2
n = n // 10
n = x
if x == 1:
return True
else:
return False
mystery(42)
?¶mystery(103)
?¶Implement the function times10(L)
that returns a list containing the same elements as L
, but multiplied by 10.
For example, times10([1,2,3])
should return [10,20,30]
.
def times10(L):
return []
Step 1: Understand the problem
Step 2: Test cases
assert( times10([10]) == [100] )
assert( times10([]) == [] )
Step 3: Algorithm
When we have to produce a list out of an existing list, we usually have at least two options:
newL
and modify newL
to match the specification. This is usually convenient if we can obtain the solution by modifiying each element of the copied list.
This also implies that the solution list is expected to be of the same size as the inputStart with an empty list and, by some sort of computation over the elements of the original list, we obtain one by one the elements of the solution and add them to the result.
Let's try the first approach, which seems to be this case:
Algorithm: copy the list L into newL, multiply each element by 10 and store the result back at the same index.
newL
len(newL)-1
2.1 newL[i] = newL[i] * 10
## Solution:
def times10(L):
newL = L[:] # one way to copy using slicing
for i in range(len(newL)):
newL[i] = newL[i]*10
return newL
assert(times10([1,2,3]) == [10,20,30])
assert(times10([]) == [])
assert(times10([10]) == [100])
print("Passed.")
Passed.
# Bad solution, what's wrong with it?
def times10(L):
newL = L[:] # one way to copy using slicing
for e in newL:
e = e*10
return newL
assert(times10([1,2,3]) == [10,20,30])
assert(times10([]) == [])
assert(times10([10]) == [100])
print("Passed.")
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-6-47ae17d3d30d> in <module> 6 return newL 7 ----> 8 assert(times10([1,2,3]) == [10,20,30]) 9 assert(times10([]) == []) 10 assert(times10([10]) == [100]) AssertionError:
## Debug it!
# Bad solution, what's wrong with it?
def times10(L):
newL = L[:] # one way to copy using slicing
for e in newL:
print("e=", e)
e = e*10
print("newL:", newL)
return newL
# it failed the first test case, so let's focus on this test case
# remove the assert and change it to print, so we can see what's the output
print(times10([1,2,3]))
e= 1 newL: [1, 2, 3] e= 2 newL: [1, 2, 3] e= 3 newL: [1, 2, 3] [1, 2, 3]
Find the maximum element of a list. You cannot use the builtin function max
def maxInList(L):
return 42
Step 1: Understand the problem
Step 2: Test cases
assert( maxInList([1, 2, 3, 4]) == 4 )
assert( maxInList([4, 3, 2, 1]) == 4 )
assert( maxInList([1, -1, 7, -7, 10.0, 15.110]) == 15.11 )
Step 3: Algorithm
Loop through the elements; keep the maximum element seen so far in one variable; if the current element is greater than the max so far; make this element the new max
Algorithm: copy the list L into newL, multiply each element by 10 and store the result at the same index.
newL
len(newL)-1
2.1 newL[i] = newL[i] * 10
You receive a list with zeros and ones corresponding to a binary number. Write the function binToDec(B)
that returns the decimal representation of B
.
For example, binToDec([1,1,0])
should return 6.
def binToDec(B):
return 42
Given a list L
and element e
, write the function count(L, e)
that counts the number of times e
occurs in L
.
def count(L, e):
return 42
Implement the function allTheSame(L)
that returns True
if all elements of list L
are the same, and False
otherwise.
def allTheSame(L):
return True