Skip to content

Commit 3e67a7a

Browse files
authored
Update CodingBat solutions - Warmup-2
1 parent 7061888 commit 3e67a7a

File tree

1 file changed

+98
-8
lines changed

1 file changed

+98
-8
lines changed

CodingBat solutions - Warmup-2

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,104 @@
33
# Used In : Python
44
# Used As : Warmup-2
55
# Thoughts =>
6-
# Given a string and a non-negative int n, return a larger string that is n copies of the original string.
7-
#
6+
# This file will contain the solutions for the codingbat.com in attempt to have digest
7+
# for my coderschool kids.It's my own version of solutions which might be diffrent then
8+
# the actual ones.
89

910
'''
10-
Warmup-1 > sleep_in
11-
The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation.
12-
We sleep in if it is not a weekday or we're on vacation. Return True if we sleep in.
13-
sleep_in(False, False) → True
14-
sleep_in(True, False) → False
15-
sleep_in(False, True) → True
11+
Warmup-2 > string_times
12+
Given a string and a non-negative int n, return a larger string that is n copies of the original string.
13+
string_times('Hi', 2) → 'HiHi'
14+
string_times('Hi', 3) → 'HiHiHi'
15+
string_times('Hi', 1) → 'Hi'
1616
'''
17+
18+
def string_times(str, n):
19+
return str*n
20+
21+
'''
22+
Warmup-2 > front_times
23+
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars,
24+
or whatever is there if the string is less than length 3. Return n copies of the front.
25+
front_times('Chocolate', 2) → 'ChoCho'
26+
front_times('Chocolate', 3) → 'ChoChoCho'
27+
front_times('Abc', 3) → 'AbcAbcAbc'
28+
'''
29+
30+
def front_times(str, n):
31+
if len(str) < 3:
32+
return str*n
33+
else:
34+
return str[0:3]*n
35+
36+
37+
'''
38+
Warmup-2 > string_bits
39+
Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo".
40+
string_bits('Hello') → 'Hlo'
41+
string_bits('Hi') → 'H'
42+
string_bits('Heeololeo') → 'Hello'
43+
'''
44+
45+
def string_bits(str):
46+
answer = ""
47+
for i in range(0,len(str),2):
48+
answer += str[i]
49+
return answer
50+
51+
52+
'''
53+
Warmup-2 > string_splosion
54+
Given a non-empty string like "Code" return a string like "CCoCodCode".
55+
string_splosion('Code') → 'CCoCodCode'
56+
string_splosion('abc') → 'aababc'
57+
string_splosion('ab') → 'aab'
58+
'''
59+
60+
def string_splosion(str):
61+
answer = ""
62+
j = -1
63+
for i in range(0,len(str)+1):
64+
j = j+1
65+
for k in range(0,j):
66+
answer += str[k]
67+
return answer
68+
69+
70+
'''
71+
Warmup-2 > last2
72+
Given a string, return the count of the number of times that a substring
73+
length 2 appears in the string and also as the last 2 chars of the string,
74+
so "hixxxhi" yields 1 (we won't count the end substring).
75+
last2('hixxhi') → 1
76+
last2('xaxxaxaxx') → 1
77+
last2('axxxaaxx') → 2
78+
'''
79+
80+
def last2(str):
81+
if len(str)<2:
82+
return 0
83+
last = str[len(str)-2:]
84+
count = 0
85+
for i in range(len(str)-2):
86+
sub = str[i:i+2]
87+
if sub == last:
88+
count = count + 1
89+
return count
90+
91+
92+
'''
93+
Warmup-2 > last2
94+
Given an array of ints, return the number of 9's in the array.
95+
array_count9([1, 2, 9]) → 1
96+
array_count9([1, 9, 9]) → 2
97+
array_count9([1, 9, 9, 3, 9]) → 3
98+
'''
99+
100+
def array_count9(nums):
101+
count = 0
102+
for i in nums:
103+
if i == 9:
104+
count = count + 1
105+
return count
106+

0 commit comments

Comments
 (0)