### notes

#### 2017-09-26 HW09

posted by Samuel Konstantinovich   [ updated ]

 ```SOLUTIONS TO Recent HW: (define (isBetween a b n) (or (and (> n a) (< n b)) (and (< n a) (> n b)))) ;alternate ; (and (> n (min a b)) (< n (max a b))))) ;alternate 2 which is similar to the 1st one````;but you were told not to use this ````; (or ; (< a n b) ; (> a n b)))) (define (xor a b) (and (or a b)(not (and a b)))) ;alternate: (not (equal? a b)))) ;this is not using boolean logic however``````;There are only 4 test cases: (xor #true #true) (xor #true #false) (xor #false #true) (xor #false #false) (define (alarmTime day isVacation) (if isVacation ;vacation (if (or (= day 0)(= day 6 )) 12 ;weekend 10);weekday ;not vacation (if (or (= day 0)(= day 6 )) 10 ;weekend 7)));weekday ;Sample test cases:``````(alarmTime 0 #true) (alarmTime 2 #true) ``````(define(gradeFix n) (cond ((>= n 90)n) ((> n 86)88) ((> n 82)85) (else 50))) ```Lab Should be doable in class!-place as a comment in the homework 9 slot when you finish-If not done in class finish at home. -These are much easier than the homework due Thursday.You have 6 functions to write (many do not use if/cond)If your code works for ONLY my test cases it is wrong. It must work as described, for all test cases. You must come up with your own test cases to convince yourself that it works. a) Write a function (or35 n) that accepts a non-negative integer and returns a boolean. -Return true if the given non-negative number is a multiple of 3 or a multiple of 5, false otherwise. (or35 6)  → #true(or35 10) → #true(or35 8)  → #falseb) Write a function (old35 n) that accepts a non-negative integer and returns a boolean. -Return true if the given non-negative number is a multiple of 3 or 5, but not both, false otherwise. (old35 6)  → #true(old35 10) → #true(old35 15) → #falsec) Write a function (more20 n) that accepts a non-negative integer and returns a boolean. -Return true if the given non-negative number is 1 or 2 more than a multiple of 20, false otherwise. (more20 20) → #false(more20 21) → #true(more20 22) → #trued) Write a function (less20 n) that accepts a non-negative integer and returns a boolean. -Return true if the given non-negative number is 1 or 2 less than a multiple of 20, false otherwise.(less20 18) → #true(less20 19) → #true(less20 20) → #false(less20 21) → #falseWrite a function (nearTen n) that accepts a non-negative integer and returns a boolean. -Given a non-negative number, return true if num is within 2 of a multiple of 10, false otherwise.(nearTen 12) → #true(nearTen 17) → #false(nearTen 19) → #trueWrite a function (categorize n) that accepts a non-negative integer and returns one of several categories:-> "strange"  when n is a multiple of 3 or 5 but not both. -> "charm"    when n is a multiple of both 3 and 5.-> "down"     when n is within 2 of a multiple of 10. This has lower priority than "strange" or "charm".-> "up"         for all other numbers.Do not re-write code that you have previously created. Use functions that you have previously defined.(categorize 15) → "charm"(categorize 20) → "strange"(categorize 30) → "charm"(categorize 19) → "down"(categorize 13) → "up"

#### 2017-09-25 HW08

posted Sep 25, 2017, 5:59 AM by Samuel Konstantinovich   [ updated ]

 `Do Now: ``(in notebooks)``0. While working, log in and lock the screen.``1. Write a function (checkSign x) , assume x is a real number.When x is positive return 1 When x is negative return 2When x is neutral return 3examples:(checkSign 4.5) -> 1(checkSign -3) -> 2(checkSign 0) -> 32. Create a (myQuotient a b) function that does the same thing as  (quotient a b). -You may use any tools EXCEPT for the quotient function. -Assume a and b are positive integers. This means you don't have to test it on negative values, or decimals. (Discuss with neighbors!)(myQuotient 10 3 ) -> 3(myQuotient 10 4 ) -> 2(myQuotient 9 3 ) -> 3``Goal: if vs cond, a cleaner way to make multiple optioins.``Classwork:``1. Rewrite checksign using cond.``2. Rewrite gradefix using cond.``3. ````;Using many if statements connected together ;starts to make the code go across the screen: (define (f g) (if (> g 99) "OK" (if (> g 90) "Whats wrong n00b?" (if (> g 80) "At least I will feed you..." "No soup for you!")))``````;Scheme has a better way to do this! (cond ( ) ( ) ( ) ... ( ) )````;Each clause has 2 parts which can be either:``a value (no parens) or an expression (with parens)``boolean value ``(> x 2) 5``#T (+ x 1)``else is the same as #T, and should be placed last ``if you want a 'catch all' case.````(cond ( ( > x 10) 2 ) ( ( > x 20) 3 ) ( ( < x 0) 4 ) )when x is 11 you get 2when x is 19 you get 2when x is 21 you get 2 because the 1st line is true, and it doen't get past it.when x is -11 you get 4when x is 5 you get NOTHING!!!! this is really bad(cond ( ( > x 20) 3 ) ; now we fixed the 3 never showing up ( ( > x 10) 2 ) ( ( < x 0) 4 ) ( #true 5 ) ; now we fixed the no-result from happening ;because #true will always happen no matter what )when x is 11 you get 2when x is 19 you get 2when x is 21 you get 3when x is -11 you get 4when x is 5 you get 5Example of cond in a function:(define (condDemo x) (cond ( (< x 10) 2 ) ( (< x 7) 3 ) ( (> x 20) 4 ) ( else 99)));else is the same result                ;as using #true ;we can put booleans into a function directly!(define (condDemo2 x b) (cond ( (< x 10) 2 ) ;less than 10 ( #false 3 ) ;this never happens ( (> x 20) 4 ) ;more than 20 ( b 4 ) ;when b is true and between 10 and 20 ( #true 99) ;default value for all other cases. ))(condDemo2 15 #true) (condDemo2 15 #false)HW08: Due Thursday 8am on HW server. Submit a text file with header.This is a challenging problem set, it does not give many easy examples to build up. The exam won't be this tricky. You have multiple days to think about the problems and work on them. (1 Test case reminder, then 2 problem solving parts each with a and b)0. For both questions and both parts of each questionYou must discuss a set of test cases with your neighbors. Test your code with it.The tests should be enough to convince everyone that your program works!Include your test cases (commented out, along with your results commented out)1. Leap Year Write a function (LeapYear year) that returns true when the year given is a leap year, false otherwise."There is a leap year every year whose number is perfectly divisible by four - except for years which are both divisible by 100 and not divisible by 400."***The second part of the rule affects century years. For example: the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not.a. Try with if/cond *easier*b. (LeapYearShort year) Same problem just use boolean logic, no if cond (but you need and/or/not) *trickier*http://www.infoplease.com/spot/leapyear2.html This is why we have this complex system for the century years.2. Military time. 24 hour time is useful. It prevents you from getting ready for school at 6pm thinking it is 6am. Also, there is no such thing as 12am or 12pm, only 12noon, 12midnight.  midnight        ->  0:00 1 am to 12 noon ->  1:00-12:00 1 pm to 11 pm    -> 13:00-23:00 Minutes are not changed at all in military time. a) Write a function (to24hourtime hour ampm)It takes an integer hour that is from 1 to 12 inclusive, and an integer ampm that is either 0 or 1, Where 0 represents am, and 1 represents pm. It returns the correct hour as an integer.Even though 12am and 12pm don't really exist, we will designate 12am as midnight for this problem.(to24hourtime 12 0) -> 0 ; this is midnight for our purposes. (to24hourtime 12 1) -> 12 ; this is noon for our purposes. (to24hourtime 1 0) -> 1 (to24hourtime 1 1) -> 13(also you can google military time to learn the conversion rules)After you write this using cond at first. Try to write it using a mathematical expression!b) *Challenge! Do your best! If you get stuck think about it later*(to24hourtimeShort hour ampm) -> same problem using no logic operators or decisions. Use math operations!Do not use:if   cond   and  or  not```

#### 2017-09-20 HW07 EXAM

posted Sep 20, 2017, 6:22 AM by Samuel Konstantinovich   [ updated Sep 20, 2017, 11:14 PM ]

 Goal: Multiple choices!NOTE: Exam next Friday!Do Now:1. Write a function that accepts three side lengths of a triangle a b and c, that assumes c is the longest side. It returns true if that triangle is a right triangle, false otherwise. (Does not require if statements)(pythCheck 3 4 5)  is #true(pythCheck 5 4 3)  is #false(pythCheck 3 4 6)  is #falseAfter writing it out , run drRacket and test at least one other Pythagorean triples!Note: You probably cannot check triangles with irrational side measures because of rounding errors.(like a  1 1  sqrt(2) triangle) 2. Discuss how can you make (isRight a b c) that uses pythCheck such that it does not assume anything about the longest side?Write this function! (returns true if the 3 values given are valid lengths of a right triangle. Assume a,b,c are all integers)Classwork:Rubber ducky!We can return words too!(if (> x 3)    "Big"     "Small")would show you a value either: (Quotes show up when typing text as a value in computer languages, and when you print it in Scheme)"Big"OR"Small"but never both.Branching an if statement:An if statement is an expression so you can put an if inside of an if:(if  you_have_cake_ingredients ;outer if     (if  you_can_bake          (go_bake_a_cake)          (go_learn_to_bake) ) ;close the inner if    (go_buy_cake_stuff)  ) ;close the outer ifTry to make a function: (gee x)- negative numbers turn positive- zero becomes -1- positive numbers become double their value.(gee -3)  is 3(gee -4) is 4(gee 0) is -1(gee 3) is 6(gee 1.5) is 3.0Try to make a function:(calcGrade n) Where n is your grade, you get different results:>= 90 : "Pass"From 70 up to but not including 90: "Fail"<70 : "Disown"LAB/HW07: Monday 8am on the HW server: (As a comment)This is a normal-sized assignment, but due to the holiday, if you have any plans that would make you unable to complete it by Monday, please provide a reason when you submit it. Directions: Write the following functions in Scheme, including the test cases you used to verify they work:1.(isBetween a b n)  where a b and n are all numbers.    -Return #true when n is strictly between (not equal) a and b.    -Return  #false otherwise    -Note: a can be smaller larger or equal to b.    -Restriction: Scheme allows you to say (< a b n)  to represent  a < b < n, but you should avoid this construct and use your knowledge of compound inequalities!2.(xor a b) where a and b are boolean values. This is similar to the or function, but is the exclusive or.Since it is a boolean function  you can use #true and #false as arguments, and you can only use and/or/not, to operate on the parameters.Returns:     #true when a and b have different truth values.    #false when they are both the same.e.g.(xor #true #true) -> #false(xor #true #false) -> #trueRestriction: Do this using the three operations:      and, or, not      (There is another easy way to do it but that avoids the heart of the problem.)3. Write a function (alarmTime day isVacation)Given a day encoded as:0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean isVacation indicating if we are on vacation, return an integer indicating when the alarm clock should ring. -Weekdays, the alarm should be 7-Weekend it should be 10 -Unless we are on vacation (isVacation is #true) Vacation mode changes the times to be:   -Weekdays should be 10   -weekends should be 12.4. Write a function (gradeFix n) such that:n >=90  return the same number backn > 86 and n< 90 return an 88n >82 and n<=86 return an 85All other cases return a 50.

#### 2017-09-19 HW06

posted Sep 19, 2017, 6:22 AM by Samuel Konstantinovich   [ updated Sep 19, 2017, 6:56 AM ]

 Goal: Decision making in scheme (conditionals)Do Now:On paper: Write the scheme functions:(myMaxOf3 a b c) -> do not use the built in max function.(isMultipleOf x y) that returns true when x is a multiple of y, false otherwise.MyMax Solutions:(define (myMax a b)  (+   (/ (abs (- a b)) 2)   (/ (+ a b) 2)))(define (midpoint a b)  (/ (+ a b) 2))(define (dist a b)  (abs (- a b)))(define (myMaxAlt a b)  (+   (midpoint a b)   (/ (dist a b) 2)))Classwork:If statements:(if     )When the boolean expression is true, the entire if evaluates to expression1.When it is false, the entire if evaluates to expression2.Example1:(if (> 10 2)    (+ 1 2)    (- 2 3))a) First evaluate the boolean (if (> 10 2)    (+ 1 2)    (- 2 3))b) choose which statement to pick(if #T    (+ 1 2)    (- 2 3))c) Replace the entire if with just that statement(+ 1 2)d) evaluate that statement3Example2:(if (= (remainder x 2) 0)    (> x 0)    (< x 0));assume x is 5:a) First evaluate the boolean   (= (remainder 5 2) 0) -> (= 1 0) -> #F(if (= (remainder x 2) 0)    (> x 0)    (< x 0))b) choose which statement to pick(if #F    (> x 0)    (< x 0))c) Replace the entire if with just that statement(< x 0)  which is (< 5 0)d) evaluate that statement#FIf statements can go inside functions as well:(define (foo x)      (if (> x 3)          1          2))(foo 1) is 2(foo 2) is 2 (foo 3) is 2(foo 4) is 1(foo 5) is 1(define (bar x y)      (if (and (> x 0) (> y 0))          10          20))(bar 1 2) is 10(bar -1 2) is 20(bar -3 -4) is 20(bar 4 -9) is 20(bar 12 5) is 10YOUR TURN TO WRITE CODE!Go To SchemingBathttp://bert.stuy.edu/pbrooks/SchemingBat/scheming.pyLab + HW6Choose: Simple decisions and do the 1st 5 problems (up to three-part function) a. Write and save your work in DrRacket, because the website does not save it for you!b. BEFORE you submit to the website, come up with 4 test cases.c. Test your work in drRacket, then paste into the website to verify that you are correct.d. Submit all 5 problems in the HW06 slot as a comment.

#### 2017-09-18 HW05

posted Sep 18, 2017, 6:25 AM by Samuel Konstantinovich   [ updated Sep 18, 2017, 10:27 AM ]

 Do Now:Write a function (midPoint A B) that returns the value of the number halfway between numbers A and B. e.g.(midPoint 1 2) -> 1.5(midPoint -1 3) -> 1Challenge Problem:How might you be able to calculate the maximum of two numerical values values using only:+ , - , *, / , absIf you use the method on 5 and 12 the result of the expression should be 12.If you use the method on 12 and 5 the result of the expression should be 12.If you use the method on 5 and -12 the result of the expression should be 5.If you use the method on -2 and 2 the result of the expression should be 2.Classwork In groups of two:-Draw a number line and sketch out what happens when you perform operations, this is more helpful than typing random things. Label your diagram with points A and B on the number line.-Think about it in terms of math: a. Is there a way to calculate the point halfway between A and B?b. Is there a way to calculate the distance between A and B?c. Does it matter if A or B is larger in the prior calculations? If it does, can you modify the calculation so that it doesn't matter?d. Label the diagram with how you can calculate each portion, then write a formula for the max of A and B as a regular math formula. e. NOW if you did all of that, you have figured out what you want to write as a scheme function (myMax A B) -> since max is already defined we are writing the function with the prefix that suggests you wrote it, and it is not the build in function.Goal Booleans.Depending on which scheme language you chose, True and False are written as:#T   #For even as:#true#falseYou can generate booleans from numerical values using comparisons:(< 3 3)(<= 3 3)(= 3 4)(= 3 3.0)(> 4 3)You can use the built in functions: and,or,not  they operate on booleans.(and #F #T)(or #F #T)(not (and #T #F))You can write a function that returns a boolean as well:(define (isGreaterThan10 x) (> x 10))(isGreaterThan10 3)(isGreaterThan10 13)(isGreaterThan10 10)LAB! Submit as HW05 as a comment on the homework server.1. Define a function that returns true (evaluates to true) when the number is positive, false otherwise.(this is just a simple test if you can write a boolean function. Normally you just write the boolean out.)(define (isPositive x)  (???))2. Define a function that evaluates to true when a number is even, false otherwise. (define (isEven x)  (???))3. Define a function that evaluates to true when the parameter is a teen number. Teens: 13,14,15,16,17,18,19(isTeen x)(isTeen 12) -> #false(isTeen 13) -> #true(isTeen 114) ->#false4. Define a function that evaluates to true when the sum of two numerical parameters is a teen, false otherwise. (isTeenSum x y)

#### 2017-09-15

posted Sep 15, 2017, 6:18 AM by Samuel Konstantinovich

#### 2017-09-14 HW04

posted Sep 14, 2017, 6:16 AM by Samuel Konstantinovich   [ updated Sep 14, 2017, 7:09 AM ]

#### 2017-09-13 HW03

posted Sep 13, 2017, 6:13 AM by Samuel Konstantinovich   [ updated Sep 14, 2017, 5:50 AM ]

#### 2017-09-12 HW02

posted Sep 12, 2017, 6:19 AM by Samuel Konstantinovich   [ updated Sep 12, 2017, 6:25 AM ]

#### 2017-09-11 HW

posted Sep 11, 2017, 5:56 AM by Samuel Konstantinovich

Goal: Infix, prefix, and postfix

Infix notation:
<value1> <operator> <value2>
examples:
2 + 3
5 * 2

Prefix notation
<operator> <value1> <value2>
examples:
+ 2 3
* 5 2

Postfix notation
<value> <value> <operator>
examples:
2 3 +
5 2 *

Scheme notation
Prefix with required the parenthesis: before the operator and after the final operand:
(<operator>  <value1> <value2>)

Some operatiors suppor more than 2 values:
(<operator>  <value1> <value2> ... <valueN>)

Converting
converting from infix to prefix requires you to know the order of operations.
2+ 3 * 4    ->  (+ 2 (* 3 4))
2 * 3 + 4  ->  (+ (*2 3) 4)

2 * 3 + 4   is really (2 * 3) + 4  which converts more easily.

Be careful when converting... because Scheme has no order of operations, but infix does. You must pay attention to this.

If you want a mechanical way of doing this here:
(((4 + 5) / 6) * 2)  Add more parenthesis to remind yourself of the order of operations
(* (/ (+4 5) 6) 2)   Now move the operators to the beginning of the set of parenthesis that contains them.

Dr Racket:

Running it
Using it to:
-evaluate a single line
-evaluate multiple things
-Save files

Homework : (Self-assessment so you know you got everything so far)
Test DrRacket at home.
Test the examples from class in DrRacket,  by evaluating them in the program, and then tracing through it using the stepper.
Make sure the way you evaluate an expression is the same way that DrRacket traces through it. This is open ended... the people that aren't 100% confident should make a few extra attempts at correcting their way of thinking. If you are confident, you should verify that you are correct.

1-10 of 11