{ }codelper
charactersP0226

Ticket number from a seat code

In a small theater every row has w seats. The ground-floor rows are A, B, …, Z from the front, and after them the balcony rows are a, b, …, z from the front. A seat code is one row letter followed by the seat number in that row (one digit from 1 to w).
For example, B3 is seat 3 in ground-floor row B. Ticket numbers go A1 = 1, A2 = 2, and when a row ends they continue with seat 1 of the next row.
Row a comes after row Z. In other words, if the row letter is row r from the front (A is row 0, Z is row 25, a is row 26, z is row 51) and the seat number is s, the ticket number is r × w + s. The first line gives w and the number of seat codes n separated by one space, and each of the next n lines gives one seat code. Print the ticket number of each seat code, one per line.

Input
1 ≤ w ≤ 9, 1 ≤ n ≤ 100.
A seat code has two characters: the first is an uppercase (A-Z) or lowercase (a-z) English letter, and the second is a digit from 1 to w
Output
n lines, each a ticket number (integer)
Example 1 · stdin
4 4
A1
B3
a2
Z4
stdout
1
7
106
104
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

How you turn one character into a number differs by language.
In C, C++, Java and C# a char is an integer holding the character code, so row - 'A' is the row index directly and seat - '0' is the seat number.
But forget the - '0' and use seat as it is, and the character code goes in without any error ('3' is 51).
Python, JS, TS and Craykon have no character type, only one-character strings, so you take the code out: Python ord(c), JS/TS c.charCodeAt(0), Craykon Str.Code(c).
Python ord("3") is 51 too, not 3 (a single digit also works with int("3")).
In JS code[1] is the string "3", so r * w + code[1] joins instead of adding and gives an answer like "43".
In Craykon too, number + string joins, so r * w + code[1] gives "43"; make a number with Str.Code first and then compute.
The second trap is the balcony.
In character codes there are 6 other characters between Z (90) and a (97), so subtracting 'A' from every row letter makes a row 32, not 26.
Check for uppercase first ('A' <= row <= 'Z', Java Character.isUpperCase, C# char.IsUpper) and count lowercase as 26 + (row - 'a').
Also remember rows count from 0.
Counting A as row 1 makes A1 come out as w + 1.

Pythonmain.py · 11 lines
w, n = map(int, input().split())for _ in range(n):    code = input().strip()    row, seat = code[0], code[1]    # 글자는 ord로 코드를 얻어 뺀다. 1층 A~Z는 0~25째 줄, 2층 a~z는 26~51째 줄    if "A" <= row <= "Z":        r = ord(row) - ord("A")    else:        r = 26 + ord(row) - ord("a")    s = ord(seat) - ord("0")        # int(seat)와 같다. ord(seat)만 쓰면 49~57이다    print(r * w + s)
JavaScriptmain.js · 14 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [w, n] = lines[0].trim().split(/\s+/).map(Number);const A = "A".charCodeAt(0), Z = "Z".charCodeAt(0), a = "a".charCodeAt(0), ZERO = "0".charCodeAt(0);const out = [];for (let i = 1; i <= n; i++) {  const code = lines[i].trim();  const row = code.charCodeAt(0);  // 1층 A~Z는 0~25째 줄, 2층 a~z는 26~51째 줄  const r = row <= Z ? row - A : 26 + row - a;  // code[1]은 문자열 "7"이다. 수에 + 하면 이어 붙여지니 코드로 바꿔 뺀다  const s = code.charCodeAt(1) - ZERO;  out.push(r * w + s);}console.log(out.join("\n"));
TypeScriptmain.ts · 12 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [w, n]: number[] = lines[0].trim().split(/\s+/).map(Number);const out: number[] = [];for (let i = 1; i <= n; i++) {  const code: string = lines[i].trim();  const row: number = code.charCodeAt(0);  const r: number = row <= "Z".charCodeAt(0) ? row - "A".charCodeAt(0) : 26 + row - "a".charCodeAt(0);  const s: number = code.charCodeAt(1) - "0".charCodeAt(0);  out.push(r * w + s);}console.log(out.join("\n"));
JavaMain.java · 23 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        StringTokenizer st = new StringTokenizer(br.readLine());        int w = Integer.parseInt(st.nextToken());        int n = Integer.parseInt(st.nextToken());        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            String code = br.readLine().trim();            char row = code.charAt(0);            char seat = code.charAt(1);            // char끼리 빼면 int다. 1층 A~Z는 0~25째 줄, 2층 a~z는 26~51째 줄            int r = Character.isUpperCase(row) ? row - 'A' : 26 + (row - 'a');            int s = seat - '0'; // seat를 그대로 계산에 쓰면 글자 코드(49~57)가 들어간다            out.append(r * w + s).append('\n');        }        System.out.print(out);    }}
C#Main.cs · 24 lines
using System;using System.Text; class Program{    static void Main()    {        string[] first = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int w = int.Parse(first[0]);        int n = int.Parse(first[1]);        var sb = new StringBuilder();        for (int i = 0; i < n; i++)        {            string code = Console.ReadLine().Trim();            char row = code[0];            char seat = code[1];            // char끼리 빼면 int다. 1층 A~Z는 0~25째 줄, 2층 a~z는 26~51째 줄            int r = char.IsUpper(row) ? row - 'A' : 26 + (row - 'a');            int s = seat - '0'; // char.GetNumericValue(seat)도 되지만 double을 돌려준다            sb.Append(r * w + s).Append('\n');        }        Console.Write(sb);    }}
C++main.cpp · 17 lines
#include <iostream>#include <string> int main() {    std::ios::sync_with_stdio(false);    int w, n;    std::cin >> w >> n;    for (int i = 0; i < n; i++) {        std::string code;        std::cin >> code;        char row = code[0], seat = code[1];        // 1층 A~Z는 0~25째 줄, 2층 a~z는 26~51째 줄        int r = (row >= 'A' && row <= 'Z') ? row - 'A' : 26 + (row - 'a');        int s = seat - '0';        std::cout << r * w + s << '\n';    }}
Cmain.c · 16 lines
#include <stdio.h> int main(void) {    int w, n;    scanf("%d %d", &w, &n);    for (int i = 0; i < n; i++) {        char code[8];        scanf("%7s", code);        char row = code[0], seat = code[1];        /* char는 작은 정수라 바로 뺄 수 있다. 1층 A~Z는 0~25째 줄, 2층 a~z는 26~51째 줄 */        int r = (row >= 'A' && row <= 'Z') ? row - 'A' : 26 + (row - 'a');        int s = seat - '0'; /* seat만 쓰면 '7'의 코드 55가 들어간다 */        printf("%d\n", r * w + s);    }    return 0;}
Craykonmain.cray · 21 lines
first = Console.ReadLine().Trim().Split(" ");w = first[0].ToNumber();n = first[1].ToNumber();for (i = 0; i < n; i++){    code = Console.ReadLine().Trim();    // 크레이콘에는 글자 타입이 없다. 한 글자 문자열을 글자.코드로 수로 바꿔 뺀다    row = Str.Code(code[0]);    seat = Str.Code(code[1]);    r = 0;    if (row <= Str.Code("Z"))    {        r = row - Str.Code("A");    }    else    {        r = 26 + row - Str.Code("a");    }    s = seat - Str.Code("0");    Console.WriteLine(r * w + s);}