1

I'm new to Python but here is the question

Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters.

Input

The input will be a single line containing a string.

Output

The output should contain the sum and average of the numbers that appear in the string. Note: Round the average value to two decimal places.

Explanation

For example, if the given string is "I am 25 years and 10 months old", the numbers are 25, 10. Your code should print the sum of the numbers(35) and the average of the numbers(17.5) in the new line.

Test Cases

Input

I am 25 years and 10 months old

Output

35 17.5

The above test case is pretty straightforward and simple,my problem comes when you trying to solve something like this.

Input

A girl64 35spaces numb6rs

Output

205 66.66

So basically what I am asking is, how do you extract the digits thats existing between a string of characters. To make it so that (164) does not become (1,6,4)

sam2611
  • 455
  • 5
  • 15
pran
  • 11
  • 1
  • 2
  • 3
    If you worked on this for two days you should have some code to show, please post it and explain what is the issue with it. – Guy Jul 14 '21 at 06:21
  • You need to convert the sentence into a list of string https://stackoverflow.com/questions/6181763/converting-a-string-to-a-list-of-words and then you need to check for each element of the list to be digit using isdigit( ). Then covert the obtained number into integer using the int( ) function and do the average and the sum – sam2611 Jul 14 '21 at 06:24
  • 1
    Yes and plz show your code that you tried for two days. – sam2611 Jul 14 '21 at 06:25
  • 1
    your question is poorly written you need to read this https://stackoverflow.com/help/how-to-ask – Manik Jul 14 '21 at 06:27
  • 1
    By the way, how does 64+35+6 adds up to 205? – Guy Jul 14 '21 at 06:29

4 Answers4

2
import re
input_str = "I am 25 years and 10 months old"
num_str = re.findall('[0-9]+',input_str)
if len (num_str) > 0:
    list_str = [int(n) for n in num_str]
    print (sum(list_str),round(sum(list_str)/len(list_str),2))
else:
    print (0,0)
Prasanna P
  • 64
  • 1
  • 6
0

This is what you need to perform.

string = 'I am 25 years and 10 months old'
lst=list(string.split())
lst1=[]
for i in lst :
  if(i.isdigit()):
      i=int(i)
      lst1.append(i)
if(len(lst1)!=0):
  avg=sum(lst1)/len(lst1)
else:
  avg=0

print(sum(lst1))
print(avg)
sam2611
  • 455
  • 5
  • 15
0

Here you can try this:

a='Hello123, I am Hello World234'
a=a.split()
t=[int(''.join([j for j in k if j.isnumeric()])) for k in a if any(m.isnumeric() for m in k)]
if len(t)!=0:
    print(sum(t),sum(t)/len(t))
else:
    print(sum(t),sum(t))

Output:

357 178.5
0

Convert str in a list using the map() function and then check if it is a digit. If it is then append it in a stack and count average and sum using stack.

total=0
avrage=0
stack=[]
line=list(map(str,input().split()))
    
for i in line:
    if i.isdigit():
        i=int(i)
        stack.append(i)
    
avrage=sum(stack) / len(stack)
total=sum(stack)
print(avrage)
print(total)
Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30