Microsoft Word Practice Exercise Set 2

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Microsoft Word Practice Exercise Set 2 - Presentation Transcript

    1. Practice Exercise Set 2 Exercise 1 Given these definitions: (define x 1) (define (add-x y) (+ x y)) (define (dbl-x x) (+ x x)) (define (add-sum y) (let ((sum (add-x x))) (+ sum y))) (define (add-list y) (every add-x y)) For each of the following expressions, if the expression can be evaluated, write its value. If the expression cannot be evaluated, explain why not. 1 x 'x y 'y (x) '(x) ('x) (add-x 1) (add-x x) (add-x 'x) (dbl-x 1) (dbl-x x) TCS INTERNAL
    2. (dbl-x 2) (add-sum 1) (add-list (1 2 3)) (add-list '(1 2 3)) (add-x '(1 2 3)) (keep even? (add-list '(1 2 3))) Exercise 2 For each expression, write down its type. Give the most specific type you can: for the expression 8, number is a better answer than word. 1. x 2. (x) 3. '(x y z) 4. 8 5. + -- 6. (+ 9 10) 7. (lambda (x) (word 'super- x)) Exercise 3 For each expression, write down whether it can be evaluated or not (do not write down the value). 1. (+ 1 2) 2. (+ 1 'two) 3. (+ 1 (2)) 4. (word 1 2) 5. (word 'super 'stitious) 6. (word '(super stitious)) 7. (every 'super '(duper sonic)) TCS INTERNAL
    3. 8. (every (lambda (x) (word 'super- x)) '(duper sonic)) 9. (every '(lambda (x) (word 'super- x)) '(duper sonic)) Exercise 4 For all questions, assume the following definition has been executed: (define wd 'view) 1. (word 'pre wd) 1. What is the value of wd in this expression? 2. What is the value of this expression? 2. ((lambda (wd) (word 'pre wd)) 'fix) 1. What is the value of wd in this expression? 2. What is the value of this expression? 3. (every (lambda (wd) (word 'pre wd)) '(view fix tense)) 1. What are the values of wd in this expression? 2. What is the value of this expression? Exercise 5 For all questions, assume the following definitions have been executed: (define s '(a b c d)) (define l '((a b) c d)) ;; That's the letter l (small L), not the numeral 1 (one) Write down the value of each of these expressions: 1. s 2. (car s) 3. (cdr s) 4. (cons 'a '(b c d)) TCS INTERNAL
    4. 5. (cons (car s) (cdr s)) 6. (car (cdr s)) 7. (cadr s) 8. l ;; That's the letter l (small L), not the numeral 1 (one) 9. (car l) 10. (cdr l) 11. (cons '(a b) '(c d)) 12. (cons (car l) (cdr l)) 13. (list '(a b) '(c d)) 14. (append '(a b) '(c d)) Exercise 6 This definition is executed: ;; Like Scheme reverse (define (rev lst) (if (null? lst) '() (append (rev (cdr lst)) (list (car lst))))) Then this expression is typed into the Scheme interpreter and evaluated. It returns the result shown: > (rev '(x y z)) The following questions all concern what happens when the expression (rev '(x y z)) is evaluated. It may be helpful to imagine what you would see if you traced the execution of rev and append. TCS INTERNAL
    5. 1. How many times is rev called? 2. What is lst bound to when rev is called the first time? 3. What is the value of (cdr lst) when rev is called the first time? 4. What is the value of (rev (cdr lst)) when rev is called the first time? 5. What is the value of (car lst) when rev is called the first time? 6. What is the value of (list (car lst)) when rev is called the first time? 7. What is the value of (append (rev (cdr lst)) (list (car lst))))) when rev is called the first time? Exercise 7 1. This table shows the number of electoral votes allocated to several states: Washington 11 Oregon 7 Iowa 7 California 54 Florida 25 Define a Scheme variable named electoral-votes to encode this table. Represent the table by a list of sublists, one for each state, where the car of each sublist is the name of the state and the cdr of each sublist is a list whose only element is the number of electoral votes for that state. TCS INTERNAL
    6. Exercise 8 This tree represents the geographic relation of some states, counties and cities in the USA: +------------------ USA -------------+ | | +----- Washington -----+ Florida | | | +----- King ----+ +- Thurston -+ +- Dade -+ | | | | | | Seattle Bellevue Olympia Lacey Miami Hialeah Define a Scheme variable named usa-tree to encode this tree. Represent the tree as a list whose car is USA and whose cdr is a list of subtrees, one for each of the two states. Continue filling in subtrees until you reach the cities. At each level, the car of the subtree holds the data and the cdr of the subtree is a list of subtrees. At each city, the cdr of the sublist is the empty list to indicate that there are no more subtrees. Exercise 9 This diagram represents the geographic relation of some cities in the Puget Sound region of Washington state. A line (possibly a crooked line) connecting two cities means you can reach one from the other without passing through any of the other cities. Bremerton ------------- Seattle | | | | | | | | Gig Harbor -------------Tacoma | | | | | | +------ Olympia ------+ Define a Scheme variable named puget-routes to encode this diagram. Represent city names by (double-quoted) strings so we can easily represent cities whose names have more than one word: \"Seattle\", \"Gig Harbor\" etc. Represent the diagram as a list of lists. The car of each list is the name of a city and the cdr of each list is a list of all the cities that can be reached from the first city. Exercise 10 Now use your reverse function to write a new function that not only reverses a list, but reverses each list element in the list as well. Call the function total-reverse. It will reverse not only the contents of a list but recursively reverse every sublist within the list. For example TCS INTERNAL
    7. > (total-reverse (list 1 2 3)) (3 2 1) > (total-reverse (list 1 (list 2 3))) ((3 2) 1) > (total-reverse (list 1 (list (list 2 3) 4))) ((4 (3 2)) 1) Exercise 11 Define a function third to return the third element in a list (e.g., (third '(2 3 4)) should return 4, (third '(4 5 6 7)) should return 6). Exercise 12 Define a (recursive) function last to return the last value of a list (e.g., (last '(2 3)) should return 3, (last '(4 5 6)) should return 6). You will find the null? function useful for detecting the end of the list. Exercise 13 Write the factorial function so that (list-factorial x) will return a list of the factorials of all the numbers between 1 and x (e.g., (list-factorial 4) should return (1 2 6 24)). Exercise 14 Write a Scheme function last-sublist to build a list of the last element in every sublist in a list. So (last-sublist '((3 2) (4 5)) would return (2 5). Perhaps this function could use the last function you defined earlier? Exercise 15 Write a scheme function that returns the nth element in a list. Exercise 16 Write a scheme function that determines if two lists are equivalent. Exercise 17 Write a scheme function that appends list2 to the end of list1. Exercise 18 Write a program that reads in three real numbers. Determine computationally which of the following cases are true: the three numbers (a) Do not represent a triangle. (b) Represent an equilateral triangle. TCS INTERNAL
    8. (c) Represent an isosceles triangle. (d) Represent a right triangle. (e) Represent a triangle that is not one of the three immediately above cases. Exercise 19 Create a structure to define Point. We want to read a set of (x, y) coordinates and a name, then sort them in order increasing distance from the origin (0, 0). Use Point. Exercise 20 Provide a structure definition and a data definition for a paycheck with check number, name, date, and amount properties. Develop a function to produce a paycheck given the following information: check number, name, date, hours worked, wage, and percent tax reported as a decimal. The amount of the check is calculated by multiplying the hours worked by the wage less any taxes Exercise 21 Provide a structure definition and a data definition for an online auction entry, which is characterized by four pieces of information: the item number, the highest bidder, the current bid, and 'Open or 'Closed. Develop a function that consumes a bidder, a bid amount, and an auction entry then returns a new entry. If the bid amount is less than the current bid or if the auction is closed, then the original entry is returned. Exercise 22 Develop the function withdrawal, which consumes an account and an amount and produces a response. In the case in which a withdrawal would cause the balance of a savings account to go below zero, 'Error would be produced. Otherwise a new savings account would be produced with a new balance and the number of transactions incremented by one. A checking account has overdraft protection up to Rs.20000, so a withdrawal would signal 'Error if the balance were lower than Rs.20000. A credit account's balance increases upon withdrawal, but cannot go beyond the spending limit. Exercise 23 Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator, that prints out the decimal representation. If the decimal representation has a repeating sequence of digits, it should be indicated by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as .(3), and 41/333 = .123123123...is denoted as .(123). Exercise 24 Consider the set of all reduced fractions (rational numbers) between 0 and 1 inclusive with denominators less than or equal to N. Here is the set when N = 5: TCS INTERNAL
    9. 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 Write a program that, given an integer N between 1 and 100 inclusive, prints the fractions in order of increasing magnitude. You should also print the total number of fractions. Exercise 25 In order to increase security the network manager has introduced some new rules concerning passwords: 1. Passwords must consist of a mixture of lowercase letters and numerical digits only, with at least one of each. 2. Passwords must be between 5 and 12 characters in length. 3. Passwords must not contain any sequence of characters immediately followed by the same sequence. Write a program that inputs a password and then prints REJECTED if the password is rejected, or ACCEPTED otherwise. Exercise 26 Write a program that inputs an integer n and ouputs each of the integers from 1 to n in random order. Each integer should appear once and once only. Exercise 27 Produce a table of function values for the below: x3 + x2 + 37x + 35 in the interval [-5; 5] in steps of 0.1. Exercise 28 Write a program that inputs two fractions in the form a/b and c/d, and outputs their sum in the form p/q cancelled down to its simplest form. Exercise 29 A sound processor filters out frequencies below a low frequency threshold and above a high frequency threshold. Such a processor might be used as part of an MP3 encoder to reduce the amount of data to be processed. Given that a frequency is a number, develop the function, filter-freq, which consumes two threshold frequencies (low and high) and a list of frequencies and returns a list of those frequencies between and including the thresholds. TCS INTERNAL
    10. Exercise 30 Develop data and structure definitions for trains. A train is one of commuter, which has the properties: number of cars, number of passengers per car, and a boolean determing whether this particular train makes all stops; amtrak, which has the properties: number of cars, number of passengers per car, and a symbol designating the type of train as 'Express, 'Local, or 'Limited; subway, which has the properties: number of cars, number of passengers per car, and a symbol representing the color of the train. Develop the function hold-all? that, given a train and a number of passengers, produces true if the train could contain them all and false if not. TCS INTERNAL

    + rampanrampan, 2 years ago

    custom

    4160 views, 1 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 4160
      • 4160 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 64
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories