#lang racket
(require loudhum)
(require 2htdp/image)
(require rackunit)

;;; File:
;;;   000000.rkt
;;; Authors:
;;;   The student currently referred to as 000000
;;;   Fahmida Hamid
;;;   Samuel A. Rebelsky
;;; Contents:
;;;   Code and solutions for Exam 2 2019S
;;; Citations:
;;;

;; +---------+--------------------------------------------------------
;; | Grading |
;; +---------+

;; This section is for the grader's use. Please do not remove it.

;; Problem 1: 
;; Problem 2:
;; Problem 3:
;; Problem 4:
;; Problem 5:
;; Problem 6:
;;           ----
;;     Total:

;;    Scaled:
;;    Errors:
;;     Times:
;;          :
;;          :
;;          :
;;           ----
;;     Total:

;; +----------+-------------------------------------------------------
;; | Prologue |
;; +----------+

; Time Log:
;   Date        Start   Finish  Elapsed Activity

; Time Spent: 

;; +-----------+------------------------------------------------------
;; | Problem 1 |
;; +-----------+

; Time Log:
;   Date        Start   Finish  Elapsed Activity

; Time Spent: 

; Citations:

; Provided code

; Solution

;;; Procedure:
;;;   hash-invert
;;; Parameters:
;;;   
;;; Purpose:
;;;   
;;; Produces:
;;;   
;;; Preconditions:
;;;   
;;; Postconditions:
;;;   
(define hash-invert
  (lambda (hash)
    hash)) ; STUB

; Examples/Tests:
#|

|#

;; +-----------+------------------------------------------------------
;; | Problem 2 |
;; +-----------+

; Time Log:
;   Date        Start   Finish  Elapsed Activity

; Time Spent: 

; Citations:

; Provided Code:

;;; Procedure:
;;;   random-elt
;;; Parameters:
;;;   lst, a non-empty list 
;;; Purpose:
;;;   Unpredictably pick an element of lst.
;;; Produces:
;;;   val, a value
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   * val is an element of lst.
;;;   * If lst contains more than one element, it is difficult to predict 
;;;     which element val is.
(define random-elt
  (lambda (lst)
    (list-ref lst (random (length lst)))))

;;; Procedure:
;;;   random-sentence
;;; Parameters:
;;;   [None]
;;; Purpose:
;;;   Generate a simple, unpredictable, sentence.
;;; Produces:
;;;   sentence, a string.
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   sentence is a simple English sentence.
(define random-sentence
  (lambda ()
    (string-append
     "The "
     (random-thing) " "
     (random-elt transitive-verbs) " "
     (add-indefinite-article (random-thing)) ".")))

;;; Procedure:
;;;   capitalize
;;; Parameters:
;;;   str, a string
;;; Purpose:
;;;   Capitalizes a string
;;; Produces:
;;;   capitalized, a string
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   * (substring capitalized 1) = (substring str 1)
;;;   * If (string-ref str 0) is a lowercase letter,
;;;     (string-ref capitalized 0) is (char-upcase (string-ref str 0))
;;;   * Otherwise, (string-ref capitalized 0) is (string-ref str 0)
(define capitalize
  (lambda (str)
    (regexp-replace "^[a-z]" str string-upcase)))

;;; Procedure:
;;;   add-indefinite-article
;;; Parameters:
;;;   str, a string
;;; Purpose:
;;;   Add the appropriate article to the front of a string
;;; Produces:
;;;   extended, a string
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   * If str begins with a lowercase vowel, extended is "an str"
;;;   * If str begins with an uppercase vowel, extended is "An str"
;;;   * If str begins with an uppercase consonant, extended is "A str"
;;;   * Otherwise, extended is "a str"
(define add-indefinite-article
  (lambda (str)
    (cond
      [(regexp-match? #px"^[aeiou]" str)
       (string-append "an " str)]
      [(regexp-match? #px"^[AEIOU]" str)
       (string-append "An " str)]
      [(regexp-match? #px"^[A-Z]" str)
       (string-append "A " str)]
      [else
       (string-append "a " str)])))
     
;;; Procedure:
;;;   random-thing
;;; Parameters:
;;;   [None]
;;; Purpose:
;;;   Generate a simple, unpredictable, noun phrase.
;;; Produces:
;;;   thing, a string
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   thing is a noun phrase, something that can serve as the
;;;   subject or object of a transitive verb.  However, thing
;;;   still requires an article.
(define random-thing
  (lambda ()
    (string-append
     (random-elt adjectives) " "
     (random-elt nouns))))

;;; Values:
;;;   adjectives
;;;   nouns
;;;   transitive-verbs
;;; Type:
;;;   list of strings
(define adjectives 
  (list "heavy" "blue" "green" "hot" "cold" "disgusting"
        "antipodean" "existential"))
(define nouns 
  (list "aardvark" "baboon" "civet" "dingo" "emu" "zebra"))
(define transitive-verbs
  (list "saw" "threw" "attacked" "considered" "studied" "visited"))

; Solution:

;;; Procedure:
;;;   random-story
;;; Parameters:
;;;   
;;; Purpose:
;;;   
;;; Produces:
;;;   
;;; Preconditions:
;;;   
;;; Postconditions:
;;;   
(define random-story
  (lambda ()
    "Once upon a time ... ")) ; STUB

; Examples/Tests:
#|

|#

;; +-----------+------------------------------------------------------
;; | Problem 3 |
;; +-----------+

; Time Log:
;   Date        Start   Finish  Elapsed Activity

; Time Spent: 

; Citations:

; Solution:

; b.

;;; Procedure:
;;;   
;;; Parameters:
;;;
;;; Purpose:
;;;   
;;; Produces:
;;;   
;;; Preconditions:
;;;   
;;; Postconditions:
;;;

; a.

(define what ( lambda ( scale)
(let* ( [p symbol?] [q list?]
[ r 'other ] [s 'symbol ] [t 
'hash] [ u number?][v hash?
] [w 'list] [x 'string] [y
 'number ] [z  string?] [
image ( lambda ( drawing
)(cond [( u drawing ) y
]      [ (and ( not (u
drawing) )( p drawing
) ) s] [ (q drawing)
w] [(or (q drawing)
( v drawing)) t][(
z   drawing ) x][
else r] ))])(map
image scale))))

; c.

#|
Put your explanation here
|#

; Examples/Tests:

#|

|#

;; +-----------+------------------------------------------------------
;; | Problem 4 |
;; +-----------+

; Time Log:
;   Date        Start   Finish  Elapsed Activity

; Time Spent: 

; Citations:

; Provided code:

;;; Procedure:
;;;   extract-tweet-text-field
;;; Parameters:
;;;   tweetstring, a tweet
;;; Purpose:
;;;   Extract the text field from the given string.
;;; Produces:
;;;   text, a string
;;; Preconditions:
;;;   tweetstring has the standard string/JSON representation of tweets,
;;;   e.g., "{\"source\:": \"...\", \"id_str\": \"...\", ...}"
;;; Postconditions:
;;;   text is the portion of the string corresponding to the text section.
(define extract-tweet-text-field
  (lambda (tweetstring)
    (regexp-replace* "^.*\"text\": \"(([^\"\\\\]|\\\\\")*?)\".*$" tweetstring "\\1")))

; Solution:

;;; Procedure:
;;;   extract-field
;;; Parameters:
;;;   field, a string
;;;   str, a string
;;; Purpose:
;;;   Extract a field from a JSON string of the form 
;;;   "{\"field1\": \"contents1\", \"field2\": \"contents2\", ...}"
;;; Produces:
;;;   contents, a string
;;; Preconditions:
;;;   str contains a field of the given form.
;;; Postconditions:
;;;   contents contains the associated value.
(define extract-field
  (lambda (field str) 
    "STUB")) ; STUB

; Examples/Tests:
#|

|#

;; +-----------+------------------------------------------------------
;; | Problem 5 |
;; +-----------+

; Time Log:
;   Date        Start   Finish  Elapsed Activity

; Time Spent: 

; Citations:

; Solution:

;;; Procedure:
;;;   sort-by-length
;;; Parameters:
;;;   strings, a list of strings [verified]
;;; Purpose:
;;;   
;;; Produces:
;;;   
;;; Preconditions:
;;;   
;;; Postconditions:
;;;
(define sort-by-length
  (lambda (strings)
    strings)) ; STUB

; Examples/Tests:
#|

|#

;; +-----------+------------------------------------------------------
;; | Problem 6 |
;; +-----------+

; Time Log:
;   Date        Start   Finish  Elapsed Activity

; Time Spent: 

; Citations:

; Provided code:

(struct post (title author content tags views))

; Solution:

; a.

;;; Procedure:
;;;   post->xml
;;; Parameters:
;;;   p, a post
;;; Purpose:
;;;   Convert the post to XML
;;; Produces:
;;;   xml, an XML expression
;;; Preconditions:
;;;   * p has the appropriate form
;;;   * (string? (post-title p))
;;;   * (string? (post-author p))
;;;   * (string? (post-content p))
;;;   * (all string? (post-tags p))
;;;   * (integer? (post-views post))
;;; Postconditions:
;;;   xml represents the same post as p, just in a different form
(define post->xml
  (lambda (p)
    '(post (title "Title")
           (author "Author")
           (content "Contents")
           (tags (tag "Tag1") (tag "Tag2"))
           (views "0")))) ; STUB

; b. 

;;; Procedure:
;;;   xml->post
;;; Parameters:
;;;   xml, an XML expression
;;; Purpose:
;;;   Convert the expression to a post struct.
;;; Produces:
;;;   p, a post
;;; Preconditions:
;;;   xml has the appropriate form (see docs elsewhere)
;;; Postconditions:
;;;   p represents the same post as xml, just in a different form
(define xml->post
  (lambda (xml)
    (post "Title"
          "Author"
          "Content"
          '("Tag1" "Tag2")
          0))) ; STUB

; Examples/Tests:
#|

|#

