Skip to content

Commit d0b89ee

Browse files
Jem ChangJem Chang
Jem Chang
authored and
Jem Chang
committed
Adding Warnup-2
1 parent 637eef5 commit d0b89ee

11 files changed

+108
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Practicing Python Programming in [CodingBat](http://codingbat.com/python)
2-
1. Warmup-1
3-
2. Warmup-2
2+
1. [Warmup-1](https://github.com/jemc36/CodingBat-Python/tree/master/Warmup-1)
3+
2. [Warmup-2](https://github.com/jemc36/CodingBat-Python/tree/master/Warmup-2)
44
3. Logic-1
55
4. Logic-2
66
5. String-1

Warmup-1/.DS_Store

-6 KB
Binary file not shown.

Warmup-2/array123.py

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

Warmup-2/array_count9.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Given an array of ints, return the number of 9's in the array.
3+
"""
4+
def array_count9(nums):
5+
count9 = int()
6+
for i in nums:
7+
if i == 9:
8+
count9 = count9 + 1
9+
else: continue
10+
return count9

Warmup-2/array_front9.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Given an array of ints, 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+
def array_front9(nums):
6+
num4 = list()
7+
if len(nums) < 4:
8+
for i in nums:
9+
if i !=9:continue
10+
return True
11+
else:
12+
num4 = nums[0:4]
13+
for j in num4:
14+
if j !=9: continue
15+
return True
16+
return False

Warmup-2/front_times.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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. Return n copies of the front;
5+
"""
6+
def front_times(x, n):
7+
a = str()
8+
allstr = str()
9+
if len(x) < 3:
10+
a = x
11+
else:
12+
a = x[0:3]
13+
for i in range(n):
14+
allstr = allstr + a
15+
return allstr

Warmup-2/last2.py

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

Warmup-2/string_bits.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Given a string, return a new string made of every other char starting with the first,
3+
so "Hello" yields "Hlo".
4+
"""
5+
def string_bits(x):
6+
allstr = str()
7+
for i in range(len(x)):
8+
if i % 2 == 0:
9+
allstr = allstr + x[i]
10+
else:
11+
allstr = allstr
12+
return allstr

Warmup-2/string_match.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Given 2 strings, a and b, return the number of the positions where they
3+
contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3,
4+
since the "xx", "aa", and "az" substrings appear in the same place in both strings.
5+
"""
6+
def string_match(a, b):
7+
count = int()
8+
for i in range(len(a)-1):
9+
if a[i:i+2] == b[i:i+2]:
10+
count = count + 1
11+
else: continue
12+
return count

Warmup-2/string_splosion.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Given a non-empty string like "Code" return a string like "CCoCodCode".
3+
"""
4+
5+
def string_splosion(x):
6+
all = str()
7+
for i in range(len(x)):
8+
all = all + x[0:i+1]
9+
return all

Warmup-2/string_times.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
def string_times(x, n):
6+
allstr = str()
7+
for i in range(n):
8+
allstr = allstr + x
9+
return allstr

0 commit comments

Comments
 (0)