Skip to content

Commit 2ab647c

Browse files
committed
Day 1: check if a list has consecutive 3
1 parent 6d24352 commit 2ab647c

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

Day 1/__init__.py

Whitespace-only changes.

Day 1/has_33.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def has_33(input_list):
2+
length = len(input_list)
3+
for i in range(0, length):
4+
if input_list[i] == 3:
5+
if i + 1 < length:
6+
if input_list[i + 1] == 3:
7+
return True
8+
9+
return False
10+
11+
12+
print(has_33([1, 2, 3])) # False
13+
print(has_33([1, 3, 3])) # True
14+
print(has_33([1, 3, 1, 3])) # False
15+
print(has_33([3, 1, 3])) # False
16+
print(has_33([3, 3, 1])) # True
17+
print(has_33([1, 2, 2, 3])) # False

Day 1/has_33_advanced.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# to check two consecutive values use list range
2+
# [i: i+2] == [3, 3]
3+
4+
def has_33(input_list):
5+
length = len(input_list) - 1
6+
for i in range(0, length):
7+
if input_list[i: i + 2] == [3, 3]:
8+
return True
9+
return False
10+
11+
12+
print(has_33([1, 2, 3])) # False
13+
print(has_33([1, 3, 3])) # True
14+
print(has_33([1, 3, 1, 3])) # False
15+
print(has_33([3, 1, 3])) # False
16+
print(has_33([3, 3, 1])) # True
17+
print(has_33([1, 2, 2, 3])) # False

Day 1/has_33_improved.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def has_33(input_list):
2+
length = len(input_list) - 1
3+
for i in range(0, length):
4+
if input_list[i] == 3 and input_list[i + 1] == 3:
5+
return True
6+
return False
7+
8+
9+
print(has_33([1, 2, 3])) # False
10+
print(has_33([1, 3, 3])) # True
11+
print(has_33([1, 3, 1, 3])) # False
12+
print(has_33([3, 1, 3])) # False
13+
print(has_33([3, 3, 1])) # True
14+
print(has_33([1, 2, 2, 3])) # False

0 commit comments

Comments
 (0)