A friend sent a note made of one long string and a list of letter numbers.
The first line gives a string s of lowercase English letters, the second line the count q, and the third line q numbers separated by spaces.
A positive number k means the k-th letter of s from the front. The first letter is the 1st.
A negative number k means the (-k)-th letter of s from the back. The last letter is -1, and the one before it is -2.
No number is 0, and every number points to a letter inside s.
Print, on one line, the letters the numbers point to, joined in the given order.
Input
1 ≤ length of s ≤ 1,000, s has lowercase letters only, 1 ≤ q ≤ 1,000, 1 ≤ |k| ≤ length of s
Output
One line: a string of q letters
Example 1 · stdin
secretnote
4
3 -3 7 -1
stdout
cone
main.py
Error List00F8 ↓ · ⇧F8 ↑
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
"The k-th from the front" is a number people count from 1, while string indexes in all eight languages start at 0. So a positive k is s[k - 1] everywhere (s.charAt(k - 1) in Java). Counting from the back differs by language. In Python s[-1] is already the last letter, so a negative number can be used as it is. Subtracting 1 there as well (s[k - 1]) shifts every letter by one. C# has from-the-end indexes with ^, like s[^1]. In JS/TS s[-1] gives undefined without an error, and joining it inserts the word "undefined". s.at(-1) does count from the back. Java charAt(-1) throws, and C s[-1] or C++ s[-1] reads outside the array, which is undefined behavior. Craykon s[-1] stops with a runtime error ("only 0 to n"). In these languages add the length: s[len + k] (Craykon s[s.Length + k]).
Pythonmain.py · 9 lines
s = input().strip()q = int(input())out = []for k in map(int, input().split()): if k > 0: out.append(s[k - 1]) # 앞에서 k번째 → 번호는 k - 1 else: out.append(s[k]) # Python은 s[-1]이 맨 끝 글자print("".join(out))
JavaScriptmain.js · 9 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const s = lines[0].trim();const ks = lines[2].trim().split(/\s+/).map(Number);let out = "";for (const k of ks) { // s[-1]은 undefined라서 음수는 길이를 더해 번호로 바꾼다(s.at(k)도 된다) out += k > 0 ? s[k - 1] : s[s.length + k];}console.log(out);
TypeScriptmain.ts · 10 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const s = lines[0].trim();const ks: number[] = lines[2].trim().split(/\s+/).map(Number);let out = "";for (const k of ks) { // s[-1]은 undefined라서 음수는 길이를 더해 번호로 바꾼다(s.at(k)도 된다) out += k > 0 ? s[k - 1] : s[s.length + k];}console.log(out);
JavaMain.java · 17 lines
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s = in.readLine().trim(); in.readLine(); StringBuilder out = new StringBuilder(); for (String t : in.readLine().trim().split("\\s+")) { int k = Integer.parseInt(t); // charAt은 0부터, 음수는 받지 않는다 → 뒤에서 세면 길이를 더한다 out.append(k > 0 ? s.charAt(k - 1) : s.charAt(s.length() + k)); } System.out.println(out); }}
C#Main.cs · 19 lines
using System;using System.Text;class Program{ static void Main() { string s = Console.ReadLine().Trim(); Console.ReadLine(); var sb = new StringBuilder(); foreach (string t in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)) { int k = int.Parse(t); // s[^m]은 뒤에서 m번째 글자(C# 8 이후) — ^1이 맨 끝 sb.Append(k > 0 ? s[k - 1] : s[^(-k)]); } Console.WriteLine(sb); }}
C++main.cpp · 16 lines
#include <iostream>#include <string>int main() { std::string s, out; int q; std::cin >> s >> q; int len = (int)s.size(); for (int i = 0; i < q; i++) { int k; std::cin >> k; // s[-1]은 정해지지 않은 동작 → 뒤에서 세면 길이를 더한다 out += k > 0 ? s[k - 1] : s[len + k]; } std::cout << out << '\n';}
Cmain.c · 19 lines
#include <stdio.h>#include <string.h>int main(void) { static char s[1001]; static char out[1001]; int q; scanf("%1000s %d", s, &q); int len = (int)strlen(s); for (int i = 0; i < q; i++) { int k; scanf("%d", &k); /* C 배열은 0부터, 음수 번호는 배열 밖을 읽는다 → 길이를 더해 바꾼다 */ out[i] = k > 0 ? s[k - 1] : s[len + k]; } out[q] = '\0'; puts(out); return 0;}
Craykonmain.cray · 16 lines
s = Console.ReadLine().Trim();q = Console.ReadLine().ToNumber();ks = Console.ReadNumbers();out = "";foreach (k in ks){ if (k > 0) { out = out + s[k - 1]; // 앞에서 k번째 → 번호는 k - 1 } else { out = out + s[s.Length + k]; // s[-1]은 오류라서 길이를 더한다 }}Console.WriteLine(out);