Ibm Ai
Ibm Ai
def get_cubed(lst):
return list(map(lambda x: x ** 3, lst))
lst = [1, 2, 3, 4]
result = get_cubed(lst)
print("Cubed values:", result)
def get_squared_evens(lst):
"""
INPUT: LIST (containing numeric elements)
OUTPUT: LIST (squared value of each even number in original list)
"""
squared_evens = []
for num in lst:
if num % 2 == 0: # Check if the number is even
squared_evens.append(num ** 2) # Square the even number
return squared_evens
# Example usage:
input_list = [1, 2, 3, 4, 5, 6, 7]
result = get_squared_evens(input_list)
print("Squared even values:", result)
boolean
integer
float
heap
Correct
Correct!
string
varchar
result_dict = {}
for key, value in zip(lst1, lst2):
result_dict[key] = value
return result_dict
# Example usage:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
result_dict = make_dict(list1, list2)
print("Created dictionary:", result_dict)
bool
int
float
set
list
string
tuple
complex
(Correct. Base Python by default processes code with a single core. Multiprocessing requires
loading additional libraries/packages.)
7. For a given input list: abbcccddddeeeeeffffffggggggghhhhhhhh
Return a dictionary of character counts
def count_characters(string):
"""
INPUT: STRING
OUTPUT: DICT (with counts of each character in input string)
return char_counts
# Example usage:
input_string = "abbcccddddeeeeeffffffggggggghhhhhhhh"
result_dict = count_characters(input_string)
print("Character counts:", result_dict)
Certainly! Let’s calculate the L1 norm (also known as the Manhattan norm) of the vector v =
[2.0, -3.5, 5.1].
The L1 norm of a vector is the sum of the absolute values of its components. For a vector v with
components x_1, x_2, …, x_n, the L1 norm is given by:
∥v∥1=∣x1∣+∣x2∣+…+∣xn∣
∥v∥1=∣2.0∣+∣−3.5∣+∣5.1∣=2.0+3.5+5.1=10.6
import numpy as np
def calculate_l1_norm(v):
"""
INPUT: LIST or ARRAY (containing numeric elements)
OUTPUT: FLOAT (L1 norm of v)
"""
l1_norm = np.linalg.norm(v, ord=1)
return l1_norm
# Example usage:
vector_v = [2.0, -3.5, 5.1]
result = calculate_l1_norm(vector_v)
print("L1 norm of vector v:", result)
return row_sums
# Example usage:
vectorLower = 1
vectorUpper = 151
result = get_vector_sum(vectorLower, vectorUpper)
print("Row sums:", result)
Row sums: [120, 345, 570, 795, 1020, 1245, 1470, 1695, 1920, 2145]
The geometric distribution is a useful tool for modeling time to event data. A successful
street vendor says that on average 1 out of every 10 people who walk by on the street
stop to buy a taco.
Args:
p (float): Probability of success in a single trial.
k (int): Number of trials (starts from 1).
Returns:
float: Geometric probability for k trials.
"""
if k < 1:
raise ValueError("Number of trials (k) must be at least 1.")
# Example usage:
success_probability = 0.1 # Assuming 1 out of 10 people stop to buy a ta
co
num_trials = 20
result = geometric_distribution(success_probability, num_trials)
print(f"Geometric probability for {num_trials} trials: {result:.4f}")
The Poisson distribution is a useful tool for modeling count data given discrete
intervals. Based on historical data the expected number of accidents at a busy
intersection is 4 per month.
Args:
mu (float): Average rate (mean) of accidents.
k (int): Number of accidents.
Returns:
float: Poisson probability for k accidents.
"""
if k < 0:
raise ValueError("Number of accidents (k) must be non-negative.")
# Example usage:
average_accidents = 4 # Average accidents per month
num_accidents = 7
result = poisson_distribution(average_accidents, num_accidents)
print(f"Probability of more than {num_accidents} accidents: {result:.4f}"
)
Args:
loc_val (float): Mean (average) of the distribution.
scale_val (float): Standard deviation of the distribution.
cdf_val (float): Value for which we want to find the cumulative p
robability.
Returns:
float: Probability of observing a score >= cdf_val.
"""
# Calculate the CDF at the given value
cdf = stats.norm(loc_val, scale_val).cdf(cdf_val)
# Example usage:
mean = 50.0
std_dev = 20.0
score = 80.0
result = gaussian_distribution(mean, std_dev, score)
print(f"Probability of observing a score >= {score}: {result:.4f}")
Args:
A (list of lists): First square matrix (size n-by-n).
B (list of lists): Second square matrix (size n-by-n).
Returns:
list of lists: Resulting matrix (product of A and B).
"""
n = len(A) # Assuming A and B are both n-by-n matrices
return C
# Example usage:
A = [[2, 3, 4], [6, 4, 2], [-1, 2, 0]]
B = [[1, 0, 2], [3, 1, 1], [2, 0, -1]]
result = matrix_multiplication(A, B)
for row in result:
print(row)
[19, 3, 3]
[22, 4, 14]
[5, 2, 0]