Skip to content

Commit 2c0d5b2

Browse files
committed
Solution for task 05.1 PassingCars
1 parent 0f9a158 commit 2c0d5b2

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

solution_05_1_passing_cars/__init__.py

Whitespace-only changes.

solution_05_1_passing_cars/main.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Codility exercise 05.1 PassingCars
3+
"""
4+
from solution_05_1_passing_cars.solution import solution
5+
6+
7+
def main():
8+
"""
9+
Main function calls solution for codility task 05.1
10+
"""
11+
while True:
12+
try:
13+
array = list(map(int, input('Enter int array with 0 or 1: ').strip().split(' ')))
14+
print(solution(array))
15+
except ValueError as ex:
16+
print(f'Wring input! {ex}. Try again.')
17+
continue
18+
except KeyboardInterrupt:
19+
return
20+
21+
22+
if __name__ == '__main__':
23+
main()
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Solution for exercise 05.1 PassingCars
3+
"""
4+
from itertools import accumulate
5+
6+
7+
def solution(a):
8+
suffix_sum = list(accumulate(reversed(a)))
9+
counter = 0
10+
prev = suffix_sum[0]
11+
for value in suffix_sum[1:]:
12+
if value == prev:
13+
counter += prev
14+
else:
15+
prev = value
16+
if counter > 1000000000:
17+
return -1
18+
return counter
217 KB
Binary file not shown.

0 commit comments

Comments
 (0)