2023. 9. 29. 14:51ㆍHackerRank-Python
Any or All | HackerRank
Return True, if any of the iterable is true or if all of it is true using the any() and all() expressions.
www.hackerrank.com
문제
Task
You are given a space separated list of integers. If all the integers are positive, then you need to check if any integer is a palindromic integer.
Input Format
The first line contains an integer N. N is the total number of integers in the list.
The second line contains the space separated list of N integers.
Output Format
Print True if all the conditions of the problem statement are satisfied. Otherwise, print False.
=> N개의 정수를 줄건데, 모든 정수가 양수이고, 하나라도 "회문수" 이면 True를 리턴하라
회문수: 대칭으로 생긴 수
코드
N = int(input())
num = list(input().split())
con_1 = all([int(x) > 0 for x in num])
con_2 = []
for i in num:
for j in range(len(i)):
con_2.append(i[j] == i[-j-1])
print(con_1 & any(con_2))
노트
any()
print(any([1>0,1==0,1<0]))
# True
print(any([1<0,2<1,3<2]))
# False
- iterable 객체를 받아 하나라도 참(True)이면 True 리턴
all()
print(all(['a'<'b','b'<'c']))
# True
print(all(['a'<'b','c'<'b']))
# False
- iterable 객체를 받아 모두 True면 True 리턴
양수 판별
N = int(input())
num = list(input().split())
con_1 = all([int(x) > 0 for x in num])
print([int(x) > 0 for x in num])
# [True, True, True, True, True]
회문수 판별
N = int(input())
num = list(input().split())
con_1 = all([int(x) > 0 for x in num])
con_2 = []
for i in num: # 인풋받은 정수들 각각에 대해
for j in range(len(i)): # 문자열로 취급해 반복문 사용
con_2.append(i[j] == i[-j-1]) # 첫 문자와, 끝 문자끼리 하나씩 비교
print(con_2)
# [False, False, True, False, False, True, False, False]
another code
N = int(input())
numbers = list(map(int, input().split()))
print(all([x > 0 for x in numbers]) and any([str(x) == str(x)[::-1] for x in numbers]))
참조
https://github.com/raleighlittles/Python-HackerRank/blob/master/Built-ins/Any%20or%20All.py
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Built-Ins > ginortS (0) | 2023.10.02 |
---|---|
Prepare > Python > Built-Ins > Athlete Sort (0) | 2023.10.01 |
Prepare > Python > Built-Ins > Python Evaluation (0) | 2023.09.28 |
Prepare > Python > Built-Ins > Input() (0) | 2023.09.27 |
Prepare > Python > Built-Ins > Zipped! (0) | 2023.09.26 |