I'm solving my way through Euler project. I have reached question 84 and I have created a simulation of the game. I have run the simulation against the statistic provided in the question and I get the right sequence. When I try to run this with 2d4 I get wrong results. Usually I get 101516. What am I missing?
Please note, I'm not looking for the solution or for you to fix my code. I only want to know where my algorithm is flawed.
from random import randint
import sys
pos = 0
doubles = 0
csqs = [2,17,33] #The cc position
hsqs = [7,22,36] #The ch positions
rounds = 0
stop = 100000
sqs = dict() #will store how many visit I had on each square
for i in range (0,40): #initial values of sqs
sqs[i] = 0
def doCC():# the cc cards. There is no real effect of randomly picking a card or kipping them in order on my result
global pos,doubles
global CC
if CC == 0:
pos = 0
elif CC == 1:
pos = 10
doubles = 0
CC += 1
if CC == 16: CC = 0
def doCH(): #CH cards
global pos, doubles, CH
if CH == 0: pos = 0
elif CH == 1:
pos = 10
doubles = 0
elif CH == 2: pos = 11
elif CH == 3: pos = 24
elif CH == 4: pos = 39
elif CH == 5: pos = 5
elif CH == 6 or CH == 7:
if pos == 7:
pos = 15
elif pos == 22:
pos = 25
elif pos == 36:
pos = 5
elif CH ==8:
if pos == 22: pos = 28
else: pos = 12
elif CH == 9:
pos -= 3
CH += 1
if CH == 16: CH = 0
while rounds < stop:
d1 = randint(1,4)
d2 = randint(1,4)
if d1 == d2:
doubles += 1 #counting doubles
else:
doubles = 0
if doubles == 3:
pos = 10
doubles = 0
pos += d1+d2
if pos>= 40 : #you have just crossed go
pos -= 40
rounds += 1
if rounds %10000 == 0: print rounds,
if pos == 30: #g2j
doubles = 0
pos = 10
if pos in hsqs:
doCH()
if pos in csqs:
doCC()
sqs[pos] += 1
sys.stdout.flush()
# Setting values
m1 = 0
m2 = 0
m3 = 0
v1 = 0
v2 = 0
v3 = 0
su = 0
for v in sqs:
m = sqs[v]
su += m
if m > m1:
m1 = m
v1 = v
for v in sqs:
m = sqs[v]
if m > m2 and m < m1:
m2 = m
v2 = v
for v in sqs:
m = sqs[v]
if m > m3 and m < m2:
m3 = m
v3 = v
for v in sqs:
sqs[v] = sqs[v]*100.0/su
print m1,m2,m3
print v1,v2,v3
print m1*100.0/su, m2*100.0/su, m3*100.0/su
print sqs
When you have three doubles in a row,
if doubles == 3:
pos = 10
doubles = 0
pos += d1+d2
you still hop out of jail, but you shouldn't.
Related
So ...
Am writing a code that makes a Matrix table then calculate how many ( uppercase Letter , Lowercase Letter , Numbers , Symbols )
That's the code i tried :
def Proc_Affiche(T,P,X):
Nb_Maj = 0
Nb_Min = 0
Nb_chiffre = 0
Nb_symbole = 0
for i in range(P):
for j in range(X):
if T[i] in ["A","Z"]:
Nb_Maj = Nb_Maj + 1
elif T[i] in ["a","z"] :
Nb_Min = Nb_Min + 1
elif T[i] in range(1,9):
Nb_chiffre = Nb_chiffre + 1
else :
Nb_symbole = Nb_symbole + 1
print("Nb_Maj= ",Nb_Maj)
print("Nb_Min= ",Nb_Min)
print("Nb_chiffre= ",Nb_chiffre)
print("Nb_symbole= ",Nb_symbole)
So the Output should be like that :
Nb_Maj= ...
Nb_Min= ...
Nb_chiffre= ...
Nb_symbole= ...
The Problem is on the part of intervals Like ["A","Z"]
Strings have some functions you can use to check what they contain
.isalpha() is true for letters
.isnumeric() is true for numbers
.isalnum() is true for letters and numbers
.isupper() is true for uppercase
Thus you could do something like
if T[i].isalpha():
if T[i].isupper():
Nb_Maj += 1
else:
Nb_Min += 1
elif T[i].isnumeric():
Nb_chiffre += 1
else:
Nb_symbole += 1
Yes it is , here is the whlole code if that would help :
from math import*
def Proc_saisie():
X = -1
while X < 1 or X > 20 :
X = int(input("Donner un entier entre 5 et 20 : "))
return X
def Proc_Remplir(P,X):
T= [[] for i in range(P)]
for i in range(P):
for j in range(X):
d = input("T["+str(i)+","+str(j)+"]=")
T[i].append(d)
return T
def Proc_Affiche(T,P,X):
Nb_Maj = 0
Nb_Min = 0
Nb_chiffre = 0
Nb_symbole = 0
for i in range(P):
for j in range(X):
if T[i] in ["A","Z"]:
Nb_Maj = Nb_Maj + 1
elif T[i] in ["a","z"] :
Nb_Min = Nb_Min + 1
elif T[i] in range(1,9):
Nb_chiffre = Nb_chiffre + 1
else :
Nb_symbole = Nb_symbole + 1
print("Nb_Maj= ",Nb_Maj)
print("Nb_Min= ",Nb_Min)
print("Nb_chiffre= ",Nb_chiffre)
print("Nb_symbole= ",Nb_symbole)
#---------------------------
L = Proc_saisie()
C = Proc_saisie()
print("L =",L)
print("C =",C)
TAB = []
TAB = Proc_Remplir(L,C)
TAB = Proc_Affiche(TAB,L,C)
I'm not sure I understand what you want 100%, but I think something like follows would fit:
def Proc_Affiche(T,P,X):
Nb_Maj = 0
Nb_Min = 0
Nb_chiffre = 0
Nb_symbole = 0
for i in range(P):
for j in range(X):
if "A" <= T[i][j] <= "Z":
Nb_Maj = Nb_Maj + 1
elif "a" <= T[i][j] <= "z" :
Nb_Min = Nb_Min + 1
elif 1 <= T[i][j] <= 9:
Nb_chiffre = Nb_chiffre + 1
else :
Nb_symbole = Nb_symbole + 1
print("Nb_Maj= ",Nb_Maj)
print("Nb_Min= ",Nb_Min)
print("Nb_chiffre= ",Nb_chiffre)
print("Nb_symbole= ",Nb_symbole)
I have dataset. For a certain condition there is a column has True or False values. If there is a sequence of rows has the same value, then let the counter of these rows be the same.
To make it clear, below is my code:
c1 = [True,True,False,False,False,True,False,True,True,False,True]
counter = 1
switch = 0 #increase the counter when the vector has switched twice
c2 = np.repeat(None, len(c1))
c2[i]=counter
for i in range(1,len(c1)):
p = c1[i-1]
x = c1[i]
if p==x:
counter=counter
c2[i]=counter
if p!=x :
switch = switch + 1
c2[i]=switch
elif switch == 2:
counter = counter + 1
switch = 0 #reset the counter
print(c2)
The actual output is
[None 1 1 1 1 2 3 4 1 5 6]
while the expected one should be
[None, 1,1,1,1,2,2,3,3,3,4]
c1 = [True,True,False,False,False,True,False,True,True,False,True]
res = []
var = 1
cur=c1[0]
flag = 0
res.append(None)
for val in c1[1:]:
if val==cur and flag == 0:
res.append(var)
elif val == cur and flag == 1:
var+=1
flag = 0
res.append(var)
elif val != cur and flag == 0:
flag = 1
res.append(var)
elif val != cur and flag == 1:
res.append(var)
else:
pass
print(res)
Output:[None, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4]
I am trying to get this code to calculate 5 and print numbers by 5's with a while statement of 7, so I want it to loop through, generating a different number 7 times; however, when it gets to a number over 10, I want it to start back over at 0 and ignore the 10.
This is my code:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
The code prints the first number, but freezes on printing any others. Why isn't this working?
Your code is not in the loop. Python's code blocks are created with indents:
while z < 7:
firstpickplusfive = int(firstpickplusfive) + 1
counts = counts + 1
if counts == 1:
if firstpickplusfive > 9:
firstpickplusfive = 0
if counts == 5:
print firstpickplusfive
z = int(z) + 1
Is this the result you were trying to achieve:
import random
x = random.randint(1,9)
for i in range(1,8):
print x
x += 5
if x >= 10:
x -= 9
This generates a random number, and adds 5 until it passes 10, then subtracts 9, and it does this seven times. If I understand your question correctly, this is what you were trying to do. Please correct me if I am wrong.
no this is not what I was trying to do here is what I am trying to do.
counts = 0
r = 0
firstpickplusfive = 7
p = firstpickplusfive + 5
while r < 3:
firstpickplusfive = p + counts
if firstpickplusfive > 6:
firstpickplusfive = firstpickplusfive + counts - 10
if p > 9:
p = firstpickplusfive + counts
print firstpickplusfive
counts = counts + 1
r = int(r) + 1
it works alone, but when I add it to the script I am trying to write it doesn't work...if there is a simpler way to do it I would appreciate knowing it.
ie.
number = number + 5 + 0
then
number = number + 5 + 1.....ect which
example 7 + 5 + 0 = 12,
7 + 5 + 1 = 13.........
if the number is equal to 10 then I want it to drop the tens place and keep the 1's place
example 7 + 5 + 0 = 2,
7 + 5 + 1 = 3
Here is an easier method:
for i in range(1,4):
num = 7
num += 5
num +=(i-1)
if num >=10:
num -= 10
print num
i+=1
Try this in your script, does it work?
When I was testing a counter, I discovered that it only seems to display the last item to go through it. For example, if something was excellent, it showed up as counted so it would be "1". However regardless of other data, the rest would be 0.
def mealrating(score, review):
for x in range(0,len(score)):
mp = 0
mg = 0
me = 0
if score[x] >= 1 and score[x] <= 3:
review.append("poor")
mp = mp + 1
if score[x] >= 4 and score[x] <= 6:
review.append("good")
mg = mg + 1
if score[x] >= 7 and score[x] <= 10:
review.append("excellent")
me = me + 1
print("The customer rated tonight's meal as:")
print('Poor:' + str(mp))
print('Good:' + str(mg))
print('Excellent:' + str(me))
print("\n")
You are resetting mp, mg, and me in each iteration.
def mealrating(score, review):
mp = 0
mg = 0
me = 0
for x in range(0,len(score)):
if score[x] >= 1 and score[x] <= 3:
review.append("poor")
mp = mp + 1
if score[x] >= 4 and score[x] <= 6:
review.append("good")
mg = mg + 1
if score[x] >= 7 and score[x] <= 10:
review.append("excellent")
me = me + 1
print("The customer rated tonight's meal as:")
print('Poor:' + str(mp))
print('Good:' + str(mg))
print('Excellent:' + str(me))
print("\n")
You must initialize the counters outside the loop:
mp = 0
mg = 0
me = 0
for x in range(0, len(score)):
# same as before
Otherwise they'll get reset at each iteration! To make your code more Pythonic, take the following tips into consideration:
A condition of the form x >= i and x <= j can be written more concisely as i <= x <= j
The idiomatic way to traverse a list is using iterators, without explicitly using indexes
The conditions are mutually exclusive, so you should use elif
Use += for incrementing a variable
This is what I mean:
mp = mg = me = 0
for s in score:
if 1 <= s <= 3:
review.append("poor")
mp += 1
elif 4 <= s <= 6:
# and so on
#!/usr/bin/env python
import random
import time
import os
class vars:
running = 1
def win ():
print("You escaped!")
vars.running = 0
time.sleep(4)
return 0
def main ():
char_loc = 11 #The characters current co-ordinates in XY format
pos_char_loc = 11
ex_y = random.randint(1, 5)
ex_x = random.randint(1, 5) * 10
ex_loc = ex_x + ex_y
while vars.running == 1:
os.system('CLS')
x0 = ["#"] * 5
x1 = ["#"] * 5
x2 = ["#"] * 5
x3 = ["#"] * 5
x4 = ["#"] * 5
if (char_loc >= 11 and char_loc <= 55):
if (char_loc >= 11 and char_loc <= 15):
i = 0; k = 11
for x in range(0, 4):
if char_loc == k:
x0.insert(i, '#')
else:
i += 1
k += 1
if (char_loc >= 21 and char_loc <= 25):
i =0; k = 21
for loop1 in range(0, 4):
if char_loc == k:
x1.insert(i, '#')
else:
i += 1
k += 1
if (char_loc >= 31 and char_loc <= 35):
i =0; k = 31
for loop2 in range(0, 4):
if char_loc == k:
x2.insert(i, '#')
else:
i += 1
k += 1
if (char_loc >= 41 and char_loc <= 45):
i =0; k = 41
for loop3 in range(0, 4):
if char_loc == k:
x3.insert(i, '#')
else:
i += 1
k += 1
if (char_loc >= 51 and char_loc <= 55):
i =0; k = 51
for loop5 in range(0, 4):
if char_loc == k:
x4.insert(i, '#')
else:
i += 1
k += 1
else:
print("fail")
print( x0[4],x1[4],x2[4],x3[4],x4[4])
print( x0[3],x1[3],x2[3],x3[3],x4[3])
print( x0[2],x1[2],x2[2],x3[2],x4[2])
print( x0[1],x1[1],x2[1],x3[1],x4[1])
print( x0[0],x1[0],x2[0],x3[0],x4[0])
print(char_loc, ex_loc)
if char_loc == ex_loc:
win()
move = input()
if move == "w" and (char_loc != 15 and char_loc != 25 and char_loc != 35 and char_loc != 45 and char_loc !=55):
char_loc += 1
print("up")
elif move == "s" and (char_loc != 11 and char_loc != 21 and char_loc != 31 and char_loc != 41 and char_loc != 51):
char_loc -= 1
print("down")
elif move == "a" and (char_loc != 11 and char_loc != 12 and char_loc != 13 and char_loc != 14 and char_loc != 15):
char_loc -= 10
print("left")
elif move == "d" and (char_loc != 51 and char_loc != 52 and char_loc != 53 and char_loc != 54 and char_loc != 55):
char_loc += 10
print("right")
else: print("You can't move there!")
if __name__ == '__main__': main()
I'm trying to make a simple text based game where you move the '#' around a grid of '#'s
and try to find the exit. I've changed the code to make it easier for me to make the grid bigger or smaller without adding or deleting huge chunks of code and it keeps on giving me this output:
fail
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
11 52
and I can't figure out what's wrong with it! Only one '#' is supposed to appear :(
I am only a newbie at python so if you have any tips for improving this please, don't hesitate, and post them!
Thanks in advance,
I think the "fail" occurs because it will occur every time the char_loc is not between 51 and 55.
if (char_loc >= 11 and char_loc <= 15):
if (char_loc >= 21 and char_loc <= 25):
if (char_loc >= 31 and char_loc <= 35):
if (char_loc >= 41 and char_loc <= 45):
if (char_loc >= 51 and char_loc <= 55):
else:
What I think you'd want to do here is use elif, which will only fire if the previous checks don't trigger.
if (char_loc >= 11 and char_loc <= 15):
elif (char_loc >= 21 and char_loc <= 25):
elif (char_loc >= 31 and char_loc <= 35):
elif (char_loc >= 41 and char_loc <= 45):
elif (char_loc >= 51 and char_loc <= 55):
else:
In regards to the multiple # symbols, I think this may play a part. Currently you have:
if char_loc == k:
x0.insert(i, '#')
else:
i += 1
k += 1
What I think you're looking to do is:
if char_loc == k:
x0.insert(i, '#')
i += 1
k += 1
Since you want k to change every time that loop iterates.
One last thing that I would suggest is since you have:
i =0; k = 21
i =0; k = 31
i =0; k = 41
i =0; k = 51
You will probably want to add
i =0; k = 11
To the first one.
Hope that helps.