Tutorial 2
Implement solutions for the following scenarios using DrRacket and Python.
1) Design a program to calculate the length of a given string and return the phrase “The
total number of characters in the string is..[answer]”.
[Link]
(define (calculate-length string)
(define length (string-length string))
(string-append "The total number of characters in the string is "
(number->string length))
Python
def calculate_length (string):
return (len(string))
print("The total number of characters in the string is " +
str(calculate_length("Hello World")))
2) Design a program to calculate the surface area of the following disk/ring with the outer
radius and the inner radius given in parameters.
[Link]
(define (area-of-ring outer inner)
(- (area-of-disk outer)
(area-of-disk inner)))
(define (area-of-disk r)
(* pi r r))
Python
def circle_area (radius):
pi = 22/7
return pi*radius*radius
def ring_area (outer,inner):
return circle_area(outer) - circle_area(inner)
print(ring_area(5,3))
3) Calculate the value of b2-4ac for the quadratic formula ax2+bx+c=0, when provided a, b,
c variable values as parameters.
Design the program to give command line inputs as well.
[Link]
(define (roots a b c)
(- (* b b) (* 4 a c))
;;;;;;;; using user input ;;;;;;;;;;;;;;
"Please enter a" (define a (read))
"Please enter b"(define b (read))
"Please enter c"(define c (read))
(- (* b b) (* 4 a c))
Python
def roots(a,b,c):
return b**2 - 4*a*c
print(roots(3,2,1))