Python Code Solutions
Python Code Solutions
a=5
b = 10
a, b = b, a
if char.isalpha():
else:
# 3. Write a program in Python to shuffle a deck of cards using the module random and draw 5 cards.
import random
deck = [f"{rank} of {suit}" for rank in ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
random.shuffle(deck)
# 5. Write a program in Python to transpose a given matrix M = [[1, 2], [4, 5], [3, 6]].
import statistics
median = statistics.median(numbers)
def find_resolution(image_path):
return img.size
print("Resolution:", find_resolution("image.jpg"))
# 8. Write a program in Python to convert a decimal number to binary, octal, and hexadecimal number.
print("Binary:", bin(decimal))
print("Octal:", oct(decimal))
print("Hexadecimal:", hex(decimal))
print("\n" + "-"*30 + "\n")
words.sort()
ages = [21, 54, 66, 44, 32, 42, 54, 62, 93, 45, 32, 70]
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.show()
# 11. Create a 3-D plot in Python for the function √(y^2 - x^2).
import numpy as np
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.sqrt(Y**2 - X**2)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.show()
% MATLAB Code Solutions
% 1. Given two sides a= 3.2 and b=4.6 of a triangle and angle theta= 60°.
a = 3.2;
b = 4.6;
disp(' ');
x = 1.5;
disp(' ');
% 3. Extend the 2-D array to 3-D array by including another 2-D array as second element.
A = [1 2 3; 5 4 3; 1 3 6];
A(:,:,2) = [7 8 9; 6 5 4; 3 2 1];
disp(A);
disp(' ');
A = [1 2 3 5; 6 7 9 10; 11 4 8 12];
disp(B);
disp(' ');
z = [2; 3; 4; 5];
A = diag(z);
B = diag(z, 1);
C = diag(z, -1);
disp(A);
disp(B);
disp(C);
disp(' ');
syms x;
integral_y = int(y, x) + 3;
disp("Integrated Polynomial:");
disp(integral_y);
disp(' ');
x = [0 1 2 4];
y = [1 6 20 100];
p = polyfit(x, y, 2);
disp(p);
disp(' ');
pause(3);
disp(' ');
fclose(fileID);
disp(' ');
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);
xlabel('x');
ylabel('y');
title('Plot of y = sin(x)');
disp(' ');
x = [1 2 3 4 5 6];
y = [10 15 25 30 27 19];
bar(x, y);
xlabel('x');
ylabel('y');
title('Bar Graph');
disp(' ');
% 12. 3-D plot with parametric equations x = t^2, y = 4t for -4 < t < 4.
t = -4:0.1:4;
x = t.^2;
y = 4 * t;
plot3(t, x, y);
xlabel('t');
ylabel('x');
zlabel('y');
disp(' ');
% 13. Write a program in MATLAB to find the count of even values in the given n numbers.
% Other questions...
% 14. Write a function in MATLAB to calculate the roots of the quadratic equation ax^2 + bx + c = 0.
elseif discriminant == 0
root = -b / (2 * a);
else
realPart = -b / (2 * a);
end
end
% Example usage:
a = 1;
b = -3;
c = 2;
disp(roots_of_quadratic);
disp(' ');
% Continue with other code sections or save each function separately if needed.