Interview Question
Scholar@SAP Interview
-Pune
SAPWrite a program to add numbers present in a String , example : Gl123a45ssd908oo6r, so the program should give 123 + 45 + 908 + 6 = 1082 as the result. There were 2 more programming questions related to stacks and linkedlists and one question on probability followed by a logical puzzle question.
Interview Answers
4 Answers
import re result = re.sub("[A-z]"," ",input) res = result.split(" ") sum=0 for i in res: if not i == "": sum+=int(i) input = input()
Cheerag on
str=input() temp=0 sum=0 for i in str: if(i.isdigit()): temp=(temp*10)+int(i) else: sum=sum+temp temp=0 print(sum)
Anonymous on
import re def sum_of_digits(s): p=re.compile('[0-9]') sum=0 for i in re.findall(p,s): sum+=int(i) return sum s="Gl123a45ssd908oo6r" print(sum_of_digits(s))
Prasun Kumar Parate on
def findSum(str1): temp = "0" Sum = 0 for ch in str1: if (ch.isdigit()): temp += ch else: Sum += int(temp) temp = "0" return Sum + int(temp) str1 = "Gl123a45ssd908oo6r" print(findSum(str1))
Darshan Gowda C D on