-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp13.py
More file actions
27 lines (23 loc) · 707 Bytes
/
p13.py
File metadata and controls
27 lines (23 loc) · 707 Bytes
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
#!/usr/bin/env python3
# P13: Run-length encoding of a list (direct solution).
def p13(list):
if list == []:
return None
else:
new_list = []
old_element = list[0]
count_same_element = 0
for item in list:
if item == old_element:
count_same_element += 1
else:
temp = [count_same_element, old_element]
new_list.append(temp)
old_element = item
count_same_element = 1
temp = [count_same_element, old_element]
new_list.append(temp)
return new_list
if __name__ == "__main__":
ret = p13([1, 2, 2, 3, 1, 5, 5, 5, 5])
print(ret)