-
FundamentalsMathematicsTutorials
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 .describe("Example") def test_group(): .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)
Output:
-
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)
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}