+ 1

Adding numbers in string without Type Casting.

Interviewer ask me to add 2 numbers present in 2 different strings without Type Casting. Example:- a='25' b='35' Output should be= '60'

22nd Jul 2022, 11:02 PM
Shubham Jain
Shubham Jain - avatar
23 odpowiedzi
+ 6
There is such a function eval() print(eval(“25+35”))
23rd Jul 2022, 7:08 AM
CodeStory
CodeStory - avatar
+ 6
Shubham Jain , it is possible to solve this task without converting the string values to int. we can use eval() as mentioned, but we need to build a string that looks like "25 + 35". this string is then used as argument for eval(...). to create this string we can use concatenation or f- string
23rd Jul 2022, 4:36 PM
Lothar
Lothar - avatar
+ 3
Consider whether the exec() function might do the job,
22nd Jul 2022, 11:11 PM
Brian
Brian - avatar
+ 3
The eval solution worked excellent. like this -> print(eval(f"{a}+{b}")) I thought of more naive solution: def charVal(ch): return "0123456789".index(ch) def stringVal(str): pow = 0 index = len(str) - 1 val = 0 while index >= 0: val += charVal(str[index]) * 10**pow index -= 1 pow += 1 return val a = "25" b = "35" print(stringVal(a) + stringVal(b)) Basicly, you construct the value of the string yourself by multiplying powers of 10 in their respective places in the string. Of course, each char has it value from its index in the string "0123456789". Longer way. Same output.
24th Jul 2022, 3:31 PM
avishay
+ 3
avishay It's a nice example, but your output is an integer value and not a string.
24th Jul 2022, 6:46 PM
Jan
Jan - avatar
+ 2
I managed to make it work but didn't save it. Afterward I noticed the sample output shows quotation marks around the answer. Are those also required?
22nd Jul 2022, 11:55 PM
Brian
Brian - avatar
+ 2
Dabbara Vamsi That's type casting
24th Jul 2022, 12:08 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
Add two separate strings of numbers like they were integer values and get an output as a string without any type casting. That's the challenge here as I understand it.
24th Jul 2022, 7:25 PM
Jan
Jan - avatar
+ 2
25th Jul 2022, 2:36 PM
Jan
Jan - avatar
+ 1
Please share what you tried. Perhaps I can suggest how to improve it.
23rd Jul 2022, 12:07 AM
Brian
Brian - avatar
+ 1
Please check the code in Python IDE
23rd Jul 2022, 9:28 AM
CodeStory
CodeStory - avatar
+ 1
Warning: using eval() wil just concatenate the two strings together. Using f-strings also does the same thing as above mentioned. We all know that data types are fundamental and are in-built. This fact eliminates the need to try and 'find' some magic bullet (haven't tried operator overloading yet, but I know you'll have to mention a type cast irregardless). This makes me want to conclude that the question might be designed to let the interviewer see your problem solving capabilities. Or maybe there is some way....Idk
24th Jul 2022, 11:56 AM
Kevin Ngigi
Kevin Ngigi - avatar
+ 1
Is creating and using a dictionary allowed? Dictionary containing the mapping of string to numbers. For ex- like this: num = {"1":1, "2":2, ... } If yes, then it is possible to solve the task
24th Jul 2022, 1:23 PM
Sandeep
Sandeep - avatar
+ 1
Quantum perhaps another function will help with the second convertion...: def convertIntToString(num): digits = "0123456789" numAsStr = "" while num > 0: numAsStr = digits[num % 10] + numAsStr num /= 10 num = round(num - 0.5) #this ^ acts like floor, but returns int instead of #floating point type if numAsStr == "": numAsStr = "0" return numAsStr
24th Jul 2022, 8:49 PM
avishay
+ 1
avishay It works quite well now, and the output is also a string.
24th Jul 2022, 9:02 PM
Jan
Jan - avatar
+ 1
Shubham Jain No, it doesn't. If you implenent the last function to the first example, then you get a string as output. If you use type on the output, then you will see that it's a string.
25th Jul 2022, 6:37 AM
Jan
Jan - avatar
0
Hi Brian. It's not working. Could you please share if it is working on your side? Thanks in advance
22nd Jul 2022, 11:51 PM
Shubham Jain
Shubham Jain - avatar
0
Yes that would work
22nd Jul 2022, 11:57 PM
Shubham Jain
Shubham Jain - avatar
0
Is that even posible? Without type conversion? Well maybe the interviewer needed you to say “its not possible”
23rd Jul 2022, 8:49 AM
olanrewaju lawson
olanrewaju lawson - avatar
0
Quantum and avishay , it's still showing interger value
24th Jul 2022, 11:11 PM
Shubham Jain
Shubham Jain - avatar