Assignment7
Assignment7
Objective:
Solve python problems using list comprehension.
Question:
1. Given an image with pixels values as follows, increase the brightness of the pixels by adding fixed
values to RGB. The maximum value of each pixel ranges from 0 to 255. Use list comprehension.
Algorithm:
Code:
print("\n\nHello. Welcome to image editor!")
pix1 = list(map(int, input("Enter the RGB pixel values (with space between each value) :
").split()))
pix2 = list(map(int, input("Enter the RGB pixel values (with space between each value) :
").split()))
pix3 = list(map(int, input("Enter the RGB pixel values (with space between each value) :
").split()))
while True:
print("\n\nMenu")
print("1. Increase overall brightness")
print("2. Decrease overall brightness")
print("3. Increase a pixel's brightness")
print("4. Decrease a pixel's brightness")
print("5. Exit")
if choice == 1:
intensity = int(input("Enter the intensity to increase by: "))
image = adjust_brightness(image, intensity)
print("Updated Image:", image)
elif choice == 2:
intensity = int(input("Enter the intensity to decrease by: "))
image = adjust_brightness(image, -intensity)
print("Updated Image:", image)
elif choice == 3:
row = int(input("Enter the row index (0-2): "))
col = int(input("Enter the column index (0-2): "))
intensity = int(input("Enter the intensity to increase by: "))
image[row][col] = min(255, image[row][col] + intensity)
print("Updated Image:", image)
elif choice == 4:
row = int(input("Enter the row index (0-2): "))
col = int(input("Enter the column index (0-2): "))
intensity = int(input("Enter the intensity to decrease by: "))
image[row][col] = max(0, image[row][col] - intensity)
print("Updated Image:", image)
elif choice == 5:
print("Exiting ...")
break
else:
print("Invalid choice. Please try again.")
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-1.py
Enter the RGB pixel values (with space between each value) : 90 120 150
Enter the RGB pixel values (with space between each value) : 210 205 215
Enter the RGB pixel values (with space between each value) : 40 60 80
Original Image: [[90, 120, 150], [210, 205, 215], [40, 60, 80]]
Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit
Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit
Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit
Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit
Question:
2. Assume that a Web Server log files has the list of entries given below. Extract the entries that contains
only the “Error” using list comprehension.
Algorithm:
Code:
# Given web server log entries
log_entries = [
"INFO: Server started",
"ERROR: Connection failed",
"INFO: User logged in",
"ERROR: Timeout",
"DEBUG: Debugging request"
]
Question:
3. Suppose you have a list of strings with special characters as given below and you want to clean them by
removing those characters. Use list comprehension to perform this task.
Algorithm:
Code:
import string
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-3.py
Cleaned Data: ['apple', 'banana', 'cherry', 'date']
Additional Programs:
Question:
1. Write a python program to transpose a given matrix.
Code:
# Function to transpose a given matrix
def transpose_matrix(matrix):
# Using list comprehension to transpose the matrix
return [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
# Example matrix
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("\nTransposed Matrix:")
for row in transposed_matrix:
print(row)
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-add1.py
Original Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Transposed Matrix:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Question:
2. Write a program to find whether a number is a perfect square or not.
Code:
num = int(input("Enter a number: "))
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-add2.py
Enter a number: 5
5 is not a perfect square.
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-add2.py
Enter a number: 9
9 is a perfect square.
Question:
3. Given a list, fruits = ["apple", "banana", "cherry", "kiwi", "mango"]. Create a new list that contains only
fruits that contains the character “a”.
Code:
# Given list of fruits
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
# Create a new list that contains only fruits with the character 'a'
fruits_with_a = [fruit for fruit in fruits if 'a' in fruit]
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-add3.py
Fruits containing 'a': ['apple', 'banana', 'mango']
Question:
4. Given 2 lists: list1 = [1, 2, 3], list2 = ['a', 'b', 'c'] . Generate the combination of all elements.
Code:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
Learning Outcomes: