Posts input() (Python 3)
Post
Cancel

input() (Python 3)

URL : https://www.hackerrank.com/challenges/input/problem

  • Code
    1
    2
    3
    4
    5
    6
    7
    8
    
    >>> input()  
    1+2
    3
    >>> company = 'HackerRank'
    >>> website = 'www.hackerrank.com'
    >>> input()
    'The company name: '+company+' and website: '+website
    'The company name: HackerRank and website: 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.
  • Constraints
    • All coefficients of polynomial P are integers.
    • x and y are also integers.
  • 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.

문제 해설

  • input을 사용하여 2가지의 입력을 받습니다.
  • 첫번째 input은 는 x와 다항식의 정답으로 예측되는 k
  • 두번째 input은 다항식 입니다.
  • 다항식 p를 입력받고 x를 적용시켜 나오는 정닶과 예측된 k값이 일치하면 true, 아니면 false를 출력하면 됩니다.
  • 첫번째 input은 한줄로 들어오기 때문에 공백으로 split해준뒤 int형으로 변환합니다.
  • 두번째 다항식은 그대로 input 받은 뒤 python에 eval함수를 통해 다항식에 x를 대입하여 결과를 출력하면 됩니다.
1
2
3
x,k=map(int, input().split())
p = input()
print(k==eval(p))
1
2
3
4
5
 1 4
 x**3 + x**2 + x + 1


True
This post is licensed under CC BY 4.0 by the author.