-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_exercise.py
More file actions
71 lines (57 loc) · 2.07 KB
/
Copy pathstring_exercise.py
File metadata and controls
71 lines (57 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
from src.main.logger.py_logger import PyLogger
logger = PyLogger.get_configured_logger()
# Count the number of characters (character appearance) in a string
def count_char_appearance(string):
char_dict = {}
for n in string:
keys = char_dict.keys()
if n in keys:
char_dict[n] += 1
else:
char_dict[n] = 1
return char_dict
# Add 'ing' at the end of a given string (length should be at least 3).
# If the given string already ends with 'ing' then add 'ly' instead.
# If the string length of the given string is less than 3, leave it unchanged
def add_string(string):
length = len(string)
if length > 2:
if string[-3:] == "ing":
string += "ly"
else:
string += "ing"
return string
# Find the first appearance of the substring 'not' and 'poor' from a given string,
# if 'not' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'.
# Return the resulting string
def replace_string(string):
string_not = string.find("not")
string_poor = string.find("poor")
if string_poor > string_not > 0 and string_poor > 0:
# replace 'not...poor' with 'good'
string = string.replace(string[string_not : (string_poor + 4)], "good")
return string
else:
return string
# takes a list of words and returns the length of the longest one
def find_longest_word(words_list):
word_len = []
for n in words_list:
word_len.append((len(n), n))
word_len.sort()
return word_len[-1][1]
# Remove the nth index character from a nonempty string
def remove_char(string, n):
first_part = string[:n] # slice the string upto nth index
last_part = string[n + 1 :] # slice the string after nth index
return first_part + last_part # concatenate the two slice
# Count the occurrences of each word in a given sentence
def count_word(string):
counts = dict()
words = string.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts