Created
June 3, 2019 05:04
-
-
Save kylelong/3756dfd061f0917e8699d13afc441880 to your computer and use it in GitHub Desktop.
Cassido's interview question for week of 5/26/19 - Implement integer division
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Integer dvision for a / b | |
@param a first number | |
@param b second number | |
@return a / b | |
=end | |
def divide(a, b) | |
sign = (a < 0) ^ (b < 0) ? -1 : 1 | |
a = a.abs | |
b = b.abs | |
count = 0 | |
while a >= b | |
a -= b | |
count += 1 | |
end | |
return count * sign | |
end | |
p divide(-15, -15) | |
p divide(5, 10) | |
p divide(10, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment