I am running some code for a class and I have no clue what to do, I submitted the assignment but it’s not helpful if I don’t know what to do in the end. Here is the code that I am running to try and track these tests. The idea is to see how many children it takes before you have at least 1 child of each sex. Then the program should be storing that to a list value as boysandgirls_stats[x]. The problem is that it won’t store all tests. It only logs around 40 out of 100 or 200 out of 1000. I don’t know where these other tests are being stored. stdarray is something we have to use from a book we are using. It tracks the tests similarly to how a list would in python but it wasn’t explained well to me. Thank you!
JavaScript
x
41
41
1
import stdio
2
import random
3
import sys
4
import stdarray
5
6
numTrials = int(sys.argv[1])
7
8
boysandgirls_stats = stdarray.create1D(4, 0)
9
boys = 0
10
girls = 0
11
children = 0
12
totalChildren = 0
13
14
15
for x in range(numTrials): # Simulation to determine sex
16
childType = random.randint(0, 1)
17
if childType == 0:
18
boys += 1
19
children += 1
20
totalChildren += 1
21
else:
22
girls += 1
23
children += 1
24
totalChildren += 1
25
for x in range(numTrials): # Keeps track of amount of children needed
26
if (boys >= 1) and (girls >= 1):
27
if children <= 5:
28
boysandgirls_stats[children - 2] += 1
29
boys = 0
30
girls = 0
31
children = 0
32
else:
33
boysandgirls_stats[3] += 1
34
boys = 0
35
girls = 0
36
children = 0
37
avgChildren = totalChildren / numTrials # Calculation for average children
38
stdio.writeln('Avg # of children: ' + str(avgChildren))
39
for x in range(len(boysandgirls_stats)):
40
stdio.writeln(str(x+2) + ' children ' + str(boysandgirls_stats[x]))
41
Advertisement
Answer
JavaScript
1
40
40
1
import sys
2
import random
3
4
## let's put the test inside a function:
5
def child_test():
6
girl = False
7
boy = False
8
total_children = 0
9
while not (boy and girl):
10
total_children += 1
11
if random.randint(0, 1) == 0:
12
boy = True
13
else:
14
girl = True
15
return total_children
16
17
18
## now the statistics:
19
# get the number of trials from user command:
20
num_trials = int(sys.argv[1])
21
22
# for every number of children, log the associated number of tests:
23
total = 0
24
indexed_count = {}
25
for i in range(num_trials):
26
num_children = child_test()
27
if num_children not in indexed_count:
28
indexed_count[num_children] = 0
29
indexed_count[num_children] += 1
30
total += num_children
31
32
# calculate and output the average:
33
average = total / num_trials
34
print(f'average is : {average}')
35
36
# show tries per number of children
37
sorted_indexed_count = sorted(indexed_count.items(), key = lambda x: x[1], reverse = True)
38
for t in sorted_indexed_count:
39
print(f' {t[0]} children : {t[1]} tests')
40
Outputs :
JavaScript
1
20
20
1
$ python3 so.py 100000
2
average is : 3.00812
3
2 children : 49702 tests
4
3 children : 25238 tests
5
4 children : 12401 tests
6
5 children : 6314 tests
7
6 children : 3159 tests
8
7 children : 1528 tests
9
8 children : 840 tests
10
9 children : 410 tests
11
10 children : 214 tests
12
11 children : 99 tests
13
12 children : 53 tests
14
13 children : 18 tests
15
14 children : 10 tests
16
15 children : 6 tests
17
16 children : 6 tests
18
18 children : 1 tests
19
17 children : 1 tests
20