Phone Number Calculations

This section allows users to submit programming challenges. Anybody that loves a good programming challenge this is a the section for you!

Phone Number Calculations

Postby codecaine » Wed Jul 01, 2009 8:16 pm

Given the following information about a US telephone touchtone keypad:

1: (NONE) 2: A,B,C 3: D,E,F
4: G,H,I 5: J,K,L 6: M,N,O
7: P,R,S 8: T,U,V 9: W,X,Y, Z

calculate the product of each characters value.

As an example, say the user enters: "Practice", the product would be:

7 * 7 * 2 * 2 * 8 * 4 * 2 * 3 = 37,632

What is the value of this string: "Programming Challenges are fun"?
User avatar
codecaine
 
Posts: 90
Joined: Sat Jun 13, 2009 8:38 pm
Location: Fort Campbell, KY

Re: Phone Number Calculations

Postby Back_Track » Wed Jul 01, 2009 8:23 pm

I think i might take the time to make a nice GUI for this in Delphi
Back_Track
 
Posts: 16
Joined: Wed Jul 01, 2009 4:18 am

Re: Phone Number Calculations

Postby code » Thu Aug 13, 2009 11:51 pm

My solution in java
Code: Select all

public class Phone {


    public static void main(String[] args) {
      System.out.println(phoneCalc("programming challenges are fun"));
    }


    public static int letterValue(char letter){
        if(Character.isUpperCase(letter))
            letter = Character.toLowerCase(letter);
        switch(letter){
            case 'a':
            case 'b':
            case 'c':
                return 2;
            case 'd':
            case 'e':
            case 'f':
                return 3;
            case 'g':
            case 'h':
            case 'i':
                return 4;
            case 'j':
            case 'k':
            case 'l':
                return 5;
            case 'm':
            case 'n':
            case 'o':
                return 6;
            case 'p':
            case 'r':
            case 's':
                return 7;
            case 't':
            case 'u':
            case 'v':
                 return 8;
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                return 9;
            default:
                return 1;
        }
    }

    private static long phoneCalc(String name){
        long dummyValue = 1;
        for(int x = 0; x < name.length(); x++){
            dummyValue *= letterValue(name.charAt(x));
        }
        return dummyValue;
    }



}

User avatar
code
Site Admin
 
Posts: 19
Joined: Fri Jun 12, 2009 5:30 pm
Location: Fort Campbell, KY

Re: Phone Number Calculations

Postby codecaine » Sat Aug 29, 2009 2:59 am

I converted my java code to c++
Code: Select all
#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int letterValue(char letter);
long phoneCalc(string name);

int main(int argc, char *argv[]){

   cout<<phoneCalc("programming challenges are fun");

}

int letterValue(char letter){
    if(isupper(letter))
        letter = toupper(letter);
    switch(letter){
        case 'a':
        case 'b':
        case 'c':
            return 2;
        case 'd':
        case 'e':
        case 'f':
            return 3;
        case 'g':
        case 'h':
        case 'i':
            return 4;
        case 'j':
        case 'k':
        case 'l':
            return 5;
        case 'm':
        case 'n':
        case 'o':
            return 6;
        case 'p':
        case 'r':
        case 's':
            return 7;
        case 't':
        case 'u':
        case 'v':
             return 8;
        case 'w':
        case 'x':
        case 'y':
        case 'z':
            return 9;
        default:
            return 1;
    }
}

long phoneCalc(string name){
    long dummyValue = 1;
    for(int x = 0; x < name.length(); x++){
        dummyValue *= letterValue(name[x]);
    }
    return dummyValue;
}
User avatar
codecaine
 
Posts: 90
Joined: Sat Jun 13, 2009 8:38 pm
Location: Fort Campbell, KY


Return to Programming Challenges

Who is online

Users browsing this forum: No registered users and 1 guest

cron