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

Python Answer chatgpt

Uploaded by

madhimullai
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)
2 views

Python Answer chatgpt

Uploaded by

madhimullai
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/ 11

Python Answer

1a. R: Create a List of Vectors and Modify:


# Create a list of vectors
my_list <- list(a = c(1, 2, 3), b = c(4, 5, 6), c = c(7, 8, 9))
# Add a new vector
my_list$d <- c(10, 11, 12)
# Remove an element
my_list$b <- NULL
# Modify an element
my_list$a <- c(100, 200, 300)
print(my_list)

1b. OpenCV: Canny Edge Detection:


import cv2
# Load an image
image = cv2.imread("input.jpg", cv2.IMREAD_GRAYSCALE)
# Apply Canny Edge Detection
edges = cv2.Canny(image, 100, 200)
# Display results
cv2.imshow("Edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

2a. Python: Find Number of Dimensions in NumPy Array:


import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
# Get the number of dimensions
print("Number of dimensions:", array.ndim)
2b. OpenCV: Smoothing Operations:
image = cv2.imread("input.jpg")
# Apply different smoothing techniques
blur = cv2.blur(image, (5, 5))
gaussian = cv2.GaussianBlur(image, (5, 5), 0)
median = cv2.medianBlur(image, 5)
bilateral = cv2.bilateralFilter(image, 9, 75, 75)
# Display results
cv2.imshow("Blur", blur)
cv2.imshow("Gaussian", gaussian)
cv2.imshow("Median", median)
cv2.imshow("Bilateral", bilateral)
cv2.waitKey(0)
cv2.destroyAllWindows()

3a. Python: Case Conversion and Length of String:


string = "Hello World"
# Case conversion
print("Uppercase:", string.upper())
print("Lowercase:", string.lower())
# Length of string
print("Length of string:", len(string))

3b. R: Scatter Plot, Box Plot, Histogram:


data <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Scatter plot
plot(data, main="Scatter Plot")
# Box plot
boxplot(data, main="Box Plot")
# Histogram
hist(data, main="Histogram", col="blue")
4a. OpenCV: Visualize RGB Channels:
image = cv2.imread("input.jpg")
# Split into channels
b, g, r = cv2.split(image)
# Display channels
cv2.imshow("Blue Channel", b)
cv2.imshow("Green Channel", g)
cv2.imshow("Red Channel", r)
cv2.waitKey(0)
cv2.destroyAllWindows()

4b. Python: Simple Calculator:


def calculator(a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a / b
else:
return "Invalid operation"
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")
print("Result:", calculator(a, b, op))
5a. R: Create a 3x3 Array from Two Vectors:
# Vectors
vec1 <- c(1, 2, 3, 4, 5, 6)
vec2 <- c(7, 8, 9, 10, 11, 12)
# Create a 3x3 array
array <- array(c(vec1, vec2), dim = c(3, 3, 2))
print(array)

5b. OpenCV: Rotate Image by Specific Angles:


import cv2
import numpy as np
# Load the image
image = cv2.imread("input.jpg")
def rotate_image(img, angle):
h, w = img.shape[:2]
center = (w // 2, h // 2)
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
return cv2.warpAffine(img, matrix, (w, h))
# Rotate and display images
angles = [60, 110, 155]
for angle in angles:
rotated = rotate_image(image, angle)
cv2.imshow(f"Rotated by {angle} degrees", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
6a. OpenCV: Write Text on Image:
image = cv2.imread("input.jpg")
# Write text
cv2.putText(image, "Hello, OpenCV!", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
2)
# Display result
cv2.imshow("Image with Text", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

6b. R: Create Matrix from Vector:


# Given vector
vec <- c(1, 2, 3, 4, 5, 6)
# Create matrix
matrix <- matrix(vec, nrow = 2, byrow = TRUE)
print(matrix)

7a. OpenCV: Arithmetic Operations:


# Load an image
image = cv2.imread("input.jpg")
value = 50
# Perform operations
added = cv2.add(image, value)
subtracted = cv2.subtract(image, value)
# Display results
cv2.imshow("Added", added)
cv2.imshow("Subtracted", subtracted)
cv2.waitKey(0)
cv2.destroyAllWindows()
7b. Python: Sort Words Alphabetically:
sentence = "Sorting words in alphabetical order"
# Sort words
words = sentence.split()
words.sort()
print(" ".join(words))

8a. OpenCV: Resize Image:


image = cv2.imread("input.jpg")
# Resize
resized = cv2.resize(image, (300, 300))
# Display resized image
cv2.imshow("Resized", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

8b. R: Extract First Two Rows:


# Sample data
data <- matrix(1:12, nrow = 4, byrow = TRUE)
# Extract first two rows
result <- data[1:2, ]
print(result)

9a. R: Create Matrix:


vec <- c(10, 20, 30, 40, 50, 60)
matrix <- matrix(vec, nrow = 2, byrow = TRUE)
print(matrix)
9b. Python: Case Conversion and String Length:
string = "Hello World"
# Case conversion
print("Uppercase:", string.upper())
print("Lowercase:", string.lower())
# Length of string
print("Length of string:", len(string))

10a. OpenCV: Capture Image with cv2.VideoCapture():


cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow("Video Feed", frame)
# Capture on pressing 'c'
if cv2.waitKey(1) & 0xFF == ord('c'):
cv2.imwrite("captured_image.jpg", frame)
break
cap.release()
cv2.destroyAllWindows()

10b. R: Scatter Plot, Box Plot, Histogram:


data <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# Scatter plot
plot(data, main="Scatter Plot")
# Box plot
boxplot(data, main="Box Plot")
# Histogram
hist(data, main="Histogram", col="blue")
11a. R: List of Vectors and Modification:
# Create a list of vectors
my_list <- list(a = c(1, 2, 3), b = c(4, 5, 6), c = c(7, 8, 9))
# Add a new vector
my_list$d <- c(10, 11, 12)
# Remove an element
my_list$b <- NULL
# Modify an element
my_list$a <- c(100, 200, 300)
print(my_list)

11b. OpenCV: Save Video:


cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
ret, frame = cap.read()
if not ret:
break
out.write(frame)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
12a. OpenCV: Display Image with imshow():
image = cv2.imread("input.jpg")
cv2.imshow("Display Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

12b. Python: Sort Words:


sentence = "Sorting words in alphabetical order"
# Sort words
words = sentence.split()
words.sort()
print(" ".join(words))

13a. OpenCV: Rotate by 90, 160 Degrees:


import cv2
import numpy as np
# Load the image
image = cv2.imread("input.jpg")
def rotate_image(img, angle):
h, w = img.shape[:2]
center = (w // 2, h // 2)
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
return cv2.warpAffine(img, matrix, (w, h))
# Rotate and display images
angles = [90,160]
for angle in angles:
rotated = rotate_image(image, angle)
cv2.imshow(f"Rotated by {angle} degrees", rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
13b. Python: Case Conversion and Length:
string = "Hello World"
# Case conversion
print("Uppercase:", string.upper())
print("Lowercase:", string.lower())
# Length of string
print("Length of string:", len(string))

14a. R: Extract First Two Rows:


# Sample data
data <- matrix(1:12, nrow = 4, byrow = TRUE)
# Extract first two rows
result <- data[1:2, ]
print(result)

14b. OpenCV: Smoothing Operations:


image = cv2.imread("input.jpg")
# Apply different smoothing techniques
blur = cv2.blur(image, (5, 5))
gaussian = cv2.GaussianBlur(image, (5, 5), 0)
median = cv2.medianBlur(image, 5)
bilateral = cv2.bilateralFilter(image, 9, 75, 75)
# Display results
cv2.imshow("Blur", blur)
cv2.imshow("Gaussian", gaussian)
cv2.imshow("Median", median)
cv2.imshow("Bilateral", bilateral)
cv2.waitKey(0)
cv2.destroyAllWindows()
15a. Python: Simple Calculator:
def calculator(a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a / b
else:
return "Invalid operation"
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")
print("Result:", calculator(a, b, op))

15b. Python: Pandas String Operations:


import pandas as pd
data = pd.Series(["apple", "banana", "cherry"])
print("Uppercase:", data.str.upper())
print("Lowercase:", data.str.lower())
print("Lengths:", data.str.len())

You might also like