Question

In: Computer Science

SCHEME [4 marks] Define a procedure called valid_date? that takes three arguments representing the year, month,...

SCHEME

  1. [4 marks] Define a procedure called valid_date? that takes three arguments representing the year, month, and day respectively. The procedure should return true (#t) if the values are numbers representing a valid date and false (#f) otherwise.
    E.g. (valid_date? 2014 10 3) → #t
    E.g. (valid_date? 2016 2 29) → #t
    E.g. (valid_date? -3000 14 87) → #f
    E.g. (valid_date? "2019" "September" "7th") → #f

Solutions

Expert Solution

; this will return true (#t) if year is leap year
; else false (#f)
(define (leap_year? year)
   ; if not number
   (cond ((not (integer? year)) #f)
       ; else part
       (else
           (or
               (and
                   (equal? (remainder year 4) 0)
                   (not (equal? (remainder year 100) 0))
               )
               (equal? (remainder year 400) 0)
           )
       )
   )
)

; (leap_year? 1700)
; (leap_year? 1800)
; (leap_year? 1900)
; (leap_year? 1600)
; (leap_year? 2000)

(define (valid_date? y m day)
   (cond ((and (integer? y) (integer? m) (integer? day))
               ; validation of y, m, d
               (cond ((and (>= y 1) (and (>= m 1) (<= m 12)) (and (>= day 1) (<= day 31)))
                       ; check is m = 2
                           (cond ((equal? m 2)
                                       ;; check is year is leap year
                                       (cond ((leap_year? y) (and (>= day 1) (<= day 29)))
                                           ; if y is not leap year
                                           (else (and (>= day 1) (<= day 28)))
                                       )
                                   )
                               ; check is m even
                               ((equal? (remainder m 2) 0) (and (>= day 1) (<= day 30)))
                               ; if m is odd
                               (else (and (>= day 1) (<= day 31)))
                           )
                       )
                   (else #f)
               )
           )
           (else #f)
   )
)

; (valid_date? 2014 10 3) → #t
; (valid_date? 2016 2 29) → #t
; (valid_date? -3000 14 87) → #f
; (valid_date? "2019" "September" "7th") → #f


Related Solutions

Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Function named FunCount takes three arguments- C, an integer representing the count of elements in input...
Function named FunCount takes three arguments- C, an integer representing the count of elements in input list IP- input list of positive integers. Item- an integer value. Function FunCount returns an integer representing the count of all the elements of List that are equal to the given integer value Key. Example: Don’t take these values in program, take all inputs from user C = 9, IP= [1,1,4,2,2,3,4,1,2], Item = 2 function will return 3 IN C PROGRAMMING
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write the definition of a Point class method called clone that takes NO arguments and returns...
Write the definition of a Point class method called clone that takes NO arguments and returns a new Point whose x and y coordinates are the same as the Point’s coordinates.
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments....
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments. Your function will then return the n largest values from that array as an array containing n elements. It is safe to assume that arr will have at least n elements. The resulting array should have the largest number on the end and the smallest number at the beginning. For Example: max_n(np.array([1,2,3,4,5]), 3) returns np.array([3,4,5]) max_n(np.array([10,9,8,7,6,5]), 4) returns np.array([7,8,9,10]) max_n(np.array([1,1,1]), 2) returns np.array([1,1])
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its...
Define a function file_to_hist() which takes a string representing a filename, opens the file, reads its contents, closes the file,* and returns a histogram based on the letter frequencies in the given file. If no such file exists, your function should return an empty histogram (i.e., an empty dictionary {}). So for example, if the file nash.txt was in the same directory as char_hist3.py and had the following contents: I have never seen a purple cow, And I never hope...
Write a function called compute_pay that accepts arguments representing the hourly wage of an employee and the number of hours that employee worked this week.
Python RephactorCompute Take Home PayWrite a function called compute_pay that accepts arguments representing the hourly wage of an employee and the number of hours that employee worked this week. The function should return the take home pay owed to the employee as a floating point value rounded to two decimal places.The normal work week is 40 hours, and the company pays employees "time and a half" for overtime. So, the total wages is the sum of regular wages and overtime...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT