+ 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'
23 Answers
+ 6
There is such a function eval()
print(eval(“25+35”))
+ 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
+ 3
Consider whether the exec() function might do the job,
+ 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.
+ 3
avishay It's a nice example, but your output is an integer value and not a string.
+ 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?
+ 2
Dabbara Vamsi That's type casting
+ 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.
+ 2
Here is a short example.....
https://code.sololearn.com/c77Bw5KlyGqV/?ref=app
+ 1
Please share what you tried. Perhaps I can suggest how to improve it.
+ 1
Please check the code in Python IDE
+ 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
+ 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
+ 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
+ 1
avishay It works quite well now, and the output is also a string.
+ 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.
0
Hi Brian. It's not working. Could you please share if it is working on your side? Thanks in advance
0
Yes that would work
0
Is that even posible? Without type conversion? Well maybe the interviewer needed you to say “its not possible”