Prepare > Python > Built-Ins > Input()

2023. 9. 27. 20:22HackerRank-Python

 

Input() | HackerRank

A Python 2 challenge: Input() is equivalent to eval(raw_input(prompt)).

www.hackerrank.com

 

문제


Task

You are given a polynomial P of a single indeterminate (or variable), x.
You are also given the values of x and k. Your task is to verify if P(x)=k.

Input Format

The first line contains the space separated values of x and k.
The second line contains the polynomial P.

Output Format

Print True if P(x)=k . Otherwise, print False.

 

=> 독립변수 x와 다항식 P, k값을 줄건데 P(x)=k 의 불린값을 출력하라

 

 

 

 

코드


x, k = map(int, input().split())
P = input()


def poly(x, k):
    calcul = eval(P)
    return calcul == k

print(poly(x, k))
x, k= map(int,input().split())

print(eval(input())==k)

 

 

 

 

노트


eval(expression, globals=None, locals=None)

 

문자열로 받은 표현식(expression)을 실행해 리턴

x = 1
print(eval('x+1'))
# 2

 

 

 

 

참조


 

Built-in Functions

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...

docs.python.org

 

[python] 파이썬 eval 함수 정리 및 예제

안녕하세요. BlockDMask 입니다. 오늘은 조금 색다른 함수인 eval 이라는 함수를 가지고 왔습니다. 이 함수는 간단해서 이해하는데는 문제가 없을 것 입니다. 하지만, 이 함수가 유용한게 맞는지 유

blockdmask.tistory.com