Number Search

 Coding Problem Keys 

 Number Search 

 Problem Statement 

The function NumberSearch(str) takes the string as parameter, search for all the numbers in the string, add them together, then return that final number divided by the total amount of letters in the string.

For example: if string is Hello6 9World 2, Nic8e D7ay! the output should be 2.

 Explanation 

First if you add up all the numbers, 6 + 9 + 2 + 8 + 7 you get 32. The there are 17 letters in the string. So 32 / 17 = 1.882, and the final answer should be rounded to the nearest whole number, so the answer is 2.

Only single digit numbers separated by spaces will be used throughout the whole string (So this won't ever be the case: hello2222 world).

Each string will also have at least one letter.

 Sample Input/Output 1 

 Input 

H3ello9-9

 Output 

4

 Sample Input/Output 2 

 Input 

One Number*1*

 Output 

0

 Solution 

 Programming Language: Python 3 Language 

def NumberSearch(s):
    arr=list(s.strip())
    s=0;c=0
    for i in arr:
        if(i.isdigit()):
            s+=int(i)
        elif(i==" "):
            pass
        else:
            c+=1
    print(round(s/c))
s=input()
NumberSearch(s)

# Published By PKJCODERS

 Programming Language: JAVA Language 

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String string = sc.nextLine();
        int count = 0, sum = 0,result;
        for(int i = 0; i < string.length(); i++){
            if(Character.isDigit(string.charAt(i))){
                sum=sum+Character.getNumericValue(string.charAt(i));
            }
            else if(Character.isLetter(string.charAt(i))){
                count++;
            }
            else if(sum==1){
                System.out.println("0");
                System.exit(0);
            }
        }
        result=sum/count;
        System.out.println(result);
    }
}

// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Reversed Sum of Pairs

Property Inheritance in Family

Sort Rows by Prime, Even and Odd Count

Reverse Characters in a string

Integer - Alphabet Pairs to Matrix

Binary - Sum ASCII Values