Ad
Mathematics
Code
Diff
  • import codewars_test as test
    import random
    from math import isnan
    
    inf = float('inf')
    nan = float('nan')
    
    def Calculator(*args):
        operators = {
            '+': lambda a, b: a + b,
            '-': lambda a, b: a - b,
            '*': lambda a, b: a * b,
            '/': lambda a, b: a / b if b != 0 else float('inf')
        }
    
        if len(args) != 3 or str(args[0]) not in operators:
            return float('nan')
    
        operator, operand1, operand2 = args
        return operators[operator](operand1, operand2)
    
    
    @test.describe("Sample tests")
    def sample_tests():
        test.assert_equals(Calculator("/", 1, 0), inf)
        test.assert_equals(Calculator("+", 42, 11), 53)
        test.assert_equals(Calculator("-", 6, 50), -44)
        test.assert_equals(Calculator("/", 32, 2), 16)
    
    
    if __name__ == "__main__":
        test.run()
    
    • import codewars_test as test
    • import random
    • from math import isnan
    • inf = float('inf')
    • nan = float('nan')
    • def Calculator(*args):
    • operators = {
    • '+': lambda a, b: a + b,
    • '-': lambda a, b: a - b,
    • '*': lambda a, b: a * b,
    • '/': lambda a, b: a / b if b != 0 else float('inf')
    • }
    • if len(args) != 3 or str(args[0]) not in operators:
    • return float('nan')
    • operator, operand1, operand2 = args
    • return operators[operator](operand1, operand2)
    • @test.describe("Sample tests")
    • def sample_tests():
    • test.assert_equals(Calculator("/", 1, 0), inf)
    • test.assert_equals(Calculator("+", 42, 11), 53)
    • test.assert_equals(Calculator("-", 6, 50), -44)
    • test.assert_equals(Calculator("/", 32, 2), 16)
    • if __name__ == "__main__":
    • test.run()
Code
Diff
  • def solution(param):
        return sum(p is True for p in param) > 1
    
    
    import unittest
    from solution import solution
    
    class TestSolution(unittest.TestCase):
        """Test Solution"""
    
        def setUp(self) -> None:
            self.sample_cases = [((True, True, True), True),
                                 ((True, False, True), True),
                                 (('True', True, 'True'), False),
                                 ((True, False, False, False, True), True),
                                 ((None, None, None, False, True), False),
                                 ((1, True, False), False),
                                 ((True, True), True),
                                 ((False, 'False', False), False),
                                 (('False', False, False, 'True', True), False),
                                 ((False, False, 2, 'True', True), False),
                                 ((False, '1', True, False, None, 1, True), True),
                                 ((1, 2, 3, 4, True), False),
                                 ((True, 1, True), True),
                                 ((True, False), False),
                                 ]
    
        def test_solution(self):
            for sample, expected in self.sample_cases:
                self.assertEqual(solution(sample), expected)
    • def solution(param):
    • return sum(p is True for p in param)>1
    • return sum(p is True for p in param) > 1
    • import unittest
    • from solution import solution
    • class TestSolution(unittest.TestCase):
    • """Test Solution"""
    • def setUp(self) -> None:
    • self.sample_cases = [((True, True, True), True),
    • ((True, False, True), True),
    • (('True', True, 'True'), False),
    • ((True, False, False, False, True), True),
    • ((None, None, None, False, True), False),
    • ((1, True, False), False),
    • ((True, True), True),
    • ((False, 'False', False), False),
    • (('False', False, False, 'True', True), False),
    • ((False, False, 2, 'True', True), False),
    • ((False, '1', True, False, None, 1, True), True),
    • ((1, 2, 3, 4, True), False),
    • ((True, 1, True), True),
    • ((True, False), False),
    • ]
    • def test_solution(self):
    • for sample, expected in self.sample_cases:
    • self.assertEqual(solution(sample), expected)