This Python program introduces the concept of sets. It takes a list of integers as input, removes any duplicates by converting the list to a set, and then calculates the average of the distinct elements. The problem is sourced from HackerRank.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution works by first converting the input list to a set, which automatically removes any duplicate elements due to the property of sets. Then, it calculates the average of the distinct elements by summing them up and dividing by the count of the elements. The result is then rounded to three decimal places for precision. Alternatively, the built-in 'mean' function from the 'statistics' module can be used to calculate the average.
import statistics
def average(array):
distinct_heights = set(array)
avg_heights = sum(distinct_heights) / len(distinct_heights)
return round(avg_heights, 3)
if __name__ == "__main__":
n = int(input())
arr = list(map(int, input().split()))
result = average(arr)
print(result)
If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.