-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha10q2.py
More file actions
64 lines (48 loc) · 1.55 KB
/
a10q2.py
File metadata and controls
64 lines (48 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from typing import *
import check
# def alan(nums: List[int]) -> List[int]:
# """Do what it does with nums."""
# answer = []
# while nums != []:
# last = nums.pop()
# if last % 2 == 0:
# answer.append(last // 2)
# else:
# answer.append(last)
# return answer
# ## You should write additional tests
# ## to be certain you understand what alan does.
# check.expect("A0", alan([0]), [0])
# check.expect("A1", alan([2,3,4]), [2, 3, 1])
# check.expect("A2", alan([2,3,5]), [5, 3, 1])
# mylist = [0]
# check.expect("A1", alan(mylist), [0])
# check.expect("A1m", mylist, [])
# check.expect("A2", alan(mylist), [])
def alan_recursive (nums: List[int], answer: List[int]=None)-> List[int]:
"""Return a new list called answer with the order of the list reversed from nums.
If number is even, divide it by 2,
if number is odd, append it the same to answer"""
if answer == None:
answer =[]
if nums != []:
last = nums.pop()
if last % 2 == 0:
answer.append(last//2)
else:
answer.append(last)
return alan_recursive(nums, answer)
else:
return answer
mylist = [0]
check.expect("Ar1", alan_recursive(mylist), [0])
check.expect("Ar1m", mylist, [])
check.expect("Ar2", alan_recursive(mylist), [])
l1 = [2,3,4,5]
check.expect("mytest1", alan_recursive(l1), [5, 2, 3, 1])
check.expect("mytest2", l1, [])
#check.expect("Ar == A 0", alan_recursive([1, 1]), alan([1, 1]))
# In[ ]: