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
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. Your C program should take the...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. * Your C program should take...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
Two sorted lists have been created, one implemented using a linked list (LinkedListLibrary linkedListLibrary) and the...
Two sorted lists have been created, one implemented using a linked list (LinkedListLibrary linkedListLibrary) and the other implemented using the built-in Vector class (VectorLibrary vectorLibrary). Each list contains 100 books (title, ISBN number, author), sorted in ascending order by ISBN number. Complete main() by inserting a new book into each list using the respective LinkedListLibrary and VectorLibrary InsertSorted() methods and outputting the number of operations the computer must perform to insert the new book. Each InsertSorted() returns the number of...
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 a flowchart using the bisection method when a=2 and b=5 and y=(x-3)3-1
Create a flowchart using the bisection method when a=2 and b=5 and y=(x-3)3-1
1 Linear Algebra in Numpy (1) Create a random 100-by-100 matrix M, using numpy method "np.random.randn(100,...
1 Linear Algebra in Numpy (1) Create a random 100-by-100 matrix M, using numpy method "np.random.randn(100, 100)", where each element is drawn from a random normal distribution. (2) Calculate the mean and variance of all the elements in M; (3) Use "for loop" to calculate the mean and variance of each row of M. (4) Use matrix operation instead of "for loop" to calculate the mean of each row of M, hint: create a vector of ones using np.ones(100, 1)?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT