Question

In: Computer Science

Use Racket ;The following two lines are required to test the code. (require rackunit) (require rackunit/text-ui)...

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)))

Solutions

Expert Solution

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:


Related Solutions

Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
Replace the lines marked with // *** with the appropriate C# code (may require more than...
Replace the lines marked with // *** with the appropriate C# code (may require more than one line of code to complete the missing parts). // Program Description: This program uses two user defined methods to compute // the gross pay and net pay for an employee after entering the employee’s // last name, hours worked, hourly rate, and percentage of tax. using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double...
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Use the preliminary test to decide whether the following series are divergent or require further testing....
Use the preliminary test to decide whether the following series are divergent or require further testing. Sum (3+2i)^n/n!
you can use either vim or nano as text editor Implement the following code in ARM...
you can use either vim or nano as text editor Implement the following code in ARM on Raspberry Pi, compile and run. g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note: answer should be calculated not hardcoded
In this assignment, you will use a LOC counting tool to count the lines of code...
In this assignment, you will use a LOC counting tool to count the lines of code of the tool itself. 1. There are several open source free tools for LOC counting such as CLOC and SLOC. 2. Run the tool to count the lines of code of the source files of the tool (usually in the src directory). 3. Submit a report which includes: a. Description of the tool 1. Name 2. Creator 3. Language(s) it is written 4. URL...
/*Design and code and test a class MaxMin that has the following properties * two integer...
/*Design and code and test a class MaxMin that has the following properties * two integer member variables, min and max where min is smaller or equal than max at all times * A default constructor that sets both min and max to 0 * A constructor that accepts one integer and sets both min and max to that integer * A constructor that accepts two integers and sets min the smallest and max the largest * * Setters and...
Use Java GUI create following: Task 1: A basic UI with a button and a TextField,...
Use Java GUI create following: Task 1: A basic UI with a button and a TextField, when you press the button, set the button text to the current text field contents. Task 2: set the text field text to the mouse coordinates when that same button is pushed.
The JAVA program should do the following: –Ask the user for how many lines of text...
The JAVA program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number supplied by the user, but...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT