DAY-9
[20-11-2024]
A11. Generate 100 random numbers between 101 and 200. Count the frequency of numbers
in the different ranges
[101-125, 126-150, 151-175, 176-200]
import random
# Step 1: Generate 100 random numbers between 101 and 200
random_numbers = [random.randint(101, 200) for _ in range(100)]
# Step 2: Initialize counters for each range
range_101_125 = 0
range_126_150 = 0
range_151_175 = 0
range_176_200 = 0
# Step 3: Count the frequency of numbers in each range
for number in random_numbers:
if 101 <= number <= 125:
range_101_125 += 1
elif 126 <= number <= 150:
range_126_150 += 1
elif 151 <= number <= 175:
range_151_175 += 1
elif 176 <= number <= 200:
range_176_200 += 1
# Step 4: Print the frequencies
print(f"Random Numbers: {random_numbers}")
print("Frequency Count:")
print(f"[101-125]: {range_101_125}")
print(f"[126-150]: {range_126_150}")
print(f"[151-175]: {range_151_175}")
print(f"[176-200]: {range_176_200}")
Random Numbers: [124, 149, 112, 171, 159, 172, 114, 164, 165, 185, 123,
175, 147, 149, 169, 152, 118, 129, 166, 124, 152, 130, 103, 160, 109,
140, 200, 177, 196, 130, 108, 193, 170, 164, 200, 101, 138, 156, 160,
155, 111, 183, 174, 168, 198, 177, 127, 188, 136, 164, 109, 153, 148,
109, 177, 117, 160, 176, 127, 119, 174, 192, 159, 162, 183, 116, 170,
196, 110, 141, 148, 160, 196, 200, 131, 147, 118, 152, 191, 185, 115,
175, 188, 111, 130, 172, 171, 147, 162, 196, 180, 104, 125, 101, 166,
160, 144, 190, 112, 192]
Frequency Count:
[101-125]: 24
[126-150]: 19
[151-175]: 33
[176-200]: 24
Algorithm
1. Generate Random Numbers:
o Use Python's random module to generate 100 random integers between 101 and
200.
2. Initialize Range Counters:
o Define four counters for the specified ranges: [101-125], [126-150], [151-175], and
[176-200].
3. Count Frequencies:
o Iterate through the list of random numbers.
o For each number, check which range it belongs to and increment the
corresponding counter.
4. Print Results:
o Display the frequency count for each range.
A12. Write a function to generate a random alphanumeric string with 6 characters. There must be
one uppercase, one lower case, one digit in the string and all string should start with an uppercase
letter.
Call the function 100 times and check how many time a digit is available at second position.
import random
import string
def generate_alphanumeric_string():
# Step 1: Ensure the first character is an uppercase letter
first_char = random.choice(string.ascii_uppercase)
# Step 2: Include at least one lowercase letter and one digit
lowercase_char = random.choice(string.ascii_lowercase)
digit_char = random.choice(string.digits)
# Step 3: Generate remaining 3 characters (can be uppercase,
lowercase, or digit)
remaining_chars = random.choices(string.ascii_letters +
string.digits, k=3)
# Combine all parts and shuffle (except for the first character)
all_chars = [lowercase_char, digit_char] + remaining_chars
random.shuffle(all_chars)
# Concatenate the first character with the shuffled rest
final_string = first_char + ''.join(all_chars)
return final_string
# Step 4: Call the function 100 times and count occurrences of a digit
at the second position
digit_at_second_position_count = 0
for _ in range(100):
generated_string = generate_alphanumeric_string()
if generated_string[1].isdigit():
digit_at_second_position_count += 1
print(f"Number of times a digit appeared at the second position:
{digit_at_second_position_count}")
Number of times a digit appeared at the second position: 27
Algorithm
1. Generate Alphanumeric String:
o Use the random module.
o Ensure the first character is an uppercase letter.
o Include at least one lowercase letter, one digit, and randomize the remaining
characters.
o Shuffle the string (except for the first character) to ensure randomness.
2. Count Strings with a Digit at the Second Position:
o Call the function 100 times.
o Check if the second character of each generated string is a digit.
o Count such occurrences.
A13. Write a program to implement heap sort.
def heapify(arr, n, i):
"""
Maintains the heap property for a subtree rooted at index `i`.
`n` is the size of the heap.
"""
largest = i # Assume root is the largest
left = 2 * i + 1 # Left child index
right = 2 * i + 2 # Right child index
# Check if left child exists and is greater than root
if left < n and arr[left] > arr[largest]:
largest = left
# Check if right child exists and is greater than current largest
if right < n and arr[right] > arr[largest]:
largest = right
# If the largest is not the root, swap and continue heapifying
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # Swap
heapify(arr, n, largest)
def heap_sort(arr):
"""
Sorts an array using the Heap Sort algorithm.
"""
n = len(arr)
# Step 1: Build a max heap
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
# Step 2: Extract elements one by one
for i in range(n - 1, 0, -1):
# Move current root to the end
arr[0], arr[i] = arr[i], arr[0]
# Call heapify on the reduced heap
heapify(arr, i, 0)
# Example usage
if __name__ == "__main__":
array = [12, 11, 13, 5, 6, 7]
print("Original array:", array)
heap_sort(array)
print("Sorted array:", array)
Original array: [12, 11, 13, 5, 6, 7]
Sorted array: [5, 6, 7, 11, 12, 13]
Algorithm
1. Build a Max-Heap:
o Rearrange the array into a max-heap, where the largest element is at the root.
2. Heapify:
o After swapping the root with the last element, restore the heap property by
heapifying the reduced heap.
3. Sort:
o Repeatedly swap the root with the last unsorted element, reduce the heap size, and
heapify.