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-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]

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