Python :: Aufgabe #20
4 Lösungen

Zahlen in Römischer Schreibweise
Anfänger - Python
von pocki
- 29.12.2012 um 19:40 Uhr
Schreibe eine Programm welches eine Ganzzahl einliest und diese anschließend in römischer Schreibweise wieder ausgibt und umgekehrt bei einer eingegebenen Zahl in römischer Schreibweise diese als normale Zahl wieder ausgibt:
Die Erkennung der Schreibweise sollte automatisch funktionieren.
Konsolenausgabe:
Geben Sie eine Zahl ein: 1234
entspricht: MCCXXXIV
Geben Sie eine Zahl ein: DXXXVII
entspricht: 537
Die Erkennung der Schreibweise sollte automatisch funktionieren.
Lösungen:

def zahlinrom(zahl): rom = '' if zahl // 10000 >= 1: anzahl = zahl // 10000 zahl = zahl % 10000 for i in range(1,anzahl+1): rom += 'ↂ' if zahl // 5000 >= 1: anzahl = zahl // 5000 zahl = zahl % 5000 for i in range(1,anzahl+1): rom+= 'ↁ' if zahl // 1000 >= 1: anzahl = zahl // 1000 zahl = zahl % 1000 for i in range(1,anzahl+1): rom+= 'M' if zahl // 500 >= 1: anzahl = zahl // 500 zahl = zahl % 500 for i in range(1,anzahl+1): rom+= 'D' if zahl // 100 >= 1: anzahl = zahl // 100 zahl = zahl % 100 for i in range(1,anzahl+1): rom+= 'C' if zahl // 50 >= 1: anzahl = zahl // 50 zahl = zahl % 50 for i in range(1,anzahl+1): rom+= 'L' if zahl // 10 >= 1: anzahl = zahl // 10 zahl = zahl % 10 for i in range(1,anzahl+1): rom+= 'X' if zahl // 5 >= 1: anzahl = zahl // 5 zahl = zahl % 5 for i in range(1,anzahl+1): rom+= 'V' if zahl // 1 >= 1: anzahl = zahl // 1 zahl = zahl % 1 for i in range(1,anzahl+1): rom+= 'I' return rom def rominzahl(rom): rom = str(rom) zahl = 0 for i in rom: if i == 'ↂ': zahl += 10000 elif i == 'ↁ': zahl+= 5000 elif i == 'M': zahl+= 1000 elif i =='D': zahl+= 500 elif i =='C': zahl+= 100 elif i =='L': zahl+= 50 elif i =='X': zahl+= 10 elif i =='V': zahl+= 5 elif i == 'I': zahl += 1 return zahl

#!/usr/bin/env python3 def history(): while True: values = {'I': 1,'V': 5,'X': 10,'L': 50,'C': 100,'D': 500,'M': 1000} string = input("Geben Sie eine Zahl ein: ") stringLen = len(string) counter = 0 for i in range(0,stringLen): if string[i] in values: counter += 1 if counter == stringLen: decimal = 0 previous_value = 0 for i in string: if i in values: if values[i] > previous_value: decimal -= previous_value else: decimal += previous_value previous_value = values[i] decimal += previous_value print("Ergebnis: " + str(decimal)) else: roman = "" previous_value = "" string = int(string) while string > 0: if string >= 1000: string -= 1000 roman += "M" if string < 1000 and string >= 500: string -= 500 roman += "D" if string < 500 and string >= 100: string -= 100 roman += "C" if string < 100 and string >= 50: string -= 50 roman += "L" if string < 50 and string >= 10: string -= 10 roman += "X" if string < 10 and string >= 5: string -= 5 roman += "V" if string < 5 and string >= 1: string -= 1 roman += "I" print("Ergebnis: " + str(roman)) def main(): history() if __name__ == "__main__": main()
Sollte so stimmen?
LG, Ah3

# -*- coding: utf-8 -*- zahlen = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] zahlzeichen = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", \ "IV", "I"] roemisch = "" arabisch = 0 i = 0 zahl = raw_input("Umzurechnende Zahl (arabisch oder roemisch): ") # arabisch in roemisch try: int(zahl) / zahlen[-1] for i in zahlen: while int(zahl) >= i: roemisch = roemisch + zahlzeichen[zahlen.index(i)] zahl = int(zahl) - i print "\nDie roemische Zahl zu deiner Eingabe lautet: " + roemisch # roemisch in arabisch except: zahl = zahl.upper() zahl = zahl + "A" while i < len(zahl) - 1: if zahl[i] + zahl[i+1] in zahlzeichen: zahlzeichen_index = zahlzeichen.index(zahl[i] + zahl[i+1]) arabisch = arabisch + zahlen[zahlzeichen_index] i = i + 1 elif zahl[i] in zahlzeichen: zahlzeichen_index = zahlzeichen.index(zahl[i]) arabisch = arabisch + zahlen[zahlzeichen_index] i = i + 1 print "\nDie arabische Zahl zu deiner Eingabe lautet: " + str(arabisch)

def ganzzahl_roemisch(zahl_eingabe): """ Erstellt Liste mit den römischen Zahlen :param zahl_eingabe: int :return: list[römische Zahlen] """ # Überprüfung Sonderfälle roemzahl = [] while zahl_eingabe > 0: if zahl_eingabe == 900: roemzahl.append("CM") zahl_eingabe = 0 elif zahl_eingabe == 400: roemzahl.append("CD") zahl_eingabe = 0 elif zahl_eingabe == 90: roemzahl.append("XC") zahl_eingabe = 0 elif zahl_eingabe == 40: roemzahl.append("XL") zahl_eingabe = 0 elif zahl_eingabe == 10: roemzahl.append("IX") zahl_eingabe = 0 elif zahl_eingabe == 4: roemzahl.append("IV") zahl_eingabe = 0 # Überprüfung und erstellen der römischen Zahlen if zahl_eingabe >= 1000: ganz_zahl = zahl_eingabe // 1000 for i in range(ganz_zahl): roemzahl.append("M") zahl_eingabe = zahl_eingabe - (ganz_zahl * 1000) elif zahl_eingabe >= 500: ganz_zahl = zahl_eingabe // 500 for i in range(ganz_zahl): roemzahl.append("D") zahl_eingabe = zahl_eingabe - (ganz_zahl * 500) elif zahl_eingabe >= 100: ganz_zahl = zahl_eingabe // 100 for i in range(ganz_zahl): roemzahl.append("C") zahl_eingabe = zahl_eingabe - (ganz_zahl * 100) elif zahl_eingabe >= 50: ganz_zahl = zahl_eingabe // 50 for i in range(ganz_zahl): roemzahl.append("L") zahl_eingabe = zahl_eingabe - (ganz_zahl * 50) elif zahl_eingabe >= 10: ganz_zahl = zahl_eingabe // 10 for i in range(ganz_zahl): roemzahl.append("X") zahl_eingabe = zahl_eingabe - (ganz_zahl * 10) elif zahl_eingabe >= 5: ganz_zahl = zahl_eingabe // 5 for i in range(ganz_zahl): roemzahl.append("V") zahl_eingabe = zahl_eingabe - (ganz_zahl * 5) elif zahl_eingabe >= 1: ganz_zahl = zahl_eingabe // 1 for i in range(ganz_zahl): roemzahl.append("I") zahl_eingabe = zahl_eingabe - (ganz_zahl * 1) return roemzahl def roemisch_ganzzahl(roemisch_eingabe): """ Wandelt die römische Zahl in eine Ganzzahl :param roemisch_eingabe: int :return: int = Ganzzahl """ roemisch = list(roemisch_eingabe) # Wechselt Buchstaben in der Liste gegen Zahlen aus for stelle, zeichen in enumerate(roemisch): if zeichen == "M": roemisch[stelle] = 1000 elif zeichen == "D": roemisch[stelle] = 500 elif zeichen == "C": roemisch[stelle] = 100 elif zeichen == "L": roemisch[stelle] = 50 elif zeichen == "X": roemisch[stelle] = 10 elif zeichen == "V": roemisch[stelle] = 5 elif zeichen == "I": roemisch[stelle] = 1 zahl = 0 # Errechnet die Ganzzahl anhand der Liste while True: if roemisch[-1] > roemisch[-2]: zahl = roemisch[-1] - roemisch[-2] for i in range(len(roemisch) - 2, 0, -1): zahl += roemisch[i - 1] break else: for i in range(len(roemisch), 0, -1): zahl += roemisch[i - 1] break return zahl def main(): print("\nDieses Program wandelt eine Ganzzahl in eine Römische-Zahl um und anders herum.\n") eingabe = input("Geben Sie eine Zahl ein: ") # Überprüfung ob ein römische oder Ganzzahl eingegeben wurde # Zusätzliche Überprüfung der richtigkeit der Eingabe if eingabe.isalpha(): eingabe = eingabe.upper() if "I" in eingabe or \ "V" in eingabe or \ "X" in eingabe or \ "L" in eingabe or \ "C" in eingabe or \ "D" in eingabe or \ "M" in eingabe: ganzzahl = roemisch_ganzzahl(eingabe) print(f"Ganzzahl: {ganzzahl}") else: print("Bitte nur Buchstaben der römischen Zahlen eingeben (I, V, X, L, C, D, M)!!!") elif eingabe.isdigit(): eingabe = int(eingabe) roemische_zahl = ganzzahl_roemisch(eingabe) print("Römische Zahl:", "".join(roemische_zahl)) else: print("Bitte Eingabe Überprüfen!!!") if __name__ == '__main__': main()