In: Computer Science
Use Racket
;The following two lines are required to test the code.
(require rackunit)
(require rackunit/text-ui)
Important Rules:
1.) You may not use loop constructs like while/for/sum. If used,
your answer will get a zero.
2.) If the instructions state something must be recursive, you will
recieve a zero if it is not recursive.
Recursive helper functions are allowed (the main function not being
recursive).
3.) You may not use the set! command. If used, your answer will get
a zero.
4.) Using If/Cond to explicitly pass tests instead of following the
instructions
will always result in a zero for that question.
5.) Use #t and #f instead of true or false. Racket treats them
differently
in some special cases.
;Question 13
; Write a recursive function exactly_one_q to check if a list of
symbols
; contains exactly one q symbol.
; Don't forget the base case and the necessary recursion.
; You may use any previously written function.
;Hint: The answer to question 11 is helpful to use here.
; Check if a list contains exactly one q
; Input: L is a list of symbols (a,b,c,...,z).
; Output: a boolean value which is true when exactly one of the
elements
; in L is equal to q and false otherwise.
; The empty list should return false.
(define (exactly_one_q L)
0;Complete this function definition.
)
(define-test-suite test_exactly_one_q
(check-equal? (exactly_one_q '(q)) #t)
(check-equal? (exactly_one_q '(x)) #f)
(check-equal? (exactly_one_q '(z r)) #f)
(check-equal? (exactly_one_q '(q d)) #t)
(check-equal? (exactly_one_q '(q q)) #f)
(check-equal? (exactly_one_q '(d e p)) #f)
(check-equal? (exactly_one_q '(q b q)) #f)
(check-equal? (exactly_one_q '(q q q)) #f)
(check-equal? (exactly_one_q '(q n q q)) #f)
(check-equal? (exactly_one_q '(m n m q)) #t)
)
(display "Question 13 exactly_one_q (10 points)")
(newline)
(define q13_score (- 10 (run-tests test_exactly_one_q
'verbose)))
Working code implemented in Dr.Racket and appropriate comments provided for better understanding.
Source Code for Question 13:
;Question 13
; Write a recursive function exactly_one_q to check if a list of
symbols
; contains exactly one q symbol.
; Don't forget the base case and the necessary recursion.
; You may use any previously written function.
;Hint: The answer to question 11 is helpful to use here.
; Check if a list contains exactly one q
; Input: L is a list of symbols (a,b,c,...,z).
; Output: a boolean value which is true when exactly one of the
elements
; in L is equal to q and false otherwise.
; The empty list should return false.
(define (exactly_one_q L)
(cond
[(null? L) #f]
[(equal? (first L) 'q) (noti (at_least_one_q (rest L)))]
[else (exactly_one_q (rest L))]
))
(define-test-suite test_exactly_one_q
(check-equal? (exactly_one_q '(q)) #t)
(check-equal? (exactly_one_q '(x)) #f)
(check-equal? (exactly_one_q '(z r)) #f)
(check-equal? (exactly_one_q '(q d)) #t)
(check-equal? (exactly_one_q '(q q)) #f)
(check-equal? (exactly_one_q '(d e p)) #f)
(check-equal? (exactly_one_q '(q b q)) #f)
(check-equal? (exactly_one_q '(q q q)) #f)
(check-equal? (exactly_one_q '(q n q q)) #f)
(check-equal? (exactly_one_q '(m n m q)) #t)
)
(display "Question 13 exactly_one_q (10 points)")
(newline)
(define q13_score (- 10 (run-tests test_exactly_one_q
'verbose)))
Code Screenshots: