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 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.
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...
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 such that There exists a list of emails List Ls =...
Write a program in python such that There exists a list of emails List Ls = ['[email protected]','[email protected]','[email protected]','[email protected]',[email protected]'] Count the number of emails IDS which ends in "ac.in" Write proper program with proper function accepting the argument list of emails and function should print the number of email IDs and also the email IDS ending with ac.in output 2 [email protected] [email protected] ================================= i am trying like this but getting confused len [True for x in Ls if x.endswith('.ac.in')] please write complete...
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
Write a Python program that performs the following list operations. Part A Define a list called...
Write a Python program that performs the following list operations. Part A Define a list called numList with elements. 84, 94, 27, 74, 19, 90, 16, 21, 56, 50, 77, 59, 41, 63, 18, 26, 80, 74, 57, 30, 40, 93, 70, 28, 14, 11, 43,65, 91, 83, 22, 53, 74, 44, 73, 55, 47, 74, 81 Display the followings: All the numbers in numList The number of elements in numList The smallest number in numList The largest number in...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT