We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1fb3e91 commit e9dfdfbCopy full SHA for e9dfdfb
1 file changed
sqr.py
@@ -0,0 +1,20 @@
1
+# Learn Python together
2
+
3
+"""Given a non-negative integer x, return the square root of x
4
+rounded down to the nearest integer.
5
+The returned integer should be non-negative as well.
6
+You must not use any built-in exponent function or operator.
7
+For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python."""
8
9
+# Solution
10
+import math
11
+def mySqrt(x):
12
+ if x <= 0:
13
+ return 0
14
+ else:
15
+ return int(math.sqrt(x))
16
+# check
17
+print(mySqrt(8)) # Output -> 2
18
+print(mySqrt(-1)) # Output -> 0
19
+print(mySqrt(0)) # Output -> 0
20
+print(mySqrt(999)) #Output -> 31
0 commit comments