-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_task.py
More file actions
executable file
·52 lines (45 loc) · 1.49 KB
/
Copy pathstring_task.py
File metadata and controls
executable file
·52 lines (45 loc) · 1.49 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
# Given a string, if its length is at least 3,
# add 'ing' to its end.
# Unless it already ends in 'ing', in which case
# add 'ly' instead.
# If the string length is less than 3, leave it unchanged.
# Return the resulting string.
#
# Example input: 'read'
# Example output: 'reading'
def verbing(s):
if len(s) >= 3:
if s[-3:] == "ing":
s = s[:-3] + "ly"
else:
s += "ing"
return s
# Given a string, find the first appearance of the
# substring 'not' and 'bad'. If the 'bad' follows
# the 'not', replace the whole 'not'...'bad' substring
# with 'good'.
# Return the resulting string.
#
# Example input: 'This dinner is not that bad!'
# Example output: 'This dinner is good!'
def not_bad(s):
fr_not, fr_bad = s.find("not"), s.find("bad")
if(fr_not != -1 and fr_bad != -1 and fr_not < fr_bad):
s = s[:fr_not] + "good" + s[fr_bad + 3:]
return s
# Consider dividing a string into two halves.
# If the length is even, the front and back halves are the same length.
# If the length is odd, we'll say that the extra char goes in the front half.
# e.g. 'abcde', the front half is 'abc', the back half 'de'.
#
# Given 2 strings, a and b, return a string of the form
# a-front + b-front + a-back + b-back
#
# Example input: 'abcd', 'xy'
# Example output: 'abxcdy'
def front_back(a, b):
n, m = len(a), len(b)
return a[:-(n // 2)] + b[:-(m // 2)] + a[-(n // 2):] + b[-(m // 2):]
# a, b = 'abcde', 'xyz'
# print(front_back(a, b))
# print(verbing("read"))