Skip to content

Commit 33078e4

Browse files
authored
Add bounds check for array in the median/mode calculation routine
1 parent 7980300 commit 33078e4

File tree

1 file changed

+6
-0
lines changed
  • Trinket_Ultrasonic_Rangefinder

1 file changed

+6
-0
lines changed

Trinket_Ultrasonic_Rangefinder/code.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def find_mode(x):
8080
should mode not be found
8181
"""
8282
n = len(x)
83+
84+
if n == 0: # If somehow there is a null array
85+
return 0;
86+
87+
if n == 1: # In trivial case of a one element array
88+
return x[0]; # just return the element as the mode.
8389

8490
max_count = 0
8591
mode = 0

0 commit comments

Comments
 (0)