In: Computer Science
LEVEL ONE
Hint: Use the inline for and range().
Hint: Explore the for … if … statement.
LEVEL TWO
Hint: Explore numpy.random.rand().
NB: THIS IS A PYTHON ASSIGNMENT
Level One
x = [ele for ele in range(1,100,5)] # list x from 1 to 100 with interval of 5
print(x) #print list x
y = [num for num in x if num % 2 == 0] # list y which has elements from x divisble by 2
print(y) #print list y
Using Numpy
import numpy as np #import numpy package
x = np.arange(1,100,5) # array x from 1 to 100 with interval of 5
print(x) #print array x
y = x[np.where(x%2==0)] # array y which has elements from x divisble by 2
print(y) #print array y
Level Two
x = np.random.uniform(0,10,100) # array of 100 elements in uniform distribution with low = 0 and high = 10
print(x[x>5]) # values from x which are greater than 5
print("*"*100)
print(min(x), max(x)) #minimum and maximum value of array x
print("*"*100)
print(np.argmin(x), np.argmax(x)) # index of minimum and maximum value of x
Screenshot
Level one without using Numpy
Level one using Numpy
Level Two
Comments has been given in the code. Also, Screenshot of code as well as output has been given for the reference.