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

Assignment7

The document outlines an assignment focused on solving Python problems using list comprehension. It includes tasks such as adjusting image brightness, extracting error entries from server logs, cleaning strings, transposing matrices, checking for perfect squares, filtering fruits, and generating combinations of elements from two lists. Each task is accompanied by code examples and expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment7

The document outlines an assignment focused on solving Python problems using list comprehension. It includes tasks such as adjusting image brightness, extracting error entries from server logs, cleaning strings, transposing matrices, checking for perfect squares, filtering fruits, and generating combinations of elements from two lists. Each task is accompanied by code examples and expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment-7

Programs using List Comprehension


Name: K.S.Vijayaraaghavan Register Number:3122247001066
Evaluation:

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!")

print("\nEnter the pixel details of the image you want to edit:\n")

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

image = [pix1, pix2, pix3]


print("Original Image:", image)

def adjust_brightness(img, intensity):


return [[min(255, max(0, pixel + intensity)) for pixel in row] for row in img]

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

choice = int(input("\n\nEnter your choice of action: "))

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

Hello. Welcome to image editor!

Enter the pixel details of the image you want to edit:

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

Enter your choice of action: 1


Enter the intensity to increase by: 100
Updated Image: [[190, 220, 250], [255, 255, 255], [140, 160, 180]]

Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit

Enter your choice of action: 2


Enter the intensity to decrease by: 80
Updated Image: [[110, 140, 170], [175, 175, 175], [60, 80, 100]]
Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit

Enter your choice of action: 1


Enter the intensity to increase by: 10
Updated Image: [[120, 150, 180], [185, 185, 185], [70, 90, 110]]

Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit

Enter your choice of action: 3


Enter the row index (0-2): 2
Enter the column index (0-2): 2
Enter the intensity to increase by: 100
Updated Image: [[120, 150, 180], [185, 185, 185], [70, 90, 210]]

Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit

Enter your choice of action: 4


Enter the row index (0-2): 1
Enter the column index (0-2): 0
Enter the intensity to decrease by: 30
Updated Image: [[120, 150, 180], [155, 185, 185], [70, 90, 210]]
Menu
1. Increase overall brightness
2. Decrease overall brightness
3. Increase a pixel's brightness
4. Decrease a pixel's brightness
5. Exit

Enter your choice of action: 5


Exiting ...

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"
]

# Extract entries that contain only "ERROR"


error_entries = [entry for entry in log_entries if entry.startswith("ERROR")]

# Print the result


print("Error Entries:", error_entries)
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-2.py
Error Entries: ['ERROR: Connection failed', 'ERROR: Timeout']

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

# Given raw data with special characters


raw_data = ["apple!", "banana@", "cherry#", "date$"]

# Remove special characters using list comprehension


cleaned_data = [''.join([char for char in word if char in string.ascii_letters]) for word
in raw_data]

# Print the result


print("Cleaned Data:", cleaned_data)

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]
]

# Transpose the matrix


transposed_matrix = transpose_matrix(matrix)

# Print the original and transposed matrix


print("Original Matrix:")
for row in matrix:
print(row)

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: "))

# List comprehension to generate squares up to num


squares = [x*x for x in range(1, int(num**0.5)+1)]
if num in squares:
print(f"{num} is a perfect square.")
else:
print(f"{num} is not a perfect square.")

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]

# Print the result


print("Fruits containing 'a':", fruits_with_a)

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']

combinations = [(x, y) for x in list1 for y in list2]


print(combinations)
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-7/a7-add4.py
[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c')]

Learning Outcomes:

You might also like