Ad
Code
Diff
  • def your_name(n="Taki"):
        return f"Hello {n}, you are very kool!"
    
    
    • your_name =lambda n = 'Taki': f"Hello {n}, you are very kool!"
    • def your_name(n="Taki"):
    • return f"Hello {n}, you are very kool!"
Code
Diff
  • prod = lambda args: __import__('functools').reduce(lambda x, y: x * y, [args[0], args[1]])
    
    • from math import prod
    • result = prod((5, 10))
    • print(result)
    • prod = lambda args: __import__('functools').reduce(lambda x, y: x * y, [args[0], args[1]])
Fundamentals
Strings
Code
Diff
  • reverse_string = lambda string: __import__('functools').reduce(lambda a, b: b + a, string)
    • def reverse_string(string): return string[::-1] #real 1 line anwser
    • reverse_string = lambda string: __import__('functools').reduce(lambda a, b: b + a, string)
Fundamentals
Strings
Code
Diff
  • def reverse_string(string):
        reversed_list = [string[letter] for letter in range(len(string) - 1, -1, -1)]
        return "".join(reversed_list)
    
    • from collections import deque
    • def reverse_string(string):
    • return string[::-1]
    • reversed_list = [string[letter] for letter in range(len(string) - 1, -1, -1)]
    • return "".join(reversed_list)
Fundamentals
Strings

Another one thread using 'deque' method

Code
Diff
  • from collections import deque
    
    def reverse_string(string):
        reversed_str = deque("")
        [reversed_str.appendleft(letter) for letter in string]
        return "".join(reversed_str)
    • from collections import deque
    • def reverse_string(string):
    • return string[::-1]
    • reversed_str = deque("")
    • [reversed_str.appendleft(letter) for letter in string]
    • return "".join(reversed_str)
Fundamentals
Strings
Code
Diff
  • from collections import deque
    
    def reverse_string(string):
        dequed_str = deque(string)
        return ''.join([dequed_str.pop() for _ in range(len(dequed_str))])
    • from collections import deque
    • def reverse_string(string):
    • return string[::-1]
    • dequed_str = deque(string)
    • return ''.join([dequed_str.pop() for _ in range(len(dequed_str))])
Code
Diff
  • from itertools import compress
    
    def test():
        string = "letters inside a string"
        digits = [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
        result = compress(string, digits)
        return ''.join(list(result))
    
    • from itertools import compress
    • def test():
    • return "".join(map(lambda i: {'1': 't', '2': 'e', '3': 's', '4': 't'}[str(i)], range(1, 5)))
    • string = "letters inside a string"
    • digits = [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
    • result = compress(string, digits)
    • return ''.join(list(result))
Code
Diff
  • from functools import reduce
    
    def est_height(gender, dad_height, mom_height):
        match gender:
            case "boy":
                return reduce(lambda x, y: x + y, [dad_height, mom_height]) / 2 + 0.5
            case "girl":
                return reduce(lambda x, y: x + y, [dad_height, mom_height]) / 2 - 0.5
        
    • est_height=lambda g,d,m:(d+m+[-1,1][g=="boy"])/2
    • from functools import reduce
    • def est_height(gender, dad_height, mom_height):
    • match gender:
    • case "boy":
    • return reduce(lambda x, y: x + y, [dad_height, mom_height]) / 2 + 0.5
    • case "girl":
    • return reduce(lambda x, y: x + y, [dad_height, mom_height]) / 2 - 0.5
Code
Diff
  • from math import prod
    def multiply(a,b):
        ints = (a, b)
        return prod(ints)
    multiply (5,10)
    print(multiply(5,10))
    
    • from math import prod
    • def multiply(a,b):
    • return a * b
    • ints = (a, b)
    • return prod(ints)
    • multiply (5,10)
    • print(multiply(5,10))