Post Reply 
Programming puzzles: processing lists!
08-16-2017, 10:21 PM
Post: #193
RE: Programming puzzles: processing lists!
(08-16-2017 08:10 PM)pier4r Wrote:  And I agree with DavidM. Whatever the implementation (even in Malebolge, no ok not in Malebolge), please share!

Okay, you asked for it :-)

This is my quick-and-dirty implementation in Common Lisp:-
Code:

(defun generate-match-pairings (players)
  "Generate a list of all player pairings.
Where PLAYERS is a list of objects unique under EQL comparisons."
  (if (eql 2 (length players))
      (list players)
      (loop
         :with a := (first players)
         :for b :in (rest players)
         :append (let ((remaining (remove b (rest players))))
                   (mapcar (lambda (match-pairing)
                             (list* a b match-pairing))
                           (generate-match-pairings remaining))))))
and here is a function that takes the lists produced by the above routine and prints them out in a more readable form:-
Code:

(defun print-match-pairings (match-pairings)
  (dolist (match-pairing match-pairings)
    (loop
       :for even := t :then (not even)
       :for players :on match-pairing
       :when even
       :do (format t " ~A-~A" (first players) (second players)))
    (format t "~%")))
so if I invoke those functions as:-
Code:

(print-match-pairings (generate-match-pairings '(1 2 3 4 5 6)))
I get the following 15 lines:-
1-2 3-4 5-6
1-2 3-5 4-6
1-2 3-6 4-5
1-3 2-4 5-6
1-3 2-5 4-6
1-3 2-6 4-5
1-4 2-3 5-6
1-4 2-5 3-6
1-4 2-6 3-5
1-5 2-3 4-6
1-5 2-4 3-6
1-5 2-6 3-4
1-6 2-3 4-5
1-6 2-4 3-5
1-6 2-5 3-4

Now I am aware that I am probably the only person here who is familiar with Common Lisp, which makes the above of somewhat limited use :-), but maybe it will pique your (or someone else's) interest enough to consider learning the language. It is very productive for this kind of explorative programming! Don't be scared by the parenthesis!

Anyway, the basic principle of operation is that the function recurses down on the input list, shortening it by two elements each time. It returns a list of match-pairings, where a match-pairing is simply a flat list of objects, implicitly pairing together items at index 1 & 2, 3 & 4, etc.

Hope this is enough to give the general idea.

Paul
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - pdo - 08-16-2017 10:21 PM



User(s) browsing this thread: 2 Guest(s)