Question

In: Computer Science

(+30) Write a python program per the following specifications: Populate an array(list) of size 50 randomly...

(+30) Write a python program per the following specifications:

  1. Populate an array(list) of size 50 randomly with only integers 0 and 1
  2. Repeat step1 n = 1000 times using either a while loop or a for loop

At this point you should have a total of 50000 observations

  1. Display the number of 0’s (use the count() function from prior labs)
  2. Display the number of 1’s (use the count() function from prior labs)
  3. Using the Binomial distribution formulas
    1. Display the expected mean (average) of the 1’s == n*size*.5
    2. Calculate and display the standard deviation (SD)

NOTE: sd should be > 100

    1. Display the range as follows:   mean + SD mean - SD
  1. Answer the question: is the total number of 1’s from 4 above. within the range as calculated in 5.c by printing

                                     ‘Yes’ if it is within the range calculated in 5c

                                     ‘No’   if it is not

  1. Display the n*size (== 50000) integers 50 integers per line That is, display the integers in myList 50 per line for 1000 lines See sample output
  2. Display the average of all n*size == 50000 integers (should be < 1 by adding the answers from 3 and 4 above and divide by n*size

my work:

import random
import math
myArray=[]
a=0
b=1
size= 50
j=0
n=1000
#populate array with size=50 random integers in range 0-1 and repeat for 1000 times
while(j<n):
for k in range(size):
randNum=random.randint(a,b)
myArray.insert(k,randNum)
j=j+1
#display array size
print("size of my array is ",len(myArray))
#print myArray 50 values per line
k=0
sub=[]
while(k<len(myArray)):
sub=myArray[k:k+50]
print("...",k,sub)
k=k+50

#count no. of ones and zeroes
zeros=0
ones=0
i=0
while(i<len(myArray)):
if(myArray[i]==1):
ones=ones+1
else:
zeros=zeros+1
i=i+1

print("number of 1 is ",ones)
print("number of 0 is ",zeros)

#calculate mean and SD of binomial distribution
#probability of occurence of 1 is p = 1/2 and occurence of 0 is q =1/2
#mean of Binomial distribution = np
#SD(standard deviation) of Binomial distribution = (npq)^1/2
#here n=50000 or length of array
n=len(myArray)
p=0.5
q=0.5
mean= n*p #expected mean of no of ones
sd = math.sqrt(n*p*q)
print("===============================")
print("mean of 1s in My Array: ",mean," sd(standard deviation): ",sd)
Lrange=int(round(mean-sd))
Rrange=int(round(mean+sd))
print("range: mean+-sd: ",Rrange," ",Lrange) #round off the range to nearest integer
#check if no of ones is within range
if(ones>= Lrange and ones<= Rrange):
output="Yes"
else:
output="No"

print("Actual number of ones is within the range mean+-sd ??? Ans is: ", output)

getting error line 11
for k in range(size):
^
IndentationError: expected an indented block please help

Solutions

Expert Solution

import random
import math
myArray=[]
a=0
b=1
size= 50
j=0
n=1000
#populate array with size=50 random integers in range 0-1 and repeat for 1000 times
while(j<n):
for k in range(size):
randNum=random.randint(a,b)
myArray.insert(k,randNum)
j=j+1
#display array size
print("size of my array is ",len(myArray))
#print myArray 50 values per line
k=0
sub=[]
while(k<len(myArray)):
sub=myArray[k:k+50]
print("...",k,sub)
k=k+50

#count no. of ones and zeroes
zeros=0
ones=0
i=0
while(i<len(myArray)):
if(myArray[i]==1):
ones=ones+1
else:
zeros=zeros+1
i=i+1

print("number of 1 is ",ones)
print("number of 0 is ",zeros)

#calculate mean and SD of binomial distribution
#probability of occurence of 1 is p = 1/2 and occurence of 0 is q =1/2
#mean of Binomial distribution = np
#SD(standard deviation) of Binomial distribution = (npq)^1/2
#here n=50000 or length of array
n=len(myArray)
p=0.5
q=0.5
mean= n*p #expected mean of no of ones
sd = math.sqrt(n*p*q)
print("===============================")
print("mean of 1s in My Array: ",mean," sd(standard deviation): ",sd)
Lrange=int(round(mean-sd))
Rrange=int(round(mean+sd))
print("range: mean+-sd: ",Rrange," ",Lrange) #round off the range to nearest integer
#check if no of ones is within range
if(ones>= Lrange and ones<= Rrange):
output="Yes"
else:
output="No"

print("Actual number of ones is within the range mean+-sd ??? Ans is: ", output)

code screenshot:

Expected output:


Related Solutions

Write a python program per the following specifications: 1. Populate an array(list) of size n =...
Write a python program per the following specifications: 1. Populate an array(list) of size n = 50 randomly with only integers 0 and 1 2. Repeat step 1 nn = 1000 times using either a while loop or a for loop see below At this point you should have a total of 50000 observations with either 0 or 1 This is our experimental data which we will compare to the theoretical expected result From Lab06template.py import random temp = -1...
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds) *Please do it in eclipse and this is java language*
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Use Python to write the following code. Program Specifications: You are to design the following menu:...
Use Python to write the following code. Program Specifications: You are to design the following menu: G] Get a number S] Display current sum A] Display current average H] Display the current highest number L] Display the current lowest number D] Display all numbers entered Q] Quit If the user enters G, S, A, H, L, or D before selecting G the program needs to advise the user to go and enter a number first. Rules for the Programmer (you)...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
(+30) Write a python program that generates four random between -50 and +50 using python’s random...
(+30) Write a python program that generates four random between -50 and +50 using python’s random number generator (RNG) see the following code (modify as needed ) import random a = 10 # FIX b =100 # FIX x = random.randint(a,b) # (1) returns an integer a <= x <= b # modify a and b according to the lab requirement print('random integer == ', x) and displays 1. the four integers 2. The maximum integer 3. The minimum integer...
(+30) Write a python program that generates four random between -50 and +50 using python’s random...
(+30) Write a python program that generates four random between -50 and +50 using python’s random number generator (RNG) see the following code (modify as needed ) import random a = 10 # FIX b =100 # FIX x = random.randint(a,b) # (1) returns an integer a <= x <= b # modify a and b according to the lab requirement print('random integer == ', x) and displays the four integers The maximum integer The minimum integer The number of...
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
write a program in python that insert a number in the middle of an array. Assume...
write a program in python that insert a number in the middle of an array. Assume that the length of an array is even. For instance, is a=(1,4,7,9) and num=100, then really=(1,4,100,7,9)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT