Homework Server Assignments

All of the assignments in this section should be submitted on the homework server in the appropriate slot UNLESS OTHERWISE NOTED.

HW N-1 (netlogo assignment 1)

posted Nov 16, 2012, 6:25 AM by Samuel Konstantinovich   [ updated Nov 26, 2012, 5:59 AM ]

(Due 11/19/2012) PRINTED.
Weekend Assignment: (Print the 'Bolded Questions' + Answers, and bring to class on Monday)
  1. Watch the video tutorials: (You already watched the "Introduction to Netlogo" video)
    • "Turtles Patches and Links" 
    • "11. Netlogo Docs-1"
  2. In netlogo, go to "HELP" -> "Netlogo User Manual". That will open a website with the user manual. Under the left menu there is a header "Introduction" that has a link to "Sample Model: Party". Click on it. 
  3. Do the Sample Model Party Activity. Then:
    • Answer all the BOLDED QUESTIONS before the "challenge" section. This is what you are bringing to class on Monday
    • Optionally: do the challenge questions.

*Challenge problems* Base conversion

posted Nov 1, 2012, 8:34 AM by Samuel Konstantinovich   [ updated Nov 16, 2012, 6:26 AM ]

;This page is under construction.

We write numbers in decimal (also known as base 10). Computers only store/process data using binary numbers (base 2). There are many other useful number bases that we use every day, base 60 is used in clocks, base 16 is used by many artists when naming colors on a computer. You should learn more about these bases, but the topic has been eliminated from school entirely. 

These problems require that you learn to convert numbers into different bases. You must understand the algorithms before you start writing the code. I don't expect you to come up with the algorithm on your own, but I do expect you to implement it yourself. You should wiki/google number base conversion to get an idea of how it works before attempting the examples. 

;;;PART A, basic conversion

1. Write a function binary to decimal:
;(btod x)    
;precondition:
;x must be an integer that consists of 1's and 0's
;that represents a binary number
;postcondition
;the value returned is the decimal equivalent of the 
;binary number x
(btod 1000) is 8
(btod 1001) is 9
(btod 10010) is 18
(btod 11110) is 30

2. Write a function to convert decimal to binary:
;(dtob x)
;precondition:
;x is any integer
;postcondition:
;the value returned is an integer that consits of 0's and 1's
;that represents a binary number equivalent to x
(dtob 10) is 1010
(dtob 8) is 1000
(dtob 13) is 1101
(dtob 50) is 110010

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;PART B, any base conversion
;You can easily convert your functions to work with any base 10 or lower:

3. Write a function base N to decimal
;(ntod x n)
;precondition:
;x must be an integer that consists of digits that are strictly less than n.
;the digits of x represent a base n number
;n <= 10
;postcondition
;the value returned is the decimal equivalent of the number x

base 3 examples:
(ntod 100 3) is 9
(ntod 101 3) is 10
(ntod 110 3) is 12

base 4 examples:
(ntod 100 4) is 16
(ntod 101 4) is 17
(ntod 212 4) is 38

4. Write a function decimal to base N
;(dton x n)
;precondition:
;x is any integer
;postcondition:
;the value returned is an integer that consits of digits strictly less than n
;that represents a base n equivalent to x

base 3 examples:
(dton 9 3) is 100
(dton 10 3) is 101
(dton 12 3) is 110

base 4 examples
(dton 16 4) is 100
(dton 17 4) is 101
(dton 36 4) is 210

5. Write a function that converts from base N to base M:
(ntom x n m)
example:
(ntom 1000 2 3)  is 22.
This means take the binary number 1000, and convert it to base 3. 
We need to use decimal as a middle step: 1000 is 8 in decimal, 8 in decimal is 22 in base 3.

How can you test this function? 
Come up with test cases that you know the answer to!


;;;PART C, advanced conversion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;How do we handle number bases higher than 10?!?!?
;We will use lists instead of integers to store the digits:
;in base 16, 13AF is a number, A stands for 10, F stands for 15... but we cannot write
;that way in an integer, instead we will write it as: '( 1 3 10 15)  

6. Improve your dton to give a list of digits as follows:
(dToAnyN x n)
;precondition:
;x must be an a list of numbers each strictly less than 10.
;postcondition
;the value returned is the base N equivalent of the number stored in list x.
(dToAnyN 13 2) -> ( 1  1  0  1)
(dToAnyN 36 4) -> ( 2  1  0 )
;now hex examples:
(dToAnyN 15 16) -> ( 15 )  
(dToAnyN 16 16) -> ( 1  0 )
(dToAnyN 33 16) -> ( 2  1 )
(dToAnyN 63 15) -> ( 3 15 )


7. improve your ntod to take a list of digits as follows:
(anyNtoD x n)
;precondition:
;x must be a list of integers each strictly less than n
;postcondition
;the value returned is the decimal equivalent of the number stored in list x. 
(anyNtoD '(1 1 0 1) 2) -> 13
(anyNtoD '(1 1 0 1) 3) -> 37 
(anyNtoD '(3 15) 16)  -> 63





HW6 + Solutions

posted Oct 29, 2012, 9:23 AM by Samuel Konstantinovich   [ updated Nov 26, 2012, 5:57 AM ]

THE HW SERVER IS NOT RUNNING. DO NOT EMAIL THIS TO ME! 

This will be due on the homework server  MONDAY NOVEMBER 5th 11:59pm
You must submit it on time, and in an RKT or SCM file. If you submit anything else you will receive 0 points.  If you submit it late you will lose points. 

WARNING: (There is no slot on the homework server yet, because it seems to be down. Don't worry if you can't reach it for now.)

I will be traveling out of state the weekend that this is due, and will not be able to answer questions.
If you would like help, please work on this early. 

SAMPLE SOLUTIONS FOR THE WEEKEND HW:
;S1. Count the number of positive integers in a list (including it's sublists)
;    (countPositive '( (2 -3) -4 (3 (-1) 0 2) ) ) -> 3    
(define (countPositive L)
  (cond
    ((null? L) 0)
    ;This next part is new. Treat the car like you would treat the cdr, THEN join the two halves of the solution
    ((list? (car L)) (+(countPositive (car L))(countPositive (cdr L))))  
    ((> (car L)0) (+ 1 (countPositive (cdr L))))
    (else (countPositive(cdr L)))))
;Notice that this solution is like countPositive from before, except the extra case when the car is a list

;S2. Reverse a list, including sublists 
;   (myReverse '( a b c ) )   -> (c b a)
;   (myReverse '( 1 2 (3 4) (5 6))) -> ((6 5)(4 3) 2 1)
(define (MyReverse L)
  (cond
    ((null? L) L)
    ;Since the car is a list, you want it inside a set
    ;of parenthesis... so we use the list command before appending. 
    ((list? (car L)) (append (MyReverse (cdr L))(list(MyReverse (car L)))))
    (else (append (MyReverse(cdr L)) (list (car L))))))
;Notice that this solution is like my reverse from before, except the extra case when the car is a list


;Write the following functions
;6.1 
;Count the number of sublists inside a list including sublists.
;    (countLists '( (2 3) (3 (5) 2) )  -> 3    
;    Think how many open parentheis are in the list...
;
;6.2
; Remove the 0's from a list of integers which may have sublists.
; H
int, you need to know the different uses of cons vs append. 
;    (stripZeros '( 0 2 (3 0 (0 ) ) 5))   -> ( 2 ( 3 () ) 5)
;
;6.3
;Add 1 to each of the values in a list of integers which may have sublists.
;    (addOne '( 9 9 (4 3) (2 (1) ) ) )   -> (10 10  (5 4) (3 ( 2 ) ) ) 
;
;6.4
;(contains L x) : Contains is true when x is found inside of L
;L is a list of integers WITHOUT sublists, x is an integer 
;Examples:
;(contains '( 1 2 3) 4)  is #f
;(contains '( 1 2 3) 2)  is #t

;6.5  
;(containsSub L x) : Contains is true when x is found inside of L or any of its sublists.
;L is a list of integers that CAN have sublists, x is an integer or list of integers
;Hint DO NOT USE (= a b) use (equal? a b) to compare elements, because you need to use equal? when comparing lists. 
;You will need to use OR
;Examples:
;(containsSub '( 1 2 3) 4)  is #f
;(containsSub '( 1 2 3) 2)  is #t
;(containsSub '( 1 (2) 3) '(1) )  is #f 
;(containsSub '( 1 (2) 3)  2 )  is #t
;(containsSub '( 1 (2 3) (4 5) )  '(3 4) )  is #f
;(containsSub '( 1 (2 3) (4 5) )  '(2 3) )  is #t
;(containsSub '( 1 (2 3) (4 5 6) )  '(4 5) )  is #f




Solutions:

;6.1
(define (countLists L)
  (cond
    ((null? L) 0)
    ((list? (car L)) (+ 1 (countLists (car L))(countLists (cdr L))))
    (else (countLists (cdr L)))))

;6.2
(define (stripZeros L)
  (cond
    ((null? L) L)
    ((list? (car L)) (cons (stripZeros (car L))(stripZeros (cdr L))))
    ((= (car L) 0) (stripZeros (cdr L)))
    (else (cons (car L) (stripZeros (cdr L))))))

;6.3
(define (addOne L)
  (cond
    ((null? L) L)
    ((list? (car L)) (cons (addOne (car L))(addOne (cdr L))))
    (else (cons (+ 1 (car L)) (addOne (cdr L))))))

;6.4
(define (contains L x) 
  (cond
    ((null? L) #f)
    ((= (car L) x) #t)
    (else (contains (cdr L ) x))))

;6.5
(define (containsSub L x) 
  (cond
    ((null? L) #f)
;this is the tricky part... the car could match x, or the car could have x inside it, or the cdr could have x inside it:
    ((list? (car L)) (or (equal? (car L) x) (containsSub (car L ) x) (containsSub (cdr L) x)))
    ((equal? (car L) x) #t)
    (else (containsSub (cdr L ) x))))
     

HW5.5 + Solutions

posted Oct 24, 2012, 5:54 AM by Samuel Konstantinovich   [ updated Oct 28, 2012, 6:20 AM ]

DO NOT SUBMIT ON HW SERVER 

Print AND STAPLE the following 8 Functions from your labs/practice problems:
BRING TO CLASS ON 10/25/2012

Practice Problems (from the 10/22, 10/23 lecture notes)
1. (countOdd L)
2. (create n)
3. (double L)
LAB4
4. (hailstone n)
5. (hailLength n)
LAB5
6. (getNthPlace L n)
7. (myLength L)
8. (countZeros L)

a. DO NOT PRINT : Things I wrote such as Questions or comments
b. You MAY print your own comments  
c. As per my HW policy : Your name must be printed on it.
d. It should be stapled if it is multiple pages!

DO NOT STAPLE IN CLASS. YOU WILL NOT HAVE TIME TO BORROW A STAPLER. (Because you will be taking a quiz on lists)
Solutions:
(define (countOdd L)
  (cond
    ((null? L) 0)
    ((odd? (car L)) (+ 1 (countOdd (cdr L))))
    (else           (countOdd (cdr L)))))
;OR
(define (countOdd L)
  (cond
    ((null? L) 0)
    (else (+ (if(odd? (car L))1 0) (countOdd (cdr L))))))

;Two different ways of writing create
(define (create N)
  (if (= N 0)
      '()
      (cons (* N N) (create (- N 1)))))

(define (createForwards N)
  (if (= N 0)
      '()
      (append (createForwards (- N 1))(list (* N N)))))


(define (double Q)
  (cond
    ((null? Q) '())
    (else (cons (* 2 (car Q))(double (cdr Q))))))


(define (hailstone n)
  (cond
    ((= n 1) (display 1))
    ((even? n) (begin
                 (display n)
                 (display ",")
                 (hailstone (/ n 2))))
    (else (begin
            (display n)
            (display ",")
            (hailstone (+ 1 (* 3 n)))))))

(define (hailLength n)
  (cond
    ((= n 1) 1)
    ((even? n) (+ 1 (hailLength (/ n 2))))
    (else      (+ 1 (hailLength (+ 1 (* 3 n)))))))

;Answer to one of the challenge problems:
;notice how similar the solution is to hailLength
(define (maxHail n)
  (cond
    ((= n 1) 1)
    ((even? n) (max n (maxHail (/ n 2))))
    (else      (max n (maxHail (+ 1 (* 3 n)))))))

(define (getNth L n)
  (cond 
    ((null? L) "error, I didn't say what to do when you reach the end of a list")
    ((= n 0) (car L))
    (else (getNth (cdr L) (- n 1)))))

(define (myLength L)
  (cond 
    ((null? L) 0)
    (else (+ 1 (myLength (cdr L))))))


(define (countZeros L)
  (cond 
    ((null? L) 0)
    ((= 0 (car L))  (+ 1 (countZeros (cdr L))))
    (else  (countZeros (cdr L)))))



HW5 + Solutions

posted Oct 2, 2012, 11:26 AM by Samuel Konstantinovich   [ updated Oct 8, 2012, 10:29 PM ]

Due Friday October 5th 11:59 pm on the homework server.

1.Boolean functions: (do not use if/cond and have it return #t and #f. Instead write a boolean expression for the function!)
1a. (xor a b) is true when a and b have different truth values, it is false when a and b are the same. 
Truth table for all possible combinations:
(xor #t #t) is #f
(xor #t #f) is #t
(xor #f #t) is #t
(xor #f #f) is #f

1b. (leapyear year) returns #t when a year is a leap year, #f when it is not. The rules of leap years are:
years that are multiples of 4 are leap years, except that every year that is a multiple of 100 is not a leap year , unless it is also a multiple of 400 then it is.
outputs:
(leapyear 2000) #t
(leapyear 1900) #f
(leapyear 1904) #t 
(leapyear 2103) #f
(leapyear 2104) #t

2. Write a military time calculator: (militaryTime hour ampm) where hour is an integer from 1 to 12 inclusive, and ampm is either "AM" or "PM". Use (equal? x y) to compare non numerical things to see if they are the same, (equal? word "AMBER") will be true when the variable word stores the string "AMBER" false otherwise.  (Look at a military time wiki if you aren't sure about the rules)

outputs: 
(militaryTime 12 "PM") returns 12 
(militaryTime 8 "PM") returns 20 
(militaryTime 5 "AM") returns 5
(militaryTime 12 "AM") returns 0 
 
3. Write a scheme function sumFromAtoB that takes two integers a and b, assume that b is larger than a. The function calculates the sum of the integers from a to be inclusive.

outputs:
 (sumFromAtoB 10 12) returns 33
 (sumFromAtoB 100 100) returns 100
 (sumFromAtoB 1 9) returns 45

4. Write a scheme function (sumOfPrimeDigits x) that looks at all of the digits of an integer x, and adds up the prime ones. A helper function would be useful to make your code cleaner by checking if the a one digit number is prime. (You need the lesson on Wednesday  for this)
(sumOfPrimeDigits 123) returns 5.
(sumOfPrimeDigits 223) returns 7.
(sumOfPrimeDigits 12345) returns 10.
(sumOfPrimeDigits 98762) returns 9.


;SOLUTIONS HW5:
;1a (xor a b)

;a or b, but NOT both...
(define (xor a b) 
  (and (or a b) (not (and a b))))

;using equals, they can't be the same:
(define (xor a b)
  (not (equal? a b)))

;1b

(define (leapyear y)
  (and 
   (= 0 (remainder y 4))    ;it is divisible by 4 AND...
   (or                      ;not divisible by 100 unless it is also divisible by 400
       (not (= 0 (remainder y 100))) 
       (= 0 (remainder y 400)))))

;2. 
;using remainders:
(define (militaryTime t ampm)
  (if (equal? ampm "AM")
      (remainder t 12)
      (+ 12 (remainder t 12))))

;using a cond
(define (militaryTime t ampm)
  (cond
    ( (and (= t 12) (equal? ampm "AM")) 0) ;12am is special
    ( (= t 12) 12) ;12pm is special
    ( (equal? ampm "AM") t) ;normal AM
    ( else (+ t 12))));normal PM

;3
(define (sumfromAtoB a b)
  (if (> a b)
      0
      (+ a (sumfromAtoB (+ a 1) b))))

;4
;make a function for single digit primes:

;1st way:
(define (isPrime? x)
  (or (= x 2)(= x 3)(= x 5) (= x 7)))

(define (sumOfPrimeDigits n)
  (cond
    ( (and (< n 10) (isPrime? n)) n)
    ( (< n 10) 0)
    ( (isPrime? (remainder n 10))  (+ (sumOfPrimeDigits (quotient n 10))(remainder n 10) ))
    ( else (sumOfPrimeDigits (quotient n 10)))))


;2nd way
(define (isPrime? x)
  (or (= x 2)(= x 3)(= x 5) (= x 7)))

(define (valueOfDigit x)
  (if (isPrime? x)
      x
      0))

(define (sumOfPrimeDigits n)
  (if
    (< n 10) 
    (valueOfDigit n)
    (+ (sumOfPrimeDigits (quotient n 10)) (valueOfDigit (remainder n 10)) )))
    

;Tests:
(display "xor\n")
(display "#f=")(xor #t #t)
(display "#f=")(xor #f #f)
(display "#t=")(xor #t #f)
(display "#t=")(xor #f #t)
(display "leapyear\n")
(display "#t=")(leapyear 2000)
(display "#t=")(leapyear 2004)
(display "#t=")(leapyear 2400)
(display "#f=")(leapyear 2500)
(display "#f=")(leapyear 2405)
(display "militaryTime\n")
(display "0=")(militaryTime 12 "AM")
(display "1=")(militaryTime 1 "AM")
(display "12=")(militaryTime 12 "PM")
(display "13=")(militaryTime 1 "PM")
(display "sum\n")
(display "15=")(sumfromAtoB 1 5)
(display "5=")(sumfromAtoB 5 5)
(display "45=")(sumfromAtoB 5 10)
(display "Sum\n")
(display "10=")(sumOfPrimeDigits 12345)
(display "8=")(sumOfPrimeDigits 11345)
(display "17=")(sumOfPrimeDigits 9123456789)

HW4 + Solutions

posted Sep 19, 2012, 6:25 PM by Samuel Konstantinovich   [ updated Oct 8, 2012, 10:30 PM ]

Announcements:
  • The homework server will be updated with students that were moved around or added this weekend. 
  • You have an exam on Friday September 28th.

Assignment Deadline Tuesday 11:59pm on the homework server.
Homework Assignment 4: Use Lambda notation for this assignment.

1. (EDIT: Changed question) 
Create a (myQuotient a b) function that does the same thing as  (quotient a b). You may use any tools EXCEPT for the existing quotient function. 

2. Write a function called isTheNthPlaceEven? that accepts two parameters x and column. Assume x is an integer, and column is an integer that must be a positive power of ten. The function returns true when that column (10, 100, 1000s place etc) of the number is even, false otherwise. 
examples:
(isTheNthPlaceEven? 30325, 100)  evaluates to #f
(isTheNthPlaceEven? 30325, 10)  evaluates to #t
(isTheNthPlaceEven? 66616, 10)  evaluates to #f

3. Write a function called gravity that calculates the gravitational force between two objects.
(gravity m1 m2 D)
Examples: (EDIT: fixed rounding)
(gravity 100000 50000 2) evaluates to 0.0834125
(gravity 25000 95000 3) evaluates to 0.017609305555555555

Follow the formula:
       G m1m2
F = ______
            D2
Where:

    G = gravitational constant 6.673×10 ^ -11 

    m1 = mass of the first object

    m2 = mass of the second object

    D = the distance between the two masses



;Sample solutions

;1 Define a quotient function without using quotient

(define myQuotient (lambda (a b) (/ (- a (remainder a b))b)))

;OR

(define myQuotient (lambda (a b)(-(/ a b)(/(remainder a b)b))))


;2 Is the Nth place even

;by defining a 2nd function

(define getNthPlace (lambda (x place)(remainder (quotient x place)10)))

(define isTheNthPlaceEven? (lambda (x place) (even? (getNthPlace x place))))

;OR writing it as one function

(define isTheNthPlaceEven? (lambda (x place) (even? (remainder (quotient x place)10))))


;3 Gravity is rad...

;Here are 3 ways to define G:

(define G (* 6.673 0.00000000001))

(define G (* 6.673 (expt 10 -11)))

(define G 6.673e-011)

;The function itself:

(define gravity (lambda (m1 m2 D)(/(* m1 m2 G)(* D D))))


HW3 + Solutions

posted Sep 13, 2012, 5:40 PM by Samuel Konstantinovich   [ updated Oct 8, 2012, 10:30 PM ]

SOLUTIONS are posted as a comment. Click this post to see comments.

This assignment has two parts, one should be submitted as an RKT file on the homework server. The other should be printed and brought to class.
PART 1:
Submit on the homework server: (Due 9/16/2012 11:59pm)
(If your name is not in the homework server, you should email me to let me know ASAP)
Write the following functions (definitions only):
Hint: You may want to define a square function before defining these three functions
1. (AreaRectangle a b) that takes two arguments a and b and returns the area of a rectangle whose length is a and whose width is b.
2.  () that takes two arguments a and b, and returns the sum of a squared and b squared. 
3.  (areacircle r) which takes an argument r, and returns the area of a circle of radius r. π is already defined for you by typing the predetermined variable pi. If not, then write the following variable definition: (define pi 3.14159)

PART 2:
Typed and printed: (Due 9/19/2012 in class)
1. Convert the infix expression to prefix.
 a.   ( 5 - 4 * 2 ) / ( 10 - 5 ) + 3
 b.   ( 2 +  3 ( 4 - 2) / ( 1 - 3 / 5 ) ) *EDITED: closed parenthesis at end* 
2. Evaluate the expressions (show the steps you took line by line) You may verify your result using DrScheme. Parts f and g require you to refer back to Part1 of the assignment. 
 a. ( * (+ 4 7 9) (/ 15 5) (- 3 -1) )
 b. (/ (- 20 -4)   (* -3 2 -1))

    (define a 4)
    (define b 5)
    (define c  (+ a b -1) )
 c. ( - (+ a (* -1 c) b) (* a b))
 d. (+ (quotient c b) (remainder a 4)) 
 e. (+ (* a b -1) (- c (* b 2)))
 f. (AreaRectangle 12 9)
 g. (sum-of-squares 4 7)



Solutions:
FUNCTIONS: (posted 9/26)
1. 
(define (AreaRectangle a b) (* a b) )
2. 
(define (sum-of-squares a b) (+ (* a a)(* b b)))
3.
(define (areacircle r) (* pi r r))
OR
(define (areacircle r) (* 3.14159 r r))


WRITTEN WORK: 
1a. The +3 happens AFTER the division:
(+ (/ (- 5 (* 4 2))(- 10 5)) 3) OR [for making the +3 more obvious] (+ 3 (/ (- 5 (* 4 2))(- 10 5)) )

1b. The 2 is added after the multiplications and divisions. 3(4-2) is a multiplication of 3 and parenthesis.
(+ 2 (/ (* 3 (- 4 2)) (- 1 (/ 3 5))))

2
a. 240
b. 4
c. -19
d. 1
e. -22
f. 108
g. 65

HW2 - Jargon (due 9/13/2012)

posted Sep 12, 2012, 5:52 AM by Samuel Konstantinovich   [ updated Sep 12, 2012, 11:36 AM ]

Jargon research assignment: 
Pick a computer related term that you have heard but don’t really know what it means. Do research from more than one source, and after reading about it, write your own description of what it is. (Do not summarize/paraphrase your sources) Write at least a few paragraphs explaining what it is and why it is important/how it is used. Cite your sources!

Deadline: 9/13/2012
Print the assignment and bring it in.

Important note:
Wikipedia is a valid tool to do help you research! However, you must go to their sources in order to cite something. Make sure you follow the links to read the primary source to verify the information, or find another source.

HW1 - Setup/Install

posted Sep 12, 2012, 5:47 AM by Samuel Konstantinovich   [ updated Sep 12, 2012, 5:47 AM ]

Download and install DrRacket. Before you click the Download button, you must FIRST select what type of machine you want to install it on. 
  • For Windows users, select Windows (x86, 32-bit) 
  • For Mac users, select Mac OS X (Intel 32-bit)
Go to the homework server link on the course webpage. Log in by selecting your period/name from the drop down menu. Your password is initially your 4 digit Stuy ID. Update your profile as follows:
  • change your password to something more secure that you will remember.
  • add your primary email address.
  • add a nickname if you want to be called by a different name. 

1-9 of 9