### 2017-09-25 HW08

posted Sep 25, 2017, 5:59 AM by Samuel Konstantinovich   [ updated Sep 27, 2017, 6:15 AM ]
 `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.``;3 simple conds:``;These 3 are equivalant. They are like 3 way if statements that``;checks for positive, negative, or neutral.````(cond ( ( > x 0) 1) ( ( < x 0) -1) ( ( = x 0) 0) );OR(cond ( ( > x 0) 1) ( ( < x 0) -1) ( #true 0) ;since the only number left is 0, we don't need to check );OR(cond ( ( > x 0) 1) ( ( < x 0) -1) ( else 0) ;else is the same as #true. )````;Not all conds are formed well. ``;This one cannot trigger the middle statement.````(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```