Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Python
#read the list of words from the file
file1=open("C:/Users/user/Downloads/EnglishWords4Letters.txt",'r')
#store the list of words in a python list
words_list=list(file1.read().split('\n'))
#take phone number as input from user
number=input('Enter an 8-digit number\n')
#store the keypad letters in key-letters format
letters={'2':'ABC','3':'DEF','4':'GHI','5':'JKL','6':'MNO','7':'PQRS','8':'TUV','9':'WXYZ'}
#extract the last four digits of phone number for translation
last_four=number[-4:]
#temporary list to store possible translations for given number
#list i initially a copy of original words_list provided
prev_list=words_list[:-1]
k=0
for i in last_four:
    new_list=[]
    #if number can no more get translated
    if i not in letters.keys():
        print('Translation failed')
        break
    #check for the possible combinations stepwise for each number in sequence
    for j in range(len(letters[i])):
        temp=letters[i][j].lower()
        for word in prev_list:
            if word[k]==temp:
                new_list.append(word)
    k+=1
    prev_list=new_list[:]
    #if no more translations can be obtained
    if not prev_list:
        print('Translation failed')
        break
else:
    #if there are possible translations print them all
    for i in prev_list:
        print(number[:-4]+i.upper())


What I have tried:

I have tried reading it and converting it but I have no experience with python, I only know C, please can someone help and convert it for me.
Posted
Updated 24-Feb-23 4:15am
v3

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

And frankly? If you blindly convert from any language into C and hand it is as your own work then two things will happen:
1) You will learn nothing about how to write C code, and will real problems when you either reach a task you can't find (and it'll be more complicated that this task), or reach your exams and can't copy from anywhere ...
2) It'll be blindingly obvious to your tutor what you did (and probably the language you found it in) and you could get an automatic course fail for plagiarism.

I'd think long and hard about your whole homework solving method, if I was you ...
 
Share this answer
 
Comments
CPallini 7-Apr-22 2:03am    
5.
The main logic is quite easy to figure out even without knowing Python. The missing bit about String slicing is explained at 3. An Informal Introduction to Python — 3.1.2. Strings[^] and the following section on Lists.
 
Share this answer
 
Comments
CPallini 7-Apr-22 2:03am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900