How can i replace words to alphabet in input ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i replace words to alphabet in input ?

https://code.sololearn.com/cKDHbTMY754F/?ref=app Like a = 1 , b = 2 , c = 3 and more.. For example i input abc, And i want to get 6 in output. How it’s gonna work ? I want to learn ,pls🐌

3rd Jul 2022, 12:32 PM
Kimia
Kimia - avatar
29 Answers
+ 9
You'd most likely have to have a dictionary with those values, ex: data = {'a': 1, 'b': 2, ...} Then you split string, access data dict with each letter and add the key's value to total.
3rd Jul 2022, 12:42 PM
Slick
Slick - avatar
+ 4
You can try use of ascii values there.. 'a' ascii value is 97 'z' ascii value is 97+26 ord() function returns ascii value. Ex: ord('a') returns 97 , 'a' ascii value.. ord('a') - 97+1 returns 1 ord('z') - 97 +1 returns 26 By using loop, you can find sum.. With @slick solution of using dictionary and mapping dict values is like: With having dictionary, replace 'a' with dict('a') so 'abcd' becomes '1234'. hope it helps...
3rd Jul 2022, 1:08 PM
Jayakrishna 🇮🇳
+ 3
Kimia If input is 'a3b8hk' then what is your expected output? briefly how?
3rd Jul 2022, 1:17 PM
Jayakrishna 🇮🇳
+ 2
What questions do you have about my answer? It gives just enough away to point you in the right direction without just giving away the answer.
3rd Jul 2022, 12:49 PM
Slick
Slick - avatar
+ 1
Okay, do you know about dictionaries and how to make and access them? Do you know about the <string>.split() function? Do you know about loops? We need to know where you're at to help guide you to where you're going.
3rd Jul 2022, 1:05 PM
Slick
Slick - avatar
+ 1
Take the big problem and break it into smaller problems. You don't need to code it all in one line. First, do it on a piece of paper than take the steps you used and translate them to code. Give it a shot and post the attempt
3rd Jul 2022, 1:19 PM
Slick
Slick - avatar
+ 1
Kimia As I said it before, Ex: ord('h') -97+1 returns 11 You can replace it with h You can repeat this within a loop. If you want to use dictionary, then learn about dictionary. It will like this ex: dic = { 'a' : 1, 'b':2, .... 'z':26 }; replace( 'a', dic('a') ) will replace 'a' with 1 (*sample way only, not exact syntax). Before replacing, check : if (not 'a'.isdigit() ) so that only alphabet gets replace...
3rd Jul 2022, 1:38 PM
Jayakrishna 🇮🇳
+ 1
This is an example in Python (everything is very detailed explained): # this links each letter of the alphabet to its corresponding numbers: a = 1, b = 2, etc... alphabet={"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,"p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26} # gets the input and assigns it to inp inp=input() # assigns the varable output to an empty string output="" # creates a loop through the input and creates the variable n (for example: if the input is "hi", the first time n equals "h" and the second time n equals "i".) for n in inp: # adds the number (see where the variable alphabet is defines) that is linked to n to the output output+=alphabet[n] # prints the output print(output)
3rd Jul 2022, 3:04 PM
Jesper Van Bommel
+ 1
dict(enumerate("abc",1)) could make the dict easier to create
5th Jul 2022, 5:45 AM
Oma Falk
Oma Falk - avatar
5th Jul 2022, 5:55 AM
Oma Falk
Oma Falk - avatar
0
Slick Can you explain more ?🐁
3rd Jul 2022, 12:47 PM
Kimia
Kimia - avatar
0
Slick I mean i don’t have idea how is working, like how can i call data in input for convert to num for sum🐌
3rd Jul 2022, 1:02 PM
Kimia
Kimia - avatar
0
Slick i know <string>.split() function but i don’t know about access dic🌚
3rd Jul 2022, 1:08 PM
Kimia
Kimia - avatar
0
Jayakrishna🇮🇳 I don’t want ascii value i just want replace alphabet words to num, and it’s working everywhere like : a3b8hk
3rd Jul 2022, 1:11 PM
Kimia
Kimia - avatar
0
In the case you just mentined, you'd have to then just use list() on a string to turn it into a list of characters. Then you'd have to use <character>.isdigit() to check if the string value is a number. If it's not a number, use the character to access the data dictionary and replace the index with the cooresponding value. Then at the end, sum up all the numbers by changing everything in the list to an int() and add them all up https://code.sololearn.com/cuyOWt8VihF8/?ref=app
3rd Jul 2022, 1:15 PM
Slick
Slick - avatar
0
Slick i just confused i think it will make me crazy🐢
3rd Jul 2022, 1:17 PM
Kimia
Kimia - avatar
0
Jayakrishna🇮🇳 input = (a3b8hk) It’s 1328811 output = 6
3rd Jul 2022, 1:20 PM
Kimia
Kimia - avatar
0
Ness Shon wow, it was my wish Can you explain code in conversation ?
4th Jul 2022, 5:38 AM
Kimia
Kimia - avatar