Ad
Code
Diff
  • result = lambda n: [n // 10, 'fail'][n < 70]
    • result = lambda n: 'fail' if n < 70 else n // 10
    • result = lambda n: [n // 10, 'fail'][n < 70]
Code
Diff
  • def letter(str):
        result = []
        words = [x for x in str.lower().strip().split() if x]
        mx = len(max(words, key=len)) if words else 0
        starts = set(x[:i] for x in words for i in range(1, mx+1))
        for s in starts:
            matching = [word for word in words if word.startswith(s)]
            if len(matching) == len(s):
                result += matching
        return sorted(result)
    • import re
    • def letter(str):
    • words = re.sub(r"[^a-z]", " ", str.lower()).split(" ")
    • lst = []
    • l1 = []
    • l2 = []
    • sol = []
    • for x in words:
    • word = ""
    • for y in x:
    • word += y
    • lst.append(word)
    • for x in sorted(set(lst)):
    • for y in words:
    • if y.startswith(x):
    • l1.append(y)
    • l2.append(list(l1))
    • l1.clear()
    • dic = dict(zip(sorted(set(lst)), l2))
    • for k, v in dic.items():
    • if len(k) == len(v):
    • for x in v:
    • sol.append(x)
    • return sorted(sol)
    • result = []
    • words = [x for x in str.lower().strip().split() if x]
    • mx = len(max(words, key=len)) if words else 0
    • starts = set(x[:i] for x in words for i in range(1, mx+1))
    • for s in starts:
    • matching = [word for word in words if word.startswith(s)]
    • if len(matching) == len(s):
    • result += matching
    • return sorted(result)
  • Added functionality and unit tests to deduce the days passed in leap and non-leap years.
Code
Diff
  • days = lambda y, m, d: sum(([31, [28, 29][all([not y % 4, (y % 100 or not y % 400)])]] + [31, 30, 31, 30, 31] * 2)[:m - 1]) + d
    • def days(months, day):
    • month_by_days = [False,
    • 0 +day, 31 +day, 59 +day,
    • 90 +day, 120 +day, 151 +day,
    • 181 +day, 212 +day, 243 +day,
    • 273 +day, 304 +day, 334 +day ]
    • return month_by_days[int(months)]
    • days = lambda y, m, d: sum(([31, [28, 29][all([not y % 4, (y % 100 or not y % 400)])]] + [31, 30, 31, 30, 31] * 2)[:m - 1]) + d
Code
Diff
  • spawn_to_range = lambda a, v, r: a[:a.index(v)] + [v] * r + a[a.index(v) + 1:] if v in a else []
    • def spawn_to_range(a, x, n):
    • # return [] if(len(a)==0) else a[0:a.index(x)] + [x]*n + a[a.index(x)+1:]
    • return [*a[0:(i:=a.index(x))],*[x]*n,*(a[j]for j in range(i+1,z))]if(z:=len(a))else[]
    • spawn_to_range = lambda a, v, r: a[:a.index(v)] + [v] * r + a[a.index(v) + 1:] if v in a else []
Code
Diff
  • def spawn_to_range(a, v, r): i = a.index(v); return a[:i] + [v] * r + a[i + 1:]
    • spawn_to_range=lambda a,v,r:a[:a.index(v)]+[v]*r+a[a.index(v)+1:]
    • def spawn_to_range(a, v, r): i = a.index(v); return a[:i] + [v] * r + a[i + 1:]
Code
Diff
  • foo = lambda a, b : list(set(a) - set(b))
    • foo = lambda a, b : [i for i in a if i not in b]
    • foo = lambda a, b : list(set(a) - set(b))
Code
Diff
  • def sort(x): z=lambda y: sorted(filter(y, x)); return ''.join(z(str.isalpha) + z(str.isdigit))
    
    • sort = lambda x: ''.join(sorted(filter(str.isalnum, x), key=lambda y: (y.isdigit(), y)))
    • def sort(x): z=lambda y: sorted(filter(y, x)); return ''.join(z(str.isalpha) + z(str.isdigit))
Code
Diff
  • def assert_equal(*args):
        assert len(set(args)) == 1
    
        
    • def assert_equal(param1, param2):
    • assert param1 == param2
    • def assert_equal(*args):
    • assert len(set(args)) == 1
  • Introduced function chaining to reuse existing functions.
  • Introduced a base kwarg to override self.num as argument for the function for a more dynamic approach/higher reusability of existing functions.
Code
Diff
  • #!/usr/bin/env python3
    """
    Debugging: FixDatTrash # 2
    """
    
    # constant:
    PI = 3.1415926535897
    
    
    class Calculator:
        """A calculator class."""
    
        def __init__(self, num):
            """Initialize attributes."""
            self.num = num
    
        def calculate_power(self, exp, base=None):
            """
            Calculates power of self.num.
            parameter `exp` is the exponent.
            parameter `base` is an optional argument to override self.num with.
            """
            return (base or self.num) ** exp
    
        def calculate_square_root(self):
            """Calculates square root of self.num or any other real number."""
            return self.calculate_power(0.5)
    
        def calculate_cube_root(self):
            """Calculates cube root of self.num or any other real number."""
            return self.calculate_power(1 / 3)
    
        def calculate_circumference(self, base=None):
            """
            Calculates circumference of circle.
            self.num is the diameter of the circle.
            """
            return PI * (base or self.num)
        
        def calculate_circle_area(self):
            """
            Calculates area of circle.
            self.num is the radius of the circle.
            A=πr2
            """
            radius_squared = self.calculate_power(2)
            return self.calculate_circumference(radius_squared)
        
    • #!/usr/bin/env python3
    • """
    • Debugging: FixDatTrash # 2
    • """
    • import math
    • from dataclasses import dataclass
    • PI = math.pi
    • # constant:
    • PI = 3.1415926535897
    • @dataclass
    • class Calculator:
    • """A calculator class."""
    • num: int
    • def calculate_power(self, exp):
    • def __init__(self, num):
    • """Initialize attributes."""
    • self.num = num
    • def calculate_power(self, exp, base=None):
    • """
    • Calculates power of self.num.
    • parameter `exp` is the exponent.
    • parameter `base` is an optional argument to override self.num with.
    • """
    • return self.num ** exp
    • return (base or self.num) ** exp
    • def calculate_square_root(self):
    • """Calculates square root of self.num."""
    • return self.num ** 0.5
    • """Calculates square root of self.num or any other real number."""
    • return self.calculate_power(0.5)
    • def calculate_cube_root(self):
    • """Calculates cube root of self.num."""
    • return self.num ** (1 / 3)
    • """Calculates cube root of self.num or any other real number."""
    • return self.calculate_power(1 / 3)
    • def calculate_circumference(self):
    • def calculate_circumference(self, base=None):
    • """
    • Calculates circumference of circle.
    • self.num is the diameter of the circle.
    • """
    • return PI * self.num
    • return PI * (base or self.num)
    • def calculate_circle_area(self):
    • """
    • Calculates area of circle.
    • self.num is the radius of the circle
    • self.num is the radius of the circle.
    • A=πr2
    • """
    • return self.num ** 2 * PI
    • radius_squared = self.calculate_power(2)
    • return self.calculate_circumference(radius_squared)
Code
Diff
  • is_armstrong = lambda n: n == sum(d ** len(str(n)) for d in [(n // max(10 ** i, 1)) % 10 for i in range(len(str(n)))])
    • from math import log10
    • def is_armstrong(n: int) -> bool:
    • temp = n
    • count = int(log10(n)) + 1
    • while temp:
    • temp, digit = divmod(temp, 10)
    • n -= digit ** count
    • return not n
    • is_armstrong = lambda n: n == sum(d ** len(str(n)) for d in [(n // max(10 ** i, 1)) % 10 for i in range(len(str(n)))])
Code
Diff
  • find_multiples = lambda n: sum([i, 0][all([i % 4, i % 6])] for i in range(n))
    • find_multiples = lambda n: sum([i for i in range(n) if i % 4 == 0 or i % 6 == 0])
    • find_multiples = lambda n: sum([i, 0][all([i % 4, i % 6])] for i in range(n))
Code
Diff
  • "H".__mul__
    • theletterh = 'H'.__mul__
    • "H".__mul__
Code
Diff
  • fruit_colours = {
        "Red": ("Apple", "Raspberry", "Strawberry"),
        "Orange": ("Orange",),
        "Yellow": ("Banana", "Lemon", "Pineapple"),
        "Green": ("Avocado", "Lime", "Melon", "Pear"),
        "Blue":  ("Blueberry", "Huckleberry"),
        "Purple": ("Plum", "Grape", "Maquiberry")
    }
    
    colour_of_fruit = lambda fruit: next((colour for colour, fruits in fruit_colours.items() if fruit in fruits), "Not a fruit!")
    • fruit_colours = {
    • "Red": ("Apple", "Raspberry", "Strawberry"),
    • "Orange": ("Orange",),
    • "Yellow": ("Banana", "Lemon", "Pineapple"),
    • "Green": ("Avocado", "Lime", "Melon", "Pear"),
    • "Blue": ("Blueberry", "Huckleberry"),
    • "Purple": ("Plum", "Grape", "Maquiberry")
    • }
    • def colour_of_fruit(fruit: str) -> str:
    • """Returns the color of given fruit."""
    • for color, fruits in fruit_colours.items():
    • if fruit in fruits: return color
    • return "Not a fruit!"
    • colour_of_fruit = lambda fruit: next((colour for colour, fruits in fruit_colours.items() if fruit in fruits), "Not a fruit!")
Fundamentals
Code
Diff
  • hello_message = lambda this=__import__("this"), s=lambda this: ''.join([this.d[x] if x.isalpha() else x for x in this.s]): ''.join([s(this)[x] for x in [1, 2, 42, 42, 8, 3, 512, 8, 30, 42, 163, 855]]).title()
    • def hello_message():
    • t = str.maketrans('_mag', ' Wld')
    • return ''.join(
    • hello_message
    • .__name__
    • .capitalize()
    • .translate(t)[:-1]
    • .replace('ess', 'or')
    • + '!'
    • )
    • hello_message = lambda this=__import__("this"), s=lambda this: ''.join([this.d[x] if x.isalpha() else x for x in this.s]): ''.join([s(this)[x] for x in [1, 2, 42, 42, 8, 3, 512, 8, 30, 42, 163, 855]]).title()
Fundamentals
Arrays
Strings
Code
Diff
  • FindTRex = lambda lst: type("FindTRex", tuple(), {"lst" : lst, "find_trex" : lambda : "tyrannosaurus" in lst})
    • class FindTRex:
    • """Find Trex class."""
    • def __init__(self, lst):
    • """Initialize attribute"""
    • self.lst = lst
    • def find_trex(self):
    • """Returns True if 'tyrannosaurus' is in self.lst, False if not."""
    • return "tyrannosaurus" in self.lst
    • FindTRex = lambda lst: type("FindTRex", tuple(), {"lst" : lst, "find_trex" : lambda : "tyrannosaurus" in lst})
Loading more items...