Challenge - 5 Problems
Complex Number Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of numpy complex array addition
What is the output of this code that adds two numpy arrays of complex numbers?
NumPy
import numpy as np arr1 = np.array([1+2j, 3+4j]) arr2 = np.array([5+6j, 7+8j]) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Remember how complex numbers add: real parts add, imaginary parts add.
✗ Incorrect
Adding complex numbers adds their real parts and imaginary parts separately. So (1+2j)+(5+6j) = (1+5)+(2+6)j = 6+8j, similarly for the second element.
❓ data_output
intermediateShape and dtype of complex numpy array
What is the shape and data type of this numpy array?
NumPy
import numpy as np arr = np.array([[1+1j, 2+2j], [3+3j, 4+4j]]) print(arr.shape, arr.dtype)
Attempts:
2 left
💡 Hint
Check the shape of the nested list and default complex dtype in numpy.
✗ Incorrect
The array is 2 rows by 2 columns, so shape is (2, 2). By default, numpy uses complex128 for complex numbers.
🔧 Debug
advancedIdentify the error in complex number array creation
What error does this code raise when trying to create a numpy array of complex numbers?
NumPy
import numpy as np arr = np.array([1+2, 3+4j])
Attempts:
2 left
💡 Hint
Check how numpy handles mixed numeric types including complex and int.
✗ Incorrect
Numpy converts all elements to a common type that can hold all values. Here, int 1+2 is just 3, so array becomes [3, 3+4j] and dtype complex128.
❓ visualization
advancedVisualizing real and imaginary parts of complex array
Which option correctly plots the real and imaginary parts of this complex numpy array?
NumPy
import numpy as np import matplotlib.pyplot as plt arr = np.array([1+2j, 3+4j, 5+6j]) plt.plot(arr.real, label='Real') plt.plot(arr.imag, label='Imaginary') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Look at what plt.plot(arr.real) and plt.plot(arr.imag) produce.
✗ Incorrect
The code plots two lines: one for the real parts and one for the imaginary parts of the complex array elements.
🧠 Conceptual
expertEffect of numpy complex dtype on arithmetic precision
Which statement about numpy complex dtypes is correct?
Attempts:
2 left
💡 Hint
Recall how floating point precision relates to complex number storage.
✗ Incorrect
complex64 stores two 32-bit floats (real and imaginary), while complex128 stores two 64-bit floats, so complex128 has higher precision.
