DR - Racket Tutorial 5
DR - Racket Tutorial 5
Implement solutions for the following scenarios using Dr. Racket and Python.
>(OddEven 7)
> 7 is Odd
Dr. Racket
(define (OddEven num)
(cond
[(= (modulo num 2) 0) (display (string-append (number->string num) "
is Even"))]
[else (display (string-append (number->string num) " is Odd"))]
)
)
Python
def OddEven(num):
if num%2 == 0:
print(str(num) + “ is Even”)
else:
print(str(num) + “ is Odd”)
2. Design a program to take 3 strings (e.g.: sentences or words), and output which string
was the longest and the number of characters in it.
e.g.: > Enter string 1: Hello World
> Enter string 2: I’m learning Racket
> Enter string 3: I’m also learning Python.
> String 3 is the longest. It has 24 characters.
Improve the program by finding out the method where you can ignore the „space‟
characters. e.g.: “Hello World” has 11 characters, but if you remove the space only 10
characters.
Dr. Racket
(display "Enter string 1:")
(define string1 (read-line))
(display "Enter string 2:")
(define string2 (read-line))
(display "Enter string 3:")
(define string3 (read-line))
(cond
[(and (> len1 len2) (> len1 len3)) (display (string-append "String 1
is the longest. It has " (number->string len1) " characters"))]
[(and (> len2 len1) (> len2 len3)) (display (string-append "String 2
is the longest. It has " (number->string len2) " characters"))]
[(and (> len3 len2) (> len3 len1)) (display (string-append "String 3
is the longest. It has " (number->string len3) " characters"))]
[else (display "There is no single longest")]
)
(string-replace string1 " " "") will remove the spaces in the string.
Space No Space
String variable
Do this for all 3 strings and calculate the string-length afterwards. Then you will get the
string lengths without counting spaces.