Uploaded by Efe

CS 104 W12 early.docx

advertisement
CS 104 – Fall 2023
Lab Exercises - Week 12
Your programs should start with the following comments: (do not forget to change “[Your Fullname]”
with your name, and “[short description]” to a sentence that highlights the task to be performed by the
program)
All of your programs should use conditionals, loops, and functions. Format your outputs as given in the
exercise.
# [Your Fullname], CS 104, LAB Week 12
# This program [Exercise 1].
Exercise 1:
Write a python program to count the occurrences of one string in another string and display it to the
user in the desired format. Follow the given steps to get a valid mark:
-
Prompt the user to enter two strings str1 and str2 as arguments.
Define a recursive function named as countSubStr(str1, str2) where str1 and str2 are strings. The
function should calculate the occurrences of the str2 in str1 and return it as an integer.
Display the output in the main function as given below.
SAMPLE RUN 1:
str1: ATGCATGACAGCAT (Input)
str2: ATG (Input)
ATG occurs in ATGCATGACAGCAT 2 times (Output)
SAMPLE RUN 2:
str1: aaaaa (Input)
str2: aaa (Input)
aaa occurs in aaaaa 3 times (Output)
# This program [Exercise 2].
Exercise 2: Write a python program to find the greatest common divisor of two numbers and display it to
the user in the desired format. Follow the given steps to get a valid mark:
-
Prompt the user to enter two numbers m and n as arguments.
Define a recursive function named as gcd(m, n) where m and n are integers. The function should
find the greatest common divisor of two numbers and return it as an integer.
Display the output in the main function as given below.
SAMPLE RUN:
M: 2024
N: 48
GCD: 8
Download