user18585713
user18585713

Reputation:

Given a phone number in words, convert the number to digits

The below code work's fine for

Input: "two one nine six eight one six four six zero"

Output: "2196816460"
n=s.split()
    l=[]
    for val in n:
        if(val=='zero'):
            l.append('0')
        elif(val=='one'):
            l.append("1")
        elif(val=='two'):
            l.append("2")
        elif(val=='three'):
            l.append("3")
        elif(val=='four'):
            l.append("4")
        elif(val=="five"):
            l.append("5")
        elif(val=="six"):
            l.append("6")
        elif(val=="seven"):
            l.append("7")
        elif(val=="eight"):
            l.append("8")
        elif(val=="nine"):
            l.append("9")
    new = ''.join(l)
    return new

But what if,

Input: "five eight double two double two four eight five six"
Required Output should be : 5822224856
or,
Input: "five one zero six triple eight nine six four"
Required Output should be : 5106888964 How can the above code be modified?

Upvotes: 0

Views: 6569

Answers (4)

Pavan D Kurdekar
Pavan D Kurdekar

Reputation: 1

package org.pavan; import java.util.Scanner;

public class PhoneNumber {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine(); 
        convertToNumber(input);
   
}

public static String convertwordtonumber(String number)
{
    
    switch(number)
    {
    case "one":
        return "1";
    
        
    case "two":
        return "2";
    
        
    case "three":
        return "3";
    
        
    case "four":
        return "4";
    
        
    case "five":
        return "5";
    
        
    case "six":
        return "6";
    
        
    case "seven":
        return "7";
    
        
    case "eight":
        return "8";
    
        
    case "nine":
        return "9";
    
        
    case "zero":
        return "0";
            
    default:
        return "Invalid String";
    }
    
}
  
public static void convertToNumber(String input) {

    StringBuilder result = new StringBuilder();
    String[] words = input.split(" ");
    
    
    for(int i=0;i<=words.length-1;i++)
    {
        
        if(words[i].equals("double"))
        {
            String num = convertwordtonumber(words[i+1]);
            result.append(num);
            result.append(num);
            i++;
            
        }
        
        else if(words[i].equals("triple")) {
            
            String num1 = convertwordtonumber(words[i+1]);
            result.append(num1);
            result.append(num1);
            result.append(num1);
            i++;
        }
        else if(words[i]!="triple" && words[i]!="double"){
            
            String num2 = convertwordtonumber(words[i]);
            result.append(num2);
            
        }
        
        
    }
    System.out.println(result);
 
         }
  
}

Upvotes: 0

Faheemuddin Naseem
Faheemuddin Naseem

Reputation: 1

#include <iostream>
#include <string>
#include <sstream>
#include <unordered_map>

using namespace std;

unordered_map<string, string> wordToDigit = {
    {"zero", "0"}, {"one", "1"}, {"two", "2"}, {"three", "3"}, {"four", "4"},
    {"five", "5"}, {"six", "6"}, {"seven", "7"}, {"eight", "8"}, {"nine", "9"}
};

string convertPhoneNumberToDigits(const string& phoneNumberInWords) {
    istringstream iss(phoneNumberInWords);
    string word, previousWord;
    string result = "";

    while (iss >> word) {
        if (word == "double" || word == "triple") {
            previousWord = word;
        } else {
            if (previousWord == "double") {
                for (int i = 0; i < 2; i++) {
                    result += wordToDigit[word];
                }
                previousWord = "";
            } else if (previousWord == "triple") {
                for (int i = 0; i < 3; i++) {
                    result += wordToDigit[word];
                }
                previousWord = "";
            } else {
                result += wordToDigit[word];
            }
        }
    }

    return result;
}

int main() {
    string input= "five one zero two four eight zero double three two";
    // cout << "Enter the phone number in words: ";
    // getline(cin, input);

    string result = convertPhoneNumberToDigits(input);
    cout << "Phone number in digits: " << result << endl;

    return 0;
}

Upvotes: 0

nadapez
nadapez

Reputation: 2727

You could use a variable to hold the repetitions of the next number.

The variable starts with value 1.

When get a repetition word ('triple') , then update the variable correspondingly.

When get a number ('three'), add to the list and update the variable back to 1

Also instead of using else..if you can use a dictionary:

repeat = 1
repeat_dict = {'double':2, 'triple':3}:

n=s.split()
l=[]
for val in n:
    if val in repeat_dict:   # got a repeat word
        repeat = repeat_dict[val]
    else:
          
        if(val=='zero'):
            l.append('0' * repeat) 
        elif(val=='one'):
            l.append("1" * repeat)
        elif(val=='two'):
            l.append("2" * repeat)
        elif(val=='three'):
            l.append("3" * repeat)
        elif(val=='four'):
            l.append("4" * repeat)
        elif(val=="five"):
            l.append("5" * repeat)
        elif(val=="six"):
            l.append("6" * repeat)
        elif(val=="seven"):
            l.append("7" * repeat)
        elif(val=="eight"):
            l.append("8" * repeat)
        elif(val=="nine"):
            l.append("9" * repeat)
        repeat = 1
new = ''.join(l)
return new

Upvotes: 0

Samwise
Samwise

Reputation: 71517

Create another mapping for the quantity modifiers, and multiply the digit string by the preceding modifier.

digits = {
    "zero": "0",
    "one": "1",
    "two": "2",
    "three": "3",
    "four": "4",
    "five": "5",
    "six": "6",
    "seven": "7",
    "eight": "8",
    "nine": "9",
}

modifiers = {
    "double": 2,
    "triple": 3,
}

number = ""
n = 1
for word in input("? ").split():
    if word in modifiers:
        n *= modifiers[word]
    else:
        number += n * digits[word]
        n = 1
print(number)
? five eight double two double two four eight five six
5822224856

Upvotes: 4

Related Questions