Python dice outcome count - python

Write a function collect_sims(nsim,N,D,p=0.5,nmax=10000) that runs your run_sim function nsim times (with parameters N, D, p) and returns a numpy array of length nmax giving the number of times that the simulation took a specified number of steps to stop. For example, suppose nsim was 8 and successive runs of run_sim gave you 3,4,4,3,6,5,4,4. You would tabulate this as “two 3s, four 4s, one 5, one 6, zero 7s, zero 8s …”
def collect_sims(nsim, N, D, p=0.5, nmax=10000):
run_sim(N=20, D=6, p=0.5, itmax=5000)
onecount = 0
twocount = 0
threecount = 0
fourcount = 0
fivecount = 0
sixcount = 0
for k in range (n):
if D == 1:
onecount += 1
if D == 2:
twocount += 1
if D == 3:
threecount += 1
if D == 4:
fourcount += 1
if D == 5:
fivecount += 1
if D == 6:
sixcount += 1
return(k)
print(onecount, "1",twocount,"2",threecount,"3",fourcount,"4",fivecount,"5",sixcount,"6")
It says my 6 variables onecount, twocount, etc are not defined, how can I define them? Also, what can I do to fix my code?

I don't know why are you returning k.
Anyway, the problem is that oncount, twocount, ... etc is in different scope that print. You can put the print() inside the function or you can return an tuple with the counts
Some like that:
def collect_sims(nsim, N, D, p=0.5, nmax=10000):
run_sim(N=20, D=6, p=0.5, itmax=5000)
onecount = 0
twocount = 0
threecount = 0
fourcount = 0
fivecount = 0
sixcount = 0
for k in range (n):
if D == 1:
onecount += 1
if D == 2:
twocount += 1
if D == 3:
threecount += 1
if D == 4:
fourcount += 1
if D == 5:
fivecount += 1
if D == 6:
sixcount += 1
return(onecount, twocount, threecount, fourcount,fivecount,sixcount)
onecount, twocount, threecount, fourcount,fivecount,sixcount = collect_sims (...)
print(onecount, "1",twocount,"2",threecount,"3",fourcount,"4",fivecount,"5",sixcount,"6")
Different Solution
Maybe this other solution can help you:
https://stackoverflow.com/a/9744274/6237334

Indent your for loop: in the code you posted, it's back at the original indentation level (none, for the for statement). This ends your function, and the loop is in the main program. Your variables aren't defined yet (since they're not the same as the ones in the function), and your return is illegal.
Try this, perhaps?
def collect_sims(nsim, N, D, p=0.5, nmax=10000):
run_sim(N=20, D=6, p=0.5, itmax=5000)
onecount = 0
twocount = 0
threecount = 0
fourcount = 0
fivecount = 0
sixcount = 0
for k in range (n):
if D == 1:
onecount += 1
if D == 2:
twocount += 1
if D == 3:
threecount += 1
if D == 4:
fourcount += 1
if D == 5:
fivecount += 1
if D == 6:
sixcount += 1
print(onecount, "1",twocount,"2",threecount,"3",fourcount,"4",fivecount,"5",sixcount,"6")
collect_sims()
I can't test, since you didn't supply enough code. Also, note that I simply left the print statement in place as a debugging trace. You have to return an array, and you've made no attempt to do that yet. Your original code returned k, which had to be n+1. This is not useful to the calling program.
FURTHER HELP
Learn to use a list of 6 elements for the counts, rather than six separate variables. Even better, put all of the die rolls into a list, and simply use the count function to determine how many of each you have.

Related

Find the sum of all the multiples of 3 or 5 below 1000. (idk what is wrong with my code)

I'm a beginner and I tried this code to list the sum of all the multiples of 3 or 5 below 100, but it gives a wrong answer and I don't know why.
result = 0
result2 = 0
som = 0
sum2 = 0
below_1000 = True
while below_1000:
result = result+3
if result < 1000:
som += result
else:
below_1000 = False
below_1000 = True
while below_1000:
result2 = result2+5
if result2 < 1000:
sum2 += result2
else:
below_1000 = False
final_result = som+sum2
print(final_result)
Since you first loop over multiples of 3, then again over multiples of 5, you are double-counting a lot of values, specifically values that are multiples of both 3 and 5 (for example 15 or 60).
To write this manually, you can use a for loop over range
total = 0
for i in range(1000):
if i % 3 == 0 or i % 5 == 0:
total += i
>>> total
233168
A more concise way to do this same thing is using a generator expression within the sum function
>>> sum(i for i in range(1000) if i % 3 == 0 or i % 5 == 0)
233168

Compare and count same digits between two numbers

I'm looking to write a function that receives two four-digit numbers (m, n) that counts how many digits are the same between m and n, including duplicates and zeroes on the left. The thing is, my professor only taught us how to use loops, and don't want us to use lists and intersections, and I'm no able to do it.
For example, if m = 331 and n = 3, it should return 2 as the amount of equal digits, bit if n = 33, it should return 3 same digits.
>>> compare_digits(331, 3)
2
>>> compare_digits(332, 33)
3
Edit: This is the code I created before, and it counts same digits more than it should, but the central idea is the usage of % and // to read each digit, but it's not working...
def compare_digits(m, n):
read_ndigits = 0
same_digits = 0
while read_ndigits < 4: #number of digits
current_n = n % 10
read_mdigits = 0
while read_mdigits < 4:
current_m = m % 10
if current_n == current_m:
same_digits += 1
m //= 10
read_mdigits += 1
n //= 10
read_ndigits += 1
return same_digits
The output is very messy and I can't even recognize any pattern.
You can use collections.Counter() with set intersection:
from collections import Counter
def compare_digits(m, n):
m_counts = Counter(str(m).zfill(4))
n_counts = Counter(str(n).zfill(4))
return sum(min(m_counts[k], n_counts[k]) for k in m_counts.keys() & n_counts.keys())
print(compare_digits(331, 3)) # 2
print(compare_digits(332, 33)) # 3
print(compare_digits(3, 331)) # 2
Well, I decided to limit the number of digits to 4 and not be generic about it, so I wrote this and it worked perfectly:
def compare_digits(m, n):
a = m % 10
m //= 10
b = m % 10
m //= 10
c = m % 10
m //= 10
d = m % 10
read_ndigits = 0
same_digits = 0
while read_ndigits < 4:
current = n % 10
if current == a:
same_digits += 1
a = None
elif current == b:
same_digits += 1
b = None
elif current == c:
same_digits += 1
c = None
elif current == d:
same_digits += 1
d = None
n //= 10
read_ndigits += 1
return same_digits
Thank you all for your help :)

Loop not functioning correctly

Working with data frames and this is the code I have for it.
numbers = 3
count=0
A = 0
B = 0
C = 0
for x in range(numbers):
if str(data.iloc[count])== 'A':
A += 1
elif str(data.iloc[count])== 'B':
B += 1
elif str(data.iloc[count])== 'C':
C += 1
count +=1
#this is to return the count to check if it works
print A
print B
print C
but for some reason when I run this code only the count for A increases.
i.e. if the data in the index had a 'A', 'B', 'B' its still returning A = 3 and B = 0 where it should be returning A = 1, B = 2, and C = 0
what am I doing wrong? thanks again.
Since your count += 1 is not within the for loop, count += 1 only runs once, after the for loop is complete. It needs to be indented. Alternatively, you do not need to use a count variable since x is already going through the range 0 to 3:
numbers = 3
A = 0
B = 0
C = 0
for x in range(numbers):
if str(data.iloc[x])== 'A':
A += 1
elif str(data.iloc[x])== 'B':
B += 1
elif str(data.iloc[x])== 'C':
C += 1
#this is to return the count to check if it works
print A
print B
print C
This also worked
count=0
numbers = 3
A = 0
B = 0
C = 0
for x in range(numbers):
count +=1
if str(data.iloc[x])== 'A':
A += 1
elif str(data.iloc[x])== 'B':
B += 1
elif str(data.iloc[x])== 'C':
C += 1
#this is to return the count to check if it works
print A
print B
print C

not getting output for euler challenge 3

I am attempting to solve Project Euler: Problem 3, and I am using the following function to test for primality,
def check_prime(x):
i = 1
b = 0
while b == 0 :
i += 1
if i >= x :
return True
b += 1
elif x%i == 0 :
return False
b += 1
Which I call from the rest of my program
a = 3
z = []
number_used = 600851475143
while a < number_used/2 :
if check_prime(a) and number_used%a == 0 :
z.append(a);
a += 2
else :
a += 2
print z
But, the code is not printing the prime factors needed for the third Euler problem. Is my code too inefficient to manage it?

How do you read loops?

What is the thought process when evaluating a loop? I really have no idea how the shell gets these answers (A: 12, B: 2, C: 4, D: 6).
A, B, C, D = 0, 0, 0, 0
while A <= 10:
A += 2
if A%3 == 0:
B += 1
else:
C += 1
D += 1
Perhaps you can read it more easily if you break it down:
A = 0
while A <= 10:
A += 2
Can you read this? Do you understand how it gets to 12?
A, D = 0, 0
while A <= 10:
A += 2
D += 1
Also including D should not make it any harder.
Can you read and understand the if-statement by itself?
if A%3 == 0:
B += 1
else:
C += 1
How about when it is inside the loop?
A, B, C, D = 0, 0, 0, 0
while A <= 10:
A += 2
if A%3 == 0:
B += 1
else:
C += 1
D += 1
B and C are related; exactly one of them are incremented in each iteration, so they should add up to the same as D, which they do.
Do you have any specific problems reading and understanding this now? :)
The other answers are good. I would highly recommend going through things with a pen and paper to make sure you understand what's going on.
Using print inside the loop is also useful to see what is going on while your program runs.
A,B,C,D = 0,0,0,0
while A <= 10:
A += 2
if A%3 == 0:
B += 1
else:
C += 1
D += 1
print "A =", A, " B =", B, " C =", C, " D =", D
The output shows you the values of A, B, C, D at the end of every loop iteration.
A = 2 B = 0 C = 1 D = 1
A = 4 B = 0 C = 2 D = 2
A = 6 B = 1 C = 2 D = 3
A = 8 B = 1 C = 3 D = 4
A = 10 B = 1 C = 4 D = 5
A = 12 B = 2 C = 4 D = 6
You can see that:
A gets incremented by 2 every loop iteration
B gets incremented by 1 IF A is divisible by 3, that is A%3 == 0
C gets incremented by 1 IF A is NOT divisible by 3
D gets incremented by 1 every loop iteration
When it comes to loops, you can think of the collection of indented code as a single "chunk" of code that gets executed once for every repetition of the loop. The formal term for this code chunk is a block. It also applies to if/else statements.
The body of the while loop will execute 6 times (for A=0,2,4,6,8,10).
At each iteration, A is incremented by 2, so after the first statement
within the loop it has values 2,4,6,8,10,12.
B is incremented by one twice (when A=6 and A=12);
C is incremented by one for the remaining values of A.
D is incremented every time round the loop.
Hence, after the loop, A=12, B=2, C=4 and D=6.

Categories