ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Reverse_t9
    myCode/ShortestCodeChallenge 2016. 6. 8. 21:13
    2000

    Consider that the English alphabet contains 26 characters, while telephones only have ten digits on the keypad. The letters are mapped onto the digits as shown below:

    When you press a digit, the corresponding letter appears on the screen. If you keep pressing the button without pauses, the letters mapped onto the button change in sequence. Let's assume that no button is pressed repeatedly more times than there are letters mapped onto it.

    Given the list of digits pressed, return the text that should appear on the screen.

    Example

    • For keys = "44 444", the output should be
      reverse_t9(keys) = "hi".

    • For keys = "999337777", the output should be
      reverse_t9(keys) = "yes".

    Check out this challenge for reference.

    Input/Output

    • [time limit] 500ms (cpp)
    • [input] string keys

      Sequence of key presses, a string of digits (all but '1') and whitespace characters, where a whitespace character means a pause. It is guaranteed that there are no two consecutive '0's or ' 's in the string.

      Constraints:
      1 ≤ keys.length ≤ 1500.

    • [output] string

      The text as a string of lowercase English letters.






    이번 문제는 '핸드폰으로 영어 메시지를 입력할 때의 과정'을 구현해보는 것이다.


    핸드폰으로 A를 치고 싶다면 2를 한 번 누르고 C를 치고싶을 땐 2를 세 번 누른다.


    "999337777" 이런식으로 주어진 input 값에 해당하는 문자를 출력하면 된다.


















    <완성 코드>



    std::string reverse_t9(std::string keys) {


        std::string ret;

        std::string temp;

        

        std::string::iterator it;

        for (it = keys.begin(); it < keys.end(); it++) {

            if (*it =='0') ret += ' ';

            else if (*it != ' ') {

                temp += *it;

                    ret += getS(temp);

                    temp = "";

                }

            }

        }

        return ret;

    }


    char getS(std::string nums) {

        char a;

        

        if (nums[0] > '1' && nums[0] <= '7') {

            a = 97+(nums[0]-50)*3;

        }

        else if (nums[0] == '8') a='t';

        else if (nums[0] == '9') a='w';

        

        for (int i=1; i<nums.size(); i++) a++; 

        

        return a;

    }





    조금 긴 코드다.


    문자열을 모두 돌면서 연속되는 문자열이 끝나는 부분을 조사하고


    연속된 숫자로 이루어진 묶음 숫자를 getS 함수로 따로 돌려서


    숫자입력에 해당하는 문자 하나를 output string에 연결시켜 덧붙이는 방식.



    예를 들어, input이 999337777 일때 999, 33, 7777로 문자열을 나눠서


    getS 함수에 999, 33, 7777을 각각 넣고 돌려서


    각각 해당하는 문자인 y, e, s를 구해내는 방식이다.


    'myCode > ShortestCodeChallenge' 카테고리의 다른 글

    Fact (Reverse Challenge)  (0) 2016.07.04
    EvenNumbers  (0) 2016.07.04
    Cipher_Zeroes [Decimal to Binary]  (0) 2016.06.04
    Turns on Road  (0) 2016.05.17
    Anagram  (0) 2016.05.14
Designed by Tistory.