-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge1.py
60 lines (48 loc) · 1.57 KB
/
challenge1.py
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
#Write some code, that will flatten an array of arbitrary
#nested arrays of integers into a flat array of integers.
#e.g.:[[1,2,[3]],4]->[1,2,3,4]
import sys
import random
def check_array(array):
'''Flatten array function'''
new_array = []
for item in array:
if isinstance(item, int):
new_array.append(item)
elif isinstance(item, list):
new_array += check_array(item)
return sorted(new_array)
def create_array(remove=False):
'''Randomize array function'''
array = []
number = random.randint(5,9)
for i in range(number):
if len(array) == 0:
array.append(i+1)
if random.randint(1,10) % 2 == 0:
new_array = [(i+1)**2 for i in range(random.randint(1,9))]
if random.randint(1,10) % 2 == 0:
new_array.append(
[(i+1)**2 for i in range(random.randint(1,9))]
)
array.append(new_array)
else:
array.append((i+1)**2)
if len(array) == number:
flat_array = check_array(array)
print ('Received array: ', array)
'''Remove duplicates if asked to'''
if remove:
print('Flattened array: ', sorted(list(set(flat_array))))
else:
print('Flattened array: ', flat_array)
if __name__ == '__main__':
repeated = raw_input(
"Would like to remove repeated items from array:\n"
"1 - Yes\n"
"2 - No\n"
)
if int(repeated) == 1:
create_array(True)
else:
create_array()