Skip to content

Commit 2140437

Browse files
authored
Merge pull request kennyledet#539 from ivanchoff/decimalToFraction_python
Decimal to fraction python
2 parents 89d31ce + 081edbc commit 2140437

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Module that cast a decimal number to fraction notation
2+
# Input:
3+
# number: number to cast
4+
# return:
5+
# tuple[0]: is the error, None is returned when the process has no error
6+
# otherwise a string with message of error is returned
7+
# tuple[1]: is number in fraction notation
8+
9+
10+
from fractions import Fraction
11+
12+
def decimalToFraction(number):
13+
if(isinstance(number,(int,float))):
14+
ans = Fraction(number).limit_denominator()
15+
return (None,str(ans))
16+
else:
17+
return ('error','arg must be number')
18+
19+
if __name__ == '__main__':
20+
print(decimalToFraction(3.45))
21+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from decimalToFraction import decimalToFraction
2+
3+
test_input = [2.34, 2.45, "hola", "mundo", 3.90]
4+
test_output_err = [None, None, 'error', 'error', None]
5+
6+
for number, error in zip(test_input,test_output_err):
7+
ans = decimalToFraction(number)
8+
if(ans[0] != error):
9+
print('the code is wrong!!')
10+
else:
11+
print(number,' -> ',ans[1])
12+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# function that check if a string is palindrome (case sensitive)
2+
# input:
3+
# str: word to check
4+
# return:
5+
# true or false
6+
7+
def palindrome?(str)
8+
str == str.reverse
9+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "./palindrome"
2+
3+
begin
4+
# raises an ArgumentError with the message "you messed up!"
5+
if palindrome?("hola") == true
6+
raise ArgumentError.new("The code for palindrome is wrong!")
7+
else
8+
print("hola is no-palindrome\n")
9+
end
10+
11+
if palindrome?("ala") == false
12+
raise ArgumentError.new("The code for palindrome is wrong!")
13+
else
14+
print("ala is palindrome\n")
15+
end
16+
17+
if palindrome?("radar") == false
18+
raise ArgumentError.new("The code for palindrome is wrong!")
19+
else
20+
print("radar is palindrome\n")
21+
end
22+
23+
rescue ArgumentError => e
24+
puts e.message
25+
end

0 commit comments

Comments
 (0)