본문 바로가기

Python345

List Filtering In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. Example filter_list([1,2,'a','b']) == [1,2] filter_list([1,'a','b',0,15]) == [1,0,15] filter_list([1,2,'aasf','1','123',123]) == [1,2,123] Solution: def filter_list(l): return [i for i in l if isinstance(i, int)] 2022. 8. 11.
Sum of a sequence Your task is to make function, which returns the sum of a sequence of integers. The sequence is defined by 3 non-negative values: begin, end, step (inclusive). If begin value is greater than the end, function should returns 0 Examples 2,2,2 --> 2 2,6,2 --> 12 (2 + 4 + 6) 1,5,1 --> 15 (1 + 2 + 3 + 4 + 5) 1,5,3 --> 5 (1 + 4) Solotion: def sequence_sum(begin_number, end_number, step): return sum(ra.. 2022. 8. 10.
Remove anchor from URL Complete the function/method so that it returns the url with anything after the anchor (#) removed. Examples "www.codewars.com#about" --> "www.codewars.com" "www.codewars.com?page=1" -->"www.codewars.com?page=1" Solution: def remove_url_anchor(url): i = url.find("#") return [url, url[:i]][i!=-1] Order Solution: def remove_url_anchor(url): return url.split('#')[0] def remove_url_anchor(url): retu.. 2022. 8. 9.
Find the unique number There is an array with some numbers. All numbers are equal except for one. Try to find it! find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2 find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55 It’s guaranteed that array contains at least 3 numbers. The tests contain some very huge arrays, so think about performance. Solution: def find_uniq(arr): n = arr[0] == arr[-1] and arr[0] or arr[1] return [i for i in arr if n != i.. 2022. 8. 8.
Count the Digit Take an integer n (n >= 0) and a digit d (0 2022. 8. 5.
Descending Order Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number. Examples: Input: 42145 Output: 54421 Input: 145263 Output: 654321 Input: 123456789 Output: 987654321 Solution: def descending_order(num): return int("".join(sorted([i for i in str(num)], r.. 2022. 8. 5.