-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-2.py
More file actions
executable file
·86 lines (72 loc) · 2.07 KB
/
string-2.py
File metadata and controls
executable file
·86 lines (72 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# double_char
def double_char(str):
'''
Given a string, return a string where for every char in the original, there are two chars.
'''
word = ''
for i in str:
word += i * 2
return word
# count_hi
def count_hi(str):
'''
Return the number of times that the string "hi" appears anywhere in the given string.
'''
ctr = 0
for i in range(len(str)-1):
if str[i:i+2] == 'hi':
ctr += 1
return ctr
# cat_dog
def cat_dog(str):
'''
Return True if the string "cat" and "dog" appear the same number of times in the given string.
'''
ctr_cat = 0
ctr_dog = 0
for i in range(len(str)-2):
if str[i:i+3] == 'dog':
ctr_dog += 1
if str[i:i+3] == 'cat:
ctr_cat += 1
return ctr_cat == ctr_dog
# count_code
def count_code(str):
'''
Return the number of times that the string "code" appears anywhere in the given string,
except we'll accept any letter for the 'd', so "cope" and "cooe" count.
'''
code1 = 'co'
code2 = 'e'
ctr = 0
for i in range(len(str)-3):
if (str[i: i+2] == code1 and str[i+3] == code2):
ctr += 1
return ctr
# end_other
def end_other(a, b):
'''
Given two strings, return True if either of the strings appears at the very end of the
other string, ignoring upper/lower case differences (in other words, the computation
should not be "case sensitive"). Note: s.lower() returns the lowercase version of a string.
'''
a = a.lower()
b = b.lower()
lstr = a
sstr = b
if len(b) > len(a):
lstr = b
sstr = a
return lstr.endswith(sstr)
# end_other solution2
def end_other(a, b):
if len(a) > len(b):
return b.lower() == a[-len(b):].lower()
return a.lower() == b[-len(a):].lower()
# xyz_there
def xyz_there(str):
'''
Return True if the given string contains an appearance of "xyz" where the xyz is not
directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
'''
return str.count('xyz') - str.count('.xyz') > 0