Question

In: Computer Science

LEVEL ONE Lists and indexing Create a list x that spans 1 to 100 using 5...

LEVEL ONE

  1. Lists and indexing
    1. Create a list x that spans 1 to 100 using 5 unit increments.

                                Hint: Use the inline for and range().

  1. Create a list y containing the elements from x that are divisible by 2.

                                Hint: Explore the for … if … statement.

  1. Do b. in another manner. Hint: Use , e.g., array() and where() from numpy.

LEVEL TWO

  1. Arrays in numpy
    1. Create an array x that contains a random set of 100 uniform(0,10) variables.
  2.                                 Hint: Explore numpy.random.rand().

    1. Find the values in x greater than 5.
    2. Find the maximum and minimum values without sorting. Hint: Explore numpy.min() and numpy.max().
    1. Find the indices associated with the min/max in c.   Hint: Explore numpy.argmin() and numpy.argmax().

NB: THIS IS A PYTHON ASSIGNMENT   

Solutions

Expert Solution

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.


Related Solutions

The variable x takes the values ​​from 1 to 100, with these values, create two lists,...
The variable x takes the values ​​from 1 to 100, with these values, create two lists, one where the prime numbers are stored in one, and another where the non-prime numbers are stored, indicate and print the result
Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to...
Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to represent a tic tac toe board, write code to check if the board has a winner.
Using Python : Create a 4×5 4 × 5 array ? X with random value. Print...
Using Python : Create a 4×5 4 × 5 array ? X with random value. Print out ? X , as well as the largest value of each column.
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Create an unsorted LIST class. Each list should be able to store 100 names.
Create an unsorted LIST class. Each list should be able to store 100 names.
A solid steel bar with dimensions of 1 x 4 inches spans a 20 foot gap....
A solid steel bar with dimensions of 1 x 4 inches spans a 20 foot gap. The beam is loaded with a single 1000 lb load a t mid span. Calculate the deflection of the beam at every foot along it s length . (That is, calculate the deflecton at x=0, 12,24, 36 ... inches) INCLUDE the weight of the beam. Plot the deflection, y, versus x
Using the Chinese remainder theorem solve for x: x = 1 mod 3 x = 5...
Using the Chinese remainder theorem solve for x: x = 1 mod 3 x = 5 mod 7 x = 5 mod 20 Please show the details, I`m trying to understand how to solve this problem since similar questions will be on my exam.
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
5 A data set lists earthquake depths. The summary statistics are n = 600, ¯ x...
5 A data set lists earthquake depths. The summary statistics are n = 600, ¯ x = 4.66 km, s = 4.42 km.Usea0.01significanceleveltotesttheclaimof a seismologist that these earthquakes are from a population with a mean equal to 4.00. Assume that a simple random sample has been selected.
Describe the following four types of lists: 1) Master Risks List 2) Risks by Services List...
Describe the following four types of lists: 1) Master Risks List 2) Risks by Services List 3) Top Risks List 4) Retired Risks List
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT