Skip to content

Commit c750d06

Browse files
committed
Warmup-2 completed
1 parent 2206c17 commit c750d06

9 files changed

+191
-0
lines changed

Warmup-2/ string_times.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# Given a string and a non-negative int n,
3+
# return a larger string that is n copies of the original string.
4+
5+
6+
# string_times('Hi', 2) → 'HiHi'
7+
# string_times('Hi', 3) → 'HiHiHi'
8+
# string_times('Hi', 1) → 'Hi'
9+
10+
def string_times(str, n):
11+
return str*n
12+
13+
14+
15+
# Official Solution:
16+
# def string_times(str, n):
17+
# result = ""
18+
# for i in range(n): # range(n) is [0, 1, 2, .... n-1]
19+
# result = result + str # could use += here
20+
# return result

Warmup-2/array123.py

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

Warmup-2/array_count9.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Given an array of ints, return the number of 9's in the array.
2+
3+
4+
# array_count9([1, 2, 9]) → 1
5+
# array_count9([1, 9, 9]) → 2
6+
# array_count9([1, 9, 9, 3, 9]) → 3
7+
8+
9+
def array_count9(nums):
10+
return nums.count(9)
11+
12+
13+
# Solution:
14+
# def array_count9(nums):
15+
# count = 0
16+
# # Standard loop to look at each value
17+
# for num in nums:
18+
# if num == 9:
19+
# count = count + 1
20+
21+
# return count

Warmup-2/array_front9.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Given an array of ints,
2+
# return True if one of the first 4 elements in the array is a 9.
3+
# The array length may be less than 4.
4+
5+
6+
# array_front9([1, 2, 9, 3, 4]) → True
7+
# array_front9([1, 2, 3, 4, 9]) → False
8+
# array_front9([1, 2, 3, 4, 5]) → False
9+
10+
def array_front9(nums):
11+
if len(nums) <= 4:
12+
return 9 in nums
13+
else:
14+
return 9 in nums[:4]
15+
16+
17+
# Solution:
18+
# def array_front9(nums):
19+
# # First figure the end for the loop
20+
# end = len(nums)
21+
# if end > 4:
22+
# end = 4
23+
24+
# for i in range(end): # loop over index [0, 1, 2, 3]
25+
# if nums[i] == 9:
26+
# return True
27+
# return False

Warmup-2/front_times.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
# Given a string and a non-negative int n,
3+
# we'll say that the front of the string is the first 3 chars,
4+
# or whatever is there if the string is less than length 3.
5+
# Return n copies of the front;
6+
7+
8+
# front_times('Chocolate', 2) → 'ChoCho'
9+
# front_times('Chocolate', 3) → 'ChoChoCho'
10+
# front_times('Abc', 3) → 'AbcAbcAbc'
11+
12+
13+
def front_times(str, n):
14+
if len(str) <= 3:
15+
return str * n
16+
else:
17+
return str[:3] * n
18+
19+
20+
# Official Solution:
21+
# def front_times(str, n):
22+
# front_len = 3
23+
# if front_len > len(str):
24+
# front_len = len(str)
25+
# front = str[:front_len]
26+
27+
# result = ""
28+
# for i in range(n):
29+
# result = result + front
30+
# return result

Warmup-2/last2.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Given a string, return the count of the number of times that a
2+
# substring length 2 appears in the string and also as the last 2 chars of the string,
3+
# so "hixxxhi" yields 1 (we won't count the end substring).
4+
5+
6+
# last2('hixxhi') → 1
7+
# last2('xaxxaxaxx') → 1
8+
# last2('axxxaaxx') → 2
9+
10+
def last2(str):
11+
if len(str) < 2:
12+
return 0
13+
last2 = str[len(str)-2:]
14+
count = 0
15+
for i in range(len(str)-2):
16+
sub = str[i:i+2]
17+
if sub == last2:
18+
count += 1
19+
return count

Warmup-2/string_bits.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Given a string, return a new string made of every other char starting with the first,
2+
# so "Hello" yields "Hlo".
3+
4+
5+
# string_bits('Hello') → 'Hlo'
6+
# string_bits('Hi') → 'H'
7+
# string_bits('Heeololeo') → 'Hello'
8+
9+
def string_bits(str):
10+
result = ''
11+
for i in range(len(str)):
12+
if i % 2 == 0:
13+
result += str[i]
14+
return result
15+
16+
17+
# Official Solution:
18+
# def string_bits(str):
19+
# result = ""
20+
# # Many ways to do this. This uses the standard loop of i on every char,
21+
# # and inside the loop skips the odd index values.
22+
# for i in range(len(str)):
23+
# if i % 2 == 0:
24+
# result = result + str[i]
25+
# return result

Warmup-2/string_match.py

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

Warmup-2/string_splosion.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# Given a non-empty string like "Code" return a string like "CCoCodCode".
3+
4+
5+
# string_splosion('Code') → 'CCoCodCode'
6+
# string_splosion('abc') → 'aababc'
7+
# string_splosion('ab') → 'aab'
8+
9+
def string_splosion(str):
10+
result = ''
11+
for i in range(len(str)+1):
12+
result += str[:i]
13+
return result

0 commit comments

Comments
 (0)