Advertisement
Guest User

Phonebook Problem Racket

a guest
Jul 13th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 1.26 KB | None | 0 0
  1. #lang racket
  2.  
  3.  
  4. (define (parts2 stars bars)
  5.   ;;if bars = 1, return a list with stars.
  6.   (cond
  7.     [(= bars 1)
  8.      (list (list stars))]
  9.     ;;if stars = 0 make a list of 0s
  10.     [(= stars 0)
  11.      (list (make-list bars 0))]
  12.     [else
  13.      (for*/list
  14.          ([h (range (+ 1 stars))]
  15.           [rest (parts2 (- stars h) (- bars 1))])
  16.        (cons h rest))]
  17.          ;;create a list from 0 to stars
  18.          ;;for each item in that list concatenate
  19.          ;;with (parts2 stars-i bars-1)
  20.     ))
  21.  
  22.  
  23. (define phone (list 16 4 17 10 15 4 4 6 7 14 9 17 27 6 1 9 0 12 20 8 0 3 4 0 3 4))
  24.  
  25. (define (diff-group li n target)
  26.   (abs (- target (apply + (take li n)))))
  27.  
  28.  
  29. (define (score partitions li target)
  30.   (define (loop sum ps ls)
  31.     (cond
  32.       [(null? ps) sum]
  33.       [else
  34.        (let ((step (car ps)))
  35.          ;(writeln ls)
  36.          (loop (+ sum (diff-group ls step target))
  37.                (cdr ps)
  38.                (drop ls step)))]))
  39.   (loop 0 partitions li))
  40.  
  41. (define (volume-partitions volume-number li)
  42.   ;;define our target
  43.   (define target (/ (apply + li) volume-number))
  44.   (define candidates (parts2 26 volume-number))
  45.   (define (score-curry candidate)
  46.     (score candidate li target))
  47.   (argmin score-curry candidates))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement