#numpy #python #programming #question #array #basic
Write a Python code snippet using NumPy to create a 2D array of shape (3, 4) filled with zeros. Then, modify the element at position (1, 2) to be 5. Print the resulting array.
Output:
By: @DataScienceQ ๐
Write a Python code snippet using NumPy to create a 2D array of shape (3, 4) filled with zeros. Then, modify the element at position (1, 2) to be 5. Print the resulting array.
import numpy as np
# Create a 2D array of zeros with shape (3, 4)
arr = np.zeros((3, 4))
# Modify the element at position (1, 2) to be 5
arr[1, 2] = 5
# Print the resulting array
print(arr)
Output:
[[0. 0. 0. 0.]
[0. 0. 5. 0.]
[0. 0. 0. 0.]]
By: @DataScienceQ ๐
โค2
#numpy #python #programming #question #array #intermediate
Write a Python program using NumPy to perform the following tasks:
1. Create a 1D array of integers from 1 to 10.
2. Reshape it into a 2D array of shape (2, 5).
3. Compute the sum of each row and store it in a new array.
4. Find the indices of elements greater than 7 in the original 1D array.
5. Print the resulting 2D array, the row sums, and the indices.
Output:
By: @DataScienceQ ๐
Write a Python program using NumPy to perform the following tasks:
1. Create a 1D array of integers from 1 to 10.
2. Reshape it into a 2D array of shape (2, 5).
3. Compute the sum of each row and store it in a new array.
4. Find the indices of elements greater than 7 in the original 1D array.
5. Print the resulting 2D array, the row sums, and the indices.
import numpy as np
# 1. Create a 1D array from 1 to 10
arr_1d = np.arange(1, 11)
# 2. Reshape into a 2D array of shape (2, 5)
arr_2d = arr_1d.reshape(2, 5)
# 3. Compute the sum of each row
row_sums = np.sum(arr_2d, axis=1)
# 4. Find indices of elements greater than 7 in the original 1D array
indices_greater_than_7 = np.where(arr_1d > 7)[0]
# 5. Print results
print("2D Array:\n", arr_2d)
print("Row sums:", row_sums)
print("Indices of elements > 7:", indices_greater_than_7)
Output:
2D Array:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
Row sums: [15 40]
Indices of elements > 7: [7 8 9]
By: @DataScienceQ ๐
โค4
#pandas #python #programming #question #dataframe #intermediate
Write a Python program using pandas to perform the following tasks:
1. Create a DataFrame from a dictionary with columns: 'Product', 'Category', 'Price', and 'Quantity' containing:
- Product: ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Headphones']
- Category: ['Electronics', 'Accessories', 'Accessories', 'Electronics', 'Accessories']
- Price: [1200, 25, 80, 300, 100]
- Quantity: [10, 50, 30, 20, 40]
2. Add a new column 'Total_Value' that is the product of 'Price' and 'Quantity'.
3. Calculate the total value for each category and print it.
4. Find the product with the highest total value and print its details.
5. Filter the DataFrame to show only products in the 'Electronics' category with a price greater than 200.
Output:
By: @DataScienceQ ๐
Write a Python program using pandas to perform the following tasks:
1. Create a DataFrame from a dictionary with columns: 'Product', 'Category', 'Price', and 'Quantity' containing:
- Product: ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Headphones']
- Category: ['Electronics', 'Accessories', 'Accessories', 'Electronics', 'Accessories']
- Price: [1200, 25, 80, 300, 100]
- Quantity: [10, 50, 30, 20, 40]
2. Add a new column 'Total_Value' that is the product of 'Price' and 'Quantity'.
3. Calculate the total value for each category and print it.
4. Find the product with the highest total value and print its details.
5. Filter the DataFrame to show only products in the 'Electronics' category with a price greater than 200.
import pandas as pd
# 1. Create the DataFrame
data = {
'Product': ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Headphones'],
'Category': ['Electronics', 'Accessories', 'Accessories', 'Electronics', 'Accessories'],
'Price': [1200, 25, 80, 300, 100],
'Quantity': [10, 50, 30, 20, 40]
}
df = pd.DataFrame(data)
# 2. Add Total_Value column
df['Total_Value'] = df['Price'] * df['Quantity']
# 3. Calculate total value by category
total_by_category = df.groupby('Category')['Total_Value'].sum()
# 4. Find product with highest total value
highest_value_product = df.loc[df['Total_Value'].idxmax()]
# 5. Filter electronics with price > 200
electronics_high_price = df[(df['Category'] == 'Electronics') & (df['Price'] > 200)]
# Print results
print("Original DataFrame:")
print(df)
print("\nTotal Value by Category:")
print(total_by_category)
print("\nProduct with Highest Total Value:")
print(highest_value_product)
print("\nElectronics Products with Price > 200:")
print(electronics_high_price)
Output:
Original DataFrame:
Product Category Price Quantity Total_Value
0 Laptop Electronics 1200 10 12000
1 Mouse Accessories 25 50 1250
2 Keyboard Accessories 80 30 2400
3 Monitor Electronics 300 20 6000
4 Headphones Accessories 100 40 4000
Total Value by Category:
Category
Accessories 7650
Electronics 18000
dtype: int64
Product with Highest Total Value:
Product Laptop
Category Electronics
Price 1200
Quantity 10
Total_Value 12000
Name: 0, dtype: object
Electronics Products with Price > 200:
Product Category Price Quantity Total_Value
0 Laptop Electronics 1200 10 12000
By: @DataScienceQ ๐
#opencv #python #programming #question #imageprocessing #intermediate
Write a Python program using OpenCV to perform the following tasks:
1. Load an image from a file named 'image.jpg' in grayscale mode.
2. Apply Gaussian blur with a kernel size of (5, 5).
3. Detect edges using Canny edge detection with thresholds of 100 and 200.
4. Find contours in the edge-detected image.
5. Draw all detected contours on the original blurred image in red color with thickness 2.
6. Save the resulting image as 'output_image.jpg'.
Note: This code assumes that 'image.jpg' exists in the working directory. The output will be a colored image with red contours drawn over the blurred grayscale image.
By: @DataScienceQ ๐
Write a Python program using OpenCV to perform the following tasks:
1. Load an image from a file named 'image.jpg' in grayscale mode.
2. Apply Gaussian blur with a kernel size of (5, 5).
3. Detect edges using Canny edge detection with thresholds of 100 and 200.
4. Find contours in the edge-detected image.
5. Draw all detected contours on the original blurred image in red color with thickness 2.
6. Save the resulting image as 'output_image.jpg'.
import cv2
import numpy as np
# 1. Load image in grayscale
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
if img is None:
raise FileNotFoundError("Image file not found")
# 2. Apply Gaussian blur
blurred = cv2.GaussianBlur(img, (5, 5), 0)
# 3. Apply Canny edge detection
edges = cv2.Canny(blurred, 100, 200)
# 4. Find contours
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 5. Create a copy of the blurred image to draw contours
result_img = cv2.cvtColor(blurred, cv2.COLOR_GRAY2BGR) # Convert to BGR for color drawing
cv2.drawContours(result_img, contours, -1, (0, 0, 255), 2) # Draw contours in red
# 6. Save the output image
cv2.imwrite('output_image.jpg', result_img)
print("Processing complete. Output saved as 'output_image.jpg'")
Note: This code assumes that 'image.jpg' exists in the working directory. The output will be a colored image with red contours drawn over the blurred grayscale image.
By: @DataScienceQ ๐
#imageprocessing #python #programming #question #dataset #intermediate
Write a Python program to process a dataset of images stored in a folder named 'images'. Perform the following tasks:
1. Load all images from the 'images' folder and convert them to grayscale.
2. Resize each image to 100x100 pixels.
3. Calculate the average pixel value for each image.
4. Store the average values in a list.
5. Find the image with the highest average pixel value and print its filename.
6. Save the processed grayscale images to a new folder named 'processed_images'.
Note: This code assumes that the 'images' folder exists and contains valid image files. It processes all PNG, JPG, and JPEG files in the folder, resizes them, calculates their average pixel intensity, and saves the processed images to a new folder.
By: @DataScienceQ ๐
Write a Python program to process a dataset of images stored in a folder named 'images'. Perform the following tasks:
1. Load all images from the 'images' folder and convert them to grayscale.
2. Resize each image to 100x100 pixels.
3. Calculate the average pixel value for each image.
4. Store the average values in a list.
5. Find the image with the highest average pixel value and print its filename.
6. Save the processed grayscale images to a new folder named 'processed_images'.
import os
import cv2
import numpy as np
# 1. Define paths
input_folder = 'images'
output_folder = 'processed_images'
# Create output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
# List to store average pixel values
avg_values = []
# 2. Process each image in the input folder
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
img_path = os.path.join(input_folder, filename)
# Load image in grayscale
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
# Resize to 100x100 pixels
resized_img = cv2.resize(img, (100, 100))
# Calculate average pixel value
avg_value = np.mean(resized_img)
avg_values.append((filename, avg_value))
# Save processed image
output_path = os.path.join(output_folder, filename)
cv2.imwrite(output_path, resized_img)
# 3. Find image with highest average pixel value
max_avg_image = max(avg_values, key=lambda x: x[1])
print(f"Image with highest average pixel value: {max_avg_image[0]}")
print(f"Average value: {max_avg_image[1]:.2f}")
print("All images processed and saved to 'processed_images' folder.")
Note: This code assumes that the 'images' folder exists and contains valid image files. It processes all PNG, JPG, and JPEG files in the folder, resizes them, calculates their average pixel intensity, and saves the processed images to a new folder.
By: @DataScienceQ ๐
#matplotlib #python #programming #question #visualization #intermediate
Write a Python program using matplotlib to perform the following tasks:
1. Generate two arrays: x from 0 to 10 with 100 points, and y = sin(x) + 0.5 * cos(2x).
2. Create a figure with two subplots arranged vertically.
3. In the first subplot, plot y vs x as a line graph with red color and marker 'o'.
4. In the second subplot, create a histogram of the y values with 20 bins.
5. Add titles, labels, and grid to both subplots.
6. Adjust the layout and save the figure as 'output_plot.png'.
Note: This code generates a sine wave with an added cosine component, creates a line plot and histogram of the data in separate subplots, adds appropriate labels and grids, and saves the resulting visualization.
By: @DataScienceQ ๐
Write a Python program using matplotlib to perform the following tasks:
1. Generate two arrays: x from 0 to 10 with 100 points, and y = sin(x) + 0.5 * cos(2x).
2. Create a figure with two subplots arranged vertically.
3. In the first subplot, plot y vs x as a line graph with red color and marker 'o'.
4. In the second subplot, create a histogram of the y values with 20 bins.
5. Add titles, labels, and grid to both subplots.
6. Adjust the layout and save the figure as 'output_plot.png'.
import numpy as np
import matplotlib.pyplot as plt
# 1. Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x) + 0.5 * np.cos(2 * x)
# 2. Create figure with two subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 10))
# 3. First subplot - line plot
ax1.plot(x, y, color='red', marker='o', linestyle='-', linewidth=2)
ax1.set_title('sin(x) + 0.5*cos(2x)')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.grid(True)
# 4. Second subplot - histogram
ax2.hist(y, bins=20, color='blue', alpha=0.7)
ax2.set_title('Histogram of y values')
ax2.set_xlabel('y')
ax2.set_ylabel('Frequency')
ax2.grid(True)
# 5. Adjust layout
plt.tight_layout()
# 6. Save the figure
plt.savefig('output_plot.png')
print("Plot saved as 'output_plot.png'")
Note: This code generates a sine wave with an added cosine component, creates a line plot and histogram of the data in separate subplots, adds appropriate labels and grids, and saves the resulting visualization.
By: @DataScienceQ ๐
#scipy #python #programming #question #scientificcomputing #intermediate
Write a Python program using SciPy to perform the following tasks:
1. Generate a random dataset of 1000 samples from a normal distribution with mean=5 and standard deviation=2.
2. Use SciPy's
3. Perform a one-sample t-test to test if the sample mean is significantly different from 5 (null hypothesis).
4. Use SciPy's
5. Print all results including the test statistic, p-value, and the minimum point.
Note: This code generates a normally distributed dataset, computes various statistical measures, performs a hypothesis test, and finds the minimum of a quadratic function using SciPy's optimization tools.
By: @DataScienceQ ๐
Write a Python program using SciPy to perform the following tasks:
1. Generate a random dataset of 1000 samples from a normal distribution with mean=5 and standard deviation=2.
2. Use SciPy's
stats module to calculate the mean, median, standard deviation, and skewness of the dataset.3. Perform a one-sample t-test to test if the sample mean is significantly different from 5 (null hypothesis).
4. Use SciPy's
optimize module to find the minimum of the function f(x) = x^2 + 3x + 2.5. Print all results including the test statistic, p-value, and the minimum point.
import numpy as np
from scipy import stats
from scipy.optimize import minimize_scalar
# 1. Generate random dataset
np.random.seed(42)
data = np.random.normal(loc=5, scale=2, size=1000)
# 2. Calculate descriptive statistics
mean = np.mean(data)
median = np.median(data)
std_dev = np.std(data)
skewness = stats.skew(data)
# 3. Perform one-sample t-test
t_stat, p_value = stats.ttest_1samp(data, popmean=5)
# 4. Find minimum of function f(x) = x^2 + 3x + 2
def objective_function(x):
return x**2 + 3*x + 2
result = minimize_scalar(objective_function)
# 5. Print all results
print("Descriptive Statistics:")
print(f"Mean: {mean:.4f}")
print(f"Median: {median:.4f}")
print(f"Standard Deviation: {std_dev:.4f}")
print(f"Skewness: {skewness:.4f}")
print("\nOne-Sample T-Test:")
print(f"T-statistic: {t_stat:.4f}")
print(f"P-value: {p_value:.4f}")
print("\nOptimization Result:")
print(f"Minimum occurs at x = {result.x:.4f}")
print(f"Minimum value = {result.fun:.4f}")
Note: This code generates a normally distributed dataset, computes various statistical measures, performs a hypothesis test, and finds the minimum of a quadratic function using SciPy's optimization tools.
By: @DataScienceQ ๐
#python #programming #question #fibonacci #intermediate #algorithm
Write a Python program that implements three different methods to generate the Fibonacci sequence up to the nth term:
1. Use an iterative approach with a loop.
2. Use recursion with memoization.
3. Use dynamic programming with a list.
For each method, calculate the 20th Fibonacci number and measure the execution time. Print the results for each method along with their respective times.
By: @DataScienceQ ๐
Write a Python program that implements three different methods to generate the Fibonacci sequence up to the nth term:
1. Use an iterative approach with a loop.
2. Use recursion with memoization.
3. Use dynamic programming with a list.
For each method, calculate the 20th Fibonacci number and measure the execution time. Print the results for each method along with their respective times.
import time
def fibonacci_iterative(n):
if n <= 1:
return n
a, b = 0, 1
for i in range(2, n + 1):
a, b = b, a + b
return b
def fibonacci_recursive_memo(n, memo={}):
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fibonacci_recursive_memo(n - 1, memo) + fibonacci_recursive_memo(n - 2, memo)
return memo[n]
def fibonacci_dp(n):
if n <= 1:
return n
dp = [0] * (n + 1)
dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
# Test all three methods for the 20th Fibonacci number
n = 20
# Method 1: Iterative
start_time = time.time()
result_iter = fibonacci_iterative(n)
iter_time = time.time() - start_time
# Method 2: Recursive with memoization
start_time = time.time()
result_rec = fibonacci_recursive_memo(n)
rec_time = time.time() - start_time
# Method 3: Dynamic Programming
start_time = time.time()
result_dp = fibonacci_dp(n)
dp_time = time.time() - start_time
print(f"20th Fibonacci number using iterative method: {result_iter} (Time: {iter_time:.6f} seconds)")
print(f"20th Fibonacci number using recursive method: {result_rec} (Time: {rec_time:.6f} seconds)")
print(f"20th Fibonacci number using DP method: {result_dp} (Time: {dp_time:.6f} seconds)")
By: @DataScienceQ ๐
#python #programming #question #simulation #intermediate #matryoshka
Write a Python program to simulate a Matryoshka doll game with the following requirements:
1. Create a class
2. Implement methods to:
- Add a smaller Matryoshka inside the current one
- Remove the smallest Matryoshka from the set
- Display all dolls in the nesting hierarchy
3. Create a main function that:
- Builds a nesting of 4 Matryoshka dolls (largest to smallest)
- Displays the complete nesting
- Removes the smallest doll
- Displays the updated nesting
Output:
By: @DataScienceQ ๐
Write a Python program to simulate a Matryoshka doll game with the following requirements:
1. Create a class
Matryoshka that represents a nested doll with attributes: size (int), color (string), and contents (list of smaller Matryoshka objects).2. Implement methods to:
- Add a smaller Matryoshka inside the current one
- Remove the smallest Matryoshka from the set
- Display all dolls in the nesting hierarchy
3. Create a main function that:
- Builds a nesting of 4 Matryoshka dolls (largest to smallest)
- Displays the complete nesting
- Removes the smallest doll
- Displays the updated nesting
class Matryoshka:
def __init__(self, size, color):
self.size = size
self.color = color
self.contents = []
def add_doll(self, doll):
if doll.size < self.size:
self.contents.append(doll)
else:
print(f"Cannot add doll of size {doll.size} into size {self.size} doll")
def remove_smallest(self):
if not self.contents:
print("No dolls to remove")
return None
# Find the smallest doll recursively
smallest = self._find_smallest()
if smallest:
self._remove_doll(smallest)
return smallest
return None
def _find_smallest(self):
if not self.contents:
return self
smallest = self
for doll in self.contents:
result = doll._find_smallest()
if result.size < smallest.size:
smallest = result
return smallest
def _remove_doll(self, target):
if self.contents:
for i, doll in enumerate(self.contents):
if doll == target:
self.contents.pop(i)
return
elif doll._remove_doll(target):
return
def display(self, level=0):
indent = " " * level
print(f"{indent}{self.color} ({self.size})")
for doll in self.contents:
doll.display(level + 1)
def main():
# Create nesting of 4 Matryoshka dolls (largest to smallest)
large = Matryoshka(4, "Red")
medium = Matryoshka(3, "Blue")
small = Matryoshka(2, "Green")
tiny = Matryoshka(1, "Yellow")
# Build the nesting
large.add_doll(medium)
medium.add_doll(small)
small.add_doll(tiny)
# Display initial nesting
print("Initial nesting:")
large.display()
print()
# Remove the smallest doll
removed = large.remove_smallest()
if removed:
print(f"Removed: {removed.color} ({removed.size})")
# Display updated nesting
print("\nUpdated nesting:")
large.display()
if __name__ == "__main__":
main()
Output:
Initial nesting:
Red (4)
Blue (3)
Green (2)
Yellow (1)
Removed: Yellow (1)
Updated nesting:
Red (4)
Blue (3)
Green (2)
By: @DataScienceQ ๐
#python #programming #question #simulation #intermediate #philosophers_dinner
Write a Python program to simulate the Dining Philosophers problem with the following requirements:
1. Create a class
2. Implement methods for:
- Attempting to pick up forks (with a delay)
- Eating
- Releasing forks
3. Use threading to simulate 5 philosophers sitting around a circular table.
4. Ensure no deadlock occurs by implementing a strategy where philosophers pick up forks in order (e.g., odd-numbered philosophers pick up left fork first, even-numbered pick up right fork first).
5. Display the state of each philosopher (thinking, eating, or waiting) at regular intervals.
By: @DataScienceQ ๐
Write a Python program to simulate the Dining Philosophers problem with the following requirements:
1. Create a class
Philosopher that represents a philosopher with attributes: name (string), left_fork (int), right_fork (int), and eating (boolean).2. Implement methods for:
- Attempting to pick up forks (with a delay)
- Eating
- Releasing forks
3. Use threading to simulate 5 philosophers sitting around a circular table.
4. Ensure no deadlock occurs by implementing a strategy where philosophers pick up forks in order (e.g., odd-numbered philosophers pick up left fork first, even-numbered pick up right fork first).
5. Display the state of each philosopher (thinking, eating, or waiting) at regular intervals.
import threading
import time
import random
class Philosopher:
def __init__(self, name, left_fork, right_fork):
self.name = name
self.left_fork = left_fork
self.right_fork = right_fork
self.eating = False
def eat(self):
print(f"{self.name} is trying to eat...")
# Pick up left fork
self.left_fork.acquire()
print(f"{self.name} picked up left fork")
# Pick up right fork
self.right_fork.acquire()
print(f"{self.name} picked up right fork")
# Eat
self.eating = True
print(f"{self.name} is eating")
time.sleep(random.uniform(1, 3))
# Release forks
self.right_fork.release()
self.left_fork.release()
print(f"{self.name} finished eating")
self.eating = False
def philosopher_thread(philosopher, num_philosophers):
while True:
# Think
print(f"{philosopher.name} is thinking")
time.sleep(random.uniform(1, 3))
# Try to eat
philosopher.eat()
# Main simulation
if __name__ == "__main__":
# Create 5 forks (semaphores)
forks = [threading.Semaphore(1) for _ in range(5)]
# Create 5 philosophers
philosophers = [
Philosopher("Philosopher 1", forks[0], forks[1]),
Philosopher("Philosopher 2", forks[1], forks[2]),
Philosopher("Philosopher 3", forks[2], forks[3]),
Philosopher("Philosopher 4", forks[3], forks[4]),
Philosopher("Philosopher 5", forks[4], forks[0])
]
# Start threads for each philosopher
threads = []
for i, philosopher in enumerate(philosophers):
thread = threading.Thread(target=philosopher_thread, args=(philosopher, len(philosophers)))
threads.append(thread)
thread.start()
# Wait for all threads to complete (infinite loop)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Simulation ended")
By: @DataScienceQ ๐
#matlab #imageprocessing #programming #question #intermediate
Write a MATLAB program to perform the following image processing tasks on a grayscale image:
1. Load an image named 'cameraman.tif' from the built-in MATLAB dataset.
2. Convert the image to grayscale if it's not already in that format.
3. Apply Gaussian blur with a 5x5 kernel.
4. Perform edge detection using the Sobel operator.
5. Display the original, blurred, and edge-detected images in a single figure with three subplots.
6. Save the edge-detected image as 'edge_result.png'.
Note: This code assumes that the 'cameraman.tif' image is available in the MATLAB path (it's a built-in dataset). The program processes the image through blurring and edge detection, displays the results in a single figure, and saves the final edge-detected image.
By: @DataScienceQ ๐
Write a MATLAB program to perform the following image processing tasks on a grayscale image:
1. Load an image named 'cameraman.tif' from the built-in MATLAB dataset.
2. Convert the image to grayscale if it's not already in that format.
3. Apply Gaussian blur with a 5x5 kernel.
4. Perform edge detection using the Sobel operator.
5. Display the original, blurred, and edge-detected images in a single figure with three subplots.
6. Save the edge-detected image as 'edge_result.png'.
% 1. Load the cameraman image
img = imread('cameraman.tif');
% 2. Convert to grayscale if necessary
if size(img, 3) == 3
img = rgb2gray(img);
end
% 3. Apply Gaussian blur with 5x5 kernel
blurred = imgaussfilt(img, 2); % sigma=2 for 5x5 kernel
% 4. Perform edge detection using Sobel operator
edges = edge(blurred, 'sobel');
% 5. Display all images in subplots
figure;
subplot(1,3,1);
imshow(img);
title('Original Image');
axis off;
subplot(1,3,2);
imshow(blurred);
title('Gaussian Blurred');
axis off;
subplot(1,3,3);
imshow(edges);
title('Edge Detected (Sobel)');
axis off;
% 6. Save the edge-detected image
imwrite(edges, 'edge_result.png');
disp('Image processing completed. Results saved.')
Note: This code assumes that the 'cameraman.tif' image is available in the MATLAB path (it's a built-in dataset). The program processes the image through blurring and edge detection, displays the results in a single figure, and saves the final edge-detected image.
By: @DataScienceQ ๐
#python #programming #question #simulation #intermediate #ludo
Write a Python program to simulate a simplified Ludo game between a user and a computer with the following requirements:
1. Create a game class `LudoGame` with attributes: player_position (int), computer_position (int), and current_player (string).
2. Implement methods for:
- Rolling a die (random number 1-6)
- Moving a player piece based on the roll
- Checking if a player has won (reached position 50)
- Switching turns between player and computer
3. The game should alternate turns between the user and computer.
4. When the user plays, prompt them to press Enter to roll the die.
5. Display the current positions of both players after each move.
6. End the game when either player reaches or exceeds position 50.
```python
import random
class LudoGame:
def __init__(self):
self.player_position = 0
self.computer_position = 0
self.current_player = "user"
def roll_dice(self):
return random.randint(1, 6)
def move_player(self, player):
dice_roll = self.roll_dice()
print(f"Dice roll: {dice_roll}")
if player == "user":
self.player_position += dice_roll
else:
self.computer_position += dice_roll
print(f"Player position: {self.player_position}")
print(f"Computer position: {self.computer_position}")
def check_winner(self):
if self.player_position >= 50:
return "user"
elif self.computer_position >= 50:
return "computer"
return None
def switch_turn(self):
self.current_player = "computer" if self.current_player == "user" else "user"
def play_game(self):
print("Welcome to Simplified Ludo!")
print("Reach position 50 to win.")
while True:
print(f"\n{self.current_player}'s turn:")
if self.current_player == "user":
input("Press Enter to roll the dice...")
self.move_player("user")
else:
self.move_player("computer")
winner = self.check_winner()
if winner:
print(f"\n{winner.capitalize()} wins!")
break
self.switch_turn()
# Start the game
game = LudoGame()
game.play_game()
```
By: @DataScienceQ ๐
Write a Python program to simulate a simplified Ludo game between a user and a computer with the following requirements:
1. Create a game class `LudoGame` with attributes: player_position (int), computer_position (int), and current_player (string).
2. Implement methods for:
- Rolling a die (random number 1-6)
- Moving a player piece based on the roll
- Checking if a player has won (reached position 50)
- Switching turns between player and computer
3. The game should alternate turns between the user and computer.
4. When the user plays, prompt them to press Enter to roll the die.
5. Display the current positions of both players after each move.
6. End the game when either player reaches or exceeds position 50.
```python
import random
class LudoGame:
def __init__(self):
self.player_position = 0
self.computer_position = 0
self.current_player = "user"
def roll_dice(self):
return random.randint(1, 6)
def move_player(self, player):
dice_roll = self.roll_dice()
print(f"Dice roll: {dice_roll}")
if player == "user":
self.player_position += dice_roll
else:
self.computer_position += dice_roll
print(f"Player position: {self.player_position}")
print(f"Computer position: {self.computer_position}")
def check_winner(self):
if self.player_position >= 50:
return "user"
elif self.computer_position >= 50:
return "computer"
return None
def switch_turn(self):
self.current_player = "computer" if self.current_player == "user" else "user"
def play_game(self):
print("Welcome to Simplified Ludo!")
print("Reach position 50 to win.")
while True:
print(f"\n{self.current_player}'s turn:")
if self.current_player == "user":
input("Press Enter to roll the dice...")
self.move_player("user")
else:
self.move_player("computer")
winner = self.check_winner()
if winner:
print(f"\n{winner.capitalize()} wins!")
break
self.switch_turn()
# Start the game
game = LudoGame()
game.play_game()
```
By: @DataScienceQ ๐
#pytorch #python #programming #question #intermediate #machinelearning
Write a PyTorch program to perform the following tasks:
1. Create a simple neural network with one hidden layer (128 units) and ReLU activation.
2. Use binary cross-entropy loss for binary classification.
3. Implement a training loop for 10 epochs on synthetic data (100 samples, 10 features).
4. Calculate accuracy during training and print it after each epoch.
5. Save the trained model's state dictionary.
By: @DataScienceQ ๐
Write a PyTorch program to perform the following tasks:
1. Create a simple neural network with one hidden layer (128 units) and ReLU activation.
2. Use binary cross-entropy loss for binary classification.
3. Implement a training loop for 10 epochs on synthetic data (100 samples, 10 features).
4. Calculate accuracy during training and print it after each epoch.
5. Save the trained model's state dictionary.
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset
import numpy as np
# 1. Set random seed for reproducibility
torch.manual_seed(42)
# 2. Generate synthetic data
X = torch.randn(100, 10) # 100 samples, 10 features
y = torch.randint(0, 2, (100,)) # Binary labels
# Create dataset and dataloader
dataset = TensorDataset(X, y)
dataloader = DataLoader(dataset, batch_size=16, shuffle=True)
# 3. Define neural network
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(10, 128)
self.fc2 = nn.Linear(128, 1)
def forward(self, x):
x = F.relu(self.fc1(x))
x = torch.sigmoid(self.fc2(x))
return x
model = SimpleNN()
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 4. Training loop
for epoch in range(10):
model.train()
total_loss = 0
correct_predictions = 0
total_samples = 0
for batch_X, batch_y in dataloader:
optimizer.zero_grad()
# Forward pass
outputs = model(batch_X).squeeze()
loss = criterion(outputs, batch_y.float())
# Backward pass
loss.backward()
optimizer.step()
total_loss += loss.item()
# Calculate accuracy
predictions = (outputs > 0.5).float()
correct_predictions += (predictions == batch_y.float()).sum().item()
total_samples += batch_y.size(0)
# Print results
avg_loss = total_loss / len(dataloader)
accuracy = correct_predictions / total_samples
print(f"Epoch {epoch+1}, Loss: {avg_loss:.4f}, Accuracy: {accuracy:.4f}")
# 5. Save the model
torch.save(model.state_dict(), 'simple_nn.pth')
print("Model saved as 'simple_nn.pth'")
By: @DataScienceQ ๐
#keras #python #programming #question #intermediate #machinelearning
Write a Keras program to perform the following tasks:
1. Load the MNIST dataset and preprocess it (normalize pixel values and convert labels to categorical).
2. Create a sequential model with two dense layers (128 units with ReLU activation, 10 units with softmax activation).
3. Compile the model using Adam optimizer and sparse categorical crossentropy loss.
4. Train the model for 5 epochs with a validation split of 0.2.
5. Evaluate the model on the test set and print the test accuracy.
6. Save the trained model to a file named 'mnist_model.h5'.
By: @DataScienceQ ๐
Write a Keras program to perform the following tasks:
1. Load the MNIST dataset and preprocess it (normalize pixel values and convert labels to categorical).
2. Create a sequential model with two dense layers (128 units with ReLU activation, 10 units with softmax activation).
3. Compile the model using Adam optimizer and sparse categorical crossentropy loss.
4. Train the model for 5 epochs with a validation split of 0.2.
5. Evaluate the model on the test set and print the test accuracy.
6. Save the trained model to a file named 'mnist_model.h5'.
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# 1. Load and preprocess data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize pixel values to range [0, 1]
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# Reshape data for the model
x_train = x_train.reshape(-1, 28*28)
x_test = x_test.reshape(-1, 28*28)
# Convert labels to categorical (one-hot encoding)
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# 2. Create sequential model
model = models.Sequential()
model.add(layers.Dense(128, activation='relu', input_shape=(784,)))
model.add(layers.Dense(10, activation='softmax'))
# 3. Compile model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 4. Train model
history = model.fit(x_train, y_train,
epochs=5,
validation_split=0.2,
verbose=1)
# 5. Evaluate model
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.4f}")
# 6. Save model
model.save('mnist_model.h5')
print("Model saved as 'mnist_model.h5'")
By: @DataScienceQ ๐
#python #programming #question #intermediate #looping
Write a Python program that demonstrates the use of for loops in various forms with the following requirements:
1. Use a standard for loop to iterate through a list of numbers and print each number.
2. Use a for loop with enumerate() to print both the index and value of each element in a list.
3. Use a for loop with zip() to iterate through two lists simultaneously and print corresponding elements.
4. Use a for loop with range() to print numbers from 1 to 10, but only print even numbers.
5. Use a nested for loop to create a multiplication table (1-5) and print it in a formatted way.
```python
# 1. Standard for loop
numbers = [1, 2, 3, 4, 5]
print("1. Standard for loop:")
for num in numbers:
print(num)
# 2. For loop with enumerate()
fruits = ['apple', 'banana', 'cherry']
print("\n2. For loop with enumerate():")
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
# 3. For loop with zip()
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
print("\n3. For loop with zip():")
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 4. For loop with range() - even numbers only
print("\n4. For loop with range() - even numbers:")
for i in range(1, 11):
if i % 2 == 0:
print(i)
# 5. Nested for loop - multiplication table
print("\n5. Nested for loop - multiplication table (1-5):")
for i in range(1, 6):
for j in range(1, 6):
print(f"{i} ร {j} = {i*j}", end="\t")
print() # New line after each row
```
By: @DataScienceQ ๐
Write a Python program that demonstrates the use of for loops in various forms with the following requirements:
1. Use a standard for loop to iterate through a list of numbers and print each number.
2. Use a for loop with enumerate() to print both the index and value of each element in a list.
3. Use a for loop with zip() to iterate through two lists simultaneously and print corresponding elements.
4. Use a for loop with range() to print numbers from 1 to 10, but only print even numbers.
5. Use a nested for loop to create a multiplication table (1-5) and print it in a formatted way.
```python
# 1. Standard for loop
numbers = [1, 2, 3, 4, 5]
print("1. Standard for loop:")
for num in numbers:
print(num)
# 2. For loop with enumerate()
fruits = ['apple', 'banana', 'cherry']
print("\n2. For loop with enumerate():")
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
# 3. For loop with zip()
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
print("\n3. For loop with zip():")
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 4. For loop with range() - even numbers only
print("\n4. For loop with range() - even numbers:")
for i in range(1, 11):
if i % 2 == 0:
print(i)
# 5. Nested for loop - multiplication table
print("\n5. Nested for loop - multiplication table (1-5):")
for i in range(1, 6):
for j in range(1, 6):
print(f"{i} ร {j} = {i*j}", end="\t")
print() # New line after each row
```
By: @DataScienceQ ๐
#python #programming #question #intermediate #listoperations
Write a comprehensive Python program that demonstrates various ways to work with lists in different scenarios:
1. Create a list of integers from 1 to 10 using the range() function and convert it to a list.
2. Demonstrate list comprehension by creating a new list containing squares of even numbers from the original list.
3. Use list slicing to extract sublists (first 5 elements, last 3 elements, every second element).
4. Show how to modify list elements by changing specific indices and using negative indexing.
5. Implement a nested list and demonstrate accessing elements at different levels.
6. Use the enumerate() function to iterate through a list while tracking index and value.
7. Create two lists and use zip() to combine them into pairs.
8. Perform list operations: append, extend, insert, remove, pop, and sort.
9. Use the count() and index() methods to find occurrences and positions of elements.
10. Create a list of strings and use the join() method to concatenate them with a separator.
By: @DataScienceQ ๐
Write a comprehensive Python program that demonstrates various ways to work with lists in different scenarios:
1. Create a list of integers from 1 to 10 using the range() function and convert it to a list.
2. Demonstrate list comprehension by creating a new list containing squares of even numbers from the original list.
3. Use list slicing to extract sublists (first 5 elements, last 3 elements, every second element).
4. Show how to modify list elements by changing specific indices and using negative indexing.
5. Implement a nested list and demonstrate accessing elements at different levels.
6. Use the enumerate() function to iterate through a list while tracking index and value.
7. Create two lists and use zip() to combine them into pairs.
8. Perform list operations: append, extend, insert, remove, pop, and sort.
9. Use the count() and index() methods to find occurrences and positions of elements.
10. Create a list of strings and use the join() method to concatenate them with a separator.
# 1. Create a list of integers from 1 to 10
numbers = list(range(1, 11))
print("Original list:", numbers)
# 2. List comprehension - squares of even numbers
squares_of_evens = [x**2 for x in numbers if x % 2 == 0]
print("Squares of even numbers:", squares_of_evens)
# 3. List slicing
print("\nList slicing examples:")
print("First 5 elements:", numbers[:5])
print("Last 3 elements:", numbers[-3:])
print("Every second element:", numbers[::2])
# 4. Modifying list elements
print("\nModifying list elements:")
numbers[0] = 100 # Change first element
numbers[-1] = 200 # Change last element
print("After modifications:", numbers)
# 5. Nested lists
print("\nNested lists:")
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Accessing nested elements:")
print("First row:", nested_list[0])
print("Element at position [1][2]:", nested_list[1][2])
# 6. Using enumerate()
print("\nUsing enumerate():")
for index, value in enumerate(numbers):
print(f"Index {index}: {value}")
# 7. Using zip() with two lists
print("\nUsing zip() with two lists:")
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 8. List operations
print("\nList operations:")
fruits = ['apple', 'banana']
print("Initial fruits:", fruits)
# Append
fruits.append('orange')
print("After append:", fruits)
# Extend
fruits.extend(['grape', 'kiwi'])
print("After extend:", fruits)
# Insert
fruits.insert(1, 'mango')
print("After insert:", fruits)
# Remove
fruits.remove('grape')
print("After remove:", fruits)
# Pop
popped_item = fruits.pop()
print(f"Popped item: {popped_item}, Remaining: {fruits}")
# Sort
fruits.sort()
print("After sorting:", fruits)
# 9. Using count() and index()
print("\nUsing count() and index():")
numbers_with_duplicates = [1, 2, 2, 3, 2, 4]
print("Numbers with duplicates:", numbers_with_duplicates)
print("Count of 2:", numbers_with_duplicates.count(2))
print("Index of first 2:", numbers_with_duplicates.index(2))
# 10. Joining strings in a list
print("\nJoining strings:")
words = ['Hello', 'world', 'from', 'Python']
sentence = ' '.join(words)
print("Joined sentence:", sentence)
# Additional example: list of dictionaries
print("\nList of dictionaries:")
students = [
{'name': 'Alice', 'grade': 85},
{'name': 'Bob', 'grade': 92},
{'name': 'Charlie', 'grade': 78}
]
print("Students data:")
for student in students:
print(f"{student['name']}: {student['grade']}")
# Example: filtering with list comprehension
print("\nFiltering students with grade > 80:")
high_performers = [student for student in students if student['grade'] > 80]
for student in high_performers:
print(f"{student['name']}: {student['grade']}")
By: @DataScienceQ ๐
#python #programming #question #advanced #datastructures #datahandling
Write a comprehensive Python program that demonstrates advanced data handling techniques with various data structures:
1. Create and manipulate nested dictionaries representing a database of employees with complex data types.
2. Use JSON to serialize and deserialize a complex data structure containing lists, dictionaries, and custom objects.
3. Implement a class to represent a student with attributes and methods for data manipulation.
4. Use collections.Counter to analyze frequency of items in a dataset.
5. Demonstrate the use of defaultdict for grouping data by categories.
6. Implement a generator function to process large datasets efficiently.
7. Use itertools to create complex combinations and permutations of data.
8. Handle missing data using pandas DataFrames with different strategies (filling, dropping).
9. Convert between different data formats (dictionary, list, DataFrame, JSON).
10. Perform data validation using type hints and Pydantic models.
Write a comprehensive Python program that demonstrates advanced data handling techniques with various data structures:
1. Create and manipulate nested dictionaries representing a database of employees with complex data types.
2. Use JSON to serialize and deserialize a complex data structure containing lists, dictionaries, and custom objects.
3. Implement a class to represent a student with attributes and methods for data manipulation.
4. Use collections.Counter to analyze frequency of items in a dataset.
5. Demonstrate the use of defaultdict for grouping data by categories.
6. Implement a generator function to process large datasets efficiently.
7. Use itertools to create complex combinations and permutations of data.
8. Handle missing data using pandas DataFrames with different strategies (filling, dropping).
9. Convert between different data formats (dictionary, list, DataFrame, JSON).
10. Perform data validation using type hints and Pydantic models.
import json
from collections import Counter, defaultdict
from itertools import combinations, permutations
import pandas as pd
from typing import Dict, List, Any, Optional
from pydantic import BaseModel, Field
import numpy as np
# 1. Create nested dictionary representing employee database
employee_db = {
'employees': [
{
'id': 1,
'name': 'Alice Johnson',
'department': 'Engineering',
'salary': 85000,
'projects': ['Project A', 'Project B'],
'skills': {'Python': 8, 'JavaScript': 6, 'SQL': 7},
'hobbies': ['reading', 'hiking']
},
{
'id': 2,
'name': 'Bob Smith',
'department': 'Marketing',
'salary': 75000,
'projects': ['Project C'],
'skills': {'Photoshop': 9, 'SEO': 8, 'Copywriting': 7},
'hobbies': ['gaming', 'cooking']
},
{
'id': 3,
'name': 'Charlie Brown',
'department': 'Engineering',
'salary': 92000,
'projects': ['Project A', 'Project D'],
'skills': {'Python': 9, 'C++': 7, 'Linux': 8},
'hobbies': ['coding', 'swimming']
}
]
}
# 2. JSON serialization and deserialization
print("JSON Serialization:")
json_data = json.dumps(employee_db, indent=2)
print(json_data)
print("\nJSON Deserialization:")
loaded_data = json.loads(json_data)
print(f"Loaded data type: {type(loaded_data)}")
# 3. Student class with methods
class Student(BaseModel):
name: str
age: int
grades: List[float]
major: str = Field(..., alias='major')
def average_grade(self) -> float:
return sum(self.grades) / len(self.grades)
def is_honors_student(self) -> bool:
return self.average_grade() >= 3.5
def get_skill_level(self, skill: str) -> Optional[int]:
if hasattr(self, 'skills') and skill in self.skills:
return self.skills[skill]
return None
# 4. Using Counter to analyze data
print("\nUsing Counter to analyze skills:")
all_skills = []
for emp in employee_db['employees']:
all_skills.extend(emp['skills'].keys())
skill_counter = Counter(all_skills)
print("Skill frequencies:", skill_counter)
# 5. Using defaultdict for grouping data
print("\nUsing defaultdict to group employees by department:")
dept_groups = defaultdict(list)
for emp in employee_db['employees']:
dept_groups[emp['department']].append(emp['name'])
for dept, names in dept_groups.items():
print(f"{dept}: {names}")
# 6. Generator function for processing large datasets
def large_dataset_generator(size: int):
"""Generator that yields numbers from 1 to size"""
for i in range(1, size + 1):
yield i * 2 # Double each number
print("\nUsing generator to process large dataset:")
gen = large_dataset_generator(1000)
print("First 10 values from generator:", [next(gen) for _ in range(10)])
# 7. Using itertools for combinations and permutations
#python #programming #question #advanced #imageprocessing #opencv
Write a Python program that demonstrates advanced image processing techniques using various libraries and approaches:
1. Load an image from file and display it in multiple formats (RGB, grayscale, HSV).
2. Perform color space conversion between RGB, Grayscale, and HSV.
3. Apply different types of filters (Gaussian, median, bilateral) to reduce noise.
4. Implement edge detection using Canny and Sobel operators.
5. Use morphological operations (erosion, dilation, opening, closing) on binary images.
6. Detect and draw contours in the image.
7. Apply image thresholding (simple, adaptive, Otsu's method).
8. Implement image transformations (rotation, scaling, translation).
9. Use OpenCV's feature detection algorithms (SIFT, ORB) to find keypoints.
10. Save processed images in different formats (JPEG, PNG, TIFF).
Write a Python program that demonstrates advanced image processing techniques using various libraries and approaches:
1. Load an image from file and display it in multiple formats (RGB, grayscale, HSV).
2. Perform color space conversion between RGB, Grayscale, and HSV.
3. Apply different types of filters (Gaussian, median, bilateral) to reduce noise.
4. Implement edge detection using Canny and Sobel operators.
5. Use morphological operations (erosion, dilation, opening, closing) on binary images.
6. Detect and draw contours in the image.
7. Apply image thresholding (simple, adaptive, Otsu's method).
8. Implement image transformations (rotation, scaling, translation).
9. Use OpenCV's feature detection algorithms (SIFT, ORB) to find keypoints.
10. Save processed images in different formats (JPEG, PNG, TIFF).
import cv2
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
# 1. Load image and display basic information
def load_and_display_info(image_path):
img = cv2.imread(image_path)
if img is None:
raise FileNotFoundError(f"Image not found: {image_path}")
print("Image loaded successfully")
print(f"Shape: {img.shape}")
print(f"Data type: {img.dtype}")
print(f"Dimensions: {len(img.shape)}")
return img
# 2. Display image in different formats
def display_images(images, titles, figsize=(15, 10)):
fig, axes = plt.subplots(2, 4, figsize=figsize)
axes = axes.ravel()
for i, (img, title) in enumerate(zip(images, titles)):
if len(img.shape) == 2: # Grayscale
axes[i].imshow(img, cmap='gray')
else: # Color
axes[i].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
axes[i].set_title(title)
axes[i].axis('off')
plt.tight_layout()
plt.show()
# 3. Convert color spaces
def convert_color_spaces(img):
# RGB to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# RGB to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Grayscale to RGB
gray_rgb = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
return gray, hsv, gray_rgb
# 4. Apply different filters
def apply_filters(img):
# Gaussian blur
gaussian = cv2.GaussianBlur(img, (5, 5), 0)
# Median filter
median = cv2.medianBlur(img, 5)
# Bilateral filter
bilateral = cv2.bilateralFilter(img, 9, 75, 75)
return gaussian, median, bilateral
# 5. Edge detection
def edge_detection(img):
# Convert to grayscale first
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Canny edge detection
canny = cv2.Canny(gray, 100, 200)
# Sobel edge detection
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
sobel = np.sqrt(sobel_x**2 + sobel_y**2)
return canny, sobel
# 6. Morphological operations
def morphological_operations(img):
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Threshold to create binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Erosion
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(binary, kernel, iterations=1)
# Dilation
dilation = cv2.dilate(binary, kernel, iterations=1)
# Opening (erosion followed by dilation)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
# Closing (dilation followed by erosion)
closing = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
return erosion, dilation, opening, closing
# 7. Thresholding methods
def thresholding_methods(img):
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Simple thresholding
_, thresh_simple = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Adaptive thresholding
thresh_adaptive = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 11, 2)
#python #programming #question #advanced #osoperations
Write a Python program that demonstrates advanced operating system interactions with the following requirements:
1. List all files and directories in the current directory with detailed information (size, modification time).
2. Create a new directory and move a file into it.
3. Execute a system command to list processes and capture its output.
4. Get the current user's home directory and environment variables.
5. Check if a process is running by name.
6. Set and get environment variables.
7. Create a temporary file and clean it up after use.
By: @DataScienceQ ๐
Write a Python program that demonstrates advanced operating system interactions with the following requirements:
1. List all files and directories in the current directory with detailed information (size, modification time).
2. Create a new directory and move a file into it.
3. Execute a system command to list processes and capture its output.
4. Get the current user's home directory and environment variables.
5. Check if a process is running by name.
6. Set and get environment variables.
7. Create a temporary file and clean it up after use.
import os
import subprocess
import shutil
import tempfile
import psutil
from pathlib import Path
import sys
# 1. List files and directories with detailed information
def list_directory_details():
print("Directory contents with details:")
for entry in os.scandir('.'):
try:
stat = entry.stat()
print(f"{entry.name:<20} {stat.st_size:>8} bytes, "
f"modified: {stat.st_mtime}")
except Exception as e:
print(f"{entry.name}: Error - {e}")
# 2. Create directory and move file
def create_and_move_file():
# Create new directory
new_dir = "test_directory"
os.makedirs(new_dir, exist_ok=True)
# Create a test file
test_file = "test_file.txt"
with open(test_file, 'w') as f:
f.write("This is a test file.")
# Move file to new directory
destination = os.path.join(new_dir, test_file)
shutil.move(test_file, destination)
print(f"Moved {test_file} to {destination}")
# 3. Execute system command and capture output
def execute_system_command():
# List processes using ps command
result = subprocess.run(['ps', '-eo', 'pid,comm'],
capture_output=True, text=True)
print("\nRunning processes:")
print(result.stdout)
# 4. Get user information
def get_user_info():
print(f"\nCurrent user: {os.getlogin()}")
print(f"Home directory: {os.path.expanduser('~')}")
print(f"Current working directory: {os.getcwd()}")
# 5. Check if process is running
def check_process_running(process_name):
for proc in psutil.process_iter(['name']):
try:
if process_name.lower() in proc.info['name'].lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
# 6. Environment variables
def manage_environment_variables():
# Get environment variables
print(f"\nPATH variable: {os.environ.get('PATH')}")
print(f"HOME variable: {os.environ.get('HOME')}")
# Set a new environment variable
os.environ['TEST_VAR'] = 'Hello from Python'
print(f"Set TEST_VAR: {os.environ.get('TEST_VAR')}")
# 7. Temporary files
def use_temporary_files():
# Create temporary file
with tempfile.NamedTemporaryFile(delete=False) as temp:
temp.write(b"This is a temporary file.")
temp_path = temp.name
print(f"Created temporary file: {temp_path}")
# Clean up
os.unlink(temp_path)
print("Temporary file deleted.")
# Main function to demonstrate all techniques
def main():
print("=== Operating System Operations Demo ===\n")
# 1. List directory details
list_directory_details()
# 2. Create directory and move file
create_and_move_file()
# 3. Execute system command
execute_system_command()
# 4. Get user info
get_user_info()
# 5. Check if process is running
print(f"\nIs Python running? {check_process_running('python')}")
# 6. Manage environment variables
manage_environment_variables()
# 7. Use temporary files
use_temporary_files()
print("\nAll operations completed successfully.")
if __name__ == "__main__":
main()
By: @DataScienceQ ๐
๐2