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

Pages: 1-4041-

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-05 12:32

Prelude> let a = "ABCDE" in [ (x,y) | x <- a, y <- a, x < y ]
[('A','B'),('A','C'),('A','D'),('A','E'),('B','C'),('B','D'),('B','E'),('C','D'),('C','E'),('D','E')]

Name: Anonymous 2012-02-05 12:34

>fictional language syntax please

Okay. Here's how to do it in my new language.

fsdfSDFsf4r466HGFJGHU7ghjghjgVCVXCVXCVXCVty45ty~~~~~~~~~ERTerertdtdrtetrsegdfG~G@G>>>SDFSD:FALDSLFG&&GGEEEEREE0xFFFFFFFFFFFFFAGGOTsdfsddlfldfldkfldkfkdlfkfkfkfkfF??fd!!!¬¬¬.call(2112);999999999999999999999999999999999999999=2</html>

Just kidding, it's perl.

Name: Anonymous 2012-02-05 12:39

Assuming array index starts at 0:

for i from 0 to array_length - 2 do
  for j from i + 1 to array_length - 1 do
    print {array[i], array[j]}
  endfor
endfor

Name: Anonymous 2012-02-05 12:42

>>2
Didn't know Haskell was a fictional language!

Name: Anonymous 2012-02-05 12:46

>>5
It's a language I made up. It's exactly like Haskell, except that the Int type is called YourAFaggot.

Name: HugePenis 2012-02-05 12:52

>>4
Wow, we have someone here who can actually think logically. Anyways, I translated your algorithm to Java. Here is what I came up with.


public class Main {

    public static void main(String[] args) {
        char[] list = {'a', 'b', 'c', 'd', 'e'};

        for (int i = 0; i < list.length - 1; i++) {
            for(int j = i + 1; j < list.length; j++) {
                System.out.println(list[i] + " , " + list[j]);
            }
        }//end for
    }//end main
}


And the corresponding output:

run:
a , b
a , c
a , d
a , e
b , c
b , d
b , e
c , d
c , e
d , e
BUILD SUCCESSFUL (total time: 0 seconds)

Name: Anonymous 2012-02-05 12:54

from itertools import permutations

for pair in permutations(list("abcde"), 2):
    print pair

Name: Anonymous 2012-02-05 12:56

>>6
Haskellite involuntarily perturbed aggressiveness. You should see a doctor about that.

Name: Anonymous 2012-02-05 13:00

>>7
Needs more enterprise design patterns.

Name: Anonymous 2012-02-05 13:11

Fictional language

stuff(Result) :-
  L1 = [a,b,c,d,e],
  append(_J0,AL,L1), AL = [A| L2],
  append(_J1,BL,L2), BL = [B|_LR],
  Result = [A,B].

Name: OP 2012-02-05 13:44

Thanks >>4 and >>7, and you made laugh hard >>3.

Name: Anonymous 2012-02-05 14:07

Wait, is ("A","B") the same as ("B","A").

Name: Anonymous 2012-02-05 14:59

>>8
Python is beauty.

Name: Anonymous 2012-02-05 15:04

>>13
It looks like everyone else here was able to figure it out. Nice job chief. You're below average.

Name: Anonymous 2012-02-05 15:37

>>15
Go scrub a toilet you mental midget.

Name: Anonymous 2012-02-05 15:50

>>13
Oh, you don't know the difference between permutation and combination?
If you're pursuing a CS degree, change your school. If you're a self-learner, study combinatorics.

Name: Anonymous 2012-02-05 16:19

>>17
I know the difference, but without reading OPs list there was no way to tell which is which, and I was being lazy.

Name: Anonymous 2012-02-05 16:57

>>18
It was pretty obvious that the output was a permutation.

Name: Anonymous 2012-02-05 16:59

>>18
And this is evident by all the previous responses.

Name: Anonymous 2012-02-05 17:08

>>19
implying

Name: Anonymous 2012-02-05 17:10

>>21
That  >>18 is kind of slow?

Name: Anonymous 2012-02-05 17:14

Extended APL:
∘.,⍨'abcde'

Name: Anonymous 2012-02-05 17:27

>>19
If it was a permutation then ("A","B") and ("B","A") would be different.

Name: Anonymous 2012-02-05 17:34

>>8
clearly winning

Name: Anonymous 2012-02-05 17:43

>>24
Look at the python version you stupid shit. It uses permutations to get the correct answer. Man, I swear, you are fucking slow.

Name: Anonymous 2012-02-05 17:43

@r.push($_ X @a) while @a.shift;

Name: Anonymous 2012-02-05 17:48

>>9
Hello, Zhivago.

Name: Anonymous 2012-02-05 17:50

An array of five elements, print all unordered pairs?
Simple, consider this in my made up language:
x: array <- {a, b, c, d, e}
stdout <- string <- {x->1 x->2,
x->1 x->3,
x->1 x->4,
x->1 x->5,
x->2 x->3,
x->2 x->4,
x->2 x->5,
x->3 x->4,
x->2 x->5,
x->4 x->5}

No need for loops when you know the array size.

Name: Anonymous 2012-02-05 17:52

>>28
Back to ##C, please!

Name: Anonymous 2012-02-05 18:56

fuck I hate zhivago's fucking guts

Name: Anonymous 2012-02-05 22:36

>>26
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.

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

Name: Anonymous 2012-02-06 0:16

Here's a cartesian product:


#!/usr/bin/lua

local tab = {}

function tab.for_all_values(A, operator)
  for _, v in pairs(A) do
    operator(v)
  end
end

function tab.values_iterator(A)
  return coroutine.wrap(function()
    tab.for_all_values(A, coroutine.yield)
  end)
end

function tab.values_iterator_generator(A)
  return function()
    return tab.values_iterator(A)
  end
end


local iter = {}

function iter.for_all(iterator, operator)
  for v in iterator do
    operator(v)
  end
end

function iter.list(...)
  return tab.values_iterator({...})
end

-- iterator is an iterator of iterators
function iter.concat(iterator)
  return coroutine.wrap(function()
    iter.for_all(iterator,
                 function(sub_iterator)
                   iter.for_all(sub_iterator, coroutine.yield)
                 end)
  end)
end

function compose(f, g)
  return function(x)
    return f(g(x))
  end
end

function iter.map1(iterator, operator)
  return coroutine.wrap(function()
    iter.for_all(iterator, compose(coroutine.yield, operator))
  end)
end

function iter.product(iterator1, iterator2_gen)
  return iter.concat(iter.map1(iterator1,
                               function(iterator1_value)
                                 return iter.map1(iterator2_gen(),
                                                  function(iterator2_value)
                                                    return {iterator1_value, iterator2_value}
                                                  end)
                               end))
end

A = {"A", "B", "C", "D", "E"}


print"test tab.for_all_values:"
tab.for_all_values(A, print)

print"test tab.values_iterator:"
i = tab.values_iterator(A)
for v in i do
  print(v)
end

print"test tab.values_iterator_generator:"
g1 = tab.values_iterator_generator(A)
i1 = g1()
for v1 in i1 do
  i2 = g1()
  for v2 in i2 do
    print(v1, v2)
  end
end


print "test iter.for_all:"
iter.for_all(tab.values_iterator(A), print)


print"test iter.list:"
iter.for_all(iter.list("hi", "there", "how", "are", "you", "doing", "today?"), print)

print "test iter.concat:"
iter.for_all(iter.concat(iter.list(iter.list(1, 2), iter.list(3, 4), iter.list(5, 6))), print)


print "test iter.map1:"
iter.for_all(iter.map1(iter.list(1, 2, 3, 4), function(x) return x*3 end), print)


print "test iter.product:"
iter.for_all(iter.product(tab.values_iterator(A),
                          tab.values_iterator_generator(A)),
             function(pair) print(pair[1], pair[2]) end)


output:


test tab.for_all_values:
A
B
C
D
E
test tab.values_iterator:
A
B
C
D
E
test tab.values_iterator_generator:
A    A
A    B
A    C
A    D
A    E
B    A
B    B
B    C
B    D
B    E
C    A
C    B
C    C
C    D
C    E
D    A
D    B
D    C
D    D
D    E
E    A
E    B
E    C
E    D
E    E
test iter.for_all:
A
B
C
D
E
test iter.list:
hi
there
how
are
you
doing
today?
test iter.concat:
1
2
3
4
5
6
test iter.map1:
3
6
9
12
test iter.product:
A    A
A    B
A    C
A    D
A    E
B    A
B    B
B    C
B    D
B    E
C    A
C    B
C    C
C    D
C    E
D    A
D    B
D    C
D    D
D    E
E    A
E    B
E    C
E    D
E    E

Name: Anonymous 2012-02-06 3:35

All n combinations from a list, in 6 lines of scheme:

(define (combinations n lis output)
  (cond ((= n 0)
         (output '()))
        ((not (null? lis))
         (begin (combinations (- n 1) (cdr lis) (lambda (x) (output (cons (car lis) x))))
                (combinations n (cdr lis) output)))))

(combinations 3 '(a b c d e f g) (lambda (x) (display x) (newline)))



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


by the way, did you know that you can pipe the content of a file being editted in vim to a command using:

:w !command

It's rather nifty when writing scripts.

Name: Anonymous 2012-02-06 5:21

>>34
I don't like your implementation, your use of lambda is most unusual.

Name: Anonymous 2012-02-06 5:23

>>35

yeah, it functions as a call back. It would be tricky to get it to extract a list, and it would require mutations. But it is trivial in languages that have message passing between threads, and the elements can be extracted lazily.

Name: Anonymous 2012-02-06 5:39

>>35

Here's a more normal functional way of writing it:


(define (combinations n lis)
  (cond ((= n 0)
         (list '())) ;; The empty set is a zero element subset of any set
        ((null? lis)
         (list)) ;; There are no non empty subsets of the empty set.
        (else
         (append (map (lambda (rest) (cons (car lis) rest))
                      (combinations (- n 1) (cdr lis)))
                 (combinations n (cdr lis))))))

(display (combinations 3 '(a b c d e f g)))

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


The appends could be inefficient though.

Name: Anonymous 2012-02-06 6:49

my_list = ['a', 'b','c', 'd', 'e']
for i in my_list:
    for j in my_list:
        print(i, j)

Name: Anonymous 2012-02-06 7:00

>>38
True “programmers” misread their specs.

Name: Anonymous 2012-02-06 7:50

foo = ["a","b","c","d","e"]
main = print [x ++ ", " ++ y | x <- foo, y <- foo]

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)

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