0% found this document useful (0 votes)
11 views

Module Assignment

The document describes creating three Python modules - string_utilities, random_generator, and data_processing - to contain functions for manipulating strings, generating random numbers, and processing data respectively. It shows implementing the functions in each module and importing/using them in a main script to reverse strings, count characters, check palindromes, generate random integers/passwords, shuffle lists, calculate averages, find max/min values, and sort lists.

Uploaded by

Noble Prince
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)
11 views

Module Assignment

The document describes creating three Python modules - string_utilities, random_generator, and data_processing - to contain functions for manipulating strings, generating random numbers, and processing data respectively. It shows implementing the functions in each module and importing/using them in a main script to reverse strings, count characters, check palindromes, generate random integers/passwords, shuffle lists, calculate averages, find max/min values, and sort lists.

Uploaded by

Noble Prince
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

1.

Create a module called `string_utilities` that contains functions for string manipulation, such as
reversing a string, counting the number of occurrences of a character, and checking if a string is a
palindrome. Import this module into another script and use the functions to manipulate and analyze
strings.
Solutions: Implementation of the `string_utilities` module that contains the functions you described:
**string_utilities.py**
def reverse_string(input_string):
return input_string[::-1]
def count_occurrences(input_string, char):
return input_string.count(char)
def is_palindrome(input_string):
return input_string == input_string[::-1]

You can save the above code in a file named `string_utilities.py`. Now, let's create another script to import this
module and use its functions:
**main.py**
import string_utilities
# Reverse a string
original_string = "Hello, World!"
reversed_string = string_utilities.reverse_string(original_string)
print("Reversed string:", reversed_string)
# Count the occurrences of a character
char_to_count = "l"
occurrences = string_utilities.count_occurrences(original_string, char_to_count)
print("Number of occurrences of 'l':", occurrences)
# Check if a string is a palindrome
palindrome_string = "racecar"
is_palindrome = string_utilities.is_palindrome(palindrome_string)
print("Is 'racecar' a palindrome?", is_palindrome)
In this example, we import the `string_utilities` module and use its functions `reverse_string`,
`count_occurrences`, and `is_palindrome` to manipulate and analyze strings. The output will be as follows:
Reversed string: !dlroW ,olleH
Number of occurrences of 'l': 3
Is 'racecar' a palindrome? True
2. Create a module called `random_generator` that contains functions for generating random
numbers, such as generating a random integer within a specified range, shuffling a list randomly, or
generating a random password. Import this module into another script and use the functions to generate
random data.
Solutions: Implementation of the `random_generator` module that contains functions for generating random
numbers, shuffling a list, and generating random passwords:
**random_generator.py**
import random
import string
def generate_random_integer(start, end):
return random.randint(start, end)
def shuffle_list(input_list):
shuffled_list = input_list[:]
random.shuffle(shuffled_list)
return shuffled_list
def generate_random_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
You can save the above code in a file named `random_generator.py`. Now, let's create another script to import
this module and use its functions:
**main.py**
import random_generator
# Generate a random integer
random_int = random_generator.generate_random_integer(1, 10)
print("Random integer:", random_int)
# Shuffle a list
original_list = [1, 2, 3, 4, 5]
shuffled_list = random_generator.shuffle_list(original_list)
print("Original list:", original_list)
print("Shuffled list:", shuffled_list)
# Generate a random password
password_length = 8
random_password = random_generator.generate_random_password(password_length)
print("Random password:", random_password)
In this example, we import the `random_generator` module and use its functions `generate_random_integer`,
`shuffle_list`, and `generate_random_password` to generate random data. The output will be different each time
you run the script due to the nature of random number generation.
3. Create a module called `data_processing` that contains functions for processing and analyzing
data, such as calculating the average, finding the maximum and minimum values, or sorting a list of
numbers. Import this module into another script and use the functions to process and analyze data.
Solutions: Implementation of the `data_processing` module that contains functions for processing and analyzing
data, such as calculating the average, finding the maximum and minimum values, and sorting a list of numbers:
**data_processing.py**
def calculate_average(data_list):
if len(data_list) == 0:
return None
return sum(data_list) / len(data_list)
def find_maximum(data_list):
if len(data_list) == 0:
return None
return max(data_list)
def find_minimum(data_list):
if len(data_list) == 0:
return None
return min(data_list)
def sort_list(data_list):
return sorted(data_list)
You can save the above code in a file named `data_processing.py`. Now, let's create another script to import
this module and use its functions:
**main.py**
import data_processing
# Calculate the average
numbers = [1, 2, 3, 4, 5]
average = data_processing.calculate_average(numbers)
print("Average:", average)
# Find the maximum and minimum values
maximum = data_processing.find_maximum(numbers)
minimum = data_processing.find_minimum(numbers)
print("Maximum:", maximum)
print("Minimum:", minimum)
# Sort a list of numbers
sorted_numbers = data_processing.sort_list(numbers)
print("Sorted numbers:", sorted_numbers)
In this example, we import the `data_processing` module and use its functions `calculate_average`,
`find_maximum`, `find_minimum`, and `sort_list` to process and analyze data. The output will be as follows:
Average: 3.0
Maximum: 5
Minimum: 1
Sorted numbers: [1, 2, 3, 4, 5]

You might also like