-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString-2.py
118 lines (82 loc) · 2.5 KB
/
String-2.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 14:43:10 2019
@author: YaoJunyan
"""
'''double_char
Given a string, return a string where for every char in the original, there are two chars.
double_char('The') → 'TThhee'
double_char('AAbb') → 'AAAAbbbb'
double_char('Hi-There') → 'HHii--TThheerree'
'''
def double_char(str):
strlist= list(str)
double= [i*2 for i in strlist]
return ''.join(double)
'''count_hi
Return the number of times that the string "hi" appears anywhere in the given string.
count_hi('abc hi ho') → 1
count_hi('ABChi hi') → 2
count_hi('hihi') → 2
'''
def count_hi(str):
count=0
for i in range(len(str)):
if str[i:i+2]=='hi':
count +=1
return count
'''cat_dog
Return True if the string "cat" and "dog" appear the same number of times in the given string.
cat_dog('catdog') → True
cat_dog('catcat') → False
cat_dog('1cat1cadodog') → True
'''
def cat_dog(str):
countcat=0
countdog =0
for i in range(len(str)-2):
if str[i:i+3]=='cat':
countcat +=1
if str[i:i+3] =='dog':
countdog +=1
return countcat== countdog
'''count_code
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.
count_code('aaacodebbb') → 1
count_code('codexxcode') → 2
count_code('cozexxcope') → 2
'''
def count_code(str):
count =0
for i in range(len(str)-3):
if str[i:i+2] =='co' and str[i+3]=='e':
count +=1
return count
'''
end_other
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.
end_other('Hiabc', 'abc') → True
end_other('AbC', 'HiaBc') → True
end_other('abc', 'abXabc') → True
'''
def end_other(a, b):
if len(a)>len(b):
if a[-len(b):].lower()==b.lower():
return True
else:
return False
else:
if b[-len(a):].lower()==a.lower():
return True
else:
return False
'''xyz_three
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.
xyz_there('abcxyz') → True
xyz_there('abc.xyz') → False
xyz_there('xyz.abc') → True
'''
def xyz_there(str):
#this is the anser i found from stackoverflow
return str.count('xyz')- str.count('.xyz')>0