Advertisement
Guest User

Untitled

a guest
Oct 30th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 8.81 KB | None | 0 0
  1. (require "./remove_duplicates.rkt")
  2.  
  3. ; This defines the basic album datatype.
  4. (define-struct album (title artist genre))
  5. ; define-struct automatically creates the following functions for you:
  6. ;
  7. ; `make-<struct-name>` (in this case `make-album`)
  8. ;   a function to create an instance of the struct
  9. ;   this function takes arguments for each of the fields listed, so for example
  10. ;   (make-struct 'Sway' 'Tove Styrke' 'Pop') will create an album struct
  11. ;    with title 'Sway', artist 'Tove Styrke' & genre 'Pop
  12. ;
  13. ; `<struct-name>-<field-name>` (for each field)
  14. ;    functions for accessing values of each field in the struct
  15. ;    for album this would mean we'd have the following functions:
  16. ;    `album-title`, `album-artist`, `album-genre`
  17. ;    the following examples creates an album and then accesses its fields
  18. ;    ```
  19. ;    (define sway (make-album 'Sway' 'Tove Styrke' 'Pop')
  20. ;    (album-title sway) ; returns 'Sway'
  21. ;    (album-artist sway) ; returns 'Tove Styrke'
  22. ;    (album-genre sway) ; returns 'Pop'
  23. ;    ```
  24. ;
  25. ; `<struct-name>?` (in this case `album?`)
  26. ;   a predicate (function which returns a boolean) that checks a value and
  27. ;   returns true if it's an instance of the struct, false otherwise
  28. ;   using the `sway` album defined in the previous example
  29. ;   ```
  30. ;   (album? sway) ; returns true
  31. ;   (album? 1) ; returns false
  32. ;   (album? 'hi') ; returns false
  33. ;   ```
  34.  
  35. ;;; Enter a list of albums below
  36. ;;; They need not be the actual albums you own.
  37. ;;; But you should include enough variety to adequately
  38. ;;; test your code.
  39. ;;;
  40. ;;; Here's what we mean.  One of the questions involves
  41. ;;; writing a procedure that finds all the albums of a
  42. ;;; given genre.  If all the albums in the library are
  43. ;;; in the rock genre, then there's only one genre and
  44. ;;; when you ask for all the rock albums and it gives
  45. ;;; back all the albums, you don't know whether that's
  46. ;;; because the code really works, or because it's
  47. ;;; not even paying attention to the genre.  So you want
  48. ;;; to make sure there are multiple artists and genres,
  49. ;;; some artists with only one album or genre, others
  50. ;;; with multiple artists or genres, etc.
  51.  
  52. (define testing-library-1
  53.   ;; Fill in the info below
  54.   (list (make-album "In My Head" "Jason Derulo" "Pop")
  55.         (make-album "Baby" "Justin Bieber" "Pop")
  56.         (make-album "Roses" "Chainsmokers" "EDM")
  57.         (make-album "Broken Arrows" "Avicii" "EDM")
  58.         (make-album "Pursuit of Hapiness" "Kid Cudi" "Banger")
  59.         (make-album "Wake Me Up" "Avicii" "Banger"))      
  60.         )
  61.  
  62. ;;; Add the procedures you write (e.g. all-genres, versatile-artists)
  63. ;;; below.  Be sure to test your procedures to make sure they work.
  64. ;;; We are not providing test cases this time, so it's up to you
  65. ;;; to make sure your code works.  We will use our own test cases
  66. ;;; when grading and assign you a grade based on the number of
  67. ;;; test cases that passed.
  68.  
  69.  
  70. ; all-titles : (listof album) -> (listof string)
  71. ; Gets all titles of all albums in the library
  72. (define all-titles
  73. (lambda (lib) (map (λ (album) (album-title album)) lib)))
  74.  
  75. (check-expect
  76.        (all-titles
  77.         (list (make-album "a" "somebody" "rock") (make-album "b" "somebody else" "country") (make-album "c" "asd" "asd")))
  78.         (list "a" "b" "c"))
  79.  
  80.  
  81. ;; all-artists: (listof album) -> (listof string)
  82. ; Gets all the artists from the albums in the library
  83. (define all-artists
  84.  (lambda (lib) (remove-duplicates (map (λ (album) (album-artist album)) lib))))
  85.  
  86. (check-expect
  87.        (all-artists
  88.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "country") (make-album "c" "c" "country")))
  89.         (list "a" "b" "c"))
  90.  
  91. ;; all-genres: (listof album) -> (listof string)
  92. ; Gets all the genres from the albums in the library
  93. (define all-genres
  94.  (lambda (lib) (remove-duplicates (map (λ (album) (album-genre album)) lib))))
  95.  
  96. (check-expect
  97.        (all-genres
  98.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
  99.         (list "rock" "country" "pop"))
  100.  
  101.  
  102. ;; artist-albums : string, (listof album) -> (listof album)
  103. ; Gets all the albums an artist made from the library
  104. (define artist-albums
  105. (lambda (desired-artist lib) (map (λ (n) (album-title n)) (filter (lambda (album)
  106.                   (string=? (album-artist album) desired-artist)) lib))
  107.               ))
  108.  
  109. (check-expect
  110.        (artist-albums "a"
  111.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
  112.         (list "a" "b"))
  113.  
  114.  
  115. ;; artist-genres: string, (listof album) -> (listof string)
  116. ; Gives all the genres of a given artist from the library
  117. (define artist-genres
  118. (lambda (desired-artist lib) (map (λ (n) (album-genre n)) (filter (lambda (album)
  119.                   (string=? (album-artist album) desired-artist)) lib))
  120.               ))
  121.  
  122. (check-expect
  123.        (artist-genres "a"
  124.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
  125.         (list "rock" "country"))
  126.  
  127.  
  128. ;; artist-is-versatile?: string, (listof album) -> boolean
  129. ; Determines whether an artist has albums in more than 1 genre in the library
  130. (define artist-is-versatile?
  131. (lambda (desired-artist lib) (> (length (artist-genres desired-artist lib)) 1)))
  132.  
  133. (check-expect
  134.        (artist-is-versatile? "a"
  135.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
  136.         #true)
  137.  
  138. (check-expect
  139.        (artist-is-versatile? "b"
  140.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
  141.         #false)
  142.  
  143.  
  144. ;; versatile-artists: (listof album) -> (listof string)
  145. (define versatile-artists
  146.   (λ (lib) (remove-duplicates (map (λ (albums) (album-artist albums)) (filter (λ (album) (artist-is-versatile? (album-artist album) lib)) lib)))))
  147.  
  148. (check-expect
  149.        (versatile-artists  
  150.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
  151.         '("a" "b"))
  152.  
  153. (check-expect
  154.        (versatile-artists  
  155.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "country") (make-album "c" "b" "country")))
  156.         '("a"))
  157.  
  158.  
  159.  
  160.  
  161. ; artist-album-count: string, (listof album) -> number
  162. (define artist-album-count
  163.       (λ (artist lib) (length (filter (λ (album) (string=? (album-artist album) artist)) lib))))
  164.  
  165. (check-expect
  166.        (artist-album-count  
  167.         "a"
  168.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
  169.         2)
  170.  
  171. ; artist-album-count-list: string, (listof album) -> (listof string number)
  172. (define artist-album-count-list
  173.   (λ (artist lib) (list artist (artist-album-count artist lib))))
  174.  
  175. (check-expect
  176.        (artist-album-count-list  
  177.         "a"
  178.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
  179.         (list "a" 2))
  180.  
  181. ;; artist-album-counts: (listof album) -> (listof (list string number))
  182. (define artist-album-counts
  183.   (λ (lib)
  184.     (remove-duplicates (map (λ (album) (artist-album-count-list (album-artist album) lib)) lib))))
  185.  
  186. (check-expect
  187.        (artist-album-counts
  188.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
  189.         (list (list "a" 2) (list "b" 2)))
  190.  
  191.  
  192. ; genre-album-count: string, (listof album) -> number
  193. (define genre-album-count
  194.   (λ (genre lib) (length (filter (λ (album) (string=? (album-genre album) genre)) lib))))
  195.  
  196. (check-expect
  197.        (genre-album-count  
  198.         "country"
  199.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
  200.         2)
  201.  
  202. ; genre-album-count-list: string, (listof album) -> (listof string number)
  203.  
  204. (define genre-album-count-list
  205.   (λ (genre lib) (list genre (genre-album-count genre lib))))
  206.  
  207. (check-expect
  208.        (genre-album-count-list  
  209.         "country"
  210.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
  211.         (list "country" 2))
  212.  
  213.  
  214. ;; genre-album-counts: (listof album) -> (listof (list string number))
  215.  
  216. (define genre-album-counts
  217.   (λ (lib)
  218.     (remove-duplicates (map (λ (album) (genre-album-count-list (album-genre album) lib)) lib))))
  219.  
  220. (check-expect
  221.        (genre-album-counts
  222.         (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
  223.         (list (list "rock" 1) (list "country" 2) (list "pop" 1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement