Interview Question

Scholar@SAP Interview

-Pune

SAP

Write 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.

AnswerAdd Tags

Interview Answers

4 Answers

1

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

0

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

0

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

1

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

Add Answers or Comments

To comment on this, Sign In or Sign Up.