# -*- coding: utf-8 -*- """is more than 2. https://www.codewars.com/kumite/67188f0627f61f2b7b0fee1c?sel=67188f0627f61f2b7b0fee1c """ def is_more_than_2(n: float | int) -> bool: """Checks if the given value is greater than two. Converts the given value to float and checks if it is greater than 2. In case of an error during type conversion (for example, if a list or a string that does not contain a number is passed), it will return False. Args: n: An integer or a floating-point number. In general, any type can be passed; there is protection against misuse. Returns: True if the given number is greater than 2. Returns False otherwise, or if the given value could not be converted to float. """ try: return float(n) > 2 except Exception: return False
def is_more_then_2(n):return n > 2- # -*- coding: utf-8 -*-
- """is more than 2.
- https://www.codewars.com/kumite/67188f0627f61f2b7b0fee1c?sel=67188f0627f61f2b7b0fee1c
- """
- def is_more_than_2(n: float | int) -> bool:
- """Checks if the given value is greater than two.
- Converts the given value to float and checks if it is greater than 2.
- In case of an error during type conversion (for example, if a list or
- a string that does not contain a number is passed), it will return False.
- Args:
- n: An integer or a floating-point number.
- In general, any type can be passed; there is protection against
- misuse.
- Returns:
- True if the given number is greater than 2. Returns False otherwise,
- or if the given value could not be converted to float.
- """
- try:
- return float(n) > 2
- except Exception:
- return False
import codewars_test as test from solution import is_more_than_2 @test.describe("Tests with numbers") def test_group_1(): @test.it("Integers") def test_case_1(): test.assert_equals(is_more_than_2(-1), False) test.assert_equals(is_more_than_2(0), False) test.assert_equals(is_more_than_2(1), False) test.assert_equals(is_more_than_2(2), False) test.assert_equals(is_more_than_2(3), True) @test.it("Floating point numbers") def test_case_2(): test.assert_equals(is_more_than_2(-1.1), False) test.assert_equals(is_more_than_2(0.1), False) test.assert_equals(is_more_than_2(1.1), False) test.assert_equals(is_more_than_2(2.1), True) test.assert_equals(is_more_than_2(3.1), True) @test.describe("Tests with other types") def test_group_2(): @test.it("Strings") def test_case_3(): test.assert_equals(is_more_than_2(""), False) test.assert_equals(is_more_than_2("Hello, world!"), False) test.assert_equals(is_more_than_2("1"), False) test.assert_equals(is_more_than_2("3"), True) test.assert_equals(is_more_than_2("2.7182"), True) @test.it("Lists") def test_case_4(): test.assert_equals(is_more_than_2([]), False) test.assert_equals(is_more_than_2([3]), False) test.assert_equals(is_more_than_2([1, 2]), False) test.assert_equals(is_more_than_2(len([1, 2, 3])), True) test.assert_equals(is_more_than_2(["3"]), False)
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example- from solution import is_more_than_2
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(1 + 1, 2)- @test.describe("Tests with numbers")
- def test_group_1():
- @test.it("Integers")
- def test_case_1():
- test.assert_equals(is_more_than_2(-1), False)
- test.assert_equals(is_more_than_2(0), False)
- test.assert_equals(is_more_than_2(1), False)
- test.assert_equals(is_more_than_2(2), False)
- test.assert_equals(is_more_than_2(3), True)
- @test.it("Floating point numbers")
- def test_case_2():
- test.assert_equals(is_more_than_2(-1.1), False)
- test.assert_equals(is_more_than_2(0.1), False)
- test.assert_equals(is_more_than_2(1.1), False)
- test.assert_equals(is_more_than_2(2.1), True)
- test.assert_equals(is_more_than_2(3.1), True)
- @test.describe("Tests with other types")
- def test_group_2():
- @test.it("Strings")
- def test_case_3():
- test.assert_equals(is_more_than_2(""), False)
- test.assert_equals(is_more_than_2("Hello, world!"), False)
- test.assert_equals(is_more_than_2("1"), False)
- test.assert_equals(is_more_than_2("3"), True)
- test.assert_equals(is_more_than_2("2.7182"), True)
- @test.it("Lists")
- def test_case_4():
- test.assert_equals(is_more_than_2([]), False)
- test.assert_equals(is_more_than_2([3]), False)
- test.assert_equals(is_more_than_2([1, 2]), False)
- test.assert_equals(is_more_than_2(len([1, 2, 3])), True)
- test.assert_equals(is_more_than_2(["3"]), False)