Fundamentals
Restricted
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 copythat map and put it wherever I used it and then, afterdeleting 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!")
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 colorreturn "Not a fruit!"- return fruits.get(fruit, "Not a fruit!")
import codewars_test as test from solution import colour_of_fruit fruit_samples = ( ("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"), ("Cabbage", "Not a fruit!"), ) @test.it("should give the color of a given fruit") def test_fruit_color(): for sample, expected in fruit_samples: test.assert_equals(colour_of_fruit(sample), expected)
import unittest- import codewars_test as test
- from solution import colour_of_fruit
class TestColorOfFruit(unittest.TestCase):def setUp(self) -> None:self.fruit_samples = (("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"),("Cabbage", "Not a fruit!"),)def test_fruit_color(self):for sample, expected in self.fruit_samples:self.assertEqual(colour_of_fruit(sample), expected)- fruit_samples = (
- ("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"), ("Cabbage", "Not a fruit!"),
- )
if __name__ == "__main__":unittest.main()- @test.it("should give the color of a given fruit")
- def test_fruit_color():
- for sample, expected in fruit_samples:
- test.assert_equals(colour_of_fruit(sample), expected)
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
import codewars_test as test from solution import is_armstrong test.assert_equals(is_armstrong(153), True) test.assert_equals(is_armstrong(43), False) test.assert_equals(is_armstrong(802), False)
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example- from solution import is_armstrong
# test.assert_equals(actual, expected, [optional] message)test.assert_equals(is_Armstrong(153), True)test.assert_equals(is_Armstrong(43), False)test.assert_equals(is_Armstrong(802), False)#Borrowed from a recent Kata for format reference"""test.assert_equals(my_automaton.read_commands(["1"]), True)test.assert_equals(my_automaton.read_commands(["1", "0", "0", "1"]), True)"""- test.assert_equals(is_armstrong(153), True)
- test.assert_equals(is_armstrong(43), False)
- test.assert_equals(is_armstrong(802), False)
Fundamentals
Arrays
Strings
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 Truereturn False- return any(
- isinstance(dinosaur, Tyrannosaurus)
- and dinosaur.meat_eater
- and dinosaur.name == 'tyrannosaurus'
- for dinosaur in lst
- )
- @dataclass
- class Tyrannosaurus(Dinosaur):
Fundamentals
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
Fundamentals
Arrays
Strings
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