Skip to content

Commit bb184ca

Browse files
committed
String-2 directory added
1 parent c4bc124 commit bb184ca

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

String-2/cat_dog.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
cat = 0
10+
dog = 0
11+
for i in range(0, len(str)-1):
12+
if str[i:i+3] == "cat":
13+
cat += 1
14+
if str[i:i+3] == "dog":
15+
dog += 1
16+
return cat == dog

String-2/count_code.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Return the number of times that the string "code" appears anywhere in the given string,
2+
# we'll accept any 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(str):
10+
alp = "abcdefghijklmnopqersuvwxuz"
11+
sum = 0
12+
for c in alp:
13+
d = "co" + c + "e"
14+
15+
for i in range(len(str)-1):
16+
if str[i:i+4] == d:
17+
sum += 1
18+
19+
return sum

String-2/count_hi.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 "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+
def count_hi(str):
9+
#return str.count("hi")
10+
sum = 0
11+
for i in range(len(str)-1):
12+
if str[i:i+2] == 'hi':
13+
sum += 1
14+
return sum

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+
str1 = ""
10+
for c in str:
11+
str1 += c*2
12+
return str1

String-2/end_other.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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
3+
# (in other words, the computation should not be "case sensitive").
4+
# Note: s.lower() returns the lowercase version of a string.
5+
6+
7+
# end_other('Hiabc', 'abc') → True
8+
# end_other('AbC', 'HiaBc') → True
9+
# end_other('abc', 'abXabc') → True
10+
11+
def end_other(a, b):
12+
a = a.lower()
13+
b = b.lower()
14+
a1 = len(a)
15+
b1 = len(b)
16+
17+
if a1 == b1 and a == b:
18+
return True
19+
elif a1 > b1:
20+
return a.endswith(b)
21+
else:
22+
return b.endswith(a)

String-2/xyz_there.py

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

0 commit comments

Comments
 (0)