Ad
Fundamentals
Restricted
Code
Diff
  • is_even=lambda*x:not(x[int()]&True)
    • is_even=lambda*x:~x[False]&True
    • is_even=lambda*x:not(x[int()]&True)
Code
Diff
  • colour_of_fruit = lambda fruit : {
        "Apple": "Red", "Raspberry": "Red", "Strawberry": "Red", 
        "Orange": "Orange","Banana": "Yellow", "Lemon": "Yellow", 
        "Pineapple": "Yellow", "Avocado": "Green", "Lime": "Green", 
        "Melon": "Green", "Pear": "Green", "Blueberry": "Blue", 
        "Huckleberry": "Blue", "Plum": "Purple", "Grape": "Purple", 
        "Maquiberry": "Purple"
    }.get(fruit, "Not a fruit!")
    • fruits = {"Apple": "Red", "Raspberry": "Red", "Strawberry": "Red", "Orange": "Orange","Banana": "Yellow", "Lemon": "Yellow", "Pineapple": "Yellow", "Avocado": "Green", "Lime": "Green", "Melon": "Green", "Pear": "Green", "Blueberry": "Blue", "Huckleberry": "Blue", "Plum": "Purple", "Grape": "Purple", "Maquiberry": "Purple"}
    • colour_of_fruit = lambda fruit : fruits[fruit] if fruit in fruits else "Not a fruit!"
    • '''
    • almost oneliner... I am hungry. I'll have a fruit.
    • Thanks for creating the dictionary (map) by the way.
    • Instead of defining the map fruits, I could just copy
    • that map and put it wherever I used it and then, after
    • deleting the comments, it would be a perfect oneliner XD.
    • I will leave this chance for someone else XD.
    • '''
    • colour_of_fruit = lambda fruit : {
    • "Apple": "Red", "Raspberry": "Red", "Strawberry": "Red",
    • "Orange": "Orange","Banana": "Yellow", "Lemon": "Yellow",
    • "Pineapple": "Yellow", "Avocado": "Green", "Lime": "Green",
    • "Melon": "Green", "Pear": "Green", "Blueberry": "Blue",
    • "Huckleberry": "Blue", "Plum": "Purple", "Grape": "Purple",
    • "Maquiberry": "Purple"
    • }.get(fruit, "Not a fruit!")
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")
    }
    
    
    fruits = {
        f: c
        for c, fs in fruit_colours.items()
        for f in fs
    }
    
    
    def colour_of_fruit(fruit: str) -> str:
        """Returns the color of given fruit."""
        return fruits.get(fruit, "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")
    • }
    • fruits = {
    • f: c
    • for c, fs in fruit_colours.items()
    • for f in fs
    • }
    • 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!"
    • return fruits.get(fruit, "Not a fruit!")
Code
Diff
  • 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
    • #This works for base-10 only. Other bases might require using text-type objects.
    • from math import log10
    • def is_Armstrong(n):
    • temp = n #Make a copy of n for use obtaining digits.
    • #Bearing in mind that n must be kept available.
    • #Get count of digits
    • def is_armstrong(n: int) -> bool:
    • temp = n
    • count = int(log10(n)) + 1
    • #Reduce n from digits calculation
    • while temp:
    • temp, digit = divmod(temp, 10)
    • n -= digit ** count
    • #Check that n has been reduced to exactly zero
    • return not n
Fundamentals
Arrays
Strings
Code
Diff
  • from dataclasses import dataclass, field
    
    
    @dataclass
    class Dinosaur:
        name: str
        meat_eater: bool = field(default=False)
    
        @staticmethod
        def find_trex(lst):
            return any(
                isinstance(dinosaur, Tyrannosaurus)
                and dinosaur.meat_eater 
                and dinosaur.name == 'tyrannosaurus'
                for dinosaur in lst
            )
    
    
    @dataclass
    class Tyrannosaurus(Dinosaur):
        meat_eater: bool = field(default=True)
    
    • from dataclasses import dataclass, field
    • @dataclass
    • class Dinosaur:
    • name: str
    • meat_eater: bool = field(default=False)
    • @staticmethod
    • def find_trex(lst):
    • for dinosaur in lst:
    • if isinstance(dinosaur, Tyrannosaurus) and dinosaur.meat_eater and dinosaur.name == 'tyrannosaurus':
    • return True
    • return False
    • return any(
    • isinstance(dinosaur, Tyrannosaurus)
    • and dinosaur.meat_eater
    • and dinosaur.name == 'tyrannosaurus'
    • for dinosaur in lst
    • )
    • @dataclass
    • class Tyrannosaurus(Dinosaur):
Code
Diff
  • longest_string = lambda a: max(a.split(), key=len)
    • def longest_string(a):
    • return max(a.split(), key=len)
    • longest_string = lambda a: max(a.split(), key=len)
Fundamentals
Code
Diff
  • def hello_message():
        t = str.maketrans('_mag', ' Wld')
        return ''.join(
            hello_message
                .__name__
                .capitalize()
                .translate(t)[:-1]
                .replace('ess', 'or')
                + '!'
        )
    
    • exec(bytes('敨汬彯敭獳条⁥‽慬扭慤›䠧汥潬圠牯摬✡', 'u16')[2:])
    • def hello_message():
    • t = str.maketrans('_mag', ' Wld')
    • return ''.join(
    • hello_message
    • .__name__
    • .capitalize()
    • .translate(t)[:-1]
    • .replace('ess', 'or')
    • + '!'
    • )
Fundamentals
Strings
Code
Diff
  • digest = lambda s: ' '.join(s)
    • exec(bytes('楤敧瑳氽浡摢⁡㩳•⸢潪湩嬨猪⥝', 'u16')[2:])
    • digest = lambda s: ' '.join(s)
Code
Diff
  • multiple = int.__mul__
    • def multiple(a,b):
    • return a*b
    • multiple = int.__mul__
Code
Diff
  • import re
    
    def clean(s: str) -> str:
        return re.sub("\d", '', s)
    
    • clean=lambda s:__import__("re").sub("[0-9]","",s)
    • import re
    • def clean(s: str) -> str:
    • return re.sub("\d", '', s)
Code
Diff
  • def clean(s: str) -> str:
        return ''.join(
            char for char in s
            if not char.isdigit()
        )
    • clean = lambda s: ''.join(x for x in s if not x.isdigit())
    • def clean(s: str) -> str:
    • return ''.join(
    • char for char in s
    • if not char.isdigit()
    • )
Fundamentals
Arrays
Strings
Code
Diff
  • from dataclasses import dataclass
    from typing import List
    
    
    @dataclass
    class FindTRex:
        dinosaurs: List[str]
    
        def find_trex(self) -> bool:
            return 'tyrannosaurus' in self.dinosaurs
    
    • from dataclasses import dataclass
    • from typing import List
    • @dataclass
    • class FindTRex:
    • """Find Trex class."""
    • def __init__(self, lst):
    • """Initialize attribute"""
    • self.lst = lst
    • dinosaurs: List[str]
    • def find_trex(self):
    • """Returns True if 'tyrannosaurus' is in self.lst, False if not."""
    • return self.lst.__contains__('tyrannosaurus')
    • def find_trex(self) -> bool:
    • return 'tyrannosaurus' in self.dinosaurs