-
Notifications
You must be signed in to change notification settings - Fork 48
/
string_editing.py
88 lines (76 loc) · 2.74 KB
/
string_editing.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
'''
Created on 10/07/2013
@author: luke
'''
class MyString():
def reverse(self, some_seq):
"""
Input: Sequence
output: Sequence:
reversed version
"""
return some_seq[::-1]
def count_vowels(self, string):
"""
@param string: String
@return: Integer:
No. of vowels or 0
"""
vowels = "aeiou"
count = {char:0 for char in vowels}
for char in string:
if char in vowels:
count[char] += 1
return count
def is_palindrome(self,some_seq):
"""
@param some_seq: sequence of anything
@return: Boolean:
palindrome check of sequence passed
"""
return some_seq == self.reverse(some_seq)
def count_words(self,string=None,file=None):
"""
@param string: A string
@param file: A file to be read
"""
word_count = 0
if string:
word_count = len(string.split())
if file:
with open(file) as f:
word_count = len(f.read().split())
return word_count
def find_in_iter(self,some_iter):
"""
just an examplke for someone.
"""
if isinstance(some_iter,str):
cond = lambda sequence, string: [char for char in string if char in sequence]
vowels = ("a,e,i,o,u")
return [word for word in some_iter.lower().split(" ") if len(cond(vowels,word)) > 1]
elif isinstance(some_iter[0],int):
return [num for num in some_iter if num > 5]
def piglatin(self,string):
"""
Pig Latin – Pig Latin is a game of alterations played on the English language game.
To create the Pig Latin form of an English word the initial consonant sound is transposed
to the end of the word and an ay is affixed
(Ex.: "banana" would yield anana-bay). Read Wikipedia for more information on rules.
"""
words = []
vowels = 'aeiou'
for word in string.split():
if len(word) > 2 and word[0] not in vowels:
words.append(word[1:]+'-'+word[0]+'ay')
else:
words.append(word+'-ay')
return ' '.join(words)
if __name__ == '__main__':
x = MyString()
string = "a barbie vanquished the knights of the round table by hitting them in the face"
# word = x.find_in_iter(string)
# num = x.find_in_iter(range(10))
print(x.count_vowels(string))
# print(word, '\n', num)
#print(x.piglatin(string))