Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

Racket, split a list in two different size lists. And randomly

I would like to code a program that given a list and a percentage, splits the list in two different size lists. It should have random pick of the elements, that way the created lists are always different. These code is able to do that:

(define (clamp x a b)
  (max (min x b) a))

(define (split pct xs)
  (define pos (exact-round (* (clamp pct 0.0 1.0) (length xs))))
  (split-at (shuffle xs) pos))

Here is an example:

(split 0.25 '(1 2 3 4 5 6 7 8 9))

'(6 2)

'(3 7 1 4 5 8 9)

But, instead of "shuffle" I would like to use this function to achieve the same:

(define (get-randomly-no-pair list)
  (list-ref list (random (length list))))

so, get-randomly-no-pair takes one element randomly from the initial list. And all the elements are used to create both lists.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
(define (shuffle-list lst)
  (define indexes (shuffle (range (length lst))))
  (lambda ()
    (begin0
      (list-ref lst (car indexes))
      (set! indexes (cdr indexes)))))

(define gen (shuffle-list (list 10 12 14 16 18 20))
(gen) ; ==> 14 (e.g.)

Now I see you assume you need to pass the list then I would rather make a mapper:

(define (shuffle-accessor len)
  (define indexes (list->vector (shuffle (range len))))
  (lambda (lst index)
    (list-ref lst (vector-ref indexes index))))

(define lst3-ref (shuffle-accessor 3))
(lst3-ref '(1 2 3) 0) ; ==> 3 (e.g.)
(lst3-ref '(6 7 8) 0) ; ==> 8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...