Last active
December 14, 2015 23:29
-
-
Save aybabtme/5165927 to your computer and use it in GitHub Desktop.
Attempt at #14, CSI2501 devoir 2.
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
// Give a recursive algorithm for finding a mode of a list of integers. | |
// (A mode is an element in the list that occurs at least as often as | |
// every other element.) | |
func mode(l list<integer>) integer { | |
if null(l) { return NaN } | |
else { return aux_mode(l, new map<int,0>) } | |
} | |
func aux_mode(l list<integer>, count map<int,int>) integer { | |
if size(l) == 0 { | |
max_pair := count.first | |
for pair in count { | |
if( pair.val > max_pair.val ) { max_pair = pair } | |
} | |
return max_pair.key | |
} else { | |
count.key( l[0] ).val++ | |
sub_l := l[1: end] | |
return aux_mode( sub_l, count ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment