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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
|
4 |
+
def has_close_elements(numbers: List[float], threshold: float) -> bool:
|
5 |
+
""" Check if in given list of numbers, are any two numbers closer to each other than
|
6 |
+
given threshold.
|
7 |
+
>>> has_close_elements([1.0, 2.0, 3.0], 0.5)
|
8 |
+
False
|
9 |
+
>>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
|
10 |
+
True
|
11 |
+
"""
|
12 |
+
|
13 |
+
from typing import List
|
14 |
+
|
15 |
+
|
16 |
+
def separate_paren_groups(paren_string: str) -> List[str]:
|
17 |
+
""" Input to this function is a string containing multiple groups of nested parentheses. Your goal is to
|
18 |
+
separate those group into separate strings and return the list of those.
|
19 |
+
Separate groups are balanced (each open brace is properly closed) and not nested within each other
|
20 |
+
Ignore any spaces in the input string.
|
21 |
+
>>> separate_paren_groups('( ) (( )) (( )( ))')
|
22 |
+
['()', '(())', '(()())']
|
23 |
+
"""
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
def truncate_number(number: float) -> float:
|
28 |
+
""" Given a positive floating point number, it can be decomposed into
|
29 |
+
and integer part (largest integer smaller than given number) and decimals
|
30 |
+
(leftover part always smaller than 1).
|
31 |
+
|
32 |
+
Return the decimal part of the number.
|
33 |
+
>>> truncate_number(3.5)
|
34 |
+
0.5
|
35 |
+
"""
|
36 |
+
|
37 |
+
from typing import List
|
38 |
+
|
39 |
+
|
40 |
+
def below_zero(operations: List[int]) -> bool:
|
41 |
+
""" You're given a list of deposit and withdrawal operations on a bank account that starts with
|
42 |
+
zero balance. Your task is to detect if at any point the balance of account fallls below zero, and
|
43 |
+
at that point function should return True. Otherwise it should return False.
|
44 |
+
>>> below_zero([1, 2, 3])
|
45 |
+
False
|
46 |
+
>>> below_zero([1, 2, -4, 5])
|
47 |
+
True
|
48 |
+
"""
|
49 |
+
|
50 |
+
from typing import List
|
51 |
+
|
52 |
+
|
53 |
+
def mean_absolute_deviation(numbers: List[float]) -> float:
|
54 |
+
""" For a given list of input numbers, calculate Mean Absolute Deviation
|
55 |
+
around the mean of this dataset.
|
56 |
+
Mean Absolute Deviation is the average absolute difference between each
|
57 |
+
element and a centerpoint (mean in this case):
|
58 |
+
MAD = average | x - x_mean |
|
59 |
+
>>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0])
|
60 |
+
1.0
|
61 |
+
"""
|
62 |
+
|
63 |
+
from typing import List
|
64 |
+
|
65 |
+
|
66 |
+
def intersperse(numbers: List[int], delimeter: int) -> List[int]:
|
67 |
+
""" Insert a number 'delimeter' between every two consecutive elements of input list `numbers'
|
68 |
+
>>> intersperse([], 4)
|
69 |
+
[]
|
70 |
+
>>> intersperse([1, 2, 3], 4)
|
71 |
+
[1, 4, 2, 4, 3]
|
72 |
+
"""
|
73 |
+
|
74 |
+
from typing import List
|
75 |
+
|
76 |
+
|
77 |
+
def parse_nested_parens(paren_string: str) -> List[int]:
|
78 |
+
""" Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
|
79 |
+
For each of the group, output the deepest level of nesting of parentheses.
|
80 |
+
E.g. (()()) has maximum two levels of nesting while ((())) has three.
|
81 |
+
|
82 |
+
>>> parse_nested_parens('(()()) ((())) () ((())()())')
|
83 |
+
[2, 3, 1, 3]
|
84 |
+
"""
|
85 |
+
|
86 |
+
from typing import List
|
87 |
+
|
88 |
+
|
89 |
+
def filter_by_substring(strings: List[str], substring: str) -> List[str]:
|
90 |
+
""" Filter an input list of strings only for ones that contain given substring
|
91 |
+
>>> filter_by_substring([], 'a')
|
92 |
+
[]
|
93 |
+
>>> filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a')
|
94 |
+
['abc', 'bacd', 'array']
|
95 |
+
"""
|
96 |
+
|
97 |
+
from typing import List, Tuple
|
98 |
+
|
99 |
+
|
100 |
+
def sum_product(numbers: List[int]) -> Tuple[int, int]:
|
101 |
+
""" For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list.
|
102 |
+
Empty sum should be equal to 0 and empty product should be equal to 1.
|
103 |
+
>>> sum_product([])
|
104 |
+
(0, 1)
|
105 |
+
>>> sum_product([1, 2, 3, 4])
|
106 |
+
(10, 24)
|
107 |
+
"""
|
108 |
+
|
109 |
+
from typing import List, Tuple
|
110 |
+
|
111 |
+
|
112 |
+
def rolling_max(numbers: List[int]) -> List[int]:
|
113 |
+
""" From a given list of integers, generate a list of rolling maximum element found until given moment
|
114 |
+
in the sequence.
|
115 |
+
>>> rolling_max([1, 2, 3, 2, 3, 4, 2])
|
116 |
+
[1, 2, 3, 3, 3, 4, 4]
|
117 |
+
"""
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
def is_palindrome(string: str) -> bool:
|
122 |
+
""" Test if given string is a palindrome """
|
123 |
+
return string == string[::-1]
|
124 |
+
|
125 |
+
|
126 |
+
def make_palindrome(string: str) -> str:
|
127 |
+
""" Find the shortest palindrome that begins with a supplied string.
|
128 |
+
Algorithm idea is simple:
|
129 |
+
- Find the longest postfix of supplied string that is a palindrome.
|
130 |
+
- Append to the end of the string reverse of a string prefix that comes before the palindromic suffix.
|
131 |
+
>>> make_palindrome('')
|
132 |
+
''
|
133 |
+
>>> make_palindrome('cat')
|
134 |
+
'catac'
|
135 |
+
>>> make_palindrome('cata')
|
136 |
+
'catac'
|
137 |
+
"""
|
138 |
+
|
139 |
+
from typing import List
|
140 |
+
|
141 |
+
|
142 |
+
def string_xor(a: str, b: str) -> str:
|
143 |
+
""" Input are two strings a and b consisting only of 1s and 0s.
|
144 |
+
Perform binary XOR on these inputs and return result also as a string.
|
145 |
+
>>> string_xor('010', '110')
|
146 |
+
'100'
|
147 |
+
"""
|
148 |
+
|
149 |
+
from typing import List, Optional
|
150 |
+
|
151 |
+
|
152 |
+
def longest(strings: List[str]) -> Optional[str]:
|
153 |
+
""" Out of list of strings, return the longest one. Return the first one in case of multiple
|
154 |
+
strings of the same length. Return None in case the input list is empty.
|
155 |
+
>>> longest([])
|
156 |
+
|
157 |
+
>>> longest(['a', 'b', 'c'])
|
158 |
+
'a'
|
159 |
+
>>> longest(['a', 'bb', 'ccc'])
|
160 |
+
'ccc'
|
161 |
+
"""
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
+
def greatest_common_divisor(a: int, b: int) -> int:
|
166 |
+
""" Return a greatest common divisor of two integers a and b
|
167 |
+
>>> greatest_common_divisor(3, 5)
|
168 |
+
1
|
169 |
+
>>> greatest_common_divisor(25, 15)
|
170 |
+
5
|
171 |
+
"""
|
172 |
+
|
173 |
+
from typing import List
|
174 |
+
|
175 |
+
|
176 |
+
def all_prefixes(string: str) -> List[str]:
|
177 |
+
""" Return list of all prefixes from shortest to longest of the input string
|
178 |
+
>>> all_prefixes('abc')
|
179 |
+
['a', 'ab', 'abc']
|
180 |
+
"""
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
def string_sequence(n: int) -> str:
|
185 |
+
""" Return a string containing space-delimited numbers starting from 0 upto n inclusive.
|
186 |
+
>>> string_sequence(0)
|
187 |
+
'0'
|
188 |
+
>>> string_sequence(5)
|
189 |
+
'0 1 2 3 4 5'
|
190 |
+
"""
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
def count_distinct_characters(string: str) -> int:
|
195 |
+
""" Given a string, find out how many distinct characters (regardless of case) does it consist of
|
196 |
+
>>> count_distinct_characters('xyzXYZ')
|
197 |
+
3
|
198 |
+
>>> count_distinct_characters('Jerry')
|
199 |
+
4
|
200 |
+
"""
|
201 |
+
|
202 |
+
from typing import List
|
203 |
+
|
204 |
+
|
205 |
+
def parse_music(music_string: str) -> List[int]:
|
206 |
+
""" Input to this function is a string representing musical notes in a special ASCII format.
|
207 |
+
Your task is to parse this string and return list of integers corresponding to how many beats does each
|
208 |
+
not last.
|
209 |
+
|
210 |
+
Here is a legend:
|
211 |
+
'o' - whole note, lasts four beats
|
212 |
+
'o|' - half note, lasts two beats
|
213 |
+
'.|' - quater note, lasts one beat
|
214 |
+
|
215 |
+
>>> parse_music('o o| .| o| o| .| .| .| .| o o')
|
216 |
+
[4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]
|
217 |
+
"""
|
218 |
+
|
219 |
+
|
220 |
+
|
221 |
+
def how_many_times(string: str, substring: str) -> int:
|
222 |
+
""" Find how many times a given substring can be found in the original string. Count overlaping cases.
|
223 |
+
>>> how_many_times('', 'a')
|
224 |
+
0
|
225 |
+
>>> how_many_times('aaa', 'a')
|
226 |
+
3
|
227 |
+
>>> how_many_times('aaaa', 'aa')
|
228 |
+
3
|
229 |
+
"""
|
230 |
+
|
231 |
+
from typing import List
|
232 |
+
|
233 |
+
|
234 |
+
def sort_numbers(numbers: str) -> str:
|
235 |
+
""" Input is a space-delimited string of numberals from 'zero' to 'nine'.
|
236 |
+
Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'.
|
237 |
+
Return the string with numbers sorted from smallest to largest
|
238 |
+
>>> sort_numbers('three one five')
|
239 |
+
'one three five'
|
240 |
+
"""
|
241 |
+
|
242 |
+
from typing import List, Tuple
|
243 |
+
|
244 |
+
|
245 |
+
def find_closest_elements(numbers: List[float]) -> Tuple[float, float]:
|
246 |
+
""" From a supplied list of numbers (of length at least two) select and return two that are the closest to each
|
247 |
+
other and return them in order (smaller number, larger number).
|
248 |
+
>>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2])
|
249 |
+
(2.0, 2.2)
|
250 |
+
>>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.0])
|
251 |
+
(2.0, 2.0)
|
252 |
+
"""
|
253 |
+
|
254 |
+
from typing import List
|
255 |
+
|
256 |
+
|
257 |
+
def rescale_to_unit(numbers: List[float]) -> List[float]:
|
258 |
+
""" Given list of numbers (of at least two elements), apply a linear transform to that list,
|
259 |
+
such that the smallest number will become 0 and the largest will become 1
|
260 |
+
>>> rescale_to_unit([1.0, 2.0, 3.0, 4.0, 5.0])
|
261 |
+
[0.0, 0.25, 0.5, 0.75, 1.0]
|
262 |
+
"""
|
263 |
+
|
264 |
+
from typing import List, Any
|
265 |
+
|
266 |
+
|
267 |
+
def filter_integers(values: List[Any]) -> List[int]:
|
268 |
+
""" Filter given list of any python values only for integers
|
269 |
+
>>> filter_integers(['a', 3.14, 5])
|
270 |
+
[5]
|
271 |
+
>>> filter_integers([1, 2, 3, 'abc', {}, []])
|
272 |
+
[1, 2, 3]
|
273 |
+
"""
|
274 |
+
|
275 |
+
|
276 |
+
|
277 |
+
def strlen(string: str) -> int:
|
278 |
+
""" Return length of given string
|
279 |
+
>>> strlen('')
|
280 |
+
0
|
281 |
+
>>> strlen('abc')
|
282 |
+
3
|
283 |
+
"""
|
284 |
+
|
285 |
+
|
286 |
+
|
287 |
+
def largest_divisor(n: int) -> int:
|
288 |
+
""" For a given number n, find the largest number that divides n evenly, smaller than n
|
289 |
+
>>> largest_divisor(15)
|
290 |
+
5
|
291 |
+
"""
|
292 |
+
|
293 |
+
from typing import List
|
294 |
+
|
295 |
+
|
296 |
+
def factorize(n: int) -> List[int]:
|
297 |
+
""" Return list of prime factors of given integer in the order from smallest to largest.
|
298 |
+
Each of the factors should be listed number of times corresponding to how many times it appeares in factorization.
|
299 |
+
Input number should be equal to the product of all factors
|
300 |
+
>>> factorize(8)
|
301 |
+
[2, 2, 2]
|
302 |
+
>>> factorize(25)
|
303 |
+
[5, 5]
|
304 |
+
>>> factorize(70)
|
305 |
+
[2, 5, 7]
|
306 |
+
"""
|
307 |
+
|
308 |
+
from typing import List
|
309 |
+
|
310 |
+
|
311 |
+
def remove_duplicates(numbers: List[int]) -> List[int]:
|
312 |
+
""" From a list of integers, remove all elements that occur more than once.
|
313 |
+
Keep order of elements left the same as in the input.
|
314 |
+
>>> remove_duplicates([1, 2, 3, 2, 4])
|
315 |
+
[1, 3, 4]
|
316 |
+
"""
|
317 |
+
|
318 |
+
|
319 |
+
|
320 |
+
def flip_case(string: str) -> str:
|
321 |
+
""" For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
|
322 |
+
>>> flip_case('Hello')
|
323 |
+
'hELLO'
|
324 |
+
"""
|
325 |
+
|
326 |
+
from typing import List
|
327 |
+
|
328 |
+
|
329 |
+
def concatenate(strings: List[str]) -> str:
|
330 |
+
""" Concatenate list of strings into a single string
|
331 |
+
>>> concatenate([])
|
332 |
+
''
|
333 |
+
>>> concatenate(['a', 'b', 'c'])
|
334 |
+
'abc'
|
335 |
+
"""
|
336 |
+
|
337 |
+
from typing import List
|
338 |
+
|
339 |
+
|
340 |
+
def filter_by_prefix(strings: List[str], prefix: str) -> List[str]:
|
341 |
+
""" Filter an input list of strings only for ones that start with a given prefix.
|
342 |
+
>>> filter_by_prefix([], 'a')
|
343 |
+
[]
|
344 |
+
>>> filter_by_prefix(['abc', 'bcd', 'cde', 'array'], 'a')
|
345 |
+
['abc', 'array']
|
346 |
+
"""
|
347 |
+
|
348 |
+
|
349 |
+
|
350 |
+
def get_positive(l: list):
|
351 |
+
"""Return only positive numbers in the list.
|
352 |
+
>>> get_positive([-1, 2, -4, 5, 6])
|
353 |
+
[2, 5, 6]
|
354 |
+
>>> get_positive([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10])
|
355 |
+
[5, 3, 2, 3, 9, 123, 1]
|
356 |
+
"""
|
357 |
+
|
358 |
+
|
359 |
+
|
360 |
+
def is_prime(n):
|
361 |
+
"""Return true if a given number is prime, and false otherwise.
|
362 |
+
>>> is_prime(6)
|
363 |
+
False
|
364 |
+
>>> is_prime(101)
|
365 |
+
True
|
366 |
+
>>> is_prime(11)
|
367 |
+
True
|
368 |
+
>>> is_prime(13441)
|
369 |
+
True
|
370 |
+
>>> is_prime(61)
|
371 |
+
True
|
372 |
+
>>> is_prime(4)
|
373 |
+
False
|
374 |
+
>>> is_prime(1)
|
375 |
+
False
|
376 |
+
"""
|
377 |
+
|
378 |
+
import math
|
379 |
+
|
380 |
+
|
381 |
+
def poly(xs: list, x: float):
|
382 |
+
"""
|
383 |
+
Evaluates polynomial with coefficients xs at point x.
|
384 |
+
return xs[0] + xs[1] * x + xs[1] * x^2 + .... xs[n] * x^n
|
385 |
+
"""
|
386 |
+
return sum([coeff * math.pow(x, i) for i, coeff in enumerate(xs)])
|
387 |
+
|
388 |
+
|
389 |
+
def find_zero(xs: list):
|
390 |
+
""" xs are coefficients of a polynomial.
|
391 |
+
find_zero find x such that poly(x) = 0.
|
392 |
+
find_zero returns only only zero point, even if there are many.
|
393 |
+
Moreover, find_zero only takes list xs having even number of coefficients
|
394 |
+
and largest non zero coefficient as it guarantees
|
395 |
+
a solution.
|
396 |
+
>>> round(find_zero([1, 2]), 2) # f(x) = 1 + 2x
|
397 |
+
-0.5
|
398 |
+
>>> round(find_zero([-6, 11, -6, 1]), 2) # (x - 1) * (x - 2) * (x - 3) = -6 + 11x - 6x^2 + x^3
|
399 |
+
1.0
|
400 |
+
"""
|
401 |
+
|
402 |
+
|
403 |
+
|
404 |
+
def sort_third(l: list):
|
405 |
+
"""This function takes a list l and returns a list l' such that
|
406 |
+
l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal
|
407 |
+
to the values of the corresponding indicies of l, but sorted.
|
408 |
+
>>> sort_third([1, 2, 3])
|
409 |
+
[1, 2, 3]
|
410 |
+
>>> sort_third([5, 6, 3, 4, 8, 9, 2])
|
411 |
+
[2, 6, 3, 4, 8, 9, 5]
|
412 |
+
"""
|
413 |
+
|
414 |
+
|
415 |
+
|
416 |
+
def unique(l: list):
|
417 |
+
"""Return sorted unique elements in a list
|
418 |
+
>>> unique([5, 3, 5, 2, 3, 3, 9, 0, 123])
|
419 |
+
[0, 2, 3, 5, 9, 123]
|
420 |
+
"""
|
421 |
+
|
422 |
+
|
423 |
+
|
424 |
+
def max_element(l: list):
|
425 |
+
"""Return maximum element in the list.
|
426 |
+
>>> max_element([1, 2, 3])
|
427 |
+
3
|
428 |
+
>>> max_element([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10])
|
429 |
+
123
|
430 |
+
"""
|
431 |
+
|
432 |
+
|
433 |
+
|
434 |
+
def fizz_buzz(n: int):
|
435 |
+
"""Return the number of times the digit 7 appears in integers less than n which are divisible by 11 or 13.
|
436 |
+
>>> fizz_buzz(50)
|
437 |
+
0
|
438 |
+
>>> fizz_buzz(78)
|
439 |
+
2
|
440 |
+
>>> fizz_buzz(79)
|
441 |
+
3
|
442 |
+
"""
|
443 |
+
|
444 |
+
|
445 |
+
|
446 |
+
def sort_even(l: list):
|
447 |
+
"""This function takes a list l and returns a list l' such that
|
448 |
+
l' is identical to l in the odd indicies, while its values at the even indicies are equal
|
449 |
+
to the values of the even indicies of l, but sorted.
|
450 |
+
>>> sort_even([1, 2, 3])
|
451 |
+
[1, 2, 3]
|
452 |
+
>>> sort_even([5, 6, 3, 4])
|
453 |
+
[3, 6, 5, 4]
|
454 |
+
"""
|
455 |
+
|
456 |
+
|
457 |
+
|
458 |
+
def encode_cyclic(s: str):
|
459 |
+
"""
|
460 |
+
returns encoded string by cycling groups of three characters.
|
461 |
+
"""
|
462 |
+
# split string to groups. Each of length 3.
|
463 |
+
groups = [s[(3 * i):min((3 * i + 3), len(s))] for i in range((len(s) + 2) // 3)]
|
464 |
+
# cycle elements in each group. Unless group has fewer elements than 3.
|
465 |
+
groups = [(group[1:] + group[0]) if len(group) == 3 else group for group in groups]
|
466 |
+
return "".join(groups)
|
467 |
+
|
468 |
+
|
469 |
+
def decode_cyclic(s: str):
|
470 |
+
"""
|
471 |
+
takes as input string encoded with encode_cyclic function. Returns decoded string.
|
472 |
+
"""
|
473 |
+
|
474 |
+
|
475 |
+
|
476 |
+
def prime_fib(n: int):
|
477 |
+
"""
|
478 |
+
prime_fib returns n-th number that is a Fibonacci number and it's also prime.
|
479 |
+
>>> prime_fib(1)
|
480 |
+
2
|
481 |
+
>>> prime_fib(2)
|
482 |
+
3
|
483 |
+
>>> prime_fib(3)
|
484 |
+
5
|
485 |
+
>>> prime_fib(4)
|
486 |
+
13
|
487 |
+
>>> prime_fib(5)
|
488 |
+
89
|
489 |
+
"""
|
490 |
+
|
491 |
+
|
492 |
+
|
493 |
+
def triples_sum_to_zero(l: list):
|
494 |
+
"""
|
495 |
+
triples_sum_to_zero takes a list of integers as an input.
|
496 |
+
it returns True if there are three distinct elements in the list that
|
497 |
+
sum to zero, and False otherwise.
|
498 |
+
|
499 |
+
>>> triples_sum_to_zero([1, 3, 5, 0])
|
500 |
+
False
|
501 |
+
>>> triples_sum_to_zero([1, 3, -2, 1])
|
502 |
+
True
|
503 |
+
>>> triples_sum_to_zero([1, 2, 3, 7])
|
504 |
+
False
|
505 |
+
>>> triples_sum_to_zero([2, 4, -5, 3, 9, 7])
|
506 |
+
True
|
507 |
+
>>> triples_sum_to_zero([1])
|
508 |
+
False
|
509 |
+
"""
|
510 |
+
|
511 |
+
|
512 |
+
|
513 |
+
def car_race_collision(n: int):
|
514 |
+
"""
|
515 |
+
Imagine a road that's a perfectly straight infinitely long line.
|
516 |
+
n cars are driving left to right; simultaneously, a different set of n cars
|
517 |
+
are driving right to left. The two sets of cars start out being very far from
|
518 |
+
each other. All cars move in the same speed. Two cars are said to collide
|
519 |
+
when a car that's moving left to right hits a car that's moving right to left.
|
520 |
+
However, the cars are infinitely sturdy and strong; as a result, they continue moving
|
521 |
+
in their trajectory as if they did not collide.
|
522 |
+
|
523 |
+
This function outputs the number of such collisions.
|
524 |
+
"""
|
525 |
+
|
526 |
+
|
527 |
+
|
528 |
+
def incr_list(l: list):
|
529 |
+
"""Return list with elements incremented by 1.
|
530 |
+
>>> incr_list([1, 2, 3])
|
531 |
+
[2, 3, 4]
|
532 |
+
>>> incr_list([5, 3, 5, 2, 3, 3, 9, 0, 123])
|
533 |
+
[6, 4, 6, 3, 4, 4, 10, 1, 124]
|
534 |
+
"""
|
535 |
+
|
536 |
+
|
537 |
+
|
538 |
+
def pairs_sum_to_zero(l):
|
539 |
+
"""
|
540 |
+
pairs_sum_to_zero takes a list of integers as an input.
|
541 |
+
it returns True if there are two distinct elements in the list that
|
542 |
+
sum to zero, and False otherwise.
|
543 |
+
>>> pairs_sum_to_zero([1, 3, 5, 0])
|
544 |
+
False
|
545 |
+
>>> pairs_sum_to_zero([1, 3, -2, 1])
|
546 |
+
False
|
547 |
+
>>> pairs_sum_to_zero([1, 2, 3, 7])
|
548 |
+
False
|
549 |
+
>>> pairs_sum_to_zero([2, 4, -5, 3, 5, 7])
|
550 |
+
True
|
551 |
+
>>> pairs_sum_to_zero([1])
|
552 |
+
False
|
553 |
+
"""
|
554 |
+
|
555 |
+
|
556 |
+
|
557 |
+
def change_base(x: int, base: int):
|
558 |
+
"""Change numerical base of input number x to base.
|
559 |
+
return string representation after the conversion.
|
560 |
+
base numbers are less than 10.
|
561 |
+
>>> change_base(8, 3)
|
562 |
+
'22'
|
563 |
+
>>> change_base(8, 2)
|
564 |
+
'1000'
|
565 |
+
>>> change_base(7, 2)
|
566 |
+
'111'
|
567 |
+
"""
|
568 |
+
|
569 |
+
|
570 |
+
|
571 |
+
def triangle_area(a, h):
|
572 |
+
"""Given length of a side and high return area for a triangle.
|
573 |
+
>>> triangle_area(5, 3)
|
574 |
+
7.5
|
575 |
+
"""
|
576 |
+
|
577 |
+
|
578 |
+
|
579 |
+
def fib4(n: int):
|
580 |
+
"""The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
|
581 |
+
fib4(0) -> 0
|
582 |
+
fib4(1) -> 0
|
583 |
+
fib4(2) -> 2
|
584 |
+
fib4(3) -> 0
|
585 |
+
fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4).
|
586 |
+
Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion.
|
587 |
+
>>> fib4(5)
|
588 |
+
4
|
589 |
+
>>> fib4(6)
|
590 |
+
8
|
591 |
+
>>> fib4(7)
|
592 |
+
14
|
593 |
+
"""
|
594 |
+
|
595 |
+
|
596 |
+
|
597 |
+
def median(l: list):
|
598 |
+
"""Return median of elements in the list l.
|
599 |
+
>>> median([3, 1, 2, 4, 5])
|
600 |
+
3
|
601 |
+
>>> median([-10, 4, 6, 1000, 10, 20])
|
602 |
+
15.0
|
603 |
+
"""
|
604 |
+
|
605 |
+
|
606 |
+
|
607 |
+
def is_palindrome(text: str):
|
608 |
+
"""
|
609 |
+
Checks if given string is a palindrome
|
610 |
+
>>> is_palindrome('')
|
611 |
+
True
|
612 |
+
>>> is_palindrome('aba')
|
613 |
+
True
|
614 |
+
>>> is_palindrome('aaaaa')
|
615 |
+
True
|
616 |
+
>>> is_palindrome('zbcd')
|
617 |
+
False
|
618 |
+
"""
|
619 |
+
|
620 |
+
|
621 |
+
|
622 |
+
def modp(n: int, p: int):
|
623 |
+
"""Return 2^n modulo p (be aware of numerics).
|
624 |
+
>>> modp(3, 5)
|
625 |
+
3
|
626 |
+
>>> modp(1101, 101)
|
627 |
+
2
|
628 |
+
>>> modp(0, 101)
|
629 |
+
1
|
630 |
+
>>> modp(3, 11)
|
631 |
+
8
|
632 |
+
>>> modp(100, 101)
|
633 |
+
1
|
634 |
+
"""
|
635 |
+
|
636 |
+
|
637 |
+
|
638 |
+
def encode_shift(s: str):
|
639 |
+
"""
|
640 |
+
returns encoded string by shifting every character by 5 in the alphabet.
|
641 |
+
"""
|
642 |
+
return "".join([chr(((ord(ch) + 5 - ord("a")) % 26) + ord("a")) for ch in s])
|
643 |
+
|
644 |
+
|
645 |
+
def decode_shift(s: str):
|
646 |
+
"""
|
647 |
+
takes as input string encoded with encode_shift function. Returns decoded string.
|
648 |
+
"""
|
649 |
+
|
650 |
+
|
651 |
+
|
652 |
+
def remove_vowels(text):
|
653 |
+
"""
|
654 |
+
remove_vowels is a function that takes string and returns string without vowels.
|
655 |
+
>>> remove_vowels('')
|
656 |
+
''
|
657 |
+
>>> remove_vowels("abcdef\nghijklm")
|
658 |
+
'bcdf\nghjklm'
|
659 |
+
>>> remove_vowels('abcdef')
|
660 |
+
'bcdf'
|
661 |
+
>>> remove_vowels('aaaaa')
|
662 |
+
''
|
663 |
+
>>> remove_vowels('aaBAA')
|
664 |
+
'B'
|
665 |
+
>>> remove_vowels('zbcd')
|
666 |
+
'zbcd'
|
667 |
+
"""
|
668 |
+
|
669 |
+
|
670 |
+
|
671 |
+
def below_threshold(l: list, t: int):
|
672 |
+
"""Return True if all numbers in the list l are below threshold t.
|
673 |
+
>>> below_threshold([1, 2, 4, 10], 100)
|
674 |
+
True
|
675 |
+
>>> below_threshold([1, 20, 4, 10], 5)
|
676 |
+
False
|
677 |
+
"""
|
678 |
+
|
679 |
+
|
680 |
+
|
681 |
+
def add(x: int, y: int):
|
682 |
+
"""Add two numbers x and y
|
683 |
+
>>> add(2, 3)
|
684 |
+
5
|
685 |
+
>>> add(5, 7)
|
686 |
+
12
|
687 |
+
"""
|
688 |
+
|
689 |
+
|
690 |
+
|
691 |
+
def same_chars(s0: str, s1: str):
|
692 |
+
"""
|
693 |
+
Check if two words have the same characters.
|
694 |
+
>>> same_chars('eabcdzzzz', 'dddzzzzzzzddeddabc')
|
695 |
+
True
|
696 |
+
>>> same_chars('abcd', 'dddddddabc')
|
697 |
+
True
|
698 |
+
>>> same_chars('dddddddabc', 'abcd')
|
699 |
+
True
|
700 |
+
>>> same_chars('eabcd', 'dddddddabc')
|
701 |
+
False
|
702 |
+
>>> same_chars('abcd', 'dddddddabce')
|
703 |
+
False
|
704 |
+
>>> same_chars('eabcdzzzz', 'dddzzzzzzzddddabc')
|
705 |
+
False
|
706 |
+
"""
|
707 |
+
|
708 |
+
|
709 |
+
|
710 |
+
def fib(n: int):
|
711 |
+
"""Return n-th Fibonacci number.
|
712 |
+
>>> fib(10)
|
713 |
+
55
|
714 |
+
>>> fib(1)
|
715 |
+
1
|
716 |
+
>>> fib(8)
|
717 |
+
21
|
718 |
+
"""
|
719 |
+
|
720 |
+
|
721 |
+
|
722 |
+
def correct_bracketing(brackets: str):
|
723 |
+
""" brackets is a string of "<" and ">".
|
724 |
+
return True if every opening bracket has a corresponding closing bracket.
|
725 |
+
|
726 |
+
>>> correct_bracketing("<")
|
727 |
+
False
|
728 |
+
>>> correct_bracketing("<>")
|
729 |
+
True
|
730 |
+
>>> correct_bracketing("<<><>>")
|
731 |
+
True
|
732 |
+
>>> correct_bracketing("><<>")
|
733 |
+
False
|
734 |
+
"""
|
735 |
+
|
736 |
+
|
737 |
+
|
738 |
+
def monotonic(l: list):
|
739 |
+
"""Return True is list elements are monotonically increasing or decreasing.
|
740 |
+
>>> monotonic([1, 2, 4, 20])
|
741 |
+
True
|
742 |
+
>>> monotonic([1, 20, 4, 10])
|
743 |
+
False
|
744 |
+
>>> monotonic([4, 1, 0, -10])
|
745 |
+
True
|
746 |
+
"""
|
747 |
+
|
748 |
+
|
749 |
+
|
750 |
+
def common(l1: list, l2: list):
|
751 |
+
"""Return sorted unique common elements for two lists.
|
752 |
+
>>> common([1, 4, 3, 34, 653, 2, 5], [5, 7, 1, 5, 9, 653, 121])
|
753 |
+
[1, 5, 653]
|
754 |
+
>>> common([5, 3, 2, 8], [3, 2])
|
755 |
+
[2, 3]
|
756 |
+
|
757 |
+
"""
|
758 |
+
|
759 |
+
|
760 |
+
|
761 |
+
def largest_prime_factor(n: int):
|
762 |
+
"""Return the largest prime factor of n. Assume n > 1 and is not a prime.
|
763 |
+
>>> largest_prime_factor(13195)
|
764 |
+
29
|
765 |
+
>>> largest_prime_factor(2048)
|
766 |
+
2
|
767 |
+
"""
|
768 |
+
|
769 |
+
|
770 |
+
|
771 |
+
def sum_to_n(n: int):
|
772 |
+
"""sum_to_n is a function that sums numbers from 1 to n.
|
773 |
+
>>> sum_to_n(30)
|
774 |
+
465
|
775 |
+
>>> sum_to_n(100)
|
776 |
+
5050
|
777 |
+
>>> sum_to_n(5)
|
778 |
+
15
|
779 |
+
>>> sum_to_n(10)
|
780 |
+
55
|
781 |
+
>>> sum_to_n(1)
|
782 |
+
1
|
783 |
+
"""
|
784 |
+
|
785 |
+
|
786 |
+
|
787 |
+
def correct_bracketing(brackets: str):
|
788 |
+
""" brackets is a string of "(" and ")".
|
789 |
+
return True if every opening bracket has a corresponding closing bracket.
|
790 |
+
|
791 |
+
>>> correct_bracketing("(")
|
792 |
+
False
|
793 |
+
>>> correct_bracketing("()")
|
794 |
+
True
|
795 |
+
>>> correct_bracketing("(()())")
|
796 |
+
True
|
797 |
+
>>> correct_bracketing(")(()")
|
798 |
+
False
|
799 |
+
"""
|
800 |
+
|
801 |
+
|
802 |
+
|
803 |
+
def derivative(xs: list):
|
804 |
+
""" xs represent coefficients of a polynomial.
|
805 |
+
xs[0] + xs[1] * x + xs[2] * x^2 + ....
|
806 |
+
Return derivative of this polynomial in the same form.
|
807 |
+
>>> derivative([3, 1, 2, 4, 5])
|
808 |
+
[1, 4, 12, 20]
|
809 |
+
>>> derivative([1, 2, 3])
|
810 |
+
[2, 6]
|
811 |
+
"""
|
812 |
+
|
813 |
+
|
814 |
+
|
815 |
+
def fibfib(n: int):
|
816 |
+
"""The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:
|
817 |
+
fibfib(0) == 0
|
818 |
+
fibfib(1) == 0
|
819 |
+
fibfib(2) == 1
|
820 |
+
fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3).
|
821 |
+
Please write a function to efficiently compute the n-th element of the fibfib number sequence.
|
822 |
+
>>> fibfib(1)
|
823 |
+
0
|
824 |
+
>>> fibfib(5)
|
825 |
+
4
|
826 |
+
>>> fibfib(8)
|
827 |
+
24
|
828 |
+
"""
|
829 |
+
|
830 |
+
|
831 |
+
FIX = """
|
832 |
+
Add more test cases.
|
833 |
+
"""
|
834 |
+
|
835 |
+
def vowels_count(s):
|
836 |
+
"""Write a function vowels_count which takes a string representing
|
837 |
+
a word as input and returns the number of vowels in the string.
|
838 |
+
Vowels in this case are 'a', 'e', 'i', 'o', 'u'. Here, 'y' is also a
|
839 |
+
vowel, but only when it is at the end of the given word.
|
840 |
+
|
841 |
+
Example:
|
842 |
+
>>> vowels_count("abcde")
|
843 |
+
2
|
844 |
+
>>> vowels_count("ACEDY")
|
845 |
+
3
|
846 |
+
"""
|
847 |
+
|
848 |
+
|
849 |
+
def circular_shift(x, shift):
|
850 |
+
"""Circular shift the digits of the integer x, shift the digits right by shift
|
851 |
+
and return the result as a string.
|
852 |
+
If shift > number of digits, return digits reversed.
|
853 |
+
>>> circular_shift(12, 1)
|
854 |
+
"21"
|
855 |
+
>>> circular_shift(12, 2)
|
856 |
+
"12"
|
857 |
+
"""
|
858 |
+
|
859 |
+
|
860 |
+
def digitSum(s):
|
861 |
+
"""Task
|
862 |
+
Write a function that takes a string as input and returns the sum of the upper characters only'
|
863 |
+
ASCII codes.
|
864 |
+
|
865 |
+
Examples:
|
866 |
+
digitSum("") => 0
|
867 |
+
digitSum("abAB") => 131
|
868 |
+
digitSum("abcCd") => 67
|
869 |
+
digitSum("helloE") => 69
|
870 |
+
digitSum("woArBld") => 131
|
871 |
+
digitSum("aAaaaXa") => 153
|
872 |
+
"""
|
873 |
+
|
874 |
+
|
875 |
+
def fruit_distribution(s,n):
|
876 |
+
"""
|
877 |
+
In this task, you will be given a string that represents a number of apples and oranges
|
878 |
+
that are distributed in a basket of fruit this basket contains
|
879 |
+
apples, oranges, and mango fruits. Given the string that represents the total number of
|
880 |
+
the oranges and apples and an integer that represent the total number of the fruits
|
881 |
+
in the basket return the number of the mango fruits in the basket.
|
882 |
+
for examble:
|
883 |
+
fruit_distribution("5 apples and 6 oranges", 19) ->19 - 5 - 6 = 8
|
884 |
+
fruit_distribution("0 apples and 1 oranges",3) -> 3 - 0 - 1 = 2
|
885 |
+
fruit_distribution("2 apples and 3 oranges", 100) -> 100 - 2 - 3 = 95
|
886 |
+
fruit_distribution("100 apples and 1 oranges",120) -> 120 - 100 - 1 = 19
|
887 |
+
"""
|
888 |
+
|
889 |
+
|
890 |
+
def pluck(arr):
|
891 |
+
"""
|
892 |
+
"Given an array representing a branch of a tree that has non-negative integer nodes
|
893 |
+
your task is to pluck one of the nodes and return it.
|
894 |
+
The plucked node should be the node with the smallest even value.
|
895 |
+
If multiple nodes with the same smallest even value are found return the node that has smallest index.
|
896 |
+
|
897 |
+
The plucked node should be returned in a list, [ smalest_value, its index ],
|
898 |
+
If there are no even values or the given array is empty, return [].
|
899 |
+
|
900 |
+
Example 1:
|
901 |
+
Input: [4,2,3]
|
902 |
+
Output: [2, 1]
|
903 |
+
Explanation: 2 has the smallest even value, and 2 has the smallest index.
|
904 |
+
|
905 |
+
Example 2:
|
906 |
+
Input: [1,2,3]
|
907 |
+
Output: [2, 1]
|
908 |
+
Explanation: 2 has the smallest even value, and 2 has the smallest index.
|
909 |
+
|
910 |
+
Example 3:
|
911 |
+
Input: []
|
912 |
+
Output: []
|
913 |
+
|
914 |
+
Example 4:
|
915 |
+
Input: [5, 0, 3, 0, 4, 2]
|
916 |
+
Output: [0, 1]
|
917 |
+
Explanation: 0 is the smallest value, but there are two zeros,
|
918 |
+
so we will choose the first zero, which has the smallest index.
|
919 |
+
|
920 |
+
Constraints:
|
921 |
+
* 1 <= nodes.length <= 10000
|
922 |
+
* 0 <= node.value
|
923 |
+
"""
|
924 |
+
|
925 |
+
|
926 |
+
def search(lst):
|
927 |
+
'''
|
928 |
+
You are given a non-empty list of positive integers. Return the greatest integer that is greater than
|
929 |
+
zero, and has a frequency greater than or equal to the value of the integer itself.
|
930 |
+
The frequency of an integer is the number of times it appears in the list.
|
931 |
+
If no such a value exist, return -1.
|
932 |
+
Examples:
|
933 |
+
search([4, 1, 2, 2, 3, 1]) == 2
|
934 |
+
search([1, 2, 2, 3, 3, 3, 4, 4, 4]) == 3
|
935 |
+
search([5, 5, 4, 4, 4]) == -1
|
936 |
+
'''
|
937 |
+
|
938 |
+
|
939 |
+
def strange_sort_list(lst):
|
940 |
+
'''
|
941 |
+
Given list of integers, return list in strange order.
|
942 |
+
Strange sorting, is when you start with the minimum value,
|
943 |
+
then maximum of the remaining integers, then minimum and so on.
|
944 |
+
|
945 |
+
Examples:
|
946 |
+
strange_sort_list([1, 2, 3, 4]) == [1, 4, 2, 3]
|
947 |
+
strange_sort_list([5, 5, 5, 5]) == [5, 5, 5, 5]
|
948 |
+
strange_sort_list([]) == []
|
949 |
+
'''
|
950 |
+
|
951 |
+
|
952 |
+
def triangle_area(a, b, c):
|
953 |
+
'''
|
954 |
+
Given the lengths of the three sides of a triangle. Return the area of
|
955 |
+
the triangle rounded to 2 decimal points if the three sides form a valid triangle.
|
956 |
+
Otherwise return -1
|
957 |
+
Three sides make a valid triangle when the sum of any two sides is greater
|
958 |
+
than the third side.
|
959 |
+
Example:
|
960 |
+
triangle_area(3, 4, 5) == 6.00
|
961 |
+
triangle_area(1, 2, 10) == -1
|
962 |
+
'''
|
963 |
+
|
964 |
+
|
965 |
+
def will_it_fly(q,w):
|
966 |
+
'''
|
967 |
+
Write a function that returns True if the object q will fly, and False otherwise.
|
968 |
+
The object q will fly if it's balanced (it is a palindromic list) and the sum of its elements is less than or equal the maximum possible weight w.
|
969 |
+
|
970 |
+
Example:
|
971 |
+
will_it_fly([1, 2], 5) ➞ False
|
972 |
+
# 1+2 is less than the maximum possible weight, but it's unbalanced.
|
973 |
+
|
974 |
+
will_it_fly([3, 2, 3], 1) ➞ False
|
975 |
+
# it's balanced, but 3+2+3 is more than the maximum possible weight.
|
976 |
+
|
977 |
+
will_it_fly([3, 2, 3], 9) ➞ True
|
978 |
+
# 3+2+3 is less than the maximum possible weight, and it's balanced.
|
979 |
+
|
980 |
+
will_it_fly([3], 5) ➞ True
|
981 |
+
# 3 is less than the maximum possible weight, and it's balanced.
|
982 |
+
'''
|
983 |
+
|
984 |
+
|
985 |
+
def smallest_change(arr):
|
986 |
+
"""
|
987 |
+
Given an array arr of integers, find the minimum number of elements that
|
988 |
+
need to be changed to make the array palindromic. A palindromic array is an array that
|
989 |
+
is read the same backwards and forwards. In one change, you can change one element to any other element.
|
990 |
+
|
991 |
+
For example:
|
992 |
+
smallest_change([1,2,3,5,4,7,9,6]) == 4
|
993 |
+
smallest_change([1, 2, 3, 4, 3, 2, 2]) == 1
|
994 |
+
smallest_change([1, 2, 3, 2, 1]) == 0
|
995 |
+
"""
|
996 |
+
|
997 |
+
|
998 |
+
def total_match(lst1, lst2):
|
999 |
+
'''
|
1000 |
+
Write a function that accepts two lists of strings and returns the list that has
|
1001 |
+
total number of chars in the all strings of the list less than the other list.
|
1002 |
+
|
1003 |
+
if the two lists have the same number of chars, return the first list.
|
1004 |
+
|
1005 |
+
Examples
|
1006 |
+
total_match([], []) ➞ []
|
1007 |
+
total_match(['hi', 'admin'], ['hI', 'Hi']) ➞ ['hI', 'Hi']
|
1008 |
+
total_match(['hi', 'admin'], ['hi', 'hi', 'admin', 'project']) ➞ ['hi', 'admin']
|
1009 |
+
total_match(['hi', 'admin'], ['hI', 'hi', 'hi']) ➞ ['hI', 'hi', 'hi']
|
1010 |
+
total_match(['4'], ['1', '2', '3', '4', '5']) ➞ ['4']
|
1011 |
+
'''
|
1012 |
+
|
1013 |
+
|
1014 |
+
def is_multiply_prime(a):
|
1015 |
+
"""Write a function that returns true if the given number is the multiplication of 3 prime numbers
|
1016 |
+
and false otherwise.
|
1017 |
+
Knowing that (a) is less then 100.
|
1018 |
+
Example:
|
1019 |
+
is_multiply_prime(30) == True
|
1020 |
+
30 = 2 * 3 * 5
|
1021 |
+
"""
|
1022 |
+
|
1023 |
+
|
1024 |
+
def is_simple_power(x, n):
|
1025 |
+
"""Your task is to write a function that returns true if a number x is a simple
|
1026 |
+
power of n and false in other cases.
|
1027 |
+
x is a simple power of n if n**int=x
|
1028 |
+
For example:
|
1029 |
+
is_simple_power(1, 4) => true
|
1030 |
+
is_simple_power(2, 2) => true
|
1031 |
+
is_simple_power(8, 2) => true
|
1032 |
+
is_simple_power(3, 2) => false
|
1033 |
+
is_simple_power(3, 1) => false
|
1034 |
+
is_simple_power(5, 3) => false
|
1035 |
+
"""
|
1036 |
+
|
1037 |
+
|
1038 |
+
def iscube(a):
|
1039 |
+
'''
|
1040 |
+
Write a function that takes an integer a and returns True
|
1041 |
+
if this ingeger is a cube of some integer number.
|
1042 |
+
Note: you may assume the input is always valid.
|
1043 |
+
Examples:
|
1044 |
+
iscube(1) ==> True
|
1045 |
+
iscube(2) ==> False
|
1046 |
+
iscube(-1) ==> True
|
1047 |
+
iscube(64) ==> True
|
1048 |
+
iscube(0) ==> True
|
1049 |
+
iscube(180) ==> False
|
1050 |
+
'''
|
1051 |
+
|
1052 |
+
|
1053 |
+
def hex_key(num):
|
1054 |
+
"""You have been tasked to write a function that receives
|
1055 |
+
a hexadecimal number as a string and counts the number of hexadecimal
|
1056 |
+
digits that are primes (prime number, or a prime, is a natural number
|
1057 |
+
greater than 1 that is not a product of two smaller natural numbers).
|
1058 |
+
Hexadecimal digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
|
1059 |
+
Prime numbers are 2, 3, 5, 7, 11, 13, 17,...
|
1060 |
+
So you have to determine a number of the following digits: 2, 3, 5, 7,
|
1061 |
+
B (=decimal 11), D (=decimal 13).
|
1062 |
+
Note: you may assume the input is always correct or empty string,
|
1063 |
+
and symbols A,B,C,D,E,F are always uppercase.
|
1064 |
+
Examples:
|
1065 |
+
For num = "AB" the output should be 1.
|
1066 |
+
For num = "1077E" the output should be 2.
|
1067 |
+
For num = "ABED1A33" the output should be 4.
|
1068 |
+
For num = "123456789ABCDEF0" the output should be 6.
|
1069 |
+
For num = "2020" the output should be 2.
|
1070 |
+
"""
|
1071 |
+
|
1072 |
+
|
1073 |
+
def decimal_to_binary(decimal):
|
1074 |
+
"""You will be given a number in decimal form and your task is to convert it to
|
1075 |
+
binary format. The function should return a string, with each character representing a binary
|
1076 |
+
number. Each character in the string will be '0' or '1'.
|
1077 |
+
|
1078 |
+
There will be an extra couple of characters 'db' at the beginning and at the end of the string.
|
1079 |
+
The extra characters are there to help with the format.
|
1080 |
+
|
1081 |
+
Examples:
|
1082 |
+
decimal_to_binary(15) # returns "db1111db"
|
1083 |
+
decimal_to_binary(32) # returns "db100000db"
|
1084 |
+
"""
|
1085 |
+
|
1086 |
+
|
1087 |
+
def is_happy(s):
|
1088 |
+
"""You are given a string s.
|
1089 |
+
Your task is to check if the string is happy or not.
|
1090 |
+
A string is happy if its length is at least 3 and every 3 consecutive letters are distinct
|
1091 |
+
For example:
|
1092 |
+
is_happy(a) => False
|
1093 |
+
is_happy(aa) => False
|
1094 |
+
is_happy(abcd) => True
|
1095 |
+
is_happy(aabb) => False
|
1096 |
+
is_happy(adb) => True
|
1097 |
+
is_happy(xyy) => False
|
1098 |
+
"""
|
1099 |
+
|
1100 |
+
|
1101 |
+
def numerical_letter_grade(grades):
|
1102 |
+
"""It is the last week of the semester and the teacher has to give the grades
|
1103 |
+
to students. The teacher has been making her own algorithm for grading.
|
1104 |
+
The only problem is, she has lost the code she used for grading.
|
1105 |
+
She has given you a list of GPAs for some students and you have to write
|
1106 |
+
a function that can output a list of letter grades using the following table:
|
1107 |
+
GPA | Letter grade
|
1108 |
+
4.0 A+
|
1109 |
+
> 3.7 A
|
1110 |
+
> 3.3 A-
|
1111 |
+
> 3.0 B+
|
1112 |
+
> 2.7 B
|
1113 |
+
> 2.3 B-
|
1114 |
+
> 2.0 C+
|
1115 |
+
> 1.7 C
|
1116 |
+
> 1.3 C-
|
1117 |
+
> 1.0 D+
|
1118 |
+
> 0.7 D
|
1119 |
+
> 0.0 D-
|
1120 |
+
0.0 E
|
1121 |
+
|
1122 |
+
|
1123 |
+
Example:
|
1124 |
+
grade_equation([4.0, 3, 1.7, 2, 3.5]) ==> ['A+', 'B', 'C-', 'C', 'A-']
|
1125 |
+
"""
|
1126 |
+
|
1127 |
+
|
1128 |
+
def prime_length(string):
|
1129 |
+
"""Write a function that takes a string and returns True if the string
|
1130 |
+
length is a prime number or False otherwise
|
1131 |
+
Examples
|
1132 |
+
prime_length('Hello') == True
|
1133 |
+
prime_length('abcdcba') == True
|
1134 |
+
prime_length('kittens') == True
|
1135 |
+
prime_length('orange') == False
|
1136 |
+
"""
|
1137 |
+
|
1138 |
+
|
1139 |
+
def starts_one_ends(n):
|
1140 |
+
"""
|
1141 |
+
Given a positive integer n, return the count of the numbers of n-digit
|
1142 |
+
positive integers that start or end with 1.
|
1143 |
+
"""
|
1144 |
+
|
1145 |
+
|
1146 |
+
def solve(N):
|
1147 |
+
"""Given a positive integer N, return the total sum of its digits in binary.
|
1148 |
+
|
1149 |
+
Example
|
1150 |
+
For N = 1000, the sum of digits will be 1 the output should be "1".
|
1151 |
+
For N = 150, the sum of digits will be 6 the output should be "110".
|
1152 |
+
For N = 147, the sum of digits will be 12 the output should be "1100".
|
1153 |
+
|
1154 |
+
Variables:
|
1155 |
+
@N integer
|
1156 |
+
Constraints: 0 ≤ N ≤ 10000.
|
1157 |
+
Output:
|
1158 |
+
a string of binary number
|
1159 |
+
"""
|
1160 |
+
|
1161 |
+
|
1162 |
+
def add(lst):
|
1163 |
+
"""Given a non-empty list of integers lst. add the even elements that are at odd indices..
|
1164 |
+
|
1165 |
+
|
1166 |
+
Examples:
|
1167 |
+
add([4, 2, 6, 7]) ==> 2
|
1168 |
+
"""
|
1169 |
+
|
1170 |
+
|
1171 |
+
def anti_shuffle(s):
|
1172 |
+
"""
|
1173 |
+
Write a function that takes a string and returns an ordered version of it.
|
1174 |
+
Ordered version of string, is a string where all words (separated by space)
|
1175 |
+
are replaced by a new word where all the characters arranged in
|
1176 |
+
ascending order based on ascii value.
|
1177 |
+
Note: You should keep the order of words and blank spaces in the sentence.
|
1178 |
+
|
1179 |
+
For example:
|
1180 |
+
anti_shuffle('Hi') returns 'Hi'
|
1181 |
+
anti_shuffle('hello') returns 'ehllo'
|
1182 |
+
anti_shuffle('Hello World!!!') returns 'Hello !!!Wdlor'
|
1183 |
+
"""
|
1184 |
+
|
1185 |
+
|
1186 |
+
def get_row(lst, x):
|
1187 |
+
"""
|
1188 |
+
You are given a 2 dimensional data, as a nested lists,
|
1189 |
+
which is similar to matrix, however, unlike matrices,
|
1190 |
+
each row may contain a different number of columns.
|
1191 |
+
Given lst, and integer x, find integers x in the list,
|
1192 |
+
and return list of tuples, [(x1, y1), (x2, y2) ...] such that
|
1193 |
+
each tuple is a coordinate - (row, columns), starting with 0.
|
1194 |
+
Sort coordinates initially by rows in ascending order.
|
1195 |
+
Also, sort coordinates of the row by columns in descending order.
|
1196 |
+
|
1197 |
+
Examples:
|
1198 |
+
get_row([
|
1199 |
+
[1,2,3,4,5,6],
|
1200 |
+
[1,2,3,4,1,6],
|
1201 |
+
[1,2,3,4,5,1]
|
1202 |
+
], 1) == [(0, 0), (1, 4), (1, 0), (2, 5), (2, 0)]
|
1203 |
+
get_row([], 1) == []
|
1204 |
+
get_row([[], [1], [1, 2, 3]], 3) == [(2, 2)]
|
1205 |
+
"""
|
1206 |
+
|
1207 |
+
|
1208 |
+
def sort_array(array):
|
1209 |
+
"""
|
1210 |
+
Given an array of non-negative integers, return a copy of the given array after sorting,
|
1211 |
+
you will sort the given array in ascending order if the sum( first index value, last index value) is odd,
|
1212 |
+
or sort it in descending order if the sum( first index value, last index value) is even.
|
1213 |
+
|
1214 |
+
Note:
|
1215 |
+
* don't change the given array.
|
1216 |
+
|
1217 |
+
Examples:
|
1218 |
+
* sort_array([]) => []
|
1219 |
+
* sort_array([5]) => [5]
|
1220 |
+
* sort_array([2, 4, 3, 0, 1, 5]) => [0, 1, 2, 3, 4, 5]
|
1221 |
+
* sort_array([2, 4, 3, 0, 1, 5, 6]) => [6, 5, 4, 3, 2, 1, 0]
|
1222 |
+
"""
|
1223 |
+
|
1224 |
+
|
1225 |
+
def encrypt(s):
|
1226 |
+
"""Create a function encrypt that takes a string as an argument and
|
1227 |
+
returns a string encrypted with the alphabet being rotated.
|
1228 |
+
The alphabet should be rotated in a manner such that the letters
|
1229 |
+
shift down by two multiplied to two places.
|
1230 |
+
For example:
|
1231 |
+
encrypt('hi') returns 'lm'
|
1232 |
+
encrypt('asdfghjkl') returns 'ewhjklnop'
|
1233 |
+
encrypt('gf') returns 'kj'
|
1234 |
+
encrypt('et') returns 'ix'
|
1235 |
+
"""
|
1236 |
+
|
1237 |
+
|
1238 |
+
def next_smallest(lst):
|
1239 |
+
"""
|
1240 |
+
You are given a list of integers.
|
1241 |
+
Write a function next_smallest() that returns the 2nd smallest element of the list.
|
1242 |
+
Return None if there is no such element.
|
1243 |
+
|
1244 |
+
next_smallest([1, 2, 3, 4, 5]) == 2
|
1245 |
+
next_smallest([5, 1, 4, 3, 2]) == 2
|
1246 |
+
next_smallest([]) == None
|
1247 |
+
next_smallest([1, 1]) == None
|
1248 |
+
"""
|
1249 |
+
|
1250 |
+
|
1251 |
+
def is_bored(S):
|
1252 |
+
"""
|
1253 |
+
You'll be given a string of words, and your task is to count the number
|
1254 |
+
of boredoms. A boredom is a sentence that starts with the word "I".
|
1255 |
+
Sentences are delimited by '.', '?' or '!'.
|
1256 |
+
|
1257 |
+
For example:
|
1258 |
+
>>> is_bored("Hello world")
|
1259 |
+
0
|
1260 |
+
>>> is_bored("The sky is blue. The sun is shining. I love this weather")
|
1261 |
+
1
|
1262 |
+
"""
|
1263 |
+
|
1264 |
+
|
1265 |
+
def any_int(x, y, z):
|
1266 |
+
'''
|
1267 |
+
Create a function that takes 3 numbers.
|
1268 |
+
Returns true if one of the numbers is equal to the sum of the other two, and all numbers are integers.
|
1269 |
+
Returns false in any other cases.
|
1270 |
+
|
1271 |
+
Examples
|
1272 |
+
any_int(5, 2, 7) ➞ True
|
1273 |
+
|
1274 |
+
any_int(3, 2, 2) ➞ False
|
1275 |
+
|
1276 |
+
any_int(3, -2, 1) ➞ True
|
1277 |
+
|
1278 |
+
any_int(3.6, -2.2, 2) ➞ False
|
1279 |
+
|
1280 |
+
|
1281 |
+
|
1282 |
+
'''
|
1283 |
+
|
1284 |
+
|
1285 |
+
def encode(message):
|
1286 |
+
"""
|
1287 |
+
Write a function that takes a message, and encodes in such a
|
1288 |
+
way that it swaps case of all letters, replaces all vowels in
|
1289 |
+
the message with the letter that appears 2 places ahead of that
|
1290 |
+
vowel in the english alphabet.
|
1291 |
+
Assume only letters.
|
1292 |
+
|
1293 |
+
Examples:
|
1294 |
+
>>> encode('test')
|
1295 |
+
'TGST'
|
1296 |
+
>>> encode('This is a message')
|
1297 |
+
'tHKS KS C MGSSCGG'
|
1298 |
+
"""
|
1299 |
+
|
1300 |
+
|
1301 |
+
|
1302 |
+
def skjkasdkd(lst):
|
1303 |
+
"""You are given a list of integers.
|
1304 |
+
You need to find the largest prime value and return the sum of its digits.
|
1305 |
+
|
1306 |
+
Examples:
|
1307 |
+
For lst = [0,3,2,1,3,5,7,4,5,5,5,2,181,32,4,32,3,2,32,324,4,3] the output should be 10
|
1308 |
+
For lst = [1,0,1,8,2,4597,2,1,3,40,1,2,1,2,4,2,5,1] the output should be 25
|
1309 |
+
For lst = [1,3,1,32,5107,34,83278,109,163,23,2323,32,30,1,9,3] the output should be 13
|
1310 |
+
For lst = [0,724,32,71,99,32,6,0,5,91,83,0,5,6] the output should be 11
|
1311 |
+
For lst = [0,81,12,3,1,21] the output should be 3
|
1312 |
+
For lst = [0,8,1,2,1,7] the output should be 7
|
1313 |
+
"""
|
1314 |
+
|
1315 |
+
|
1316 |
+
def check_dict_case(dict):
|
1317 |
+
"""
|
1318 |
+
Given a dictionary, return True if all keys are strings in lower
|
1319 |
+
case or all keys are strings in upper case, else return False.
|
1320 |
+
The function should return False is the given dictionary is empty.
|
1321 |
+
Examples:
|
1322 |
+
check_dict_case({"a":"apple", "b":"banana"}) should return True.
|
1323 |
+
check_dict_case({"a":"apple", "A":"banana", "B":"banana"}) should return False.
|
1324 |
+
check_dict_case({"a":"apple", 8:"banana", "a":"apple"}) should return False.
|
1325 |
+
check_dict_case({"Name":"John", "Age":"36", "City":"Houston"}) should return False.
|
1326 |
+
check_dict_case({"STATE":"NC", "ZIP":"12345" }) should return True.
|
1327 |
+
"""
|
1328 |
+
|
1329 |
+
|
1330 |
+
def count_up_to(n):
|
1331 |
+
"""Implement a function that takes an non-negative integer and returns an array of the first n
|
1332 |
+
integers that are prime numbers and less than n.
|
1333 |
+
for example:
|
1334 |
+
count_up_to(5) => [2,3]
|
1335 |
+
count_up_to(11) => [2,3,5,7]
|
1336 |
+
count_up_to(0) => []
|
1337 |
+
count_up_to(20) => [2,3,5,7,11,13,17,19]
|
1338 |
+
count_up_to(1) => []
|
1339 |
+
count_up_to(18) => [2,3,5,7,11,13,17]
|
1340 |
+
"""
|
1341 |
+
|
1342 |
+
|
1343 |
+
def multiply(a, b):
|
1344 |
+
"""Complete the function that takes two integers and returns
|
1345 |
+
the product of their unit digits.
|
1346 |
+
Assume the input is always valid.
|
1347 |
+
Examples:
|
1348 |
+
multiply(148, 412) should return 16.
|
1349 |
+
multiply(19, 28) should return 72.
|
1350 |
+
multiply(2020, 1851) should return 0.
|
1351 |
+
multiply(14,-15) should return 20.
|
1352 |
+
"""
|
1353 |
+
|
1354 |
+
|
1355 |
+
def count_upper(s):
|
1356 |
+
"""
|
1357 |
+
Given a string s, count the number of uppercase vowels in even indices.
|
1358 |
+
|
1359 |
+
For example:
|
1360 |
+
count_upper('aBCdEf') returns 1
|
1361 |
+
count_upper('abcdefg') returns 0
|
1362 |
+
count_upper('dBBE') returns 0
|
1363 |
+
"""
|
1364 |
+
|
1365 |
+
|
1366 |
+
def closest_integer(value):
|
1367 |
+
'''
|
1368 |
+
Create a function that takes a value (string) representing a number
|
1369 |
+
and returns the closest integer to it. If the number is equidistant
|
1370 |
+
from two integers, round it away from zero.
|
1371 |
+
|
1372 |
+
Examples
|
1373 |
+
>>> closest_integer("10")
|
1374 |
+
10
|
1375 |
+
>>> closest_integer("15.3")
|
1376 |
+
15
|
1377 |
+
|
1378 |
+
Note:
|
1379 |
+
Rounding away from zero means that if the given number is equidistant
|
1380 |
+
from two integers, the one you should return is the one that is the
|
1381 |
+
farthest from zero. For example closest_integer("14.5") should
|
1382 |
+
return 15 and closest_integer("-14.5") should return -15.
|
1383 |
+
'''
|
1384 |
+
|
1385 |
+
|
1386 |
+
def make_a_pile(n):
|
1387 |
+
"""
|
1388 |
+
Given a positive integer n, you have to make a pile of n levels of stones.
|
1389 |
+
The first level has n stones.
|
1390 |
+
The number of stones in the next level is:
|
1391 |
+
- the next odd number if n is odd.
|
1392 |
+
- the next even number if n is even.
|
1393 |
+
Return the number of stones in each level in a list, where element at index
|
1394 |
+
i represents the number of stones in the level (i+1).
|
1395 |
+
|
1396 |
+
Examples:
|
1397 |
+
>>> make_a_pile(3)
|
1398 |
+
[3, 5, 7]
|
1399 |
+
"""
|
1400 |
+
|
1401 |
+
|
1402 |
+
def words_string(s):
|
1403 |
+
"""
|
1404 |
+
You will be given a string of words separated by commas or spaces. Your task is
|
1405 |
+
to split the string into words and return an array of the words.
|
1406 |
+
|
1407 |
+
For example:
|
1408 |
+
words_string("Hi, my name is John") == ["Hi", "my", "name", "is", "John"]
|
1409 |
+
words_string("One, two, three, four, five, six") == ["One", "two", "three", "four", "five", "six"]
|
1410 |
+
"""
|
1411 |
+
|
1412 |
+
|
1413 |
+
def choose_num(x, y):
|
1414 |
+
"""This function takes two positive numbers x and y and returns the
|
1415 |
+
biggest even integer number that is in the range [x, y] inclusive. If
|
1416 |
+
there's no such number, then the function should return -1.
|
1417 |
+
|
1418 |
+
For example:
|
1419 |
+
choose_num(12, 15) = 14
|
1420 |
+
choose_num(13, 12) = -1
|
1421 |
+
"""
|
1422 |
+
|
1423 |
+
|
1424 |
+
def rounded_avg(n, m):
|
1425 |
+
"""You are given two positive integers n and m, and your task is to compute the
|
1426 |
+
average of the integers from n through m (including n and m).
|
1427 |
+
Round the answer to the nearest integer and convert that to binary.
|
1428 |
+
If n is greater than m, return -1.
|
1429 |
+
Example:
|
1430 |
+
rounded_avg(1, 5) => "0b11"
|
1431 |
+
rounded_avg(7, 5) => -1
|
1432 |
+
rounded_avg(10, 20) => "0b1111"
|
1433 |
+
rounded_avg(20, 33) => "0b11010"
|
1434 |
+
"""
|
1435 |
+
|
1436 |
+
|
1437 |
+
def unique_digits(x):
|
1438 |
+
"""Given a list of positive integers x. return a sorted list of all
|
1439 |
+
elements that hasn't any even digit.
|
1440 |
+
|
1441 |
+
Note: Returned list should be sorted in increasing order.
|
1442 |
+
|
1443 |
+
For example:
|
1444 |
+
>>> unique_digits([15, 33, 1422, 1])
|
1445 |
+
[1, 15, 33]
|
1446 |
+
>>> unique_digits([152, 323, 1422, 10])
|
1447 |
+
[]
|
1448 |
+
"""
|
1449 |
+
|
1450 |
+
|
1451 |
+
def by_length(arr):
|
1452 |
+
"""
|
1453 |
+
Given an array of integers, sort the integers that are between 1 and 9 inclusive,
|
1454 |
+
reverse the resulting array, and then replace each digit by its corresponding name from
|
1455 |
+
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine".
|
1456 |
+
|
1457 |
+
For example:
|
1458 |
+
arr = [2, 1, 1, 4, 5, 8, 2, 3]
|
1459 |
+
-> sort arr -> [1, 1, 2, 2, 3, 4, 5, 8]
|
1460 |
+
-> reverse arr -> [8, 5, 4, 3, 2, 2, 1, 1]
|
1461 |
+
return ["Eight", "Five", "Four", "Three", "Two", "Two", "One", "One"]
|
1462 |
+
|
1463 |
+
If the array is empty, return an empty array:
|
1464 |
+
arr = []
|
1465 |
+
return []
|
1466 |
+
|
1467 |
+
If the array has any strange number ignore it:
|
1468 |
+
arr = [1, -1 , 55]
|
1469 |
+
-> sort arr -> [-1, 1, 55]
|
1470 |
+
-> reverse arr -> [55, 1, -1]
|
1471 |
+
return = ['One']
|
1472 |
+
"""
|
1473 |
+
|
1474 |
+
|
1475 |
+
def f(n):
|
1476 |
+
""" Implement the function f that takes n as a parameter,
|
1477 |
+
and returns a list of size n, such that the value of the element at index i is the factorial of i if i is even
|
1478 |
+
or the sum of numbers from 1 to i otherwise.
|
1479 |
+
i starts from 1.
|
1480 |
+
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
|
1481 |
+
Example:
|
1482 |
+
f(5) == [1, 2, 6, 24, 15]
|
1483 |
+
"""
|
1484 |
+
|
1485 |
+
|
1486 |
+
def even_odd_palindrome(n):
|
1487 |
+
"""
|
1488 |
+
Given a positive integer n, return a tuple that has the number of even and odd
|
1489 |
+
integer palindromes that fall within the range(1, n), inclusive.
|
1490 |
+
|
1491 |
+
Example 1:
|
1492 |
+
|
1493 |
+
Input: 3
|
1494 |
+
Output: (1, 2)
|
1495 |
+
Explanation:
|
1496 |
+
Integer palindrome are 1, 2, 3. one of them is even, and two of them are odd.
|
1497 |
+
|
1498 |
+
Example 2:
|
1499 |
+
|
1500 |
+
Input: 12
|
1501 |
+
Output: (4, 6)
|
1502 |
+
Explanation:
|
1503 |
+
Integer palindrome are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11. four of them are even, and 6 of them are odd.
|
1504 |
+
|
1505 |
+
Note:
|
1506 |
+
1. 1 <= n <= 10^3
|
1507 |
+
2. returned tuple has the number of even and odd integer palindromes respectively.
|
1508 |
+
"""
|
1509 |
+
|
1510 |
+
|
1511 |
+
def count_nums(arr):
|
1512 |
+
"""
|
1513 |
+
Write a function count_nums which takes an array of integers and returns
|
1514 |
+
the number of elements which has a sum of digits > 0.
|
1515 |
+
If a number is negative, then its first signed digit will be negative:
|
1516 |
+
e.g. -123 has signed digits -1, 2, and 3.
|
1517 |
+
>>> count_nums([]) == 0
|
1518 |
+
>>> count_nums([-1, 11, -11]) == 1
|
1519 |
+
>>> count_nums([1, 1, 2]) == 3
|
1520 |
+
"""
|
1521 |
+
|
1522 |
+
|
1523 |
+
def move_one_ball(arr):
|
1524 |
+
"""We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The
|
1525 |
+
numbers in the array will be randomly ordered. Your task is to determine if
|
1526 |
+
it is possible to get an array sorted in non-decreasing order by performing
|
1527 |
+
the following operation on the given array:
|
1528 |
+
You are allowed to perform right shift operation any number of times.
|
1529 |
+
|
1530 |
+
One right shift operation means shifting all elements of the array by one
|
1531 |
+
position in the right direction. The last element of the array will be moved to
|
1532 |
+
the starting position in the array i.e. 0th index.
|
1533 |
+
|
1534 |
+
If it is possible to obtain the sorted array by performing the above operation
|
1535 |
+
then return True else return False.
|
1536 |
+
If the given array is empty then return True.
|
1537 |
+
|
1538 |
+
Note: The given list is guaranteed to have unique elements.
|
1539 |
+
|
1540 |
+
For Example:
|
1541 |
+
|
1542 |
+
move_one_ball([3, 4, 5, 1, 2])==>True
|
1543 |
+
Explanation: By performin 2 right shift operations, non-decreasing order can
|
1544 |
+
be achieved for the given array.
|
1545 |
+
move_one_ball([3, 5, 4, 1, 2])==>False
|
1546 |
+
Explanation:It is not possible to get non-decreasing order for the given
|
1547 |
+
array by performing any number of right shift operations.
|
1548 |
+
|
1549 |
+
"""
|
1550 |
+
|
1551 |
+
|
1552 |
+
def exchange(lst1, lst2):
|
1553 |
+
"""In this problem, you will implement a function that takes two lists of numbers,
|
1554 |
+
and determines whether it is possible to perform an exchange of elements
|
1555 |
+
between them to make lst1 a list of only even numbers.
|
1556 |
+
There is no limit on the number of exchanged elements between lst1 and lst2.
|
1557 |
+
If it is possible to exchange elements between the lst1 and lst2 to make
|
1558 |
+
all the elements of lst1 to be even, return "YES".
|
1559 |
+
Otherwise, return "NO".
|
1560 |
+
For example:
|
1561 |
+
exchange([1, 2, 3, 4], [1, 2, 3, 4]) => "YES"
|
1562 |
+
exchange([1, 2, 3, 4], [1, 5, 3, 4]) => "NO"
|
1563 |
+
It is assumed that the input lists will be non-empty.
|
1564 |
+
"""
|
1565 |
+
|
1566 |
+
|
1567 |
+
def histogram(test):
|
1568 |
+
"""Given a string representing a space separated lowercase letters, return a dictionary
|
1569 |
+
of the letter with the most repetition and containing the corresponding count.
|
1570 |
+
If several letters have the same occurrence, return all of them.
|
1571 |
+
|
1572 |
+
Example:
|
1573 |
+
histogram('a b c') == {'a': 1, 'b': 1, 'c': 1}
|
1574 |
+
histogram('a b b a') == {'a': 2, 'b': 2}
|
1575 |
+
histogram('a b c a b') == {'a': 2, 'b': 2}
|
1576 |
+
histogram('b b b b a') == {'b': 4}
|
1577 |
+
histogram('') == {}
|
1578 |
+
|
1579 |
+
"""
|
1580 |
+
|
1581 |
+
|
1582 |
+
def reverse_delete(s,c):
|
1583 |
+
"""Task
|
1584 |
+
We are given two strings s and c, you have to deleted all the characters in s that are equal to any character in c
|
1585 |
+
then check if the result string is palindrome.
|
1586 |
+
A string is called palindrome if it reads the same backward as forward.
|
1587 |
+
You should return a tuple containing the result string and True/False for the check.
|
1588 |
+
Example
|
1589 |
+
For s = "abcde", c = "ae", the result should be ('bcd',False)
|
1590 |
+
For s = "abcdef", c = "b" the result should be ('acdef',False)
|
1591 |
+
For s = "abcdedcba", c = "ab", the result should be ('cdedc',True)
|
1592 |
+
"""
|
1593 |
+
|
1594 |
+
|
1595 |
+
def odd_count(lst):
|
1596 |
+
"""Given a list of strings, where each string consists of only digits, return a list.
|
1597 |
+
Each element i of the output should be "the number of odd elements in the
|
1598 |
+
string i of the input." where all the i's should be replaced by the number
|
1599 |
+
of odd digits in the i'th string of the input.
|
1600 |
+
|
1601 |
+
>>> odd_count(['1234567'])
|
1602 |
+
["the number of odd elements 4n the str4ng 4 of the 4nput."]
|
1603 |
+
>>> odd_count(['3',"11111111"])
|
1604 |
+
["the number of odd elements 1n the str1ng 1 of the 1nput.",
|
1605 |
+
"the number of odd elements 8n the str8ng 8 of the 8nput."]
|
1606 |
+
"""
|
1607 |
+
|
1608 |
+
|
1609 |
+
def minSubArraySum(nums):
|
1610 |
+
"""
|
1611 |
+
Given an array of integers nums, find the minimum sum of any non-empty sub-array
|
1612 |
+
of nums.
|
1613 |
+
Example
|
1614 |
+
minSubArraySum([2, 3, 4, 1, 2, 4]) == 1
|
1615 |
+
minSubArraySum([-1, -2, -3]) == -6
|
1616 |
+
"""
|
1617 |
+
|
1618 |
+
|
1619 |
+
def max_fill(grid, capacity):
|
1620 |
+
import math
|
1621 |
+
"""
|
1622 |
+
You are given a rectangular grid of wells. Each row represents a single well,
|
1623 |
+
and each 1 in a row represents a single unit of water.
|
1624 |
+
Each well has a corresponding bucket that can be used to extract water from it,
|
1625 |
+
and all buckets have the same capacity.
|
1626 |
+
Your task is to use the buckets to empty the wells.
|
1627 |
+
Output the number of times you need to lower the buckets.
|
1628 |
+
|
1629 |
+
Example 1:
|
1630 |
+
Input:
|
1631 |
+
grid : [[0,0,1,0], [0,1,0,0], [1,1,1,1]]
|
1632 |
+
bucket_capacity : 1
|
1633 |
+
Output: 6
|
1634 |
+
|
1635 |
+
Example 2:
|
1636 |
+
Input:
|
1637 |
+
grid : [[0,0,1,1], [0,0,0,0], [1,1,1,1], [0,1,1,1]]
|
1638 |
+
bucket_capacity : 2
|
1639 |
+
Output: 5
|
1640 |
+
|
1641 |
+
Example 3:
|
1642 |
+
Input:
|
1643 |
+
grid : [[0,0,0], [0,0,0]]
|
1644 |
+
bucket_capacity : 5
|
1645 |
+
Output: 0
|
1646 |
+
|
1647 |
+
Constraints:
|
1648 |
+
* all wells have the same length
|
1649 |
+
* 1 <= grid.length <= 10^2
|
1650 |
+
* 1 <= grid[:,1].length <= 10^2
|
1651 |
+
* grid[i][j] -> 0 | 1
|
1652 |
+
* 1 <= capacity <= 10
|
1653 |
+
"""
|
1654 |
+
|
1655 |
+
|
1656 |
+
def sort_array(arr):
|
1657 |
+
"""
|
1658 |
+
In this Kata, you have to sort an array of non-negative integers according to
|
1659 |
+
number of ones in their binary representation in ascending order.
|
1660 |
+
For similar number of ones, sort based on decimal value.
|
1661 |
+
|
1662 |
+
It must be implemented like this:
|
1663 |
+
>>> sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5]
|
1664 |
+
>>> sort_array([-2, -3, -4, -5, -6]) == [-6, -5, -4, -3, -2]
|
1665 |
+
>>> sort_array([1, 0, 2, 3, 4]) [0, 1, 2, 3, 4]
|
1666 |
+
"""
|
1667 |
+
|
1668 |
+
|
1669 |
+
def select_words(s, n):
|
1670 |
+
"""Given a string s and a natural number n, you have been tasked to implement
|
1671 |
+
a function that returns a list of all words from string s that contain exactly
|
1672 |
+
n consonants, in order these words appear in the string s.
|
1673 |
+
If the string s is empty then the function should return an empty list.
|
1674 |
+
Note: you may assume the input string contains only letters and spaces.
|
1675 |
+
Examples:
|
1676 |
+
select_words("Mary had a little lamb", 4) ==> ["little"]
|
1677 |
+
select_words("Mary had a little lamb", 3) ==> ["Mary", "lamb"]
|
1678 |
+
select_words("simple white space", 2) ==> []
|
1679 |
+
select_words("Hello world", 4) ==> ["world"]
|
1680 |
+
select_words("Uncle sam", 3) ==> ["Uncle"]
|
1681 |
+
"""
|
1682 |
+
|
1683 |
+
|
1684 |
+
def get_closest_vowel(word):
|
1685 |
+
"""You are given a word. Your task is to find the closest vowel that stands between
|
1686 |
+
two consonants from the right side of the word (case sensitive).
|
1687 |
+
|
1688 |
+
Vowels in the beginning and ending doesn't count. Return empty string if you didn't
|
1689 |
+
find any vowel met the above condition.
|
1690 |
+
|
1691 |
+
You may assume that the given string contains English letter only.
|
1692 |
+
|
1693 |
+
Example:
|
1694 |
+
get_closest_vowel("yogurt") ==> "u"
|
1695 |
+
get_closest_vowel("FULL") ==> "U"
|
1696 |
+
get_closest_vowel("quick") ==> ""
|
1697 |
+
get_closest_vowel("ab") ==> ""
|
1698 |
+
"""
|
1699 |
+
|
1700 |
+
|
1701 |
+
def match_parens(lst):
|
1702 |
+
'''
|
1703 |
+
You are given a list of two strings, both strings consist of open
|
1704 |
+
parentheses '(' or close parentheses ')' only.
|
1705 |
+
Your job is to check if it is possible to concatenate the two strings in
|
1706 |
+
some order, that the resulting string will be good.
|
1707 |
+
A string S is considered to be good if and only if all parentheses in S
|
1708 |
+
are balanced. For example: the string '(())()' is good, while the string
|
1709 |
+
'())' is not.
|
1710 |
+
Return 'Yes' if there's a way to make a good string, and return 'No' otherwise.
|
1711 |
+
|
1712 |
+
Examples:
|
1713 |
+
match_parens(['()(', ')']) == 'Yes'
|
1714 |
+
match_parens([')', ')']) == 'No'
|
1715 |
+
'''
|
1716 |
+
|
1717 |
+
|
1718 |
+
def maximum(arr, k):
|
1719 |
+
"""
|
1720 |
+
Given an array arr of integers and a positive integer k, return a sorted list
|
1721 |
+
of length k with the maximum k numbers in arr.
|
1722 |
+
|
1723 |
+
Example 1:
|
1724 |
+
|
1725 |
+
Input: arr = [-3, -4, 5], k = 3
|
1726 |
+
Output: [-4, -3, 5]
|
1727 |
+
|
1728 |
+
Example 2:
|
1729 |
+
|
1730 |
+
Input: arr = [4, -4, 4], k = 2
|
1731 |
+
Output: [4, 4]
|
1732 |
+
|
1733 |
+
Example 3:
|
1734 |
+
|
1735 |
+
Input: arr = [-3, 2, 1, 2, -1, -2, 1], k = 1
|
1736 |
+
Output: [2]
|
1737 |
+
|
1738 |
+
Note:
|
1739 |
+
1. The length of the array will be in the range of [1, 1000].
|
1740 |
+
2. The elements in the array will be in the range of [-1000, 1000].
|
1741 |
+
3. 0 <= k <= len(arr)
|
1742 |
+
"""
|
1743 |
+
|
1744 |
+
|
1745 |
+
def solution(lst):
|
1746 |
+
"""Given a non-empty list of integers, return the sum of all of the odd elements that are in even positions.
|
1747 |
+
|
1748 |
+
|
1749 |
+
Examples
|
1750 |
+
solution([5, 8, 7, 1]) ==> 12
|
1751 |
+
solution([3, 3, 3, 3, 3]) ==> 9
|
1752 |
+
solution([30, 13, 24, 321]) ==>0
|
1753 |
+
"""
|
1754 |
+
|
1755 |
+
|
1756 |
+
def add_elements(arr, k):
|
1757 |
+
"""
|
1758 |
+
Given a non-empty array of integers arr and an integer k, return
|
1759 |
+
the sum of the elements with at most two digits from the first k elements of arr.
|
1760 |
+
|
1761 |
+
Example:
|
1762 |
+
|
1763 |
+
Input: arr = [111,21,3,4000,5,6,7,8,9], k = 4
|
1764 |
+
Output: 24 # sum of 21 + 3
|
1765 |
+
|
1766 |
+
Constraints:
|
1767 |
+
1. 1 <= len(arr) <= 100
|
1768 |
+
2. 1 <= k <= len(arr)
|
1769 |
+
"""
|
1770 |
+
|
1771 |
+
|
1772 |
+
def get_odd_collatz(n):
|
1773 |
+
"""
|
1774 |
+
Given a positive integer n, return a sorted list that has the odd numbers in collatz sequence.
|
1775 |
+
|
1776 |
+
The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined
|
1777 |
+
as follows: start with any positive integer n. Then each term is obtained from the
|
1778 |
+
previous term as follows: if the previous term is even, the next term is one half of
|
1779 |
+
the previous term. If the previous term is odd, the next term is 3 times the previous
|
1780 |
+
term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.
|
1781 |
+
|
1782 |
+
Note:
|
1783 |
+
1. Collatz(1) is [1].
|
1784 |
+
2. returned list sorted in increasing order.
|
1785 |
+
|
1786 |
+
For example:
|
1787 |
+
get_odd_collatz(5) returns [1, 5] # The collatz sequence for 5 is [5, 16, 8, 4, 2, 1], so the odd numbers are only 1, and 5.
|
1788 |
+
"""
|
1789 |
+
|
1790 |
+
|
1791 |
+
def valid_date(date):
|
1792 |
+
"""You have to write a function which validates a given date string and
|
1793 |
+
returns True if the date is valid otherwise False.
|
1794 |
+
The date is valid if all of the following rules are satisfied:
|
1795 |
+
1. The date string is not empty.
|
1796 |
+
2. The number of days is not less than 1 or higher than 31 days for months 1,3,5,7,8,10,12. And the number of days is not less than 1 or higher than 30 days for months 4,6,9,11. And, the number of days is not less than 1 or higher than 29 for the month 2.
|
1797 |
+
3. The months should not be less than 1 or higher than 12.
|
1798 |
+
4. The date should be in the format: mm-dd-yyyy
|
1799 |
+
|
1800 |
+
for example:
|
1801 |
+
valid_date('03-11-2000') => True
|
1802 |
+
|
1803 |
+
valid_date('15-01-2012') => False
|
1804 |
+
|
1805 |
+
valid_date('04-0-2040') => False
|
1806 |
+
|
1807 |
+
valid_date('06-04-2020') => True
|
1808 |
+
|
1809 |
+
valid_date('06/04/2020') => False
|
1810 |
+
"""
|
1811 |
+
|
1812 |
+
|
1813 |
+
def split_words(txt):
|
1814 |
+
'''
|
1815 |
+
Given a string of words, return a list of words split on whitespace, if no whitespaces exists in the text you
|
1816 |
+
should split on commas ',' if no commas exists you should return the number of lower-case letters with odd order in the
|
1817 |
+
alphabet, ord('a') = 0, ord('b') = 1, ... ord('z') = 25
|
1818 |
+
Examples
|
1819 |
+
split_words("Hello world!") ➞ ["Hello", "world!"]
|
1820 |
+
split_words("Hello,world!") ➞ ["Hello", "world!"]
|
1821 |
+
split_words("abcdef") == 3
|
1822 |
+
'''
|
1823 |
+
|
1824 |
+
|
1825 |
+
def is_sorted(lst):
|
1826 |
+
'''
|
1827 |
+
Given a list of numbers, return whether or not they are sorted
|
1828 |
+
in ascending order. If list has more than 1 duplicate of the same
|
1829 |
+
number, return False. Assume no negative numbers and only integers.
|
1830 |
+
|
1831 |
+
Examples
|
1832 |
+
is_sorted([5]) ➞ True
|
1833 |
+
is_sorted([1, 2, 3, 4, 5]) ➞ True
|
1834 |
+
is_sorted([1, 3, 2, 4, 5]) ➞ False
|
1835 |
+
is_sorted([1, 2, 3, 4, 5, 6]) ➞ True
|
1836 |
+
is_sorted([1, 2, 3, 4, 5, 6, 7]) ➞ True
|
1837 |
+
is_sorted([1, 3, 2, 4, 5, 6, 7]) ➞ False
|
1838 |
+
is_sorted([1, 2, 2, 3, 3, 4]) ➞ True
|
1839 |
+
is_sorted([1, 2, 2, 2, 3, 4]) ➞ False
|
1840 |
+
'''
|
1841 |
+
|
1842 |
+
|
1843 |
+
def intersection(interval1, interval2):
|
1844 |
+
"""You are given two intervals,
|
1845 |
+
where each interval is a pair of integers. For example, interval = (start, end) = (1, 2).
|
1846 |
+
The given intervals are closed which means that the interval (start, end)
|
1847 |
+
includes both start and end.
|
1848 |
+
For each given interval, it is assumed that its start is less or equal its end.
|
1849 |
+
Your task is to determine whether the length of intersection of these two
|
1850 |
+
intervals is a prime number.
|
1851 |
+
Example, the intersection of the intervals (1, 3), (2, 4) is (2, 3)
|
1852 |
+
which its length is 1, which not a prime number.
|
1853 |
+
If the length of the intersection is a prime number, return "YES",
|
1854 |
+
otherwise, return "NO".
|
1855 |
+
If the two intervals don't intersect, return "NO".
|
1856 |
+
|
1857 |
+
|
1858 |
+
[input/output] samples:
|
1859 |
+
intersection((1, 2), (2, 3)) ==> "NO"
|
1860 |
+
intersection((-1, 1), (0, 4)) ==> "NO"
|
1861 |
+
intersection((-3, -1), (-5, 5)) ==> "YES"
|
1862 |
+
"""
|
1863 |
+
|
1864 |
+
|
1865 |
+
def prod_signs(arr):
|
1866 |
+
"""
|
1867 |
+
You are given an array arr of integers and you need to return
|
1868 |
+
sum of magnitudes of integers multiplied by product of all signs
|
1869 |
+
of each number in the array, represented by 1, -1 or 0.
|
1870 |
+
Note: return None for empty arr.
|
1871 |
+
|
1872 |
+
Example:
|
1873 |
+
>>> prod_signs([1, 2, 2, -4]) == -9
|
1874 |
+
>>> prod_signs([0, 1]) == 0
|
1875 |
+
>>> prod_signs([]) == None
|
1876 |
+
"""
|
1877 |
+
|
1878 |
+
|
1879 |
+
def minPath(grid, k):
|
1880 |
+
"""
|
1881 |
+
Given a grid with N rows and N columns (N >= 2) and a positive integer k,
|
1882 |
+
each cell of the grid contains a value. Every integer in the range [1, N * N]
|
1883 |
+
inclusive appears exactly once on the cells of the grid.
|
1884 |
+
|
1885 |
+
You have to find the minimum path of length k in the grid. You can start
|
1886 |
+
from any cell, and in each step you can move to any of the neighbor cells,
|
1887 |
+
in other words, you can go to cells which share an edge with you current
|
1888 |
+
cell.
|
1889 |
+
Please note that a path of length k means visiting exactly k cells (not
|
1890 |
+
necessarily distinct).
|
1891 |
+
You CANNOT go off the grid.
|
1892 |
+
A path A (of length k) is considered less than a path B (of length k) if
|
1893 |
+
after making the ordered lists of the values on the cells that A and B go
|
1894 |
+
through (let's call them lst_A and lst_B), lst_A is lexicographically less
|
1895 |
+
than lst_B, in other words, there exist an integer index i (1 <= i <= k)
|
1896 |
+
such that lst_A[i] < lst_B[i] and for any j (1 <= j < i) we have
|
1897 |
+
lst_A[j] = lst_B[j].
|
1898 |
+
It is guaranteed that the answer is unique.
|
1899 |
+
Return an ordered list of the values on the cells that the minimum path go through.
|
1900 |
+
|
1901 |
+
Examples:
|
1902 |
+
|
1903 |
+
Input: grid = [ [1,2,3], [4,5,6], [7,8,9]], k = 3
|
1904 |
+
Output: [1, 2, 1]
|
1905 |
+
|
1906 |
+
Input: grid = [ [5,9,3], [4,1,6], [7,8,2]], k = 1
|
1907 |
+
Output: [1]
|
1908 |
+
"""
|
1909 |
+
|
1910 |
+
|
1911 |
+
def tri(n):
|
1912 |
+
"""Everyone knows Fibonacci sequence, it was studied deeply by mathematicians in
|
1913 |
+
the last couple centuries. However, what people don't know is Tribonacci sequence.
|
1914 |
+
Tribonacci sequence is defined by the recurrence:
|
1915 |
+
tri(1) = 3
|
1916 |
+
tri(n) = 1 + n / 2, if n is even.
|
1917 |
+
tri(n) = tri(n - 1) + tri(n - 2) + tri(n + 1), if n is odd.
|
1918 |
+
For example:
|
1919 |
+
tri(2) = 1 + (2 / 2) = 2
|
1920 |
+
tri(4) = 3
|
1921 |
+
tri(3) = tri(2) + tri(1) + tri(4)
|
1922 |
+
= 2 + 3 + 3 = 8
|
1923 |
+
You are given a non-negative integer number n, you have to a return a list of the
|
1924 |
+
first n + 1 numbers of the Tribonacci sequence.
|
1925 |
+
Examples:
|
1926 |
+
tri(3) = [1, 3, 2, 8]
|
1927 |
+
"""
|
1928 |
+
|
1929 |
+
|
1930 |
+
def digits(n):
|
1931 |
+
"""Given a positive integer n, return the product of the odd digits.
|
1932 |
+
Return 0 if all digits are even.
|
1933 |
+
For example:
|
1934 |
+
digits(1) == 1
|
1935 |
+
digits(4) == 0
|
1936 |
+
digits(235) == 15
|
1937 |
+
"""
|
1938 |
+
|
1939 |
+
|
1940 |
+
def is_nested(string):
|
1941 |
+
'''
|
1942 |
+
Create a function that takes a string as input which contains only square brackets.
|
1943 |
+
The function should return True if and only if there is a valid subsequence of brackets
|
1944 |
+
where at least one bracket in the subsequence is nested.
|
1945 |
+
|
1946 |
+
is_nested('[[]]') ➞ True
|
1947 |
+
is_nested('[]]]]]]][[[[[]') ➞ False
|
1948 |
+
is_nested('[][]') ➞ False
|
1949 |
+
is_nested('[]') ➞ False
|
1950 |
+
is_nested('[[][]]') ➞ True
|
1951 |
+
is_nested('[[]][[') ➞ True
|
1952 |
+
'''
|
1953 |
+
|
1954 |
+
|
1955 |
+
|
1956 |
+
def sum_squares(lst):
|
1957 |
+
"""You are given a list of numbers.
|
1958 |
+
You need to return the sum of squared numbers in the given list,
|
1959 |
+
round each element in the list to the upper int(Ceiling) first.
|
1960 |
+
Examples:
|
1961 |
+
For lst = [1,2,3] the output should be 14
|
1962 |
+
For lst = [1,4,9] the output should be 98
|
1963 |
+
For lst = [1,3,5,7] the output should be 84
|
1964 |
+
For lst = [1.4,4.2,0] the output should be 29
|
1965 |
+
For lst = [-2.4,1,1] the output should be 6
|
1966 |
+
|
1967 |
+
|
1968 |
+
"""
|
1969 |
+
|
1970 |
+
|
1971 |
+
def check_if_last_char_is_a_letter(txt):
|
1972 |
+
'''
|
1973 |
+
Create a function that returns True if the last character
|
1974 |
+
of a given string is an alphabetical character and is not
|
1975 |
+
a part of a word, and False otherwise.
|
1976 |
+
Note: "word" is a group of characters separated by space.
|
1977 |
+
|
1978 |
+
Examples:
|
1979 |
+
check_if_last_char_is_a_letter("apple pie") ➞ False
|
1980 |
+
check_if_last_char_is_a_letter("apple pi e") ➞ True
|
1981 |
+
check_if_last_char_is_a_letter("apple pi e ") ➞ False
|
1982 |
+
check_if_last_char_is_a_letter("") ➞ False
|
1983 |
+
'''
|
1984 |
+
|
1985 |
+
|
1986 |
+
def can_arrange(arr):
|
1987 |
+
"""Create a function which returns the largest index of an element which
|
1988 |
+
is not greater than or equal to the element immediately preceding it. If
|
1989 |
+
no such element exists then return -1. The given array will not contain
|
1990 |
+
duplicate values.
|
1991 |
+
|
1992 |
+
Examples:
|
1993 |
+
can_arrange([1,2,4,3,5]) = 3
|
1994 |
+
can_arrange([1,2,3]) = -1
|
1995 |
+
"""
|
1996 |
+
|
1997 |
+
|
1998 |
+
def largest_smallest_integers(lst):
|
1999 |
+
'''
|
2000 |
+
Create a function that returns a tuple (a, b), where 'a' is
|
2001 |
+
the largest of negative integers, and 'b' is the smallest
|
2002 |
+
of positive integers in a list.
|
2003 |
+
If there is no negative or positive integers, return them as None.
|
2004 |
+
|
2005 |
+
Examples:
|
2006 |
+
largest_smallest_integers([2, 4, 1, 3, 5, 7]) == (None, 1)
|
2007 |
+
largest_smallest_integers([]) == (None, None)
|
2008 |
+
largest_smallest_integers([0]) == (None, None)
|
2009 |
+
'''
|
2010 |
+
|
2011 |
+
|
2012 |
+
def compare_one(a, b):
|
2013 |
+
"""
|
2014 |
+
Create a function that takes integers, floats, or strings representing
|
2015 |
+
real numbers, and returns the larger variable in its given variable type.
|
2016 |
+
Return None if the values are equal.
|
2017 |
+
Note: If a real number is represented as a string, the floating point might be . or ,
|
2018 |
+
|
2019 |
+
compare_one(1, 2.5) ➞ 2.5
|
2020 |
+
compare_one(1, "2,3") ➞ "2,3"
|
2021 |
+
compare_one("5,1", "6") ➞ "6"
|
2022 |
+
compare_one("1", 1) ➞ None
|
2023 |
+
"""
|
2024 |
+
|
2025 |
+
|
2026 |
+
def is_equal_to_sum_even(n):
|
2027 |
+
"""Evaluate whether the given number n can be written as the sum of exactly 4 positive even numbers
|
2028 |
+
Example
|
2029 |
+
is_equal_to_sum_even(4) == False
|
2030 |
+
is_equal_to_sum_even(6) == False
|
2031 |
+
is_equal_to_sum_even(8) == True
|
2032 |
+
"""
|
2033 |
+
|
2034 |
+
|
2035 |
+
def special_factorial(n):
|
2036 |
+
"""The Brazilian factorial is defined as:
|
2037 |
+
brazilian_factorial(n) = n! * (n-1)! * (n-2)! * ... * 1!
|
2038 |
+
where n > 0
|
2039 |
+
|
2040 |
+
For example:
|
2041 |
+
>>> special_factorial(4)
|
2042 |
+
288
|
2043 |
+
|
2044 |
+
The function will receive an integer as input and should return the special
|
2045 |
+
factorial of this integer.
|
2046 |
+
"""
|
2047 |
+
|
2048 |
+
|
2049 |
+
def fix_spaces(text):
|
2050 |
+
"""
|
2051 |
+
Given a string text, replace all spaces in it with underscores,
|
2052 |
+
and if a string has more than 2 consecutive spaces,
|
2053 |
+
then replace all consecutive spaces with -
|
2054 |
+
|
2055 |
+
fix_spaces("Example") == "Example"
|
2056 |
+
fix_spaces("Example 1") == "Example_1"
|
2057 |
+
fix_spaces(" Example 2") == "_Example_2"
|
2058 |
+
fix_spaces(" Example 3") == "_Example-3"
|
2059 |
+
"""
|
2060 |
+
|
2061 |
+
|
2062 |
+
def file_name_check(file_name):
|
2063 |
+
"""Create a function which takes a string representing a file's name, and returns
|
2064 |
+
'Yes' if the the file's name is valid, and returns 'No' otherwise.
|
2065 |
+
A file's name is considered to be valid if and only if all the following conditions
|
2066 |
+
are met:
|
2067 |
+
- There should not be more than three digits ('0'-'9') in the file's name.
|
2068 |
+
- The file's name contains exactly one dot '.'
|
2069 |
+
- The substring before the dot should not be empty, and it starts with a letter from
|
2070 |
+
the latin alphapet ('a'-'z' and 'A'-'Z').
|
2071 |
+
- The substring after the dot should be one of these: ['txt', 'exe', 'dll']
|
2072 |
+
Examples:
|
2073 |
+
file_name_check("example.txt") # => 'Yes'
|
2074 |
+
file_name_check("1example.dll") # => 'No' (the name should start with a latin alphapet letter)
|
2075 |
+
"""
|
2076 |
+
|
2077 |
+
|
2078 |
+
|
2079 |
+
|
2080 |
+
def sum_squares(lst):
|
2081 |
+
""""
|
2082 |
+
This function will take a list of integers. For all entries in the list, the function shall square the integer entry if its index is a
|
2083 |
+
multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not
|
2084 |
+
change the entries in the list whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries.
|
2085 |
+
|
2086 |
+
Examples:
|
2087 |
+
For lst = [1,2,3] the output should be 6
|
2088 |
+
For lst = [] the output should be 0
|
2089 |
+
For lst = [-1,-5,2,-1,-5] the output should be -126
|
2090 |
+
"""
|
2091 |
+
|
2092 |
+
|
2093 |
+
def words_in_sentence(sentence):
|
2094 |
+
"""
|
2095 |
+
You are given a string representing a sentence,
|
2096 |
+
the sentence contains some words separated by a space,
|
2097 |
+
and you have to return a string that contains the words from the original sentence,
|
2098 |
+
whose lengths are prime numbers,
|
2099 |
+
the order of the words in the new string should be the same as the original one.
|
2100 |
+
|
2101 |
+
Example 1:
|
2102 |
+
Input: sentence = "This is a test"
|
2103 |
+
Output: "is"
|
2104 |
+
|
2105 |
+
Example 2:
|
2106 |
+
Input: sentence = "lets go for swimming"
|
2107 |
+
Output: "go for"
|
2108 |
+
|
2109 |
+
Constraints:
|
2110 |
+
* 1 <= len(sentence) <= 100
|
2111 |
+
* sentence contains only letters
|
2112 |
+
"""
|
2113 |
+
|
2114 |
+
|
2115 |
+
def simplify(x, n):
|
2116 |
+
"""Your task is to implement a function that will simplify the expression
|
2117 |
+
x * n. The function returns True if x * n evaluates to a whole number and False
|
2118 |
+
otherwise. Both x and n, are string representation of a fraction, and have the following format,
|
2119 |
+
<numerator>/<denominator> where both numerator and denominator are positive whole numbers.
|
2120 |
+
|
2121 |
+
You can assume that x, and n are valid fractions, and do not have zero as denominator.
|
2122 |
+
|
2123 |
+
simplify("1/5", "5/1") = True
|
2124 |
+
simplify("1/6", "2/1") = False
|
2125 |
+
simplify("7/10", "10/2") = False
|
2126 |
+
"""
|
2127 |
+
|
2128 |
+
|
2129 |
+
def order_by_points(nums):
|
2130 |
+
"""
|
2131 |
+
Write a function which sorts the given list of integers
|
2132 |
+
in ascending order according to the sum of their digits.
|
2133 |
+
Note: if there are several items with similar sum of their digits,
|
2134 |
+
order them based on their index in original list.
|
2135 |
+
|
2136 |
+
For example:
|
2137 |
+
>>> order_by_points([1, 11, -1, -11, -12]) == [-1, -11, 1, -12, 11]
|
2138 |
+
>>> order_by_points([]) == []
|
2139 |
+
"""
|
2140 |
+
|
2141 |
+
|
2142 |
+
def specialFilter(nums):
|
2143 |
+
"""Write a function that takes an array of numbers as input and returns
|
2144 |
+
the number of elements in the array that are greater than 10 and both
|
2145 |
+
first and last digits of a number are odd (1, 3, 5, 7, 9).
|
2146 |
+
For example:
|
2147 |
+
specialFilter([15, -73, 14, -15]) => 1
|
2148 |
+
specialFilter([33, -2, -3, 45, 21, 109]) => 2
|
2149 |
+
"""
|
2150 |
+
|
2151 |
+
|
2152 |
+
def get_max_triples(n):
|
2153 |
+
"""
|
2154 |
+
You are given a positive integer n. You have to create an integer array a of length n.
|
2155 |
+
For each i (1 ≤ i ≤ n), the value of a[i] = i * i - i + 1.
|
2156 |
+
Return the number of triples (a[i], a[j], a[k]) of a where i < j < k,
|
2157 |
+
and a[i] + a[j] + a[k] is a multiple of 3.
|
2158 |
+
|
2159 |
+
Example :
|
2160 |
+
Input: n = 5
|
2161 |
+
Output: 1
|
2162 |
+
Explanation:
|
2163 |
+
a = [1, 3, 7, 13, 21]
|
2164 |
+
The only valid triple is (1, 7, 13).
|
2165 |
+
"""
|
2166 |
+
|
2167 |
+
|
2168 |
+
def bf(planet1, planet2):
|
2169 |
+
'''
|
2170 |
+
There are eight planets in our solar system: the closerst to the Sun
|
2171 |
+
is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn,
|
2172 |
+
Uranus, Neptune.
|
2173 |
+
Write a function that takes two planet names as strings planet1 and planet2.
|
2174 |
+
The function should return a tuple containing all planets whose orbits are
|
2175 |
+
located between the orbit of planet1 and the orbit of planet2, sorted by
|
2176 |
+
the proximity to the sun.
|
2177 |
+
The function should return an empty tuple if planet1 or planet2
|
2178 |
+
are not correct planet names.
|
2179 |
+
Examples
|
2180 |
+
bf("Jupiter", "Neptune") ==> ("Saturn", "Uranus")
|
2181 |
+
bf("Earth", "Mercury") ==> ("Venus")
|
2182 |
+
bf("Mercury", "Uranus") ==> ("Venus", "Earth", "Mars", "Jupiter", "Saturn")
|
2183 |
+
'''
|
2184 |
+
|
2185 |
+
|
2186 |
+
def sorted_list_sum(lst):
|
2187 |
+
"""Write a function that accepts a list of strings as a parameter,
|
2188 |
+
deletes the strings that have odd lengths from it,
|
2189 |
+
and returns the resulted list with a sorted order,
|
2190 |
+
The list is always a list of strings and never an array of numbers,
|
2191 |
+
and it may contain duplicates.
|
2192 |
+
The order of the list should be ascending by length of each word, and you
|
2193 |
+
should return the list sorted by that rule.
|
2194 |
+
If two words have the same length, sort the list alphabetically.
|
2195 |
+
The function should return a list of strings in sorted order.
|
2196 |
+
You may assume that all words will have the same length.
|
2197 |
+
For example:
|
2198 |
+
assert list_sort(["aa", "a", "aaa"]) => ["aa"]
|
2199 |
+
assert list_sort(["ab", "a", "aaa", "cd"]) => ["ab", "cd"]
|
2200 |
+
"""
|
2201 |
+
|
2202 |
+
|
2203 |
+
def x_or_y(n, x, y):
|
2204 |
+
"""A simple program which should return the value of x if n is
|
2205 |
+
a prime number and should return the value of y otherwise.
|
2206 |
+
|
2207 |
+
Examples:
|
2208 |
+
for x_or_y(7, 34, 12) == 34
|
2209 |
+
for x_or_y(15, 8, 5) == 5
|
2210 |
+
|
2211 |
+
"""
|
2212 |
+
|
2213 |
+
|
2214 |
+
def double_the_difference(lst):
|
2215 |
+
'''
|
2216 |
+
Given a list of numbers, return the sum of squares of the numbers
|
2217 |
+
in the list that are odd. Ignore numbers that are negative or not integers.
|
2218 |
+
|
2219 |
+
double_the_difference([1, 3, 2, 0]) == 1 + 9 + 0 + 0 = 10
|
2220 |
+
double_the_difference([-1, -2, 0]) == 0
|
2221 |
+
double_the_difference([9, -2]) == 81
|
2222 |
+
double_the_difference([0]) == 0
|
2223 |
+
|
2224 |
+
If the input list is empty, return 0.
|
2225 |
+
'''
|
2226 |
+
|
2227 |
+
|
2228 |
+
def compare(game,guess):
|
2229 |
+
"""I think we all remember that feeling when the result of some long-awaited
|
2230 |
+
event is finally known. The feelings and thoughts you have at that moment are
|
2231 |
+
definitely worth noting down and comparing.
|
2232 |
+
Your task is to determine if a person correctly guessed the results of a number of matches.
|
2233 |
+
You are given two arrays of scores and guesses of equal length, where each index shows a match.
|
2234 |
+
Return an array of the same length denoting how far off each guess was. If they have guessed correctly,
|
2235 |
+
the value is 0, and if not, the value is the absolute difference between the guess and the score.
|
2236 |
+
|
2237 |
+
|
2238 |
+
example:
|
2239 |
+
|
2240 |
+
compare([1,2,3,4,5,1],[1,2,3,4,2,-2]) -> [0,0,0,0,3,3]
|
2241 |
+
compare([0,5,0,0,0,4],[4,1,1,0,0,-2]) -> [4,4,1,0,0,6]
|
2242 |
+
"""
|
2243 |
+
|
2244 |
+
|
2245 |
+
def Strongest_Extension(class_name, extensions):
|
2246 |
+
"""You will be given the name of a class (a string) and a list of extensions.
|
2247 |
+
The extensions are to be used to load additional classes to the class. The
|
2248 |
+
strength of the extension is as follows: Let CAP be the number of the uppercase
|
2249 |
+
letters in the extension's name, and let SM be the number of lowercase letters
|
2250 |
+
in the extension's name, the strength is given by the fraction CAP - SM.
|
2251 |
+
You should find the strongest extension and return a string in this
|
2252 |
+
format: ClassName.StrongestExtensionName.
|
2253 |
+
If there are two or more extensions with the same strength, you should
|
2254 |
+
choose the one that comes first in the list.
|
2255 |
+
For example, if you are given "Slices" as the class and a list of the
|
2256 |
+
extensions: ['SErviNGSliCes', 'Cheese', 'StuFfed'] then you should
|
2257 |
+
return 'Slices.SErviNGSliCes' since 'SErviNGSliCes' is the strongest extension
|
2258 |
+
(its strength is -1).
|
2259 |
+
Example:
|
2260 |
+
for Strongest_Extension('my_class', ['AA', 'Be', 'CC']) == 'my_class.AA'
|
2261 |
+
"""
|
2262 |
+
|
2263 |
+
|
2264 |
+
def cycpattern_check(a , b):
|
2265 |
+
"""You are given 2 words. You need to return True if the second word or any of its rotations is a substring in the first word
|
2266 |
+
cycpattern_check("abcd","abd") => False
|
2267 |
+
cycpattern_check("hello","ell") => True
|
2268 |
+
cycpattern_check("whassup","psus") => False
|
2269 |
+
cycpattern_check("abab","baa") => True
|
2270 |
+
cycpattern_check("efef","eeff") => False
|
2271 |
+
cycpattern_check("himenss","simen") => True
|
2272 |
+
|
2273 |
+
"""
|
2274 |
+
|
2275 |
+
|
2276 |
+
def even_odd_count(num):
|
2277 |
+
"""Given an integer. return a tuple that has the number of even and odd digits respectively.
|
2278 |
+
|
2279 |
+
Example:
|
2280 |
+
even_odd_count(-12) ==> (1, 1)
|
2281 |
+
even_odd_count(123) ==> (1, 2)
|
2282 |
+
"""
|
2283 |
+
|
2284 |
+
|
2285 |
+
def int_to_mini_roman(number):
|
2286 |
+
"""
|
2287 |
+
Given a positive integer, obtain its roman numeral equivalent as a string,
|
2288 |
+
and return it in lowercase.
|
2289 |
+
Restrictions: 1 <= num <= 1000
|
2290 |
+
|
2291 |
+
Examples:
|
2292 |
+
>>> int_to_mini_roman(19) == 'xix'
|
2293 |
+
>>> int_to_mini_roman(152) == 'clii'
|
2294 |
+
>>> int_to_mini_roman(426) == 'cdxxvi'
|
2295 |
+
"""
|
2296 |
+
|
2297 |
+
|
2298 |
+
def right_angle_triangle(a, b, c):
|
2299 |
+
'''
|
2300 |
+
Given the lengths of the three sides of a triangle. Return True if the three
|
2301 |
+
sides form a right-angled triangle, False otherwise.
|
2302 |
+
A right-angled triangle is a triangle in which one angle is right angle or
|
2303 |
+
90 degree.
|
2304 |
+
Example:
|
2305 |
+
right_angle_triangle(3, 4, 5) == True
|
2306 |
+
right_angle_triangle(1, 2, 3) == False
|
2307 |
+
'''
|
2308 |
+
|
2309 |
+
|
2310 |
+
def find_max(words):
|
2311 |
+
"""Write a function that accepts a list of strings.
|
2312 |
+
The list contains different words. Return the word with maximum number
|
2313 |
+
of unique characters. If multiple strings have maximum number of unique
|
2314 |
+
characters, return the one which comes first in lexicographical order.
|
2315 |
+
|
2316 |
+
find_max(["name", "of", "string"]) == "string"
|
2317 |
+
find_max(["name", "enam", "game"]) == "enam"
|
2318 |
+
find_max(["aaaaaaa", "bb" ,"cc"]) == ""aaaaaaa"
|
2319 |
+
"""
|
2320 |
+
|
2321 |
+
|
2322 |
+
def eat(number, need, remaining):
|
2323 |
+
"""
|
2324 |
+
You're a hungry rabbit, and you already have eaten a certain number of carrots,
|
2325 |
+
but now you need to eat more carrots to complete the day's meals.
|
2326 |
+
you should return an array of [ total number of eaten carrots after your meals,
|
2327 |
+
the number of carrots left after your meals ]
|
2328 |
+
if there are not enough remaining carrots, you will eat all remaining carrots, but will still be hungry.
|
2329 |
+
|
2330 |
+
Example:
|
2331 |
+
* eat(5, 6, 10) -> [11, 4]
|
2332 |
+
* eat(4, 8, 9) -> [12, 1]
|
2333 |
+
* eat(1, 10, 10) -> [11, 0]
|
2334 |
+
* eat(2, 11, 5) -> [7, 0]
|
2335 |
+
|
2336 |
+
Variables:
|
2337 |
+
@number : integer
|
2338 |
+
the number of carrots that you have eaten.
|
2339 |
+
@need : integer
|
2340 |
+
the number of carrots that you need to eat.
|
2341 |
+
@remaining : integer
|
2342 |
+
the number of remaining carrots thet exist in stock
|
2343 |
+
|
2344 |
+
Constrain:
|
2345 |
+
* 0 <= number <= 1000
|
2346 |
+
* 0 <= need <= 1000
|
2347 |
+
* 0 <= remaining <= 1000
|
2348 |
+
|
2349 |
+
Have fun :)
|
2350 |
+
"""
|
2351 |
+
|
2352 |
+
|
2353 |
+
def do_algebra(operator, operand):
|
2354 |
+
"""
|
2355 |
+
Given two lists operator, and operand. The first list has basic algebra operations, and
|
2356 |
+
the second list is a list of integers. Use the two given lists to build the algebric
|
2357 |
+
expression and return the evaluation of this expression.
|
2358 |
+
|
2359 |
+
The basic algebra operations:
|
2360 |
+
Addition ( + )
|
2361 |
+
Subtraction ( - )
|
2362 |
+
Multiplication ( * )
|
2363 |
+
Floor division ( // )
|
2364 |
+
Exponentiation ( ** )
|
2365 |
+
|
2366 |
+
Example:
|
2367 |
+
operator['+', '*', '-']
|
2368 |
+
array = [2, 3, 4, 5]
|
2369 |
+
result = 2 + 3 * 4 - 5
|
2370 |
+
=> result = 9
|
2371 |
+
|
2372 |
+
Note:
|
2373 |
+
The length of operator list is equal to the length of operand list minus one.
|
2374 |
+
Operand is a list of of non-negative integers.
|
2375 |
+
Operator list has at least one operator, and operand list has at least two operands.
|
2376 |
+
|
2377 |
+
"""
|
2378 |
+
|
2379 |
+
|
2380 |
+
def solve(s):
|
2381 |
+
"""You are given a string s.
|
2382 |
+
if s[i] is a letter, reverse its case from lower to upper or vise versa,
|
2383 |
+
otherwise keep it as it is.
|
2384 |
+
If the string contains no letters, reverse the string.
|
2385 |
+
The function should return the resulted string.
|
2386 |
+
Examples
|
2387 |
+
solve("1234") = "4321"
|
2388 |
+
solve("ab") = "AB"
|
2389 |
+
solve("#a@C") = "#A@c"
|
2390 |
+
"""
|
2391 |
+
|
2392 |
+
|
2393 |
+
def string_to_md5(text):
|
2394 |
+
"""
|
2395 |
+
Given a string 'text', return its md5 hash equivalent string.
|
2396 |
+
If 'text' is an empty string, return None.
|
2397 |
+
|
2398 |
+
>>> string_to_md5('Hello world') == '3e25960a79dbc69b674cd4ec67a72c62'
|
2399 |
+
"""
|
2400 |
+
|
2401 |
+
|
2402 |
+
def generate_integers(a, b):
|
2403 |
+
"""
|
2404 |
+
Given two positive integers a and b, return the even digits between a
|
2405 |
+
and b, in ascending order.
|
2406 |
+
|
2407 |
+
For example:
|
2408 |
+
generate_integers(2, 8) => [2, 4, 6, 8]
|
2409 |
+
generate_integers(8, 2) => [2, 4, 6, 8]
|
2410 |
+
generate_integers(10, 14) => []
|
2411 |
+
"""
|
retriever_index.faiss
CHANGED
Binary files a/retriever_index.faiss and b/retriever_index.faiss differ
|
|