def calculator(num1, num2, kind): operations = { '+': lambda x, y: x + y, '-': lambda x, y: x - y, '*': lambda x, y: x * y, '/': lambda x, y: x / y if y != 0 else float('inf'), '//': lambda x, y: x // y if y != 0 else float('inf'), '%': lambda x, y: x % y if y != 0 else float('nan'), '**': lambda x, y: x ** y, } if kind not in operations: raise ValueError(f"Unsupported operation: {kind}") return operations[kind](num1, num2)
- def calculator(num1, num2, kind):
return eval(f'{num1} {kind} {num2}')- operations = {
- '+': lambda x, y: x + y,
- '-': lambda x, y: x - y,
- '*': lambda x, y: x * y,
- '/': lambda x, y: x / y if y != 0 else float('inf'),
- '//': lambda x, y: x // y if y != 0 else float('inf'),
- '%': lambda x, y: x % y if y != 0 else float('nan'),
- '**': lambda x, y: x ** y,
- }
- if kind not in operations:
- raise ValueError(f"Unsupported operation: {kind}")
- return operations[kind](num1, num2)
class Movie: def __init__(self, title, rating): self.title = title self.rating = rating def is_hit(self): return "hit" if self.rating >= 8 else "flap" def display_info(self): print(f"title: {self.title} rating: {self.rating}")
class movie:- class Movie:
- def __init__(self, title, rating):
- self.title = title
- self.rating = rating
- def is_hit(self):
if self.rating >= 8:return "hit"else:return "flap"- return "hit" if self.rating >= 8 else "flap"
- def display_info(self):
print(f" title: {self.title} rating: {self.rating}")- print(f"title: {self.title} rating: {self.rating}")
import codewars_test as test from solution import Movie @test.describe("Movie class tests") def test_group(): @test.it("should identify hits and flaps") def test_case(): movie1 = Movie("Inception", 9) movie2 = Movie("Cats", 3) test.assert_equals(movie1.is_hit(), "hit", "Inception should be a hit") test.assert_equals(movie2.is_hit(), "flap", "Cats should be a flap")
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example- from solution import Movie
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")- @test.describe("Movie class tests")
- def test_group():
@test.it("test case")- @test.it("should identify hits and flaps")
- def test_case():
test.assert_equals(1 + 1, 2)- movie1 = Movie("Inception", 9)
- movie2 = Movie("Cats", 3)
- test.assert_equals(movie1.is_hit(), "hit", "Inception should be a hit")
- test.assert_equals(movie2.is_hit(), "flap", "Cats should be a flap")