Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Implement a function which multiplies two numbers.
def multiply(a,b): return a*b multiply(8,15)
Implement a function which multiplies two numbers.
def multiply(a, b): return a*b multiply(16,100)
Implement a function which multiplies two numbers.
def multiply (a, c): return a * c
Implement a function which multiplies two numbers.
def multiply(y,n): return y * n
Implement a function which multiplies two numbers.
def multiply(num1, num2): resultado = num1 * num2 return resultado
Implement a function which multiplies two numbers.
#your code here def multiply(a,b): x=a*b return(a*b)
Implement a function which multiplies two numbers.
import operator def multiply(op1,op2): return op1*op2
Implement a function which multiplies two numbers.
def multiply(num, num1): return(num * num1)
Implement a function which multiplies two numbers.
def multiply(n, m, acc = 0): if m <= 0: return acc else: return multiply(n, m - 1, acc + n)
Implement a function which multiplies two numbers.
def multiply(a, b): number = a * b return number
Implement a function which multiplies two numbers.
def multiply(number_one, number_two): return number_one * number_two
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
def split_all_even_numbers(numbers, split_type): result = [] for a in numbers: if a % 2: result.append(a) else: pairs = [(b, a - b) for b in range(1, a // 2 + 1, 2) if a - b % 2] if split_type == 0: result.extend(pairs[-1]) elif split_type == 1: result.extend(pairs[0]) elif split_type == 2: for c, _ in reversed(pairs): quo, rem = divmod(a, c) if not rem: result.extend([c] * quo) break elif split_type == 3: result.extend([1] * a) return result
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
from itertools import chain def split_all_even_numbers(lst, way): def convert(n): s = 1 - (n//2) % 2 # Shift for closest odd numbers return ([n] if n%2 else # Is already odd [n//2-s, n//2+s] if way == 0 else # Two closest odd sum [1,n-1] if way == 1 else # Two farthest odd sum [1]*n if way == 3 else # Split in ones split_all_even_numbers([n//2]*2, 2)) # Split in highest possible odds return list(chain(*map(convert, lst)))
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
def split_even_number0(n): if not n % 4: return [n / 2 - 1,n / 2 + 1] return [n / 2,n / 2] def split_even_number1(n): return [1,n - 1] def split_even_number2(n): times = 1 tmp = n while not tmp % 2: tmp /= 2 times *= 2 return [tmp] * times def split_even_number3(n): return [1] * n def split_all_even_numbers(numbers, way): result = [] for i in numbers: if i % 2: result += [i] else: if way == 0: result += split_even_number0(i) elif way == 1: result += split_even_number1(i) elif way == 2: result += split_even_number2(i) else: result += split_even_number3(i) return result
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
def split_all_even_numbers(numbers, way): split = (split_closest, split_furthest, split_max_equal, split_ones)[way] return sum(([n] if n % 2 else split(n) for n in numbers), []) split_closest = lambda n: [n//2 + s * (n%4 == 0) for s in (-1, 1)] split_furthest = lambda n: [1, n-1] split_max_equal = lambda n: [n] if n % 2 else split_max_equal(n // 2) * 2 split_ones = lambda n: [1 for _ in range(n)]
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
split0 = lambda x: [x] if x&1 else [(x>>1) - ((x&2)>>1^1), (x>>1) + ((x&2)>>1^1)] split1 = lambda x: [x] if x&1 else [1, x-1] split2 = lambda x: [x] if x&1 else split2(x>>1)*2 split3 = lambda x: [x] if x&1 else [1]*x splits = (split0, split1, split2, split3) def split_all_even_numbers(numbers, way): return [y for x in numbers for y in splits[way](x)]
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
def split_all_even_numbers(arr, way): if way==0 : return sum([[[(i//2)-(not(i//2)&1),(i//2)+(not(i//2)&1)],[i]][i&1] for i in arr],[]) if way==1 : return sum([[[1, i - 1],[i]][i&1] for i in arr],[]) if way == 2: li = [] for i in arr: if not i & 1: a, b = next([k, i // k] for k in range(i - 1, 0, -1) if i % k == 0 and k & 1) li.extend([a] * b) else : li.append(i) return li return sum([[[1] * i,[i]][i&1] for i in arr],[])
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
from itertools import chain def f1(n): x = n // 2 if x % 2: return x, n-x else: return x-1, x+1 def f3(n): m = n while m % 2 == 0: m //= 2 return [m] * (n // m) fs = [f1, lambda n: [1, n-1], f3, lambda n: [1] * n] def split_all_even_numbers(numbers, way): return list(chain.from_iterable([n] if n % 2 else fs[way](n) for n in numbers))
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
ways = { 0 : lambda n : [n//2]*2 if (n//2)%2 else [n//2-1,n//2+1], 1 : lambda n : [1,n-1], 2 : lambda n : [ [n//x]*x for x in range(2,n+1) if sum([n//x]*x)==n and (n//x)%2][0], 3 : lambda n : [1]*n } def split_all_even_numbers(numbers, way): return [x for lists in [ways[way](x) if not x%2 else [x] for x in numbers] for x in lists]
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
def even(n): return n % 2 == 0 def split_all_even_numbers(numbers, way): lst = [] for n in numbers: if even(n): lst += split(n, way) else: lst += [n] return lst def split(num, way): if way == 0: half = num / 2 return [half-1, half+1] if even(half) else [half, half] elif way == 1: return [1, num - 1] elif way == 2: return next([d] * (num // d) for d in range(num-1, 0, -2) if num % d == 0) elif way == 3: return [1] * num
#Split all even numbers to odd ones in different ways Your task is to split all even numbers from an array to odd ones. So your method has to return a new array with only odd numbers. For "splitting" the numbers there are four ways. ``` 0 -> Split into two odd numbers, that are closest to each other. (e.g.: 8 -> 3,5) 1 -> Split into two odd numbers, that are most far from each other. (e.g.: 8 -> 1,7) 2 -> All new odd numbers from the splitting should be equal and the maximum possible number. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) 3 -> Split into 1s. (e.g.: 8 -> 1, 1, 1, 1, 1, 1, 1, 1) The new numbers (from the splitting) have always to be in ascending order. So in the array every even number is replaced by the new odd numbers from the splitting. ``` Your method will get as parameters the input-array and the number of the way for splitting the even numbers. Some Examples ``` [1,10,1,3],0 -> [1,5,5,1,3] [1,10,1,3],1 -> [1,1,9,1,3] [1,10,1,3],2 -> [1,5,5,1,3] [1,10,1,3],3 -> [1,1,1,1,1,1,1,1,1,1,1,1,3] [1,1,3,8],0 -> [1,1,3,3,5] [1,1,3,8],1 -> [1,1,3,1,7] [1,1,3,8],2 -> [1,1,3,1,1,1,1,1,1,1,1] [1,1,3,8],3 -> [1,1,3,1,1,1,1,1,1,1,1] ``` The array will never be null and will always contain only integer numbers > 0. Also your result-array must contain only integer numbers > 0. The way-parameter will always be between inclusive 0 and inclusive 3 (0,1,2,3). You must not change the input-array! Have fun coding it and please don't forget to vote and rank this kata! :-) I have also created other katas. Take a look if you enjoyed this kata!
def split_all_even_numbers(numbers, way): def split0(n): return (n // 2, n // 2) if n//2 % 2 else (n//2 - 1, n//2 + 1) def split1(n): return 1, n-1 def split2(n): ctr = 0 while n % 2 == 0: ctr, n = ctr + 1, n//2 return [n]*(2**ctr) def split3(n): return [1]*n split_ops = [split0, split1, split2, split3] return [x for lst in [split_ops[way](n) if n % 2 == 0 else [n] for n in numbers] for x in lst]
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(a, b): return (ord(a[0]) + int(a[1])) % 2 == (ord(b[0]) + int(b[1])) % 2
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): return sum(map(ord,cell1)) % 2 == sum(map(ord,cell2)) % 2
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): is_black = lambda c: (c[0] in "BDFH") ^ (c[1] in "1357") return is_black(cell1) == is_black(cell2)
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
b,chess_board_cell_color=lambda c:sum(map(ord,c))&1,lambda c,d:b(c)==b(d)
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): return ((ord(cell1[0]) + int(cell1[1])) % 2) == ((ord(cell2[0]) + int(cell2[1])) % 2)
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
black = lambda cell: (cell[0] in {'A', 'C', 'E', 'G'}) ^ int(cell[1])%2 chess_board_cell_color = lambda a, b : black(a)==black(b)
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def check(c): if c[0] in "ACEG": if int(c[1]) % 2 == 0: return "W" else: return "B" elif c[0] in "BDFH": if int(c[1]) % 2 == 0: return "B" else: return "W" def chess_board_cell_color(cell1, cell2): return check(cell1) == check(cell2)
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): file1 = cell1[0] rank1 = cell1[1] file2 = cell2[0] rank2 = cell2[1] row = abs(ord(file1) - ord(file2)) col = abs(int(rank1) - int(rank2)) same_color = (row + col) % 2 == 0 return same_color
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def Color(cell) : return ('ABCDEFGH'.index(cell[0])+'12345678'.index(cell[1]))%2 def chess_board_cell_color(cell1, cell2): return Color(cell1)==Color(cell2)
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): return sum(ord(c) + int(d) for c, d in (cell1, cell2)) % 2 == 0
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(c1, c2): return len(set(x[1] in '1357' if x[0] in 'ACEG' else x[1] in '2468' for x in [c1, c2])) == 1
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(c1, c2): return isWhite(c1) == isWhite(c2) def isWhite(c): i = ('ABCDEFGH'.index(c[0])+1) % 2 return i != 0 if int(c[1]) % 2 == 0 else i == 0
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): return get_colour(cell1) == get_colour(cell2) def get_colour(cell): letters = "ABCDEFGH" if letters.index(cell[0]) % 2 == 0: if int(cell[1]) % 2 == 1: return 'b' else: return 'w' else: if int(cell[1]) % 2 == 1: return 'w' else: return 'b'
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): r1, c1 = cell1 r2, c2 = cell2 return abs(ord(r1) - ord(r2)) % 2 == (int(c1) - int(c2)) % 2
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): return True if (ord(cell1[0]) - ord(cell2[0])) % 2 == (ord(cell1[1]) - ord(cell2[1])) % 2 else False
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): return int(cell1, 35) % 2 == int(cell2, 35) % 2
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): return (ord(cell1[0]) + ord(cell1[1]) + ord(cell2[0]) + ord(cell2[1])) % 2 == 0
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): board = { 'A': 'BW'*4, 'B': 'WB'*4, 'C': 'BW'*4, 'D': 'WB'*4, 'E': 'BW'*4, 'F': 'WB'*4, 'G': 'BW'*4, 'H': 'WB'*4, } return board[cell1[0]][int(cell1[1])-1] == board[cell2[0]][int(cell2[1])-1]
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): dark = ["A1","C1","E1","G1","B2","D2","F2","H2","A3","C3","E3",\ "G3","B4","D4","F4","H4","A5","C5", "E5", "G5","B6","D6","F6","H6",\ "A7","C7","E7","G7","B8","D8", "F8", "H8"] x = 0 y = 0 if cell1 in dark: x = 1 if cell2 in dark: y = 1 if x == y: return True else: return False
# Task Given two cells on the standard chess board, determine whether they have the same color or not. # Example For `cell1 = "A1" and cell2 = "C3"`, the output should be `true`. For `cell1 = "A1" and cell2 = "H3"`, the output should be `false`. # Input/Output - `[input]` string `cell1` - `[input]` string `cell2` - `[output]` a boolean value `true` if both cells have the same color, `false` otherwise.
def chess_board_cell_color(cell1, cell2): strings = 'ABCDEFG' ce1 = 0 ce2 = 0 for i, let in enumerate(strings): if let == cell1[0]: ce1 = i+1 if let == cell2[0]: ce2 = i+1 if ((int(cell1[1])%2) == 0 and (ce1%2) == 0) or ((int(cell1[1])%2) == 1 and (ce1%2) != 0): color1 = 'r' else: color1 = 'w' if ((int(cell2[1])%2) == 0 and (ce2%2) == 0) or ((int(cell2[1])%2) == 1 and (ce2%2) != 0): #if ((int(cell2[1])%2) == 0 and ce2%2 == 0) or ((int(cell2[1])%2) == 1 and ce2%2 != 0): color2 = 'r' else: color2 = 'w' if color1 == color2: return True else: return False
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
import os def get_output(s): return os.popen(s).read()
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
from subprocess import check_output def get_output(s): return check_output(s.split()).decode('utf-8')
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
def get_output(s): import subprocess return subprocess.check_output(s.split()).decode('ascii')
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
get_output = lambda c, o=__import__("os"): o.popen(c).read()
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
from os import popen def get_output(s): return popen(s).read()
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
from subprocess import check_output def get_output(s): return check_output(s, shell=True).decode('utf-8')
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
import os get_output=lambda Q:''.join(os.popen(Q).readlines()) import subprocess
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
get_output=lambda s:__import__('os').popen(s).read()
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
from os import popen get_output=lambda s:popen(s).read()
As you see in Example test cases, the os running this service is ```posix```. Return the output by executing the command given as the string on posix os. See the example test cases for the expected data format.
import subprocess def get_output(s): res=subprocess.run(s.split(' '),stdout=subprocess.PIPE) return res.stdout.decode()
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
import json def f(data, level): if level == 0: yield data['name'] return children = data['children'] if len(children) >= 7 and all(child['gender'] == 'male' for child in children[:7]): yield from f(children[6], level-1) for child in children: yield from f(child, 2) def find_seventh_sons_of_seventh_sons(jstring): data = json.loads(jstring) return set(f(data, 2))
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
def find_seventh_sons_of_seventh_sons(jstring): seventh_of_seventh = set() def find_seventh_sons(father, seventh_son=False): seven_brother = True for i, son in enumerate(father['children'], 1): if son['gender'] == 'female': seventh_son = seven_brother = False if i == 7 and seventh_son: seventh_of_seventh.add(son['name']) find_seventh_sons(son, i == 7 and seven_brother) find_seventh_sons(json.loads(jstring)) return seventh_of_seventh
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
import json def find_seventh_sons_of_seventh_sons(jstring): return seek4SeventhJSon(json.loads(jstring)) def seek4SeventhJSon(parent, _7up=False): nSons, _7sons7 = 0, set() for n,child in enumerate(parent['children'],1): if child['gender'] == 'male': nSons += 1 _7sons7 |= seek4SeventhJSon(child, n == nSons == 7) if n == nSons == 7 and _7up: _7sons7.add(child['name']) return _7sons7
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
import json def find_seventh_sons_of_seventh_sons(jstring): result, stack = set(), [json.loads(jstring)] while stack: person = stack.pop() if (len(person['children']) >= 7 and all(child['gender'] == 'male' for child in person['children'][:7]) and len(person['children'][6]['children']) >= 7 and all(child['gender'] == 'male' for child in person['children'][6]['children'][:7])): result.add(person['children'][6]['children'][6]['name']) stack.extend(person['children']) return result
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
import json def validate_sons(data): if data["gender"] == "male" and len(data["children"]) >= 7: if all([x["gender"] == "male" for x in data["children"][:7]]): if len(data["children"][6]["children"]) >= 7: if all([x["gender"] == "male" for x in data["children"][6]["children"][:7]]): return True return False def find_seventh_sons_of_seventh_sons(jstring): data = json.loads(jstring) result = [] stack = [data] while len(stack) != 0: a = stack.pop() for children in a["children"]: stack.append(children) if validate_sons(a): result.append(a["children"][6]["children"][6]["name"]) return set(result)
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
def find77(j,son=False,sons=[]): sc = 0 for c in j['children']: if c['gender']=='male': sc+=1 else: sc=8 if sc==7 and 'children' in list(c.keys()): sons = find77(c,True,sons) elif 'children' in list(c.keys()): sons = find77(c,False,sons) if sc==7 and son: sons = [c['name']]+sons return sons def find_seventh_sons_of_seventh_sons(jstring): jstring=eval(jstring) sonlist = find77(jstring,False) if len(sonlist)==0: return set() else: return eval(str(sonlist).replace("[","{").replace("]","}"))
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
def find_seventh_sons_of_seventh_sons(jstring): return set(has_7_of_7_son(json.loads(jstring))) def has_7_of_7_son(person, parent_is_7=False): counter = 0 result = [] broken = False if person['children']: for child in person['children']: if child['gender'] == 'male': # and counter += 1 else: broken = True if counter == 7 and not broken: if parent_is_7: result.append(child['name']) result.extend(has_7_of_7_son(child, True)) result.extend(has_7_of_7_son(child, False)) return result
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
import json def find_seventh_sons_of_seventh_sons(jstring,z=[]): if type(jstring)==str: jstring=json.loads(jstring) z=[] if len(jstring['children'])>6 and all(children['gender']=='male' for children in jstring['children'])and len(jstring['children'][6]['children'])>6 and all(jstring['children'][6]['children'][i]['gender']=='male' for i in range(7)): z.append(jstring['children'][6]['children'][6]['name']) for x in jstring['children']: find_seventh_sons_of_seventh_sons(x,z) return set(z)
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
import json def isItAChainOfSons(listOfCildren): chainOfSons=True for i in range (0,7): if(listOfCildren[i]['gender']=='female'): chainOfSons=False return chainOfSons def rekursiveSonFinder(family, secondGeneration): if ((family['gender'] == 'female') or (len(family['children'])<7) ): return '/' elif(isItAChainOfSons(family['children'])): # a seventh son of a seventh son if(secondGeneration): # print('found name: ', family['children'][6]['name']) return '/' + family['children'][6]['name'] # a seventh son else: return rekursiveSonFinder(family['children'][6], True) def rekursiveIteration(family): if(len(family['children']) == 0): return '/' else: ownSolution = rekursiveSonFinder(family, False) solutionOfChildren='' for child in family['children']: if(len(child['children'])>0): solutionOfChildren = solutionOfChildren + rekursiveIteration(child) if(ownSolution== None): return solutionOfChildren else: return ownSolution + solutionOfChildren def find_seventh_sons_of_seventh_sons(jstring): familyTree = json.loads(jstring) listOfSons = rekursiveIteration(familyTree) #print(listOfSons) listOfSons = listOfSons.split('/') #print(listOfSons) solution = set() for name in listOfSons: if (name !=''): solution.add(name) return solution
# Context According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**" # Your task You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__. ## Tips * Have a good look at the sample test cases. * For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though. * **You may want to use the json module for this one.**
def find_seventh_sons_of_seventh_sons(family_dict, ss_set = set()): """ List of seven sons of seventh son is checked if any girl is born in between. """ if type(family_dict) == str: ss_set.clear() family_dict = json.loads(family_dict) if len(family_dict['children']) > 6 and len(family_dict['children'][6]['children']) > 6: children_genders = [x['gender'] for x in family_dict['children'][:7]] + [x['gender'] for x in family_dict['children'][6]['children'][:7]] if not 'female' in children_genders: ss_set.add(family_dict['children'][6]['children'][6]['name']) for child in family_dict['children']: find_seventh_sons_of_seventh_sons(child, ss_set) return ss_set
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(wealth): wealth[:] = [sum(wealth) / len(wealth)] * len(wealth)
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
from statistics import mean def redistribute_wealth(wealth): avg = mean(wealth) for i in range(len(wealth)): wealth[i] = avg
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
from numpy import mean def redistribute_wealth(wealth): wealth[:] = [mean(wealth)]*len(wealth)
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(w): w[:] = [sum(w) / len(w)] * len(w)
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(wealth): wealth[:] = len(wealth)*[sum(wealth)/float(len(wealth))]
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(wealth): val = sum(wealth)/len(wealth) wealth[:] = map(lambda x : val,wealth)
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(wealth): # mean=sum(wealth)/len(wealth) wealth[:]=[mean]*len(wealth)
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(a): n = len(a) x = sum(a) / n a.clear() for _ in range(n): a.append(x)
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(wealth): mean = sum(wealth) / len(wealth) for i, x in enumerate(wealth): wealth[i] = mean
The year is 2088 and the Radical Marxist Socialist People's Party (RMSPP) has just seized power in Brazil. Their first act in power is absolute wealth equality through coercive redistribution. Create a function that redistributes all wealth equally among all citizens. Wealth is represented as an array/list where every index is the wealth of a single citizen. The function should mutate the input such that every index has the same amount of wealth. **MUTATE the input array/list, don't return anything.** See example: ```python wealth = [5, 10, 6] # This represents: # citizen 1 has wealth 5 # citizen 2 has wealth 10 # citizen 3 has wealth 6 redistribute_wealth(wealth) # mutates wealth list wealth => [7, 7, 7] # wealth has now been equally redistributed ``` Info: - **MUTATE the input array/list, don't return anything** - Input is garantueed to hold at least 1 or more citizens - Wealth of citizen will an integer with minimum 0 (negative wealth not possible) - Handling of floating point error will not be tested
def redistribute_wealth(wealth): s=sum(wealth)/len(wealth) for i in range(len(wealth)): wealth[i]=s
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
from collections import Counter from operator import itemgetter def letter_frequency(text): items = Counter(c for c in text.lower() if c.isalpha()).items() return sorted( sorted(items, key=itemgetter(0)), key=itemgetter(1), reverse=True )
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
from collections import Counter def letter_frequency(text): return sorted(Counter(filter(str.isalpha, text.lower()) ).most_common(), key=lambda t:(-t[1],t[0]))
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
# return a list of tuples sorted by frequency with # the most frequent letter first. Any letters with the # same frequency are ordered alphabetically def letter_frequency(text): text = text.lower() freq = sorted([(l, text.count(l)) for l in set(text) if l.isalpha()], key=lambda k: k[0]) return sorted(freq, key=lambda k: k[1], reverse=True)
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
def letter_frequency(text): d = {} for i in text: if i.isalpha(): i = i.lower() d[i] = d[i] + 1 if i in d else 1 return sorted(d.items(), key=lambda (k, v): (-v, k))
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
import re from collections import Counter def letter_frequency(text): letters = re.findall('[a-z]', text.lower()) freqs = Counter(letters).most_common() return sorted(freqs, lambda a, b: cmp(b[1], a[1]) or cmp(a[0], b[0]))
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
def letter_frequency(text): text = text.lower() freq_count = [(c, text.count(c)) for c in text if c.isalpha()] return sorted(list(set(freq_count)), key=lambda x: (-x[1], x[0]))
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
from collections import Counter def letter_frequency(text): return sorted(Counter(''.join([c for c in text.lower() if c.isalpha()])).items(), key=lambda x: (-x[1],x[0]))
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
def letter_frequency(text): text = [c for c in text.lower() if c.isalpha()] result = [(char, text.count(char)) for char in sorted(set(text))] return sorted(result, key = lambda char_freq: char_freq[1], reverse = True)
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
from collections import Counter def letter_frequency(text): cnts = Counter(c for c in text.lower() if c.isalpha()).most_common() return sorted(cnts, key=lambda x: (-x[1], x[0]))
Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase. The function should return a list of tuples (in Python and Haskell) or arrays (in other languages) sorted by the most frequent letters first. The Rust implementation should return an ordered BTreeMap. Letters with the same frequency are ordered alphabetically. For example: ```python letter_frequency('aaAabb dddDD hhcc') ``` ```C++ letter_frequency("aaAabb dddDD hhcc") ``` will return ```python [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)] ``` ```C++ std::vector>{{'d',5}, {'a',4}, {'b',2}, {'c',2}, {'h',2}} ``` Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.
def letter_frequency(text): text = text.lower() return sorted([(e, text.count(e)) for e in set(text) if e.isalpha()], key = lambda e: (-e[1], e[0]))
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
import operator as o class v: def __init__(s,a,b): s.a,s.b=a,b def compute(s): return getattr(o,type(s).__name__)(s.a,s.b) class value(int): pass class add(v): pass class sub(v): pass class mul(v): pass class truediv(v): pass class mod(v): pass class pow(v): pass
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
import operator as op class N: def __init__(s, *v):s.v=v def compute(s):return getattr(op,s.__class__.__name__)(s.v[0].c(),s.v[1].c()) class add(N):pass class sub(N):pass class mul(N):pass class truediv(N):pass class mod(N):pass class pow(N):pass class value(N): def c(s): return s.v[0]
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
class value: def __init__(self,v):self.v=v class o: def __init__(self,a,b):A,B=a.v,b.v;self.r={add:A+B,sub:A-B,mul:A*B,truediv:A/B,mod:A%B,pow:A**B}[type(self)] def compute(self):return self.r class add(o):1 class sub(o):1 class mul(o):1 class truediv(o):1 class mod(o):1 class pow(o):1
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
def i(s,x,y=0):s.x=getattr(x.x,s._)(y.x)if y else x class v:__init__,compute=i,lambda s:s.x for p in dir(0):value=vars()[p[2:-2]]=type(p,(v,),{"_":p})
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
class value: def __init__(s,a,b=0): s.v=a if b!=0:s.v={add:a.v+b.v,sub:a.v-b.v,mul:a.v*b.v,truediv:a.v/b.v,mod:a.v%b.v,pow:a.v**b.v}[type(s)] class v(value): def compute(s):return s.v class add(v):1 class sub(v):1 class mul(v):1 class truediv(v):1 class mod(v):1 class pow(v):1
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
class value: def __init__(s,x,y=0):s.x=getattr(x.x,s._)(y.x)if y else x def compute(s):return s.x locals().update({p[2:-2]:type(p,(value,),{"_":p})for p in dir(int)if"_"==p[-2]})
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
def M(H): class R:__init__,compute=lambda O,Q,S=[]:setattr(O,'O',Q if []==S else getattr(int,f'__{H}__')(Q.O,S.O)),lambda O:O.O return R value,add,sub,mul,truediv,mod,pow=map(M,'* add sub mul truediv mod pow'.split())
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
class F: def __init__(S,*a):S.a=a compute=lambda S:__import__("operator").__dict__[type(S).__name__](*(x.compute()for x in S.a)) class value(F):compute=lambda S:S.a[0] class add(F):pass class sub(F):pass class mul(F):pass class truediv(F):pass class mod(F):pass class pow(F):pass
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
class N: def __init__(s,a,b):s.a,s.b=a,b class value(int):1 class add(N):compute=lambda s:s.a+s.b class sub(N):compute=lambda s:s.a-s.b class mul(N):compute=lambda s:s.a*s.b class truediv(N):compute=lambda s:s.a/s.b class mod(N):compute=lambda s:s.a%s.b class pow(N):compute=lambda s:s.a**s.b
# Story You were supposed to implement a node-based calculator. Hopefully for you, a colleague agreed to do the task. When the management saw the code, they were infuriated with its low quality, and as a punishment told you to shorten it as much as possible... ___ # Task You will be given a ready solution passing all the fixed and random tests. Unfortunately for you, it is very long and overly repetitive. Current code length is `901` characters. Your task is to shorten it to at most `300` characters. **Note/hint**: you can modify the implementation any way you want, as long as it is written using only OOP; the sample tests check solution's functionality the same way as the final tests do - if your solution passes the former, it will also pass the latter.
from operator import add,sub,mul,pow,truediv,mod class value: def __init__(self, *v): self.v = v def compute(self): return self.f(self.v[0].v[0],self.v[1].v[0]) add, sub, mul, pow, truediv, mod = [type(f.__name__, (value,), {"f": f}) for f in [add, sub, mul, pow, truediv, mod]]
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
def distance_between_points(a, b): return ((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2) ** 0.5
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
def distance_between_points(a, b): return (sum((getattr(a, i) - getattr(b, i))**2 for i in 'xyz'))**0.5
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
def distance_between_points(a, b): return ((a.x-b.x)**2 + (a.y-b.y)**2 + (a.z-b.z)**2)**0.5
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
def distance_between_points(a, b): return sum((getattr(a, attr) - getattr(b, attr)) ** 2 for attr in "xyz") ** 0.5
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
import math def distance_between_points(a, b): x_prime = b.x - a.x y_prime = b.y - a.y z_prime = b.z - a.z distance = math.sqrt(x_prime ** 2 + y_prime ** 2 + z_prime ** 2) return distance
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
import math distance_between_points = lambda a,b : round(math.sqrt((a.x -b.x)**2 + (a.y -b.y)**2 + (a.z -b.z)**2),6)
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
from collections import namedtuple def distance_between_points(a, b): return ((b.x - a.x)**2 + (b.y - a.y)**2 + (b.z - a.z)**2)**0.5
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
def distance_between_points(a, b): return sum(x * x for x in [a.x - b.x, a.y - b.y, a.z - b.z]) ** 0.5
This series of katas will introduce you to basics of doing geometry with computers. `Point` objects have `x`, `y`, and `z` attributes. For Haskell there are `Point` data types described with record syntax with fields `x`, `y`, and `z`. Write a function calculating distance between `Point a` and `Point b`. Tests round answers to 6 decimal places. Tests in Haskell will not round.
distance_between_points=lambda a,b: round(sum((a[0]-a[1])**2 for a in zip([a.x,a.y,a.z],[b.x,b.y,b.z]))**0.5,6)