일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- 캐치카페
- ubuntu
- Docker
- Kaggle
- 우분투
- Git
- AI 경진대회
- 데이콘
- 파이썬
- 편스토랑
- PYTHON
- 프로그래머스 파이썬
- programmers
- ChatGPT
- Baekjoon
- 편스토랑 우승상품
- SW Expert Academy
- dacon
- 맥북
- 금융문자분석경진대회
- 코로나19
- 백준
- 프로그래머스
- hackerrank
- Real or Not? NLP with Disaster Tweets
- github
- gs25
- 자연어처리
- leetcode
- 더현대서울 맛집
- Today
- Total
솜씨좋은장씨
[HackerRank] Day 14: Scope (Python) 본문
The absolute difference between two integers, a and b, is written as |a - b|. The maximum absolute difference between two integers in a set of positive integers, elements, is the largest absolute difference between any two integers in elements.
The Difference class is started for you in the editor. It has a private integer array (elements) for storing N non-negative integers, and a public integer (maximumDifference) for storing the maximum absolute difference.
Task
Complete the Difference class by writing the following:
- A class constructor that takes an array of integers as a parameter and saves it to the elements instance variable.
- A computeDifference method that finds the maximum absolute difference between any 2 numbers in N and stores it in the maximumDifference instance variable.
Input Format
You are not responsible for reading any input from stdin. The locked Solution class in your editor reads in lines of input; the first line contains , and the second line describes the array.
Constraints
- 1 <= N <= 10
- 1<= elements [ i ] <= 100, where 0 <= i <= N - 1
Output Format
You are not responsible for printing any output; the Solution class will print the value of the maximumDifference instance variable.
Sample Input
3
1 2 5
Sample Output
4
Explanation
The scope of the elements array and maximumDifference integer is the entire class instance. The class constructor saves the argument passed to the constructor as the elements instance variable (where the computeDifference method can access it).
To find the maximum difference, computeDifference checks each element in the array and finds the maximum difference between any elements:
| 1 - 2 | = 1
| 1 - 5 | = 4
| 2 - 5 | = 3
The maximum of these differences is 4, so it saves the value 4 as the maximumDifference instance variable. The locked stub code in the editor then prints the value stored as maximumDifference, which is 4.
Solution
class Difference:
def __init__(self, a):
self.__elements = a
self.maximumDifference = 0
# Add your code here
def computeDifference(self):
max_num = max(self.__elements)
min_num = min(self.__elements)
max_differ = abs(max_num - min_num)
self.maximumDifference = max_differ
# End of Difference class
_ = input()
a = [int(e) for e in input().split(' ')]
d = Difference(a)
d.computeDifference()
print(d.maximumDifference)
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 231. Power of Two (Python) (0) | 2020.07.12 |
---|---|
[leetCode] 989. Add to Array-Form of Integer (Python) (0) | 2020.07.11 |
[leetCode] 198. House Robber (Python) (0) | 2020.07.09 |
[BaekJoon] 9095번 : 1, 2, 3 더하기 (Python) (0) | 2020.07.08 |
[BaekJoon] 10808번 : 알파벳 개수 (Python) (0) | 2020.07.07 |