KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I came across an old problem that you Mathematica/StackOverflow folks will probably like and that seems valuable to have on StackOverflow for posterity. Suppose you have a list of lists and you want to pick one element from each and put them in a new list so that the number of elements that are identical to their next neighbor is maximized. In other words, for the resulting list l, minimize Length@Split[l]. In yet other words, we want the list with the fewest interruptions of identical contiguous elements. For example: pick[{ {1,2,3}, {2,3}, {1}, {1,3,4}, {4,1} }] --> { 2, 2, 1, 1, 1 } (Or {3,3,1,1,1} is equally good.) Here's a preposterously brute force solution: pick[x_] := argMax[-Length@Split[#]&, Tuples[x]] where argMax is as described here: posmax: like argmax but gives the position(s) of the element x for which f[x] is maximal Can you come up with something better? The legendary Carl Woll nailed this for me and I'll reveal his solution in a week.
Tags (comma-separated)
Save Edits
Cancel