15
5
Your task is to calculate the total number of key-presses required to enter a given text on an old cellphone.
The keymaps are:
1:1
2:abcABC2
3:defDEF3
4:ghiGHI4
5:jklJKL5
6:mnoMNO6
7:pqrsPQRS7
8:tuvTUV8
9:wxyzWXYZ9
0:<space><newline>0
To type exaMPle TExt 01 , you would press 33 99 2 6666 77777 555 33 0 8888 33333 99 8 0 <a 1-sec pause here in real life but we'll ignore it>000 1 for a total of 37 keypresses.
The * key brings up a map of special characters:
.,'?!
"-()@
/:_;+
&%*=<
>£€$¥
¤[]{}
\~^¡¿
§#|`
with the first one (.) highlighted. You can move to highlight the required character using rectangular navigation keys and it takes another keypress to select.
So to insert $, you would press *↓↓↓↓→→→<select> i.e. a total of 9 key-presses.
- Input will be from a file called
sourceplaced in the current directory/directory of your program. EDIT: Per requests in comments, I'm addingSTDINas a valid input method. Apologies for changing the spec after receiving answers. - You must output
Total key presses <total_keypresses> - If the input file contains any character not in the given keymap, then your program must output
Invalid character <character> in sourceand exit.
In short, the input and output of your program must resemble that of this(ungolfed) python script:
# This Python file uses the following encoding: utf-8
from __future__ import print_function
import sys
general_dict = { '1':1,
'a':1, 'b':2, 'c':3, 'A':4, 'B':5, 'C':6, '2':7,
'd':1, 'e':2, 'f':3, 'D':4, 'E':5, 'F':6, '3':7,
'g':1, 'h':2, 'i':3, 'G':4, 'H':5, 'I':6, '4':7,
'j':1, 'k':2, 'l':3, 'J':4, 'K':5, 'L':6, '5':7,
'm':1, 'n':2, 'o':3, 'M':4, 'N':5, 'O':6, '6':7,
'p':1, 'q':2, 'r':3, 's':4, 'P':5, 'Q':6, 'R':7, 'S':8, '7':9,
't':1, 'u':2, 'v':3, 'T':4, 'U':5, 'V':6, '8':7,
'w':1, 'x':2, 'y':3, 'z':4, 'W':5, 'X':6, 'Y':7, 'Z':8, '9':9,
' ':1, '\n':2, '0':3
}
special_chars = ['.',',',"'",'?','!','"','-','(',')','@','/',':','_',';','+','&','%','*','=','<','>','£','€','$','¥','¤','[',']','{','}','\\','~','^','¡','¿','§','#','|','`']
for x in special_chars:
general_dict[x]=(special_chars.index(x)/5) + (special_chars.index(x)%5) + 2
key_press_total = 0
with open('source') as f: # or # with sys.stdin as f:
for line in f:
for character in line:
if character in general_dict:
key_press_total+=general_dict[character]
else:
print('Invalid character',character,'in source')
sys.exit(1)
print('Total key presses',key_press_total)
This is code-golf, shortest program in bytes wins.
Shameless disclaimer: I made this challenge to have translations of the above python script in different languages which will be used to score this challenge in the sandbox.
Should we error and exit immediately or when the invalid character is encountered? – nyuszika7h – 2014-07-02T09:16:58.717
@nyuszika7h It's up to you, but you must print what invalid character that was. Suppose there are 10 invalid characters in source, you can select any one of them, print that that character is invalid and exit. It doesn't need to be the first occurrence of an invalid character. – user80551 – 2014-07-02T09:19:10.903
7The input requirement is a shame. File I/O is insanely expensive in some languages and entirely impossible in others. – Dennis – 2014-07-02T14:05:54.970
1If you were to allow more free-form IO, I have a J solution of 171 chars, who's sha1 hash is
1ce5a2fdd0316e37c0a07d151d02db766a3adbb7. – ɐɔıʇǝɥʇuʎs – 2014-07-02T15:47:28.393Does
ValueError: invalid literal for int() with base 10:count as exiting with an invalid char? – TheDoctor – 2014-07-02T16:29:22.0302@Dennis Allowed STDIN – user80551 – 2014-07-02T16:42:10.177
@TheDoctor No, sorry. – user80551 – 2014-07-02T16:44:53.830
@user80551 gives up – TheDoctor – 2014-07-02T16:50:53.167
@TheDoctor It wasn't much as "giving up" as me seeing open requests to change the spec about 15 mins ago – user80551 – 2014-07-02T16:54:09.970
I suppose it not okay to just ignore invalid characters? – ɐɔıʇǝɥʇuʎs – 2014-07-02T18:31:47.743
@ɐɔıʇǝɥʇuʎs See third bullet point. – user80551 – 2014-07-02T18:35:11.227