Jump to content

Pairs of digits as sublists?


Recommended Posts

I wondered if someone would be willing to help me with a snippet of code.

 

I'm trying to generate a series of 100 sublist 2 digit-pairs. The first of those digit-pairs randomly (or according to a predefined vector) alternates between 1 and 2 only: '((1 #) (2 #) (2 #) (1 #) (2 #) (1 #) (1 #) etc). The second of those digit-pairs unfolds numbers from 1 to 100 between every consecutive sub-list pair: '((# 1) (# 2) (# 3) (# 4) (# 5) etc).

 

The result would be something like this: '((1 1) (2 2) (2 3) (1 4) (2 5) (1 6) (1 7) ... (2 100))

 

Thank you!

Link to comment
Share on other sites

here is a pure LISP/CCL solution

 

(loop for i from 1 to 100
  collect (list (1+ (random 2)) i))

=> ((1 1) (2 2) (2 3) (2 4) (1 5) (1 6) (1 7) (2 8) (1 9) (1 10) (1 11) (1 12) (2 13) (2 14) (1 15) (2 16) (2 17) (2 18) (2 19) (2 20) (1 21) (2 22) (2 23) (1 24) (2 25) (2 26) (1 27) (2 28) (2 29) (2 30) (2 31) (2 32) (2 33) (1 34) (2 35) (2 36) (1 37) (1 38) (2 39) (1 40) (1 41) (1 42) (1 43) (2 44) (2 45) (2 46) (2 47) (1 48) (2 49) (1 50) (2 51) (1 52) (2 53) (1 54) (1 55) (1 56) (2 57) (2 58) (1 59) (2 60) (1 61) (1 62) (1 63) (2 64) (1 65) (1 66) (2 67) (1 68) (1 69) (2 70) (2 71) (2 72) (1 73) (1 74) (2 75) (1 76) (1 77) (2 78) (2 79) (1 80) (2 81) (2 82) (1 83) (2 84) (2 85) (1 86) (1 87) (2 88) (2 89) (1 90) (2 91) (1 92) (1 93) (1 94) (2 95) (2 96) (2 97) (1 98) (1 99) (2 100))

 

as a function

 

(defun pairs (n)
  (loop for i from 1 to n
    collect (list (1+ (random 2)) i)))
  
(pairs 200)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy