-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWarmUp_Solution_Two.py
142 lines (115 loc) · 4.24 KB
/
WarmUp_Solution_Two.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# ----------------------------------------------------------------------------------------------------------------------
#
# Given a string and a non-negative int n, return a larger string that is n copies of the original string.
#
#
# string_times('Hi', 2) → 'HiHi'
# string_times('Hi', 3) → 'HiHiHi'
# string_times('Hi', 1) → 'Hi'
def string_times(str, n):
return (n * str)
# ----------------------------------------------------------------------------------------------------------------------
#
# Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars,
# or whatever is there if the string is less than length 3. Return n copies of the front;
#
#
# front_times('Chocolate', 2) → 'ChoCho'
# front_times('Chocolate', 3) → 'ChoChoCho'
# front_times('Abc', 3) → 'AbcAbcAbc'
def front_times(str, n):
if len(str) <= 3:
return n * str
else:
return n * str[:3]
# ----------------------------------------------------------------------------------------------------------------------
# #
# Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo".
#
#
# string_bits('Hello') → 'Hlo'
# string_bits('Hi') → 'H'
# string_bits('Heeololeo') → 'Hello'
def string_bits(str):
value = ""
for index in range(len(str)):
if index % 2 == 0:
value = value + str[index]
return value
# ----------------------------------------------------------------------------------------------------------------------
# #
# Given a non-empty string like "Code" return a string like "CCoCodCode".
#
#
# string_splosion('Code') → 'CCoCodCode'
# string_splosion('abc') → 'aababc'
# string_splosion('ab') → 'aab
def string_splosion(str):
result = ""
for i in range(len(str)):
result = result + str[:i + 1]
return result
# ----------------------------------------------------------------------------------------------------------------------
# #
# Given a string, return the count of the number of times that a substring length 2 appears in the string and
# also as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't count the end substring).
#
#
# last2('hixxhi') → 1
# last2('xaxxaxaxx') → 1
# last2('axxxaaxx') → 2
def last2(str):
if len(str) < 2:
return 0
last2 = str[len(str) - 2:]
count = 0
for i in range(len(str) - 2):
sub = str[i:i + 2]
if sub == last2:
count = count + 1
return count
# ----------------------------------------------------------------------------------------------------------------------
# #
# Given an array of ints, return the number of 9's in the array.
#
#
# array_count9([1, 2, 9]) → 1
# array_count9([1, 9, 9]) → 2
# array_count9([1, 9, 9, 3, 9]) → 3
def array_count9(nums):
count = 0
for i in nums:
if (i == 9):
count = count + 1
return count
# ----------------------------------------------------------------------------------------------------------------------
#
# Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere.
#
#
# array123([1, 1, 2, 3, 1]) → True
# array123([1, 1, 2, 4, 1]) → False
# array123([1, 1, 2, 1, 2, 3]) → True
def array123(nums):
for i in range(len(nums) - 2):
if (nums[i] == 1) and (nums[i + 1] == 2) and (nums[i + 2] == 3):
return True
return False
# ----------------------------------------------------------------------------------------------------------------------
#
# Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring.
# So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.
#
#
# string_match('xxcaazz', 'xxbaaz') → 3
# string_match('abc', 'abc') → 2
# string_match('abc', 'axc') → 0
def string_match(a, b):
shorter = min(len(a), len(b))
count = 0
for i in range(shorter - 1):
a_sub = a[i:i + 2]
b_sub = b[i:i + 2]
if a_sub == b_sub:
count = count + 1
return count