Skip to content

Commit 495adb9

Browse files
Jem ChangJem Chang
authored andcommitted
Adding String-2
1 parent 6ea730b commit 495adb9

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

String-2/cat_dog.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Return True if the string "cat" and "dog" appear the same number of times in the given string.
3+
"""
4+
def cat_dog(x):
5+
cat = 0
6+
dog = 0
7+
for i in range(len(x)-2):
8+
if x[i:i+3] == 'cat':
9+
cat = cat + 1
10+
if x[i:i+3] == 'dog':
11+
dog = dog + 1
12+
if dog == cat:
13+
return True
14+
else:
15+
return False

String-2/count_code.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Return the number of times that the string "code" appears anywhere in the given string,
3+
except we'll accept any letter for the 'd', so "cope" and "cooe" count.
4+
"""
5+
def count_code(x):
6+
count = 0
7+
for i in range(len(x)-3):
8+
if x[i:i+2] == 'co' and x[i+3:i+4] == 'e':
9+
count = count + 1
10+
else:
11+
count = count
12+
return count

String-2/count_hi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Return the number of times that the string "hi" appears anywhere in the given string.
3+
"""
4+
def count_hi(x):
5+
count = 0
6+
for i in range(len(x)-1):
7+
if x[i:i+2] == 'hi':
8+
count = count + 1
9+
else:
10+
count = count
11+
return count

String-2/double_char.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Given a string, return a string where for every char in the original, there are two chars.
3+
"""
4+
5+
def double_char(x):
6+
all = str()
7+
for i in x:
8+
all = all + i + i
9+
return all

String-2/end_other.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Given two strings, return True if either of the strings appears at the very end of the other string,
3+
ignoring upper/lower case differences (in other words, the computation should not be "case sensitive").
4+
Note: s.lower() returns the lowercase version of a string.
5+
"""
6+
def end_other(a, b):
7+
alow = a.lower()
8+
blow = b.lower()
9+
if len(a) > len(b) and blow == alow[-(len(b)):]:
10+
return True
11+
if len(b) > len(a) and alow == blow[-(len(a)):]:
12+
return True
13+
if alow == blow:
14+
return True
15+
else:
16+
return False

String-2/xyz_there.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded
3+
by a period (.). So "xxyz" counts but "x.xyz" does not.
4+
"""
5+
def xyz_there(x):
6+
if x.count('xyz') == 0:
7+
return False
8+
elif x.count('.xyz') != 0:
9+
y = x.replace('.xyz','')
10+
if y.count('xyz') > 0:
11+
return True
12+
return False
13+
else:
14+
return True
15+

0 commit comments

Comments
 (0)