Lec 2: Buzz Project
This demonstrates a function with the correct definition of:
- 0 or more inputs
- No free variables
#lang racket
(require (planet dyoo/simply-scheme:2:2))
;Buzz game
; Rules
; Count upwards
; If your number is divisible by 7 you have to say buzz.
; If your number contains a 7 you have to say buzz.
; Buzz(1) = 1
; Buzz(7) = Buzz
; Buzz(8) = 8
; Buzz(17) = Buzz
; Test
(define (is_equal? n x)
(if (equal? n x)
(sentence n x '=> '✅)
(sentence n x '=> '❌)))
; Game function
(define (Buzz n)
(cond ((equal? (modulo n 7) 0) (word 'Buzz))
((member? 7 n) (word 'Buzz))
(else n)))
;Tests
(is_equal? (Buzz 1) 1)
(is_equal? (Buzz 2) 2)
(is_equal? (Buzz 3) 3)
(is_equal? (Buzz 4) 4)
(is_equal? (Buzz 5) 5)
(is_equal? (Buzz 6) 6)
(is_equal? (Buzz 7) 'Buzz)
(is_equal? (Buzz 8) 8)
(is_equal? (Buzz 9) 9)
(is_equal? (Buzz 10) 10)
(is_equal? (Buzz 11) 11)
(is_equal? (Buzz 12) 12)
(is_equal? (Buzz 13) 13)
(is_equal? (Buzz 14) 'Buzz)
(is_equal? (Buzz 15) 15)
(is_equal? (Buzz 16) 16)
(is_equal? (Buzz 17) 'Buzz)
(is_equal? (Buzz 18) 18)
(is_equal? (Buzz 71) 'Buzz)
I had initially only been checking the last digit of the input to see if it contains a 7. This of course only checks the last digit not the rest of them. So, '781' for instance, would fail.
Assuming my test cases are correct, this works.
'(1 1 => ✅)
'(2 2 => ✅)
'(3 3 => ✅)
'(4 4 => ✅)
'(5 5 => ✅)
'(6 6 => ✅)
'("Buzz" Buzz => ✅)
'(8 8 => ✅)
'(9 9 => ✅)
'(10 10 => ✅)
'(11 11 => ✅)
'(12 12 => ✅)
'(13 13 => ✅)
'("Buzz" Buzz => ✅)
'(15 15 => ✅)
'(16 16 => ✅)
'("Buzz" Buzz => ✅)
'(18 18 => ✅)
'("Buzz" Buzz => ✅)