The first line gives a string s and the second line gives a single character c. Print the position where c first appears in s and the position where it last appears, separated by one space. Positions count from 0. If c never appears in s, print "-1 -1". If it appears exactly once, both numbers are the same.
Input
s: lowercase letters and digits, length 1 to 1,000. c: one lowercase letter or digit
Output
the first and last position (-1 -1 if absent)
Example 1 · stdin
banana
a
stdout
1 5
Example 2 · stdin
hello
z
stdout
-1 -1
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
First and last position: Python s.find(c)/s.rfind(c), JS/TS/Java indexOf/lastIndexOf, C# IndexOf/LastIndexOf (ordinal when given a char), C++ find/rfind, C strchr/strrchr. Craykon gets the first position with s.IndexOf(c) and finds the last with a loop from the end asking s[i] == c (there is no search-from-the-back member). What you get when it's absent differs: usually -1, but Python index/rindex raise ValueError, and C++ find returns std::string::npos (the largest size_t, 18446744073709551615), so printing it directly does not give -1. C's strchr returns a pointer or NULL: check for NULL first, then the position is p - s. A single character is a char (single quotes, 'a') in Java, C#, C and C++; Python, JS, TS and Craykon have no separate type, so it's a string of length 1.
Pythonmain.py · 3 lines
s = input()c = input()[0]print(s.find(c), s.rfind(c))