Character At Position

Character At Position

Easy Python Error Handling 10 views
Explanation Complexity

Problem Statement

String s and position p are provided (0-based). Output character at that position. If p is not integer or out of range, output INVALID.

Input Format

Line1 s. Line2 p.

Output Format

One character or INVALID.

Example

hello
1
e

Constraints

Length of s

Input / Output Format

Input Format
Line1 s. Line2 p.
Output Format
One character or INVALID.
Constraints
Length of s

Examples

Input:
hello 1
Output:
e

Example Solution (Public)

Python
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2:
  sys.stdout.write('INVALID')
else:
  s=lines[0]
  try:
    p=int(lines[1].strip())
    sys.stdout.write(s[p])
  except Exception:
    sys.stdout.write('INVALID')

Official Solution Code

import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2:
  sys.stdout.write('INVALID')
else:
  s=lines[0]
  try:
    p=int(lines[1].strip())
    sys.stdout.write(s[p])
  except Exception:
    sys.stdout.write('INVALID')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.