Fundamentals
Task
Given integer n
, return the next smaller even number.
Examples
Input = 1
Output = 0
Input = 6
Output = 6
Input = 9
Output = 8
import codewars_test as test import random from solution import e def answer(n): if n % 2 == 0: return n else: return n - 1 @test.describe("Example tests") def test_group(): @test.it("Example test cases") def test_case(): test.assert_equals(e(1), 0) test.assert_equals(e(2), 2) test.assert_equals(e(6), 6) test.assert_equals(e(9), 8) @test.describe("Random tests") def tests(): for i in range(100): n = random.randint(1, 100) expected = answer(n) actual = e(n) test.assert_equals(actual, expected, "Output should be next smaller even.")
- import codewars_test as test
- import random
# TODO Write testsimport solution # or from solution import example- from solution import e
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")- def answer(n):
- if n % 2 == 0:
- return n
- else:
- return n - 1
- @test.describe("Example tests")
- def test_group():
@test.it("test case")- @test.it("Example test cases")
- def test_case():
- test.assert_equals(e(1), 0)
- test.assert_equals(e(2), 2)
- test.assert_equals(e(6), 6)
- test.assert_equals(e(9), 8)
- @test.describe("Random tests")
- def tests():
for i in range(10):- for i in range(100):
- n = random.randint(1, 100)
expected = n - n % 2test.assert_equals(e(n), expected)- expected = answer(n)
- actual = e(n)
- test.assert_equals(actual, expected, "Output should be next smaller even.")