Question

In: Computer Science

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

n = 50

nn = 1000 # note nn*n == 50000

# Theoretical calculations

print('======Theoretical results (what is expected) ==========')

#Using the Binomial distribution formulas on page 1

p = .5

print('1 the theoretical mean(average)of 1''s ( n*nn*p)

==', temp) # fix u == n*nn*p this is the expected

(average number of 1’s

print('2 the theoretical variance v == ', temp)

# fix # v = n*nn*p*(1-p)

print('3 the standard deviation SD (should be > 100) == ', temp)

# sd = square root(v) sd = sqrt(v)

# at this point in the code # we have the theoretical mean, variance, standard deviation

# now for the experimental results

print(' ========== Experimental results ========== ')

print(' ========== Populate a list and count 1’s ========== ')

temp = -1

a = 0

b = 1

MyList = []

j = 0 while (j < nn): # loop for 1000 times

i = 0

while i < n : # loop 50 times populate list of nn

randNum = random.randint(a,b)

MyList.append(randNum)

# count the number of 1’s and the number of 0

# use the count function # the number of 1’s + number 0’s == 50

# keep track of these counts and update the results

# with the next iteration

i = i +1

j = j + 1

print('4 the length of MyList == ', temp ) # FIX

use len()

print('5 the number of 1’s is ', temp ) # FIX

print('6 the number of 0’s is ', temp ) # fix

print('7 the average of all the ', n*nn,' integers is...', 'should be < 1) ', temp) # fix

print()

# fix SD = sqrt(v)

# Display the range (+- 2sd ) as follows:

# mean+ 2*SD mean – 2*SD

print('8 mean - 2*SD == ', temp) #fix

print('9 mean + 2*SD == ', temp) #fix

# Answer the question:

# is the total number of 1’s from 5 above

# mean – 2*SD < mean < mean + 2*SD true ? Yes or no

# within the range as calculated in 8 and 9 by printing

#‘Yes’ if it is within the range and

# ‘No’ if it is not within the range

answer = 'NO'

print('10 Is the number of 1''s within ave +- 2*SD ', answer) # FIX

#Display 10 lines, 25 integers per line of the n*nn (== 50000) integers

# That is, display the integers in MyList, 25 per line, for 10 lines

# and include the line number as in 1 , 2, 3, …10

# NOTE only display 10 lines with 25 values (= 0 or 1)

# and not all 50,000 integers

print('11 displaying 10 numbered lines with 25 integers per line ...')

# for your consideration will need adjustments

k = 0

sub = []

while (k < len(MyList) ):

sub = MyList[k: k+20] # Splice

print("...",k, sub) # do NOT print k only the # line number as in 1, 2,3

k = k+2000

Solutions

Expert Solution

import random

import math

temp = -1

n = 50

nn = 1000 # note nn*n == 50000

# Theoretical calculations

print('======Theoretical results (what is expected) ==========')

#Using the Binomial distribution formulas on page 1

p = .5

print('1 the theoretical mean(average)of 1''s ( n*nn*p)==', n*nn*p) # fix u == n*nn*p this is the expected(average number of 1’s

print('2 the theoretical variance v == ', n*nn*p*(1-p))# fix # v = n*nn*p*(1-p)

print('3 the standard deviation SD (should be > 100) == ', math.sqrt(n*nn*p*(1-p)))

# sd = square root(v) sd = sqrt(v)

# at this point in the code # we have the theoretical mean, variance, standard deviation

# now for the experimental results

print(' ========== Experimental results ========== ')

print(' ========== Populate a list and count 1’s ========== ')

temp = 0

a = 0

b = 1

MyList = []

j = 0

while (j < nn): # loop for 1000 times

    i = 0

    while i < n : # loop 50 times populate list of nn

        randNum = random.randint(a,b)

        MyList.append(randNum)

# count the number of 1’s and the number of 0

# use the count function # the number of 1’s + number 0’s == 50

# keep track of these counts and update the results

# with the next iteration

        temp=temp+randNum

        i = i +1

    j = j + 1

print('4 the length of MyList == ', len(MyList) ) # FIX use len()

print('5 the number of 1’s is ', temp ) # FIX

print('6 the number of 0’s is ', n*nn-temp ) # fix

print('7 the average of all the ', n*nn,' integers is...', 'should be < 1) ', temp/(n*nn)) # fix

print()

# fix SD = sqrt(v)

# Display the range (+- 2sd ) as follows:

# mean+ 2*SD mean – 2*SD

print('8 mean - 2*SD == ', temp-2*(math.sqrt( (temp/(n*nn)) * (n*nn-temp)) ) ) #fix

print('9 mean + 2*SD == ', temp+2*(math.sqrt((temp/(n*nn))*(n*nn-temp)))) #fix

# Answer the question:

# is the total number of 1’s from 5 above

# mean – 2*SD < mean < mean + 2*SD true ? Yes or no

# within the range as calculated in 8 and 9 by printing

#‘Yes’ if it is within the range and

# ‘No’ if it is not within the range

answer = 'YES'

print('10 Is the number of 1''s within ave +- 2*SD ', answer) # FIX

#Display 10 lines, 25 integers per line of the n*nn (== 50000) integers

# That is, display the integers in MyList, 25 per line, for 10 lines

# and include the line number as in 1 , 2, 3, …10

# NOTE only display 10 lines with 25 values (= 0 or 1)

# and not all 50,000 integers

print('11 displaying 10 numbered lines with 25 integers per line ...')

# for your consideration will need adjustments

k = 0

sub = []

while (k < len(MyList) ):

    sub = MyList[k: k+20] # Splice

    print("...",(k/2000)+1, sub) # do NOT print k only the # line number as in 1, 2,3

    k = k+2000


Related Solutions

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.
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...
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
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 C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
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...
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
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...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT