Jump to content
Cristy07

Program in python

Recommended Posts

Posted (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 by Cristy07
Posted (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 by pulamea5
  • Active Members
Posted



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')

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...