Tutorial 3
Implement solutions for the following scenarios using [Link] and Python.
1) A Bank pays 4% for deposits of up to Rs. 1000 (inclusive), 4.5% for deposits up to Rs.
5000 (inclusive), and 5% for deposits of more than Rs. 5000. Design a function that
returns your interest for a given amount of deposit.
[Link]
(define (interest amount)
(cond
[(<= amount 1000) (* amount 0.04)]
[(<= amount 5000) (* amount 0.045)]
[(> amount 5000) (* amount 0.05)]
Python
def interestRate(amount):
if amount>5000:
return amount*0.5
elif amount>1000:
return amount*0.045
elif amount>=0:
return amount*0.04
else: return 0
print(interestRate(2))
2) The roots of the quadratic function ax2+bx+c = 0 is given by,
The existence of roots is determined by,
b2>4ac, two roots
b2=4ac, one root
b2<4ac, no roots
For any given a,b,c values determine the number of roots for the corresponding
function and also calculate the roots.
[Link]
(define (roots a b c)
(define value (- (* b b) (* 4 a c)))
(cond
[(> value 0) (display "Two roots\n")
(define root1 (/ (+ (* b -1) (sqrt value)) (* 2 a)))
(define root2 (/ (- (* b -1) (sqrt value)) (* 2 a)))
(display (string-append (number->string root1) " and "
(number->string root2)))]
[(= value 0) (display "One root\n")
(/ (* b -1) (* 2 a))]
[(< value 0) "No roots"]
)
Python
import math
def det(a,b,c):
det = b**2 - 4*a*c
if det == 0:
root = -b/2*a
print("One root:"+ str(root))
elif det > 0:
root1 = (-b+[Link](det))/2*a
root2 = (-[Link](det))/2*a
print("Two roots:" + str(root1) + " and " + str(root2))
else:
print("No roots")
det(3,2,-3)
Python Nested conditions Racket Nested conditions
mark = 60 (define mark 60)
awards= 40 (define awards 20)
if(mark>50): (if (>= mark 50)
if(awards>30): (if (> awards 10)
print("MERIT PASS") (display "merit pass")
else: (display "non merit
print("NON MERIT pass"))
PASS")
else: (display "Failed"))
print("FAILED")