Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/my_project/interviews/top_150_questions_round_11/sqrtx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def mySqrt(self, x: int) -> int:

left, right = 0, x

while left <= right:

mid = (left + right)//2

if mid ** 2 < x:
left = mid + 1
elif mid ** 2 > x:
right = mid - 1
else:
return mid

return min(left, right)
17 changes: 17 additions & 0 deletions tests/test_150_questions_round_11/test_sqrtx_round_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from src.my_project.interviews.top_150_questions_round_11\
.sqrtx import Solution

class SqrtxTestCase(unittest.TestCase):

def test_even_sqrtx(self):
solution = Solution()
output = solution.mySqrt(x=4)
target = 2
self.assertEqual(output, target)

def test_odd_sqrtx(self):
solution = Solution()
output = solution.mySqrt(x=8)
target = 2
self.assertEqual(output, target)
Loading