Skip to content

Commit 79eeccf

Browse files
committed
String-2 Solutions
1 parent 2099df1 commit 79eeccf

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

String-2/cat_dog.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Return True if the string "cat" and "dog" appear the same number of times in the given string.
2+
#
3+
#
4+
# cat_dog('catdog') → True
5+
# cat_dog('catcat') → False
6+
# cat_dog('1cat1cadodog') → True
7+
8+
def cat_dog(str):
9+
return str.count("cat") == str.count("dog")

String-2/count_code.py

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

String-2/count_hi.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Return the number of times that the string "hi" appears anywhere in the given string.
2+
#
3+
#
4+
# count_hi('abc hi ho') → 1
5+
# count_hi('ABChi hi') → 2
6+
# count_hi('hihi') → 2
7+
8+
9+
def count_hi(str):
10+
return str.count("hi")
11+
12+
13+

String-2/double_char.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Given a string, return a string where for every char in the original, there are two chars.
2+
#
3+
#
4+
# double_char('The') → 'TThhee'
5+
# double_char('AAbb') → 'AAAAbbbb'
6+
# double_char('Hi-There') → 'HHii--TThheerree'
7+
8+
def double_char(str):
9+
return "".join([x * 2 for x in str])
10+
11+
12+
print(double_char("The"))

String-2/end_other.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Given two strings, return True if either of the strings appears at the very end of the other string,
2+
# ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note:
3+
# s.lower() returns the lowercase version of a string.
4+
#
5+
#
6+
# end_other('Hiabc', 'abc') → True
7+
# end_other('AbC', 'HiaBc') → True
8+
# end_other('abc', 'abXabc') → True
9+
10+
def end_other(a, b):
11+
low_a = a.lower()
12+
low_b = b.lower()
13+
return low_a.endswith(low_b) or low_b.endswith(low_a)

String-2/xyz_there.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period
2+
# (.). So "xxyz" counts but "x.xyz" does not.
3+
#
4+
#
5+
# xyz_there('abcxyz') → True
6+
# xyz_there('abc.xyz') → False
7+
# xyz_there('xyz.abc') → True
8+
9+
def xyz_there(str):
10+
if str[:3] == "xyz":
11+
return True
12+
for i in range(len(str) - 2):
13+
if str[i - 1] != "." and str[i:i + 3] == "xyz":
14+
return True
15+
return False

0 commit comments

Comments
 (0)