saved some chars and and operations by just cheking the least significant bit.
a more maximalist version with type annotations like a java programmer who is learning python. But wait... a twist, mypy doesn't like this if you check the type annotations, a really good writeup of the issue can be found here: https://stackoverflow.com/questions/69334475/how-to-hint-at-number-types-i-e-subclasses-of-number-not-numbers-themselv/69383462#69383462. I hope I get to share this bit of serendipity with someone one day.
from numbers import Number def kube (l: Number, w: Number, h: Number) -> Number: if any(not isinstance(x, Number) for x in (l, w, h)): raise TypeError("sides must be numeric") if any(x < 0 for x in (l, w, h)): raise ValueError("Cube edges must have positive length") else: return l * w * h
kube = lambda l, w, h: "Invalid Cube" if any(x < 0 for x in (l, w, h)) else l * w * h- from numbers import Number
- def kube (l: Number, w: Number, h: Number) -> Number:
- if any(not isinstance(x, Number) for x in (l, w, h)):
- raise TypeError("sides must be numeric")
- if any(x < 0 for x in (l, w, h)):
- raise ValueError("Cube edges must have positive length")
- else:
- return l * w * h
import codewars_test as test # TODO Write tests from solution import kube # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(kube(7, 9, 4), 252) test.assert_equals(kube(5, 12, 7), 420) test.assert_equals(kube(3, 7, 49), 1029) test.expect_error("shouldn't work with lists", lambda:kube([], 3, 99), exception=TypeError) test.expect_error("side lengths cant be negative", lambda:kube(-1,1,4), exception=ValueError)
- import codewars_test as test
- # TODO Write tests
- from solution import kube
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(kube(7, 9, 4), 252)
- test.assert_equals(kube(5, 12, 7), 420)
- test.assert_equals(kube(3, 7, 49), 1029)
test.assert_equals(kube(-1,1,4), "Invalid Cube")- test.expect_error("shouldn't work with lists", lambda:kube([], 3, 99), exception=TypeError)
- test.expect_error("side lengths cant be negative", lambda:kube(-1,1,4), exception=ValueError)