-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodewars.py
More file actions
223 lines (180 loc) · 5.59 KB
/
codewars.py
File metadata and controls
223 lines (180 loc) · 5.59 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#CODEWARS CHALLENGES
"""
def song_decoder(song):
#ASSIGN SONG TO A VAR SO WE CAN REPLACE ALL "WUB" FOR WHITESPACE
txt = song.replace("WUB", " " )
#THEN WE PULL OUT ANY WHITESPACE LEFT BY THE REPLACE METHOD
whitespace = txt.strip()
#AT LAST IF BY ANY CHANCE THE ARE MULTIPLE WHITESPACE, I USED JOIN and split to format to 1 whitespace per letter
return ' '.join(whitespace.split())
print(song_decoder("AWUBBWUBC"), "A B C","WUB should be replaced by 1 space")
print(song_decoder("AWUBWUBWUBBWUBWUBWUBC"), "A B C","multiples WUB should be replaced by only 1 space")
print(song_decoder("WUBAWUBBWUBCWUB"), "A B C","heading or trailing spaces should be removed")
"""
"""
#GET MIDDLE CHARACTER
def get_middle(s):
return s[(len(s)-1)//2:(len(s)+2)//2]
print(get_middle("middle"),"dd")
"""
"""
#disemvowel a string using a for loop
def disemvowel(string):
for i in "aeiouAEIOU":
string = string.replace(i,"")
return string
print(disemvowel("this is aweosme"), "ths s wsm")
def disemvowel(s):
return s.translate(None, "aeiouAEIOU")
def disemvowel(string):
return "".join(c for c in string if c.lower() not in "aeiou")
"""
"""
#FILTERING A LIST OF INTS AND STRINGS
def filter_list(l):
for x in l[:]:
if isinstance(x, str):
l.remove(x)
return l
#filter_list([1,2,'a','b']) == [1,2]
#filter_list([1,'a','b',0,15]) == [1,0,15]
#filter_list([1,2,'aasf','1','123',123]) == [1,2,123]
"""
"""
def array_diff(a, b):
while a is not b:
for x in a:
print(array_diff([1,2,2], [1]), [2,2], "a was [1,2,2], b was [1], expected [2,2]")
"""
"""
#List of Lists
def open_or_senior(data):
#create and empty list to store the result at the end
lst= []
#iterate through data list of lists
for i in data:
#if iterable its more or equal than 55 and iterable its more than 7
if i[0] >= 55 and i[1]>7:
#then print a list by appendding our list var
lst.append('Senior')
#else print a list by appendding our list var
else:
lst.append('Open')
return lst
print(open_or_senior([[18, 20],[45, 2],[61, 12],[37, 6],[21, 21],[78, 9]]))
#ONE LINE ANSWER
def openOrSenior(data):
return ["Senior" if age >= 55 and handicap >= 8 else "Open" for (age, handicap) in data]
#isogram
def is_isogram(string):
clean_word = string.lower()
letter_list = []
for letter in clean_word:
if letter.isalpha():
if letter in letter_list:
return False
letter_list.append(letter)
return True
def is_isogram(string):
lower= string.lower()
for l in lower:
if lower.count(l) > 1: return False
return True
def is_isogram(string):
return len(string) == len(set(string.lower()))
"""
"""
#ENDS WITH
def solution(string, ending):
return string.endswith(ending)
print(solution('abc', 'bc'))
print(solution('abc', 'd'))
"""
"""
#DIRECTIONS REDUCTION
def dirReduc(arr):
dict = {"NORTH": "SOUTH","SOUTH":"NORTH","EAST":"WEST","WEST":"EAST"}
res = []
for i in arr:
if res and dict[i] == res[-1]:
res.pop()
else:
res.append(i)
return res
a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
test.assert_equals(dirReduc(a), ['WEST'])
u=["NORTH", "WEST", "SOUTH", "EAST"]
test.assert_equals(dirReduc(u), ["NORTH", "WEST", "SOUTH", "EAST"])
"""
""""
for item in arr:
if arr.count(item) == 1:
return item
"""
"""
import re
def sum_digits(number):
new = [int(i) for i in str(number)]
res = re.findall('[-+]?\d+', new)
return res
#print(sum_digits(-32), 5)
print(sum_digits(10), 1)
ANAGRAM
# write the function is_anagram
def is_anagram(test, original):
if (sorted(test.lower())== sorted(original.lower())):
return True
else:
return False
"""
"""
def max_sequence(arr):
iter_items = iter(arr) # -2 1 etc
try:
temp_sum = next(iter_items) # next iter item in arr
except StopIteration:
temp_sum = 0
max_sum = temp_sum
for item in iter_items:
temp_sum = max(temp_sum + item, item)
max_sum = max(temp_sum, max_sum)
return max(max_sum, 0)
#which is O(N) in time and O(1) in memory(complexity)
print(max_sequence([]), 0)
print(max_sequence([-2, 1, -3, 4, -1, 2, 1, -5, 4]), 6)
"""
""""
#if letter is upper add a space and the join the string to create the space and correct the missing space
def solution(s):
return ''.join(list(map(upper, list(s))))
def upper(letter):
'''Converts capital letters to a space and the lower case letter'''
return ' ' + letter if letter.isupper() else letter
print(solution("breakCamelCase"), "break Camel Case")
"""
#calculating with functions
"""
def zero(f = None): return 0 if not f else f(0)
def one(f = None): return 1 if not f else f(1)
def two(f = None): return 2 if not f else f(2)
def three(f = None): return 3 if not f else f(3)
def four(f = None): return 4 if not f else f(4)
def five(f = None): return 5 if not f else f(5)
def six(f = None): return 6 if not f else f(6)
def seven(f = None): return 7 if not f else f(7)
def eight(f = None): return 8 if not f else f(8)
def nine(f = None): return 9 if not f else f(9)
#a lambda function can take any number of arguments
def plus(y): return lambda x: x+y
def minus(y): return lambda x: x-y
def times(y): return lambda x: x*y
def divided_by(y): return lambda x: x//y
"""
#to the right its the function to the operator and the left number
#to the left its the function that defines the left number with the lambda function
"""
test.assert_equals(seven(times(five())), 35)
test.assert_equals(four(plus(nine())), 13)
test.assert_equals(eight(minus(three())), 5)
test.assert_equals(six(divided_by(two())), 3)
"""