There was a topic back on UTI to find all modes of a list in the shortest code-- read it if you want.

You are given a list of at most 50 real numbers through either Ans or Input, which will have a unique mode. In 21 bytes of z80 TI-BASIC (don't count the program header), output the mode.

By comparison, Weregoose had this in 2007:
Ans→X:seq(sum(Ans=Ans(X)),X,1,dim(Ans:Ans=max(Ans:LX(max(Ansseq(X,X,1,dim(Ans—(40 bytes)
(Paste to see it; we don't have spoilers)
which can be optimized to
Ans→X:seq(sum(Ans=Ans(X)),X,1,dim(Ans:Ans=max(Ans:LX(max(AnscumSum(1 or Ans—(36 bytes)

DarkerLine's 24-byte snippet later on in the thread fails for numbers of sufficient magnitude, so it's invalid for this challenge.
It turns out I've forgotten the routine. In fact, I've been working on it for about an hour without any progress. So I'd appreciate help in figuring this out Laughing. I faintly remember the routine having an e^( or 10^( or something, though I don't know *why*?

Here's Weregoose's 36 byte routine that returns one mode of a list that doesn't necessarily have a unique mode, unspoilered:


Code:
Ans→X:seq(sum(Ans=Ans(I)),I,1,dim(Ans:Ans=max(Ans:|LX(max(AnscumSum(1 or Ans


And here's my 29 byte attempt for a list with a unique mode.


Code:
Ans->X:seq(sum(Ans=Ans(I)),I,1,dim(Ans:sum(|LXnot(Ans!=max(Ans
I rediscovered the routine. Now I remember what I was thinking about when I first wrote it—finding ways to make the optional frequency arguments of the statistics commands—mean(, median(, stdDev(, variance(.


Code:
median(Ans,e^(4seq(sum(Ans=Ans(I)),I,1,dim(Ans


This relies on the idea that, when more than half of a list is a single element, that element is the median. We use the frequency list argument to arrange that the mode has a frequency at least half of the total. Because e^(4X) = (e^4)^X ~= 54.6^X, the frequency value of an element increases by a factor of 54.6 for every increase in its actual frequency. As an example, look at a list {0,0,3,4,5,...,50}, which has 50 elements. The median of this list is calculated as if 0 occurs 54^2 times, which is slightly greater than the sum of the other elements' 54 each. Thus, in the median( calculation, more than half of the elements are 0, and therefore the median is 0, the mode of the original list.

When there are more than 54 elements, many non-modes may overwhelm one of the modes in frequency. In addition, when there are multiple modes, there is no guarantee that one will be selected as the median—{1,1,2,2} returns 1.5, because the median is the average of the middle two numbers in this case. {1,1,2,3,3} also breaks the routine. I can't just replace e^(4... with e^(5..., because that would cause an overflow error when the same element is repeated multiple times.

Here's another brainteaser. At the cost of just one byte, the maximum list size that this routine can handle can be increased to about 150. How?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement