Ancestor:
def cake_maker(word: str) -> str:
ingredients = 'cake'
cake_mix = set()
idx = 0
while idx != len(word):
if word[idx].lower() in ingredients:
cake_mix.add(word[idx].lower())
idx += 1
if len(cake_mix) == len(ingredients):
return 'Cake'
return 'Not enough to make the cake!'
- if you are only using indexes for access -> use a for loop
- second time lower is used. use it in one location
so, the loop thing looks like this
for char in word.lower():
if char in ingredients:
cake_mix.add(char)
and now should be obvious why we use a list/set "comprehention" instead of starting from an empty set.
Also, I like the idea of using sets for this one.
But that means we can use default set operations to check the subset part:
cake_mix >= ingredients
is enough.
Taking everything together we arive at my solution.
Timings:
I wanted to compare set speed to other clean version in the ancestory.
I chose:
def list_maker(word: str) -> str:
return 'Cake' if 'cake' == ''.join([i for i in 'cake' if i in word.lower()]) else "Not enough to make the cake!"
and
def not_in_maker(word: str) -> str:
for l in "cake":
if l not in word.lower():
return "Not enough to make the cake!"
return "Cake"
For the success case I used the alphabet repeated 1_000_000 times.
For the failing case I used the alphabet without 'a' repeated 1_000_000 times
(I timed it on my machine obv)
- Ancestor:
- success: 6.95 seconds
- failiour: 6.47 seconds
- MyVersion (set_maker):
- success: 0.23 seconds
- failiour: 0.22 seconds
- list_maker:
- success: 0.09 seconds
- failiour: 0.08 seconds
- not_in_maker:
- success: 0.07 seconds
- failiour: 0.04 seconds
From the rough results we can conclude that what looks simplest in code is also the fastest in this case.
def cake_maker(word: str) -> str: ingredients = {"c","a","k","e"} cake_mix = set(word.lower()) if cake_mix >= ingredients: return 'Cake' else: return 'Not enough to make the cake!'
- def cake_maker(word: str) -> str:
ingredients = 'cake'cake_mix = set()idx = 0while idx != len(word):if word[idx].lower() in ingredients:cake_mix.add(word[idx].lower())idx += 1if len(cake_mix) == len(ingredients):- ingredients = {"c","a","k","e"}
- cake_mix = set(word.lower())
- if cake_mix >= ingredients:
- return 'Cake'
return 'Not enough to make the cake!'- else:
- return 'Not enough to make the cake!'
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("Are you any good at baking cakes?") def test_case(): test.assert_equals(cake_maker("Cupcake"), "Cake", "Should return 'Cake'") test.assert_equals(cake_maker("Pancake"), "Cake", "Should return 'Cake'") test.assert_equals(cake_maker("CheeseCake"), "Cake", "Should return 'Cake'") test.assert_equals(cake_maker("RaCket"), "Cake", "Should return 'Cake'") test.assert_equals(cake_maker("Take"), "Not enough to make the cake!", "Shouldn't return 'Cake'") test.assert_equals(cake_maker("Lake"), "Not enough to make the cake!", "Shouldn't return 'Cake'") test.assert_equals(cake_maker("ThE cAkE iS a LiE!"), "Cake", "Should return 'Cake'")
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("Are you any good at baking cakes?")
- def test_case():
- test.assert_equals(cake_maker("Cupcake"), "Cake", "Should return 'Cake'")
- test.assert_equals(cake_maker("Pancake"), "Cake", "Should return 'Cake'")
- test.assert_equals(cake_maker("CheeseCake"), "Cake", "Should return 'Cake'")
- test.assert_equals(cake_maker("RaCket"), "Cake", "Should return 'Cake'")
- test.assert_equals(cake_maker("Take"), "Not enough to make the cake!", "Shouldn't return 'Cake'")
- test.assert_equals(cake_maker("Lake"), "Not enough to make the cake!", "Shouldn't return 'Cake'")
- test.assert_equals(cake_maker("ThE cAkE iS a LiE!"), "Cake", "Should return 'Cake'")
def return_10(): return int(ord("ᕟ")^ord("ᕕ"))
def r():def t():return int("10",2)def e():return int("1000",2)return t() ^ e()return_10 = r- def return_10():
- return int(ord("ᕟ")^ord("ᕕ"))
import codewars_test as test # TODO Write tests from solution import return_10 # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("results in 10") def test_case(): test.assert_equals(return_10(), 10)
- import codewars_test as test
- # TODO Write tests
- from solution import return_10
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
@test.it("test case 1")- @test.it("results in 10")
- def test_case():
- test.assert_equals(return_10(), 10)