0% found this document useful (0 votes)
6 views3 pages

DR - Racket Tutorial 5

The document provides tutorials for implementing functions in Dr. Racket and Python to determine if a number is even or odd, and to find the longest of three input strings while considering character counts with and without spaces. The OddEven function is defined for both languages, demonstrating the use of conditionals and string manipulation. Additionally, it includes a method to compare string lengths and output the longest string along with its character count, accounting for spaces.

Uploaded by

Randy Bale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

DR - Racket Tutorial 5

The document provides tutorials for implementing functions in Dr. Racket and Python to determine if a number is even or odd, and to find the longest of three input strings while considering character counts with and without spaces. The OddEven function is defined for both languages, demonstrating the use of conditionals and string manipulation. Additionally, it includes a method to compare string lengths and output the longest string along with its character count, accounting for spaces.

Uploaded by

Randy Bale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial 5

Implement solutions for the following scenarios using Dr. Racket and Python.

1. Design a function to decide if a given number is Even or Odd.


e.g.: >(OddEven 4)
> 4 is Even

>(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))

(define len1 (string-length string1))


(define len2 (string-length string2))
(define len3 (string-length string3))

(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.

You might also like