Skip to content

Commit a20d7fe

Browse files
committed
completed warmup-2 problems
1 parent 7767dd5 commit a20d7fe

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Solutions/warmup-2/array123.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Given an array of ints, return True if the sequence of
2+
# numbers 1, 2, 3 appears in the array somewhere.
3+
4+
# array123([1, 1, 2, 3, 1]) → True
5+
# array123([1, 1, 2, 4, 1]) → False
6+
# array123([1, 1, 2, 1, 2, 3]) → True
7+
8+
9+
def array123(nums):
10+
for i in range(0, len(nums) - 2):
11+
if nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3:
12+
return True
13+
return False

Solutions/warmup-2/array_front9.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Given an array of ints, return True if one of the first 4 elements
2+
# in the array is a 9. The array length may be less than 4.
3+
4+
# array_front9([1, 2, 9, 3, 4]) → True
5+
# array_front9([1, 2, 3, 4, 9]) → False
6+
# array_front9([1, 2, 3, 4, 5]) → False
7+
8+
9+
def array_front9(nums):
10+
if len(nums) >= 4:
11+
return 9 in nums[:4]
12+
return 9 in nums

Solutions/warmup-2/string_match.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Given 2 strings, a and b, return the number of the positions
2+
# where they contain the same length 2 substring. So "xxcaazz"
3+
# and "xxbaaz" yields 3, since the "xx", "aa", and "az"
4+
# substrings appear in the same place in both strings.
5+
6+
# string_match('xxcaazz', 'xxbaaz') → 3
7+
# string_match('abc', 'abc') → 2
8+
# string_match('abc', 'axc') → 0
9+
10+
11+
def string_match(a, b):
12+
shorter = min(len(a), len(b))
13+
count = 0
14+
for i in range(shorter - 1):
15+
if a[i] == b[i] and a[i+1] == b[i+1]:
16+
count += 1
17+
return count

0 commit comments

Comments
 (0)