Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Loop all pairs

Name: Anonymous 2012-02-05 12:30

Let's say I have this array:

{a, b, c, d, e}

How can I look through all the possible pairs?

a, b
a, c
a, d
a, e
b, c
b, d
b, e
c, d
c, e
d, e

Fictional language syntax please.

Name: Anonymous 2012-02-06 7:54

Name: Anonymous 2012-02-06 10:21

>>32
>No dumb shit, I wanted to know whether or not OP was asking >for combinations or permutations, because I didn't read his >list of required outputs. However after reading the list it is >obvious that he is looking for all combinations with two >indices.

It's kind of funny how everyone, except for you, thought it was a permutation.

>You seem to be confused. In a permutation order matters, in a >combination order does not. Try and think before speaking you >mental midget.

Stop projecting. I was able to post working code that produced the correct output you didn't.

Name: Anonymous 2012-02-06 10:24

>>38
nope

Name: Anonymous 2012-02-06 10:26

>>32
[quote]
No dumb shit, I wanted to know whether or not OP was asking for combinations or permutations, because I didn't read his list of required outputs. However after reading the list it is obvious that he is looking for all combinations with two indices.
[/quote]

It's kind of funny how everyone, except for you, thought it was a permutation.

[quote]
You seem to be confused. In a permutation order matters, in a combination order does not. Try and think before speaking you mental midget.
[/quote]

Stop projecting. I was able to post working code that produced the correct output. You didn't. Presumably because you are a clueless twat that doesn't have an ounce of programmers blood in them.

Name: Anonymous 2012-02-06 10:29

>>43
I swear this is the special olypmics of computer programmin. In on corner we have the homo who can't come up with a correct programming solution. In the other corner we have the minimum wage toilet scrubber who doesn't seem to understand the difference between a combination and a permutation.

Name: Anonymous 2012-02-06 10:30

>>45
And apparently I can't type for shit this morning.

Name: Anonymous 2012-02-07 7:33

This one might be more efficient than the binary recursive one. It still has an append though. And the sets are backwards, and it needs a cut off number for the size of the lists. It calculates each subset of n elements, giving it a best case running time of 2^n,


(define (combinations lis)
  (letrec ((helper (lambda (lis accumulation)
             (if (null? lis)
                 accumulation
                 (helper (cdr lis)
                         (append accumulation
                                 (map (lambda (rest) (cons (car lis) rest))
                                      accumulation)))))))
    (helper lis (list '())))) ;; start with the empty set in accumulation

(combinations '(a b c d e))

:w !scm
#<unspecified>
(() (a) (b) (b a) (c) (c a) (c b) (c b a) (d) (d a) (d b) (d b a) (d c) (d c a) (d c b) (d c b a) (e) (e a) (e b) (e b a) (e c) (e c a) (e c b) (e c b a) (e d) (e d a) (e d b) (e d b a) (e d c) (e d c a) (e d c b) (e d c b a))

Name: Anonymous 2012-02-07 8:02

A zomg optimized version that avoids using append, but the ordering of the results becomes very strange. There's a lot of reused memory here. IE, (e c b) and (d c b) have the same cdr.


(define (power-set lis)
  (letrec ((add-element-to-sets (lambda (element sets)
             (letrec ((helper (lambda (sets accumulation)
                        (if (null? sets)
                             accumulation
                             (helper (cdr sets)
                                     (cons (cons element (car sets))
                                           accumulation))))))
               (helper sets sets))))

           (helper (lambda (lis accumulation)
             (if (null? lis)
                 accumulation
                 (helper (cdr lis) (add-element-to-sets (car lis) accumulation))))))

    (helper lis (list '()))))


(power-set '(a b c d e))

:w !scm
#<unspecified>
((e) (e a) (e b a) (e b) (e c b) (e c b a) (e c a) (e c) (e d c) (e d c a) (e d c b a) (e d c b) (e d b) (e d b a) (e d a) (e d) (d) (d a) (d b a) (d b) (d c b) (d c b a) (d c a) (d c) (c) (c a) (c b a) (c b) (b) (b a) (a) ())

Name: Anonymous 2012-02-07 8:42

So why aren't your solutions as easy as >>8?
I mean if your language is going to be slow as shit it might as well offer the things that Python offer.

Name: Anonymous 2012-02-07 8:57

>>49
because they aren't delegating the work to a standard library? And >>8 isn't correct, since it's supposed to be 2-combinations.

Name: Anonymous 2012-02-07 10:05

>>50
I just ran the code. And guess what you fucking jew? The program produced the output that the OP wanted. So now, why this this wrong you idiot?

Name: Anonymous 2012-02-07 10:07

>>50
And WTF. You still never produced a solution. So not only does your math suck, but on top of that, it appears you don't know how to program.

Name: Anonymous 2012-02-07 10:10

>>51
I should have looked up what permutations() did in python. My apologies, I assumed it would be consistent with the mathematical definition. How foolish of me.

Name: Anonymous 2012-02-07 10:12

>>52
your anobreaker is broken. You might need to update it.

Name: Anonymous 2012-02-07 10:12

>>51
Op wants 4 + 3 + 2 + 1 = 10 results.


>>> from itertools import permutations
>>> len(list(permutations(list("abcde"), 2)))
20


Truly, goyim are barely fit to serve the master race.

Name: Anonymous 2012-02-07 10:13

>>50
2-combinations
What do you think the last argument 2 is all about?

Name: Anonymous 2012-02-07 10:14

>>56
2-combinations and 2-permutations are not the same thing.

Name: Anonymous 2012-02-07 10:20

I think "all the possible pairs" are "permutations", if you really want combinations then the fix is trivial.

>>> from itertools import combinations
>>> for pair in combinations('abcde', 2):
...     print pair
...
('a', 'b')
('a', 'c')
('a', 'd')
('a', 'e')
('b', 'c')
('b', 'd')
('b', 'e')
('c', 'd')
('c', 'e')
('d', 'e')

Name: Anonymous 2012-02-07 10:24

>>1
I ran your post through a MM1X interpreter and this is what it came up with.

array of length n

for i from 0 to n
  for j from i+1 to n
    print array[i], array[j]

Name: not_the_op_or_sage 2012-02-07 10:32

>>58
Oh shit, I didn't notice the possible error in >>8's code.

Name: Anonymous 2012-02-07 11:31

l = {'A', 'B', 'C', 'D', 'E'}
createPairs(x) = c:x, d:x (c != d) -> c", "d
print(createPairs(l), newline)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List