0
0
NumPydata~10 mins

Shuffling arrays in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to shuffle the array in-place using NumPy.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
np.random.[1](arr)
print(arr)
Drag options to blanks, or click blank then click option'
Ashuffle
Brandomize
Cpermutation
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.permutation() which returns a new shuffled array instead of shuffling in-place.
Using np.random.sort() which sorts the array instead of shuffling.
2fill in blank
medium

Complete the code to create a shuffled copy of the array without changing the original.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
shuffled_arr = np.random.[1](arr)
print(shuffled_arr)
print(arr)
Drag options to blanks, or click blank then click option'
Apermutation
Bshuffle
Crandom
Dchoice
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.shuffle() which shuffles the array in-place and changes the original.
Using np.random.choice() which samples elements but may repeat or reduce size.
3fill in blank
hard

Fix the error in the code to shuffle the array in-place.

NumPy
import numpy as np
arr = np.array([5, 6, 7, 8])
np.random.[1](arr)
print(arr)
Drag options to blanks, or click blank then click option'
Achoice
Bpermutation
Cshuffle
Dsample
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.permutation() with a size argument which is invalid.
Using np.random.choice() or np.random.sample() which do not shuffle in-place.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword > 3
Clen(word) > 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Checking if the word string is greater than 3 instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 3 letters.

NumPy
words = ['pear', 'fig', 'grape', 'kiwi']
result = { [1]: [2] for w in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 3
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using the word itself as value instead of length.
Not filtering words by length.