;;Practice exam: CMPU 101 Exam 2 Practice exam, Spring 2013 ; ; Problem 1: ; ; Write a function PR-SQR that takes a string (str) and a number ; (n) as input and prints a n x n square of the str. This ; function should not return anything, it should just have side- ; effect printing. Examples of running the function are given ; below: ; ; > (pr-sqr "X" 4) ; XXXX ; XXXX ; XXXX ; XXXX ; ; > (pr-sqr "X " 5) ; X X X X X ; X X X X X ; X X X X X ; X X X X X ; X X X X X ; ; Hint: You may want to break the function down into a main ; and a helper function as you did in the printURtriangle ; function in lab 5. ; ; ; Problem 2: ; ; (a) The sum of the squares of the natural numbers from 0 to n is ; given as ; ; 1^2 + 2^2 + 3^2 +...+ n^2 ; ; Write a recursive function that takes a natural number n and ; computes the sum of the squares up to n. ; ; (b) Write the same function as in part (a), but use an accumula- ; tor. ; ; (c) Write another version of the function that uses higher ; order functions instead of explicit recursion. ; ; (d) The value computed in part (a) is equal to the following: ; ; (n(n+1)(2n+1))/6 ; ; Write a function that computes the equation given above and ; use check-expect statements to show the equality of the ; expressions from parts (a) and (b). ; ; ; Problem 3: ; ; Write a predicate function called SMALL-VALUE? that takes in a ; data item (any valid Racket entity) as input and that returns ; #t if the input is a Boolean value or a number between 0 and ; 20 (including 0 and 20). For full credit, your solution ; should not use an if or a cond. If you have trouble writing ; a solution with no decision statement, try writing the ; function with a decision statement first and it might give ; you some insight into writing it without if or cond. ; ; > (small-value? 7) ; #t ; ; > (small-value? #f) ; #t ; ; > (small-value? 'foo) ; #f ; ; ; Problem 4: ; ; Write a function called KEEP-SYMBOLS that takes a list of any ; length and containing any valid Racket types and returns a ; list containing only the quoted symbols found in the input ; list. ; ; > (keep-symbols '(a b c)) ; (list 'a 'b 'c) ; ; > (keep-symbols empty) ; empty ; ; > (keep-symbols '(3 4 6 a)) ; (list 'a) ; ; Write this function using both a higher-order function and ; using explicit recursion. ; ; ; Problem 5: ; ; (a) Write a function called NAME-TO-DIGIT that consumes a quoted ; symbol and, if the input symbol is the name of a digit ('zero ; through 'nine, in lowercase letters), the corresponding digit ; is returned. Otherwise, the input value is returned ; unchanged. ; ; > (name-to-digit 'zero) ; 0 ; ; > (name-to-digit 'eighty-two) ; eighty-two ; ; > (name-to-digit 'four) ; 4 ; ; > (name-to-digit 'SIX) ; SIX ; ; ; (b) Write a function called SUM-ALL that takes a list as its ; input. This function computes the sum of the values ; specified in the list, and your solution should use the ; NAME-TO-DIGIT function from part (a). The list given as ; input can contain any valid Racket entities, but should ; only sum the items that are either numbers or quoted ; symbols representing digits as specified in part (a). ; ; > (sum-all '(1 2 3 4)) ; 10 ; ; > (sum-all (list 'three 'two 'six 9)) ; 20 ; ; > (sum-all (list "three" "two" 'seven)) ; 7 ; ; > (sum-all empty) ; 0 ; ; ; Problem 6: ; ; Write down the value of each of the following Racket ; expressions, assuming that each expression is evaluated after ; the definition of the constant FOOD-GROUPS, shown below. If ; the evaluation of an expression results in an error, give a ; brief description of the cause of the error: ; ; (define FOOD-GROUPS '(sugar fat coffee chocolate)) ; ; a) (cons (first (rest FOOD-GROUPS)) ; (list (first (rest (rest FOOD-GROUPS))))) ; ; b) (first (rest (rest (rest FOOD-GROUPS)))) ; ; c) (first (rest (rest (rest (rest FOOD-GROUPS))))) ; ; d) (append (list (first (rest (rest FOOD-GROUPS)))) '(salt)) ; ; e) (cons (first FOOD-GROUPS) (cons (first (rest FOOD-GROUPS)) ; (cons 'oreos (rest (rest FOOD-GROUPS))))) ; ; f) (rest (first FOOD-GROUPS)) ; ; ; Problem 7: ; ; (a) Write a string-reverse function using explicit recursion over the ; input string. ; ; Do not use any list functions in your solution. ; ; (b) Write a string-reverse function that uses list functions to ; reverse the input string. ; ;