1. Write a program to request the user enter a desired number of values. Create
ID: 3856736 • Letter: 1
Question
1. Write a program to request the user enter a desired number of values. Create a loop to load the desired number of user-specified values into a list. Create two lists with the same values but generated in different ways. One list will be originally initialized to have the desired length before entering the loop and values placed at the appropriate index position cach time through the loop. The other list will begin empty and values appended for every iteration of the loop. Output fro m the program shoul d appear as follows: How many values do you want? !5 Enter val #1: 5 Enter val #2: 4 Enter val #3 : 3 Enter val #4: 2 Enter val #5: 1 The first list is [5.0, 4.0, 3.0, 2.0, 1.0] The second list is [5.0, 4.0, 3.0, 2.0, 1.0] 2. Using the numpy.random.rand command, create a 5x4 array of random values. Display the array and the row/col position of the maximum value using the numpy.where command. Output should look like: Row of largest value is xxx Column of largest value is xxx 3. Use the matplotlib.image.imread command to load the stinkbug.png file, which returns a NumPy array containing floats. The array is 375 rows by 500 columns because the image resolution is 500x375 pixels, and the grayscale pixel values vary between 0 and 1. Without using a built-in function, manually create a histogram by looping through 20 equally spaced intervals between 0 and 1 and finding the number of image pixels in each range. Display the grayscale image using the matplotlib.image.imshow command by setting the cmap input argument to 'gray' (below left). Then use the matplotlib.pyplot.bar command to display the histogram (below right) Note that the default width of 0.8 will be too large for this case.Explanation / Answer
Here is the program for the first problem
Code:
print "How many values do you want?"
'''length is a variable that stores the desired number of values'''
length=input()
'''list1 and list2 are the two lists.
list1 is having a predefined size of length '''
list1=[None]*length
list2=list()
'''range(length) stands for 0 to length. For loop interates from 0 to length'''
for i in range(length):
print "Enter val #",(i+1)
temp=input() #input for list
list1[i]=temp #insert at i location or use insert(position,value) method
list2.append(temp) #append method is used to append the number in list2
print "The first list is ",list1
print "The second list is ",list2
Note: ''' xyz '''. Three quotes stand for multi line comments. # stands for line comments. I have explained the purpose of each statement using comments. Comments are shown in bold for your reference.
If you need .0 values in list as shown in output then multiply temp with 1.0. Replace the statements (list1[i]=temp) with (list[i]=temp*1.0) and (list2.append(temp) ) with (list2.append(temp*1.0)) .
The four statements after the for loop statement needs to be indented if you receive the answer left aligned.
Code for second problem
Code:
import numpy
a=numpy.random.random((5, 4)) #array a is generated using the given command
print a #Displaying the array
x=numpy.where(a==numpy.max(a)) #returns a tuple of row,column where max value is found
print "Row of largest value is ",int(x[0])
print "Column of largest value is ",int(x[1])
'''numpy.where() returns an array of row index and an array of columns index while used on 2d array'''
Note: As python follows zero indexing I have added 1 to the row and column value while printing, remove it if not needed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.