Cristy07 Posted March 14, 2016 Report Share Posted March 14, 2016 (edited) Buna, am inceput sa invat sa programez in python si mio fost dat un exerciciu de un coleg ce face informatica la facultate. Exerciciu este: trebuie sa simulez o tastatura la un telefon antic cu numere. Deci, daca apas de 2 ori pe tecla 2 imi apare litera B si tot asa... Avand o string "222 2333 332" obtin cuvantul "CAFEA". spatiul reprezinta timpul care astept pana sa pot apasa pe aceasi numar ca sa imi apara alta litera.. am inceput cu o functie, si am salvat literele corespunzatoare la fiecare cifra def tastatura(mensaj): litere = {'2':'A', '22':'B', '222':'C', '3':'D', '33':'E', '333':'F', '4':'G', '44':'H', '444':'I', '5':'J', '55':'K', '555':'L', '6':'M', '66':'N', '666':'O', '7':'P', '77':'Q', '777':'R', '7777':'S', '8':'T', '88':'U', '888':'V', '9':'W', '99':'X', '999':'Y', '9999':'Z'} dar acuma nu stiu de unde sa incep si cum sa fac... Poate cineva sa ma ajute? Edited March 14, 2016 by Cristy07 Quote Link to comment Share on other sites More sharing options...
pulamea5 Posted March 15, 2016 Report Share Posted March 15, 2016 (edited) import sys def tastatura(mesaj): litere = {'2':'A', '22':'B', '222':'C', '3':'D', '33':'E', '333':'F', '4':'G', '44':'H', '444':'I', '5':'J', '55':'K', '555':'L', '6':'M', '66':'N', '666':'O', '7':'P', '77':'Q', '777':'R', '7777':'S', '8':'T', '88':'U', '888':'V', '9':'W', '99':'X', '999':'Y', '9999':'Z'} text = sys.argv[1] for plm in text: for numbers in litere: if plm in litere[numbers]: result = numbers + " " + litere[numbers] print result tastatura(1) Gata, e un bun start de pornire, folosire: python fisier.py CAFEA sau orice alt cuvant vrei tu. Edited March 15, 2016 by pulamea5 Quote Link to comment Share on other sites More sharing options...
Cristy07 Posted March 15, 2016 Author Report Share Posted March 15, 2016 Dar eu vreau rezultatul invers... >>> tastatura( "222 2333 332") "CAFEA" cum faceai tu era mai simplu... asa este mai greu Quote Link to comment Share on other sites More sharing options...
Active Members dancezar Posted March 15, 2016 Active Members Report Share Posted March 15, 2016 litere = {'2':'A', '22':'B', '222':'C', '3':'D', '33':'E', '333':'F', '4':'G', '44':'H', '444':'I', '5':'J', '55':'K', '555':'L', '6':'M', '66':'N', '666':'O', '7':'P', '77':'Q', '777':'R', '7777':'S', '8':'T', '88':'U', '888':'V', '9':'W', '99':'X', '999':'Y', '9999':'Z'} def tastatura(string): spl=string.split(" ") result="" for s in spl: try: result+=litere[s] except KeyError: result+=continua(s) return result def continua(string): last="" res="" for s in string: try: last+=s litere[last] except KeyError: res+=litere[last[:-1]] last=s try: return res+litere[last] except KeyError: return res print tastatura('75556 222 2333 332') print tastatura('755562222333332') Quote Link to comment Share on other sites More sharing options...
MasterLight Posted May 11, 2016 Report Share Posted May 11, 2016 (edited) . Edited July 5, 2016 by MasterLight Quote Link to comment Share on other sites More sharing options...