-
Notifications
You must be signed in to change notification settings - Fork 0
/
8kyu_Codewars.py
501 lines (321 loc) · 9.57 KB
/
8kyu_Codewars.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# A Needle in the Haystack
def find_needle(haystack):
return f"found the needle at position {haystack.index("needle")}"
#---------------------------------------------------
# Abbreviate a Two Word Name
def abbrev_name(name):
return name.upper().split()[0][0] + "." + name.upper().split()[1][0]
#---------------------------------------------------
# Are You Playing Banjo?
def are_you_playing_banjo(name):
return f"{name} plays banjo" if name[0].upper() == "R" else f"{name} does not play banjo"
#---------------------------------------------------
# Basic Mathematical Operations
def basic_op(operator, value1, value2):
if operator == "+":
return value1 + value2
elif operator == "-":
return value1 - value2
elif operator == "*":
return value1 * value2
elif operator == "/":
return value1 / value2
else:
return "Неверный математический оператор"
#---------------------------------------------------
# Calculate average
def find_average(numbers):
return sum(numbers) / len(numbers) if len(numbers) != 0 else 0
#---------------------------------------------------
# Calculate BMI
def bmi(weight, height):
bmi = weight / height ** 2
if bmi <= 18.5:
return "Underweight"
elif bmi <= 25.0:
return "Normal"
elif bmi <= 30.0:
return "Overweight"
else:
return "Obese"
#---------------------------------------------------
# Century From Year
def century(year):
return int((year + 99) / 100)
#---------------------------------------------------
# Beginner Series #2 Clock
def past(h, m, s):
if h > 23 or m > 59 or s > 59:
return "Error input"
else:
return h * 3600000 + m * 60000 + s * 1000
#---------------------------------------------------
# Convert a Boolean to a String
def boolean_to_string(b):
return "True" if b == True else "False"
#---------------------------------------------------
# Convert boolean values to strings 'Yes' or 'No'.
def bool_to_word(boolean):
return "Yes" if boolean == True else "No"
#---------------------------------------------------
# Convert number to reversed array of digits
def digitize(n):
return [int(x) for x in str(n)[::-1]]
#---------------------------------------------------
# Convert a Number to a String!
def number_to_string(num):
return str(num)
#---------------------------------------------------
# Convert a String to a Number!
def string_to_number(s):
return int(s)
#---------------------------------------------------
# Count by X
def count_by(x, n):
arr = [i * x for i in range(1, n + 1)]
return arr
#---------------------------------------------------
# Count of positives / sum of negatives
def count_positives_sum_negatives(arr):
if arr:
count_positive = 0
summ_negatives = 0
for i in arr:
if i > 0:
count_positive += 1
elif i <= 0:
summ_negatives += i
return [count_positive, summ_negatives] if count_positive or \
summ_negatives != 0 else [0, 0]
else:
return []
#---------------------------------------------------
# If you can't sleep, just count sheep!!
def count_sheep(n):
answer = ""
for i in range(1, n + 1):
answer += str(i) + " sheep..."
return answer
#---------------------------------------------------
# Counting sheep...
def count_sheeps(sheep):
count_sheeps = 0
for i in sheep:
if i == True:
count_sheeps += 1
return count_sheeps
#---------------------------------------------------
# DNA to RNA Conversion
def dna_to_rna(dna):
list_dna = list(dna)
list_rna = []
for i in list_dna:
if i == "T":
i = "U"
list_rna.append(i)
else:
list_rna.append(i)
str_rna = "".join(list_rna)
return str_rna
#---------------------------------------------------
# You Can't Code Under Pressure #1
def double_integer(i):
return i * 2
#---------------------------------------------------
# Even and Odds
def even_or_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
#---------------------------------------------------
# Fake Binary
def fake_bin(x):
result = ""
for num in x:
if int(num) < 5:
result = result + "0"
else:
result = result + "1"
return result
#---------------------------------------------------
# Find the smallest integer in the array
def find_smallest_int(arr):
small_int = arr[0]
for i in arr:
if i < small_int:
small_int = i
return small_int
#---------------------------------------------------
# Function 1 - hello world
def greet():
text = "hello world!"
return text
#---------------------------------------------------
# Grasshopper - Summation
def summation(num):
summ = 0
while num != 0:
summ += num
num -= 1
return summ
#---------------------------------------------------
# How good are you really?
def better_than_average(class_points, you_points):
return True if you_points > sum(class_points) / len(class_points) else False
#---------------------------------------------------
# Invert values
def invert(lst):
return [-i for i in lst]
#---------------------------------------------------
# Is he gonna survive?
def hero(bullets, dragons):
if bullets / dragons >= 2:
return True
else:
return False
#---------------------------------------------------
# Is n divisible by x and y?
def is_divisible(n, x, y):
if n % x == 0 and n % y == 0:
return True
return False
#---------------------------------------------------
# Keep Hydrated!
def litres(time):
return time // 2
#---------------------------------------------------
# Beginner - Lost Without a Map
def maps(a):
return [i * 2 for i in a]
#---------------------------------------------------
# MakeUpperCase
def make_upper_case(s):
return s.upper()
#---------------------------------------------------
# Multiply the numbers
def multiply(n):
str_n = str(n)
if n >= 0:
res = n * 5 ** len(str_n)
else:
res = n * 5 ** (len(str_n) - 1)
return res
#---------------------------------------------------
# Multiply
def multiply(a, b):
return a * b
#---------------------------------------------------
# Opposite number
def opposite(number):
return -number
#---------------------------------------------------
# Opposites Attract
def lovefunc(flower1, flower2):
if (flower1 + flower2) % 2 == 0:
return False
return True
#---------------------------------------------------
# Beginner - Reduce but Grow
def grow(arr):
mult = 1
for i in arr:
mult *= i
return mult
#---------------------------------------------------
# Remove First and Last Character
def remove_char(s):
return s[1:len(s)-1]
#---------------------------------------------------
# Remove String Spaces
def no_space(x):
return x.replace(" ", "")
#---------------------------------------------------
# Return Negative
def make_negative(number):
if number > 0:
return number * (-1)
else:
return number
#---------------------------------------------------
# Returning Strings
def greet(name):
return "Hello, {0} how are you doing today?".format(name)
#---------------------------------------------------
# Reversed sequence
def reverse_seq(n):
arr = list(range(n, 0, -1))
return arr
#---------------------------------------------------
# Reversed Strings
def solution(string):
string = string[::-1]
return string
#---------------------------------------------------
# Rock Paper Scissors!
def rps(p1, p2):
if p1 == p2:
return "Draw!"
elif (p1 == "scissors" and p2 == "paper") or (p1 == "paper" and p2 == "rock") or \
(p1 == "rock" and p2 == "scissors"):
return "Player 1 won!"
else:
return "Player 2 won!"
#---------------------------------------------------
# Beginner Series #1 School Paperwork
def paperwork(n, m):
if n < 0 or m < 0:
return 0
return n * m
#---------------------------------------------------
# Sentence Smash
def smash(words):
str_res = " ".join(words)
return str_res
#---------------------------------------------------
# Simple multiplication
def simple_multiplication(number):
return number * 8 if number % 2 == 0 else number * 9
#---------------------------------------------------
# Square(n) Sum
def square_sum(numbers):
summ = 0
for num in numbers:
squ = num * num
summ += squ
return summ
#---------------------------------------------------
# String repeat
def repeat_str(repeat, string):
return repeat * string
#---------------------------------------------------
# Sum Arrays
def sum_array(a):
summ = 0
for i in a:
summ += i
return summ
#---------------------------------------------------
# Sum of positive
def positive_sum(arr):
summ = 0
for num in arr:
if num > 0:
summ += num
return summ
#---------------------------------------------------
# Will you make it?
def zero_fuel(distance_to_pump, mpg, fuel_left):
if mpg * fuel_left >= distance_to_pump:
return True
else:
return False
#---------------------------------------------------
# You only need one - Beginner
def check(seq, elem):
for i in seq:
if i == elem:
return True
else:
return False
#---------------------------------------------------