You will be given numbers and the task is to return even numbers as they are, but subtract 1 from odd numbers.
So if you get 4
you return 4
. If you get 23
you return 22
.
Your solution should be 22 or less characters long.
def e(n): return n-n%2
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("test case")
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)
You will be given numbers and the task is to return odd numbers as they are, but subtract 1 from even numbers.
So if you get 3
you return 3
. If you get 24
you return 23
.
Your solution should be 27 or less characters long.
def o(n): return n-(n%2==0)
import codewars_test as test
import random
# 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("test case")
def test_case():
test.assert_equals(o(1), 1)
test.assert_equals(o(2), 1)
test.assert_equals(o(5), 5)
test.assert_equals(o(8), 7)
@test.describe("Random tests")
def tests():
for i in range(10):
n = random.randint(1, 100)
expected = n-(n%2==0)
test.assert_equals(o(n), expected)