Created
May 26, 2020 00:26
-
-
Save davidli3100/ce6ddb54ed21d5425e2cbbcb90f21c61 to your computer and use it in GitHub Desktop.
Sample code for Recurse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import stdin | |
class reading: | |
def __init__(self,r,c): | |
super().__init__() | |
self.reading = r | |
self.count = c | |
def sort_r_c(reading): | |
return reading.count | |
def sort_r_r(reading): | |
return reading.reading | |
n = int(stdin.readline().strip()) | |
readings = [reading(i,0) for i in range(1001)] | |
for i in range(n): | |
r = int(stdin.readline().strip()) | |
readings[r].count = readings[r].count + 1 | |
sortedReadings = sorted(readings, key=sort_r_c, reverse=True) | |
multipleHighs = sortedReadings[0].count == sortedReadings[2].count | |
multipleSecondaryhighs = sortedReadings[0].count != sortedReadings[1].count and sortedReadings[1].count == sortedReadings[2].count | |
if multipleHighs: | |
i = 1 | |
while i < 1001 and sortedReadings[0].count == sortedReadings[i].count: | |
i = i+1 | |
sortedMR = sorted(sortedReadings[0:i], key=sort_r_r, reverse=True) | |
# print(sortedMR[0].reading) | |
print(abs(sortedMR[0].reading-sortedMR[-1].reading)) | |
elif multipleSecondaryhighs: | |
i = 2 | |
mi = abs(sortedReadings[0].reading - sortedReadings[1].reading) | |
while i < 1001 and sortedReadings[1].count == sortedReadings[i].count: | |
mi = max(mi, abs(sortedReadings[0].reading - sortedReadings[i].reading)) | |
i = i+1 | |
print(mi) | |
else: | |
print(abs(sortedReadings[0].reading - sortedReadings[1].reading)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment