Created
March 8, 2013 02:01
-
-
Save strongriley/5113667 to your computer and use it in GitHub Desktop.
Evernote Top Four Challenge
This file contains 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
""" | |
Evernote Top Four Challenge | |
Author: Riley Strong <[email protected]> | |
Last Updated: 2013-03-07 | |
""" | |
class TopValues(object): | |
""" | |
Distills a list of numbers to the top values (largest integers). | |
""" | |
top_values = [] # i=0 is highest value. | |
length = None | |
def __init__(self, length, values): | |
self.length = length | |
for value in values: | |
self.insert_or_discard(value) | |
def print_values(self): | |
for value in self.top_values: | |
print value | |
def insert_or_discard(self, value): | |
""" | |
Inserts the value into the top_value list if it is large enough. | |
""" | |
insertion_point = 0 | |
for top_value in self.top_values: | |
if value > top_value: | |
break | |
insertion_point = insertion_point + 1 | |
if insertion_point < self.length: | |
self.top_values.insert(insertion_point, value) | |
if len(self.top_values) > self.length: | |
self.top_values.pop() | |
def calculate_top_four(): | |
# Gather input | |
length = int(raw_input()) | |
values = [] | |
for i in range(length): | |
values.append(int(raw_input())) | |
top = TopValues(4, values) | |
top.print_values() | |
if __name__ == "__main__": | |
calculate_top_four() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment