Numbers - First and Last Digits Same - Spicy Coders

Recent

Saturday, November 25, 2017

Numbers - First and Last Digits Same

N numbers A(1) to A(N) are passed as input. The program must print only the X numbers which have same first and last digit.
Input Format:
The first line contains N.
The second line contains N numbers separated by a space.
Output Format:
The first line contains the X numbers separated by a space.
Boundary Conditions:

  • 2 <= N <= 200
  • 10 <= A(i) <= 9999999
  • 1 <= X <= N
Example Input/Output 1:
Input:
4
102 333 282 500
Output:
333 282

Source Code://Python

n=int(input())
v=[i for i in input().split()]
for i in v:
        if i[0]==i[len(i)-1]:
            print(i)

Aliter:

import math
n,a=int(input()),[int(i) for i in input().split()]
for i in range(n):
    l=a[i]%10
    f=a[i]//10**int(math.log(a[i],10))
    if f==l:

        print(a[i],end=" ")

No comments:

Post a Comment