aboutsummaryrefslogtreecommitdiff
path: root/challenge-227/robert-dicicco/python/ch-2.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-07-31 15:49:22 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-07-31 15:49:22 +0800
commite7b6313261ef4541d4dcc303c46ef0d886649b70 (patch)
tree8cff819a2036dd8d0172b6343b0a199d7b81721a /challenge-227/robert-dicicco/python/ch-2.py
parent4fda4a4a398e64921020704733556a2ec6dae78a (diff)
parente511966ce2280dbedb2c916d9e6254708800639e (diff)
downloadperlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.tar.gz
perlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.tar.bz2
perlweeklychallenge-club-e7b6313261ef4541d4dcc303c46ef0d886649b70.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-227/robert-dicicco/python/ch-2.py')
-rw-r--r--challenge-227/robert-dicicco/python/ch-2.py131
1 files changed, 131 insertions, 0 deletions
diff --git a/challenge-227/robert-dicicco/python/ch-2.py b/challenge-227/robert-dicicco/python/ch-2.py
new file mode 100644
index 0000000000..e089dcad97
--- /dev/null
+++ b/challenge-227/robert-dicicco/python/ch-2.py
@@ -0,0 +1,131 @@
+#!/usr/bin/env python
+'''
+I got these Roman-Int, Int-Roman routines from O'Reilly
+Module: Roman Numerals
+Credit: Paul M. Winkler
+
+AUTHOR: Robert DiCicco
+DATE : 2023-07-26
+Challenge 227 Task 2 Roman Maths ( Python )
+'''
+import sys
+
+class py_solution:
+ def int_to_Roman(self, num):
+ val = [
+ 1000, 900, 500, 400,
+ 100, 90, 50, 40,
+ 10, 9, 5, 4,
+ 1
+ ]
+ syb = [
+ "M", "CM", "D", "CD",
+ "C", "XC", "L", "XL",
+ "X", "IX", "V", "IV",
+ "I"
+ ]
+ roman_num = ''
+ i = 0
+ while num > 0:
+ for _ in range(num // val[i]):
+ roman_num += syb[i]
+ num -= val[i]
+ i += 1
+ return roman_num
+
+
+ def roman_to_int(self, input):
+ input = input.upper()
+ roman_numeral_map = (('M', 1000, 3), ('CM', 900, 1),
+ ('D', 500, 1), ('CD', 400, 1),
+ ('C', 100, 3), ('XC', 90, 1),
+ ('L', 50, 1), ('XL', 40, 1),
+ ('X', 10, 3), ('IX', 9, 1),
+ ('V', 5, 1), ('IV', 4, 1), ('I', 1, 3))
+ result, index = 0, 0
+ for numeral, value, maxcount in roman_numeral_map:
+ count = 0
+ while input[index: index+len(numeral)] == numeral:
+ count += 1 # how many of this numeral we have
+ result += value
+ index += len(numeral)
+ return result
+
+def main():
+ n = len(sys.argv)
+ if n != 2:
+ print("Please enter a math problem using Roman numerals")
+ sys.exit(1)
+
+ problem = sys.argv[1]
+ parts = problem.split()
+ left = parts[0]
+ op = parts[1]
+ right = parts[2]
+ lft = py_solution().roman_to_int(left)
+ rt = py_solution().roman_to_int(right)
+
+ if ( op == '-' and lft - rt == 0):
+ print(f"{problem} est nulla")
+ sys.exit(1)
+ elif (op == '/' and (lft % rt) > 0 ):
+ print(f"{problem} non potest")
+ sys.exit(1)
+ elif (op == '+' and lft + rt > 3999):
+ print(f"{problem} non potest");
+ sys.exit(1)
+ elif (op == '-' and (lft - rt < 0)):
+ print(f"{problem} non potest")
+ sys.exit(1)
+ elif (op == '/' and (lft % rt != 0)):
+ print(f"{problem} non potest")
+ sys.exit(1)
+
+ if ( op == '+'):
+ val = py_solution().int_to_Roman(lft + rt)
+ elif ( op == '-'):
+ val = py_solution().int_to_Roman(lft - rt)
+ elif ( op == '/'):
+ val = py_solution().int_to_Roman(int(lft / rt))
+ elif ( op == '*'):
+ val = py_solution().int_to_Roman(int(lft * rt))
+ elif ( op == '**'):
+ val = py_solution().int_to_Roman(int(lft ** rt))
+
+ print(f"{problem} => {val}")
+
+if __name__ == "__main__":
+ main()
+
+'''
+-----------------------------------------------------------------
+SAMPLE OUTPUT
+python .\RomanMath.py "IV + V"
+IV + V => IX
+
+python .\RomanMath.py "M - I"
+M - I => CMXCIX
+
+python .\RomanMath.py "X / II"
+X / II => V
+
+python .\RomanMath.py "XI * VI"
+XI * VI => LXVI
+
+python .\RomanMath.py "VII ** III"
+VII ** III => CCCXLIII
+
+python .\RomanMath.py "V - V"
+V - V est nulla
+
+python .\RomanMath.py "V / II"
+V / II non potest
+
+python .\RomanMath.py "MMM + M"
+MMM + M non potest
+
+python .\RomanMath.py "V - X"
+V - X non potest
+'''
+
+