Pythonmain.py · 60 行import sys # 글자 키마다 차례로 돌아가며 나오는 글자들KEYS = { "1": ".,?!", "2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",}LOWER, ONCE, CAPS = 0, 1, 2 # 소문자, 한 글자 대문자, 대문자 고정 def type_line(presses): text = [] mode = LOWER key, count = None, 0 # 쓰는 중인 글자: 키와 누른 횟수 (key가 None이면 없음) def commit(): nonlocal key, mode if key is None: return letters = KEYS[key] ch = letters[(count - 1) % len(letters)] if ch.isalpha() and mode != LOWER: ch = ch.upper() if mode == ONCE: mode = LOWER text.append(ch) key = None for p in presses: match p: case "_": commit() case "0": commit() text.append(" ") case "#": commit() mode = (mode + 1) % 3 case "*": if key is not None: key = None # 쓰는 중인 글자만 버린다 elif text: text.pop() case _: # 글자 키 1~9 if p == key: count += 1 else: commit() key, count = p, 1 commit() return "".join(text) def main(): lines = sys.stdin.read().split() n = int(lines[0]) print("\n".join("<" + type_line(lines[i]) + ">" for i in range(1, n + 1))) main()
JavaScriptmain.js · 60 行// 글자 키마다 차례로 돌아가며 나오는 글자들const KEYS = { "1": ".,?!", "2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",};const LOWER = 0, ONCE = 1, CAPS = 2; // 소문자, 한 글자 대문자, 대문자 고정 function typeLine(presses) { const text = []; let mode = LOWER; let key = null, count = 0; // 쓰는 중인 글자: 키와 누른 횟수 (key가 null이면 없음) function commit() { if (key === null) return; const letters = KEYS[key]; let ch = letters[(count - 1) % letters.length]; if (ch >= "a" && ch <= "z" && mode !== LOWER) { ch = ch.toUpperCase(); if (mode === ONCE) mode = LOWER; } text.push(ch); key = null; } for (const p of presses) { switch (p) { // p는 한 글자 문자열이라 case도 "0"처럼 문자열로 쓴다 case "_": commit(); break; case "0": commit(); text.push(" "); break; case "#": commit(); mode = (mode + 1) % 3; break; case "*": if (key !== null) key = null; // 쓰는 중인 글자만 버린다 else if (text.length > 0) text.pop(); break; default: // 글자 키 1~9 if (p === key) { count++; } else { commit(); key = p; count = 1; } } } commit(); return text.join("");} const words = require("fs").readFileSync(0, "utf8").split(/\s+/).filter((w) => w.length > 0);const n = Number(words[0]);const out = [];for (let i = 1; i <= n; i++) out.push("<" + typeLine(words[i]) + ">");console.log(out.join("\n"));
TypeScriptmain.ts · 63 行import { readFileSync } from "node:fs"; // 글자 키마다 차례로 돌아가며 나오는 글자들const KEYS: Record<string, string> = { "1": ".,?!", "2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",};enum Mode { Lower, Once, Caps } // 소문자, 한 글자 대문자, 대문자 고정 function typeLine(presses: string): string { const text: string[] = []; let mode: Mode = Mode.Lower; let key: string | null = null; // 쓰는 중인 글자의 키 (null이면 없음) let count = 0; // 그 키를 누른 횟수 function commit(): void { if (key === null) return; const letters: string = KEYS[key]; let ch: string = letters[(count - 1) % letters.length]; if (ch >= "a" && ch <= "z" && mode !== Mode.Lower) { ch = ch.toUpperCase(); if (mode === Mode.Once) mode = Mode.Lower; } text.push(ch); key = null; } for (const p of presses) { switch (p) { // p는 한 글자 문자열이라 case도 "0"처럼 문자열로 쓴다 case "_": commit(); break; case "0": commit(); text.push(" "); break; case "#": commit(); mode = (mode + 1) % 3; break; case "*": if (key !== null) key = null; // 쓰는 중인 글자만 버린다 else if (text.length > 0) text.pop(); break; default: // 글자 키 1~9 if (p === key) { count++; } else { commit(); key = p; count = 1; } } } commit(); return text.join("");} const words: string[] = readFileSync(0, "utf8").split(/\s+/).filter((w: string) => w.length > 0);const n: number = Number(words[0]);const out: string[] = [];for (let i = 1; i <= n; i++) out.push("<" + typeLine(words[i]) + ">");console.log(out.join("\n"));
JavaMain.java · 72 行import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public class Main { // 글자 키 1~9마다 차례로 돌아가며 나오는 글자들 static final String[] KEYS = {"", ".,?!", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; static final int LOWER = 0, ONCE = 1, CAPS = 2; // 소문자, 한 글자 대문자, 대문자 고정 static StringBuilder text; static int mode; static char key; // 쓰는 중인 글자의 키 ('\0'이면 없음) static int count; static void commit() { if (key == '\0') return; String letters = KEYS[key - '0']; char ch = letters.charAt((count - 1) % letters.length()); if (ch >= 'a' && ch <= 'z' && mode != LOWER) { ch = Character.toUpperCase(ch); if (mode == ONCE) mode = LOWER; } text.append(ch); key = '\0'; } static String typeLine(String presses) { text = new StringBuilder(); mode = LOWER; key = '\0'; count = 0; for (char p : presses.toCharArray()) { // 화살표 꼴 switch는 다음 case로 이어 내려가지 않는다 switch (p) { case '_' -> commit(); case '0' -> { commit(); text.append(' '); } case '#' -> { commit(); mode = (mode + 1) % 3; } case '*' -> { if (key != '\0') key = '\0'; // 쓰는 중인 글자만 버린다 else if (text.length() > 0) text.setLength(text.length() - 1); } default -> { // 글자 키 1~9 if (p == key) { count++; } else { commit(); key = p; count = 1; } } } } commit(); return text.toString(); } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { out.append('<').append(typeLine(in.readLine().trim())).append(">\n"); } System.out.print(out); }}
C#Main.cs · 88 行using System;using System.Text; class Program{ // 글자 키 1~9마다 차례로 돌아가며 나오는 글자들 static readonly string[] Keys = { "", ".,?!", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; enum Mode { Lower, Once, Caps } // 소문자, 한 글자 대문자, 대문자 고정 static StringBuilder text = new StringBuilder(); static Mode mode; static char key; // 쓰는 중인 글자의 키 ('\0'이면 없음) static int count; static void Commit() { if (key == '\0') return; string letters = Keys[key - '0']; char ch = letters[(count - 1) % letters.Length]; if (ch >= 'a' && ch <= 'z' && mode != Mode.Lower) { ch = char.ToUpper(ch); if (mode == Mode.Once) mode = Mode.Lower; } text.Append(ch); key = '\0'; } static string TypeLine(string presses) { text.Clear(); mode = Mode.Lower; key = '\0'; count = 0; foreach (char p in presses) { // C#은 내용이 있는 case 끝에 break가 없으면 컴파일 오류다 switch (p) { case '_': Commit(); break; case '0': Commit(); text.Append(' '); break; case '#': Commit(); mode = mode switch { Mode.Lower => Mode.Once, Mode.Once => Mode.Caps, _ => Mode.Lower, }; break; case '*': if (key != '\0') key = '\0'; // 쓰는 중인 글자만 버린다 else if (text.Length > 0) text.Length--; break; default: // 글자 키 1~9 if (p == key) { count++; } else { Commit(); key = p; count = 1; } break; } } Commit(); return text.ToString(); } static void Main() { int n = int.Parse(Console.ReadLine().Trim()); var output = new StringBuilder(); for (int i = 0; i < n; i++) { output.Append('<').Append(TypeLine(Console.ReadLine().Trim())).Append(">\n"); } Console.Write(output); }}
C++main.cpp · 72 行#include <iostream>#include <string> // 글자 키 1~9마다 차례로 돌아가며 나오는 글자들const std::string KEYS[10] = {"", ".,?!", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};enum class Mode { Lower, Once, Caps }; // 소문자, 한 글자 대문자, 대문자 고정 std::string typeLine(const std::string& presses) { std::string text; Mode mode = Mode::Lower; char key = '\0'; // 쓰는 중인 글자의 키 ('\0'이면 없음) int count = 0; auto commit = [&]() { if (key == '\0') return; const std::string& letters = KEYS[key - '0']; char ch = letters[(count - 1) % letters.size()]; if (ch >= 'a' && ch <= 'z' && mode != Mode::Lower) { ch = static_cast<char>(ch - 'a' + 'A'); if (mode == Mode::Once) mode = Mode::Lower; } text += ch; key = '\0'; }; for (char p : presses) { switch (p) { case '_': commit(); break; case '0': commit(); text += ' '; break; case '#': commit(); switch (mode) { // enum class도 switch로 다음 방식을 고른다 case Mode::Lower: mode = Mode::Once; break; case Mode::Once: mode = Mode::Caps; break; case Mode::Caps: mode = Mode::Lower; break; } break; case '*': if (key != '\0') key = '\0'; // 쓰는 중인 글자만 버린다 else if (!text.empty()) text.pop_back(); break; default: // 글자 키 1~9 if (p == key) { count++; } else { commit(); key = p; count = 1; } break; } } commit(); return text;} int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; std::string line; for (int i = 0; i < n; i++) { std::cin >> line; std::cout << '<' << typeLine(line) << ">\n"; } return 0;}
Cmain.c · 69 行#include <stdio.h>#include <string.h> /* 글자 키 1~9마다 차례로 돌아가며 나오는 글자들 */static const char *KEYS[10] = {"", ".,?!", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};enum { LOWER, ONCE, CAPS }; /* 소문자, 한 글자 대문자, 대문자 고정 */ static char text[1001];static int len, mode;static char key; /* 쓰는 중인 글자의 키 ('\0'이면 없음) */static int count; static void commit(void) { if (key == '\0') return; const char *letters = KEYS[key - '0']; char ch = letters[(count - 1) % (int)strlen(letters)]; if (ch >= 'a' && ch <= 'z' && mode != LOWER) { ch = (char)(ch - 'a' + 'A'); if (mode == ONCE) mode = LOWER; } text[len++] = ch; key = '\0';} int main(void) { int n; static char line[1001]; if (scanf("%d", &n) != 1) return 0; for (int t = 0; t < n; t++) { scanf("%1000s", line); len = 0; mode = LOWER; key = '\0'; count = 0; for (int i = 0; line[i] != '\0'; i++) { char p = line[i]; switch (p) { case '_': commit(); break; case '0': commit(); text[len++] = ' '; break; case '#': commit(); mode = (mode + 1) % 3; break; case '*': if (key != '\0') key = '\0'; /* 쓰는 중인 글자만 버린다 */ else if (len > 0) len--; break; default: /* 글자 키 1~9 */ if (p == key) { count++; } else { commit(); key = p; count = 1; } break; } } commit(); text[len] = '\0'; printf("<%s>\n", text); } return 0;}
Craykonmain.cray · 84 行// 글자 키 1~9마다 차례로 돌아가며 나오는 글자들KEYS = ["", ".,?!", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]; // 쓰는 중인 글자가 있으면 문자열 끝에 적는다// st: "text" 적은 글자 목록, "mode" 0 소문자·1 한 글자 대문자·2 대문자 고정,// "key" 쓰는 중인 글자의 키(""이면 없음), "count" 그 키를 누른 횟수적기(st){ もし (st["key"] == "") { 返す 0; } letters = KEYS[st["key"].数に()]; ch = letters[(st["count"] - 1) % letters.長さ]; もし (ch >= "a" && ch <= "z" && st["mode"] != 0) { ch = ch.大文字(); もし (st["mode"] == 1) { st["mode"] = 0; } } st["text"].追加(ch); st["key"] = ""; 返す 0;} // 한 줄의 키를 차례로 누른 결과 문자열줄치기(presses){ st = 辞書(); st["text"] = []; st["mode"] = 0; st["key"] = ""; st["count"] = 0; 繰り返す (i = 0; i < presses.長さ; i = i + 1) { p = presses[i]; 選択 (p) { 場合 "_": 적기(st); 抜ける; 場合 "0": 적기(st); st["text"].追加(" "); 抜ける; 場合 "#": 적기(st); st["mode"] = (st["mode"] + 1) % 3; 抜ける; 場合 "*": もし (st["key"] != "") { st["key"] = ""; // 쓰는 중인 글자만 버린다 } でなければ もし (st["text"].個数 > 0) { st["text"].位置で取り除く(st["text"].個数 - 1); } 抜ける; それ以外: もし (p == st["key"]) { st["count"] = st["count"] + 1; } でなければ { 적기(st); st["key"] = p; st["count"] = 1; } 抜ける; } } 적기(st); 返す st["text"].つなぐ("");} n = コンソール.行読み().空白除去().数に();繰り返す (t = 0; t < n; t = t + 1){ コンソール.書く("<" + 줄치기(コンソール.行読み().空白除去()) + ">");}