How to Design Functions Problem Sets

Submit your assignment at: jestlearn.com/how_to_code

Exercise 3.00-string-first
(define PNAME 'string-first)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: string-first
Design a function named 'string-first', which extracts the first character from a non-empty string. 
Don’t worry about empty strings
For example:
"dog" -> "d"
|#

Answer
(define PNAME 'string-first)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM: string-first
Design a function named 'string-first', which extracts the first character from a non-empty string. 
Don’t worry about empty strings
For example:
"dog" -> "d"
|#

; string-first: (String -> String)
; extracts the first character from a non-empty-string

(check-expect (string-first "boo") "b")
(check-expect (string-first "dog") "d")

#;
(define (string-first st)
  (... st))

(define (string-first st)
  (substring st 0 1))


Exercise 3.01-less-than-5
(define PNAME 'less-than-5?)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: less-than-5?
Design a function named 'less-than-5?' that determines if a strings length is less than 5
|#

Answer
(define PNAME 'less-than-5?)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM: less-than-5?
Design a function named 'less-than-5?' that determines if a strings length is less than 5
|#

; less-than-5: (String -> Boolean)
; produces true whether the length of the string is less than 5

(check-expect (less-than-5? "bob") #true)
(check-expect (less-than-5? "meow") #true)
(check-expect (less-than-5? "steam") #false)
(check-expect (less-than-5? "hungry") #false)

#;
(define (less-than-5? str)
    (... str))

(define (less-than-5? str)
    (< (string-length str) 5))


Exercise 3.01-pig-latin
(define PNAME 'pig-latin)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: 
Design a function named 'pig-latin', translates english to piglatin.

Pig latin is a dumb language to modify words to sound funny. There are many pig-latin rules but
for now assume it is just taking the first letter of the word and adding an "ay" at the end.
For example:
"dog" -> "ogday"
"happy" -> "appyhay"
Assume that the string is never empty
|#

Answer
(define PNAME 'pig-latin)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: 
Design a function named 'pig-latin', translates english to piglatin.

Pig latin is a dumb language to modify words to sound funny. There are many pig-latin rules but
for now assume it is just taking the first letter of the word and adding an "ay" at the end.
For example:
"dog" -> "ogday"
"happy" -> "appyhay"
Assume that the string is never empty
|#

; pig-latin: (String -> String)
; translates the given english word to pig latin
(check-expect (pig-latin "meow") "eowmay")
(check-expect (pig-latin "raw") "awray")

#;
(define (pig-latin str)
  (... str))

(define (pig-latin str)
  (string-append
   (substring str 1) 
   (substring str 0 1)
   "ay"))


Exercise 3.01-string-last
(define PNAME 'string-last)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: string-last
Design a function named 'string-last', which extracts the last character from a non-empty string.
Example:
"hey" -> "y"
|#

Answer
(define PNAME 'string-last)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM: string-last
Design a function 'string-last', which extracts the last character from a non-empty string.
Example:
"hey" -> "y"
|#

; string-last: (String -> String)
; extracts the last character from a non-empty string
(check-expect (string-last "hey") "y")
(check-expect (string-last "barn") "n")

#;
(define (string-last st)
  (... st))

(define (string-last st)
  (substring st (sub1 (string-length st))))


Exercise 3.02-string-rest
(define PNAME 'string-rest)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: string-rest
Design a function 'string-rest', which produces a string like the given one with the first character removed.
Example:
"room" -> "oom"
|#

Answer
(define PNAME 'string-rest)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM: string-rest
Design a function 'string-rest', which produces a string like the given one with the first character removed.
Example:
"room" -> "oom"
|#

; string-rest: (String -> String)
; produces the given string with the first character removed
(check-expect (string-rest "woah") "oah")
(check-expect (string-rest "omg") "mg")

#;
(define (string-rest st)
  (... st))

(define (string-rest st)
  (substring st 1 (string-length st)))


Exercise 3.03-string-remove-last
(define PNAME 'string-remove-last)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: string-remove-last
Design a function 'string-remove-last', which produces a string like the given one with the last 
character removed.
NOTE: In the case of the empty string, produce an empty string
"hello" -> "hell"
|#

Answer
(define PNAME 'string-remove-last)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM: string-remove-last
Design a function 'string-remove-last', which produces a string like the given one with the last 
character removed.
NOTE: In the case of the empty string, produce an empty string
"hello" -> "hell"
|#

; string-remove-last: (String -> String)
; produce a given string with the last character removed
(check-expect (string-remove-last "hello") "hell")
(check-expect (string-remove-last "cat") "ca")
(check-expect (string-remove-last "") "")

#;
(define (string-remove-last st)
  (... st))

(define (string-remove-last st)
  (if (zero? (string-length st))
      ""  
      (substring st 0 (sub1 (string-length st)))))


Exercise 3.04-formalize
(define PNAME 'formalize)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: formalize
We have provided helper functions: string-first, string-rest, from the previous problem, use them!

On smartphones, whenever you text someone it will auto-capitalize the first letter of a word, e.g:
"hey guys" -> "Hey guys"
"where u at?" -> "Where u at?"
"wotcha doin" -> "Wotcha doin"
Design a function named 'formalize' that will capitalize the first letter of a sentence. Assume that 
a string is not empty.
|#

; string-first: (String -> String)
; extracts the first character from a non-empty-string
(define (string-first st)
  (substring st 0 1))

; string-rest: (String -> String)
; produces the given string with the first character removed
(define (string-rest st)
  (substring st 1 (string-length st)))

Answer
(define PNAME 'formalize)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM: formalize
We have provided helper functions: string-first, string-rest, from the previous problem, use them!

On smartphones, whenever you text someone it will auto-capitalize the first letter of a word, e.g:
"hey guys" -> "Hey guys"
"where u at?" -> "Where u at?"
"wotcha doin" -> "Wotcha doin"
Design a function named 'formalize' that will capitalize the first letter of a sentence. Assume that 
a string is not empty.
|#

; string-first: (String -> String)
; extracts the first character from a non-empty-string
(define (string-first st)
  (substring st 0 1))

; string-rest: (String -> String)
; produces the given string with the first character removed
(define (string-rest st)
  (substring st 1 (string-length st)))

; formalize: (String -> String)
; produces a string capitalizing the first letter
(check-expect (formalize "hey guys") "Hey guys")
(check-expect (formalize "where u at?") "Where u at?")

; template 
; (define (formalize txt)
;    (... txt))

(define (formalize txt)
   (string-append (string-upcase (string-first txt)) (string-rest txt)))


Exercise 3.05-TLDR
(define PNAME 'tldr)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM:
Design a function named 'tldr' that consumes a string and a number that is the capacity. 
If the string is longer than the given capacity, produce only the string up to the given capacity 
appended by "..." , otherwise produce the string.
Example:
(tldr "dog" 3) -> "dog"
(tldr "hungry hippos" 3) -> "hun..."
|#

Answer
(define PNAME 'tldr)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM:
Design a function named 'tldr' that consumes a string and a number that is the capacity. 
If the string is longer than the given capacity, produce only the string up to the given capacity 
appended by "..." , otherwise produce the string.
Example:
(tldr "dog" 3) -> "dog"
(tldr "hungry hippos" 3) -> "hun..."
|#

; tldr: String Number -> String
; produces the string appended ... if greater than the length, otherwise produces the string
(check-expect (tldr "once upon a time" 4) "once...")
(check-expect (tldr "once" 4) "once")
(check-expect (tldr "super fantastic" 3) "sup...")

#;
(define (tldr str cap)
  (... str cap))

(define (tldr str cap)
  (if (> (string-length str) cap)
      (string-append (substring str 0 cap) "...")
      str))


Exercise 3.06-can-ride
(define PNAME 'can-ride)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM:
Design a function 'can-ride?' that consumes an age and whether or not an adult is with you.
You can only ride this rollercoaster if you are 12 or older or have an adult with you!
|#

Answer
(define PNAME 'can-ride)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM:
Design a function 'can-ride?' that consumes an age and whether or not an adult is with you.
You can only ride this rollercoaster if you are 12 or older or have an adult with you!
|#

; can-ride?: (Number Boolean) -> Boolean
; produces true if age is greater than 12 or if you have an adult with you

(check-expect (can-ride? 13 #true) #true)
(check-expect (can-ride? 13 #false) #true)
(check-expect (can-ride? 12 #true) #true)
(check-expect (can-ride? 12 #false) #true)
(check-expect (can-ride? 11 #false) #false)
(check-expect (can-ride? 11 #true) #true)

#;
(define (can-ride? age with-adult)
    (... age with-adult))

(define (can-ride? age with-adult)
    (or (>= age 12) with-adult))


Exercise 3.50-image-larger
(require 2htdp/image)
(define PNAME 'image-larger?)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: 
Design a function named 'image-larger?' that consumes two images and produces whether the first given 
image is larger than the second
|#

Answer
(require 2htdp/image)
(define PNAME 'image-larger?)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ✅
    2. Examples (aka check-expect, elaborate the concrete) ✅
    3. Template(aka Sketch/Outline) ✅
    4. Code body ✅
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ✅
|#

; ############################################################################

#| PROBLEM: 
Design a function named 'image-larger?' that consumes two images and produces whether the first given 
image is larger than the second
|#

; image-area: Image -> Number
; produces the area of the given image
(define (image-area img)
    (* (image-width img) (image-height img)))

; image-larger?: Image Image -> Boolean
; produces true if the area of the first image is bigger than the second image

(check-expect (image-larger? (rectangle 4 4 "solid" "red") (rectangle 4 4 "solid" "red")) #false)
(check-expect (image-larger? (rectangle 7 4 "solid" "red") (rectangle 4 4 "solid" "red")) #true)
(check-expect (image-larger? (rectangle 4 4 "solid" "red") (rectangle 7 4 "solid" "red")) #false)

#;
(define (image-larger? img1 img2)
    (... img1 img2))

; working solution but can do better!
#;
(define (image-larger? img1 img2)
    (> (* (image-width img1) (image-height img1)) (* (image-width img2) (image-height img2))))

; better solution, make a helper function 'image-area'
(define (image-larger? img1 img2)
    (> (image-area img1) (image-area img2)))


Exercise 3.51-distance
(define PNAME 'distance)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: 
Design a function named 'distance' that consumes 4 numbers: x1, y1, and x2, y2 and calculates
the distance between the two points using the distance formula as described here:
  __________________________
\/ (x2 - x1)^2 + (y2 - y1)^2

More background:
https://youtu.be/0IOEPcAHgi4
https://www.varsitytutors.com/hotmath/hotmath_help/topics/distance-formula
|#

; Number Number Number Number -> Number
; produces the distance between the two given points

; template
#;
(define (distance x1 y1 x2 y2)
    (... x1 y1 x2 y2))

Answer
(define PNAME 'distance)
#|
CHEATSHEET: https://docs.racket-lang.org/htdp-langs/beginner.html
TEMPLATES: https://howtocode.pages.dev/htdp_templates
ASK FOR HELP: https://discord.com/invite/6Zq8sH5
HTDF STEPS: Turn all ❌ into ✅ for each step you complete
    1. Signature, purpose, stub ❌
    2. Examples (aka check-expect, elaborate the concrete) ❌
    3. Template(aka Sketch/Outline) ❌
    4. Code body ❌
    5. Test, review, and refactor(review all steps, ctrl+i to auto-format) ❌
|#

; ############################################################################

#| PROBLEM: 
Design a function named 'distance' that consumes 4 numbers: x1, y1, and x2, y2 and calculates
the distance between the two points using the distance formula as described here:
  __________________________
\/ (x2 - x1)^2 + (y2 - y1)^2

More background:
https://youtu.be/0IOEPcAHgi4
https://www.varsitytutors.com/hotmath/hotmath_help/topics/distance-formula
|#

; Number Number Number Number -> Number
; produces the distance between the two given points

(check-expect (distance 3 0 0 4) 5)
(check-within (distance 0 1 1 0) (sqrt 2) .00001)

; template
#;
(define (distance x1 y1 x2 y2)
    (... x1 y1 x2 y2))

(define (distance x1 y1 x2 y2)
    (sqrt (+ (sqr (- x2 x1))
             (sqr (- y2 y1)))))