Move History

Fork Selected
  • Fundamentals
    Mathematics
    Tutorials
    Code
    import math
    
    def is_square(n):
        return int(math.sqrt(n) + 0.5) ** 2 == n
    Test Cases
    import codewars_test as test
    from solution import is_square
    
    
    @test.describe("Example")
    def test_group():
        @test.it("test case")
        def test_case():
            perfect_squares_samples = (4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900,961)
            non_perfect_squares_samples = [n + 1 for n in perfect_squares_samples]
            for s in perfect_squares_samples:
                test.assert_equals(is_square(s), True)
            for s in non_perfect_squares_samples:
                test.assert_equals(is_square(s), False)
  • Code
    • function isSquare(n){
    • return Number.isInteger(Math.sqrt(n));
    • }
    • import math
    • def is_square(n):
    • return int(math.sqrt(n) + 0.5) ** 2 == n
    Test Cases
    • // Since Node 10, we're using Mocha.
    • // You can use `chai` for assertions.
    • const chai = require("chai");
    • const assert = chai.assert;
    • // Uncomment the following line to disable truncating failure messages for deep equals, do:
    • // chai.config.truncateThreshold = 0;
    • // Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
    • // Uncomment the following to use the old assertions:
    • const Test = require("@codewars/test-compat");
    • import codewars_test as test
    • from solution import is_square
    • describe("Solution", function() {
    • it("should test for something", function() {
    • Test.assertEquals(isSquare(7), false);
    • Test.assertEquals(isSquare(9), true);
    • });
    • });
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • perfect_squares_samples = (4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900,961)
    • non_perfect_squares_samples = [n + 1 for n in perfect_squares_samples]
    • for s in perfect_squares_samples:
    • test.assert_equals(is_square(s), True)
    • for s in non_perfect_squares_samples:
    • test.assert_equals(is_square(s), False)