-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_three_largest_numbers.py
executable file
·34 lines (30 loc) · 1.36 KB
/
find_three_largest_numbers.py
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
32
33
34
"""
Write a function that takes in an array of at least three integers and, without sorting the input array,
returns a sorted array of the three largest integers in the input array.
The function should return duplicate integers if necessary;
For example, it should return [10, 10, 12] for an input array of [10, 5, 9, 10, 12]
"""
def findThreeLargestNumbers(array):
# Write your code here.
import math
first_greatest_num = second_greatest_num = third_greatest_num = -math.inf
"""
iterate the array with a while loop
if current element is greater than any of the 3 nums, assign it to the appropriate greatest num
"""
for num in array:
if num > third_greatest_num:
if num > second_greatest_num:
if num > first_greatest_num:
third_greatest_num = second_greatest_num
second_greatest_num = first_greatest_num
first_greatest_num = num
else:
third_greatest_num = second_greatest_num
second_greatest_num = num
else:
third_greatest_num = num
# print([third_greatest_num, second_greatest_num, first_greatest_num])
return [third_greatest_num, second_greatest_num, first_greatest_num]
three_largest_nums = findThreeLargestNumbers([10, 5, 9, 10, 12])
print(three_largest_nums)