【hackerrank】-Day 14: Scope

24 篇文章 0 订阅
24 篇文章 0 订阅

30-scope
Objective
Today we’re discussing scope. Check out the Tutorial tab for learning materials and an instructional video!

The absolute difference between two integers, and , is written as . The maximum absolute difference between two integers in a set of positive integers, , is the largest absolute difference between any two integers in .

The Difference class is started for you in the editor. It has a private integer array () for storing non-negative integers, and a public integer () 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 instance variable.
A computeDifference method that finds the maximum absolute difference between any numbers in and stores it in the 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

, where
Output Format

You are not responsible for printing any output; the Solution class will print the value of the instance variable.

Sample Input

3
1 2 5
Sample Output

4
Explanation

The scope of the array and integer is the entire class instance. The class constructor saves the argument passed to the constructor as the 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:

The maximum of these differences is , so it saves the value as the instance variable. The locked stub code in the editor then prints the value stored as , which is .


class Difference:
    def __init__(self, a):
        self.__elements = a
    # Add your code here
    def computeDifference(self):
        self.__elements.sort()
        ll = len(self.__elements)
        return abs(self.__elements[0]-self.__elements[ll-1])

_ = input()
a = [int(e) for e in input().split(' ')]
d = Difference(a)
maximumDifference = d.computeDifference()
print(maximumDifference)

"""
# End of Difference class
_ = input()
a = [int(e) for e in input().split(' ')]
d = Difference(a)
d.computeDifference()
print(d.maximumDifference)
"""

Congratulations
You solved this challenge. Would you like to challenge your friends?

Next Challenge
Testcase 0 Testcase 1 Testcase 2 Testcase 3
7 Testcases
Input (stdin)Download
3
1 2 5
Expected OutputDownload
4
Compiler Message
Success

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值