How to write a Scheme function to find mode of a list of numbers?
-
Write the function mode that takes in a list of integers and returns the most frequently occuring number. For instance,(mode '(1 2 2 3 3 3 4)) returns 3, as it occurs in that list more than any other number. you can assume that the input list will have only one mode; that is, you won't see an input such as '(224 4). You can also assume that the input list is SORTED in ascending order. if the input list is null, simply return '() I'm not sure how to even get started on this if you could give me any direction I would really appreciate it.
-
Answer:
A straighfoward way of doing it http://pastebin.com/Y5TmNcvD uncreative, but it works. Based on the idea that the longest run of equal numbers in a sequential list is the mode. A bit more clever goes like so http://pastebin.com/92dXNXPh based on the idea if you remove the last number in a run of equal number in a sequential list, the mode will be the last number left before the list is empty, if there is only one candidate for the mode. You could even catch cases on multiple modes if instead of "(null? (cdr L))" as the test in the second cond clause, you wrote a "no-duplicates?" predicate function.
sunkist sweetheart at Yahoo! Answers Visit the source
Other answers
A straighfoward way of doing it http://pastebin.com/Y5TmNcvD uncreative, but it works. Based on the idea that the longest run of equal numbers in a sequential list is the mode. A bit more clever goes like so http://pastebin.com/92dXNXPh based on the idea if you remove the last number in a run of equal number in a sequential list, the mode will be the last number left before the list is empty, if there is only one candidate for the mode. You could even catch cases on multiple modes if instead of "(null? (cdr L))" as the test in the second cond clause, you wrote a "no-duplicates?" predicate function.
ratter_o...
Related Q & A:
- How To Write A Resume?Best solution by Yahoo! Answers
- How to access a nested function from another nested function in javascript?Best solution by devarticles.com
- How to fire a function before and after automatically in a jquery plugin?Best solution by catchmyfame.com
- How to write a persuasive letter to a bank?Best solution by wikihow.com
- How to graph a quadratic function on a graphing calculator?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.