"The Generala" in Python
I am subscribed to Google Alert and I get the things from Python and today I started to review and I saw this interesting program that was sent to make a student.
Tip if you study programming and are lazy to do such a simple program really let me tell you that you made a career mistake.
It is hard the life of the programmer and you have to enjoy spending hours trying to solve your code problems or that of other programmers.
If you like to learn and investigate and you also try to run your programs welcome to the Club.
There is nothing wrong with making mistakes. For example, I studied Chemical Engineering and now I am a programmer, I like what I do and pay well.
Hands down, the statement reads:
The game "General" consists of throwing five dice and setting the score of the play. Of the possible ones, the ladder is given when some of the three progressions happen: 1-2-3-4-5, 2-3-4-5-6 and 3-4-5-6-1. Perform a program that throws the 5 dice at random and determines if a ladder occurred.
It's a bit simple if you know the python structures, maybe you can do better than what I did but this I did in about 10 minutes along with the other code.
First code solving the task:
# -*- coding: utf-8 -*- import random ### El juego "Generala" consiste en tirar cinco dados y establecer el puntaje de la ### jugada. De las posibles, la escalera se da cuando suceden algunas de las tres progresiones: 1-2-3-4- ### 5, 2-3-4-5-6 y 3-4-5-6-1. Realice un programa que tire los 5 dados al azar y determine si se produjo ### una escalera.Iteraciones = 0
Ganaste = 0
while Ganaste != 1000:
Lista_dados=[]
dado_1= random.randrange(1,7)
Lista_dados.append(dado_1)dado_2= random.randrange(1,7) Lista_dados.append(dado_2) dado_3= random.randrange(1,7) Lista_dados.append(dado_3) dado_4= random.randrange(1,7) Lista_dados.append(dado_4) dado_5= random.randrange(1,7) Lista_dados.append(dado_5) Lista_dados.sort() if ( Lista_dados == [1, 2, 3, 4, 5] ) or ( Lista_dados == [ 2, 3, 4, 5,6] ) or ( Lista_dados == [1, 3, 4, 5, 6] ): Iteraciones +=1 print "Ganaste en: ", Iteraciones, "Lanzadas" print Lista_dados Ganaste += 1000 else: Iteraciones +=1 print "Sigue intentando solo son: ", Iteraciones, "Lanzadas"Try the code and you will see the output, explain this briefly the other not.
We import Random to add random to the code, we create a while loop that will run until Ganaste is different to 1 or there is 1 winner, we throw dice from 1 to 6, remember that range does not take the last number, at the end of the dice the We draw by default from lowest to highest, and we create 1 if, if the dice appear as in any of the winning lists, we add to win and break the loop, we also add 1 to iterations and write wins in X iterations and we print the winning list , This is done until Win is 1 or there is 1 winner if 1 is not added to iterations, a consolation message is displayed and Loop is resumed .. easy truth
Now as I am idle and I like to think and try to translate my ideas into code, I wanted to know what the highest probability of winning is to say if I throw the dice x times how many times I will win in X, but as my thinking was something not explicit Create a list with the 10 most common iterations in winning this is the result:
# -*- coding: utf-8 -*- from collections import Counter, OrderedDict from prettytable import PrettyTable import randomLista_ganadas=[]
iteraciones_total = 5000
for lanzada in range(1,iteraciones_total):
Ganaste = 0
Iteraciones = 0
while Ganaste != 1:
Lista_dados=[]
dado_1= random.randrange(1,7)
Lista_dados.append(dado_1)dado_2= random.randrange(1,7) Lista_dados.append(dado_2) dado_3= random.randrange(1,7) Lista_dados.append(dado_3) dado_4= random.randrange(1,7) Lista_dados.append(dado_4) dado_5= random.randrange(1,7) Lista_dados.append(dado_5) Lista_dados.sort() if ( Lista_dados == [1, 2, 3, 4, 5] ) or ( Lista_dados == [ 2, 3, 4, 5,6] ) or ( Lista_dados == [1, 3, 4, 5, 6] ): print "Ganaste en: ", lanzada, "Lanzadas" print Lista_dados Ganaste += 1 Iteraciones += 1 Lista_ganadas.append(Iteraciones) else: print "Sigue intentando solo son: ", lanzada, "Lanzadas" Iteraciones += 1c = Counter(Lista_ganadas)
mas_comunes= c.most_common()[:10]print "mas_comunes",mas_comunes
primer = mas_comunes[0][1]numero_de_repeticiones = len(mas_comunes)
porcentajes = []
iteracion_ganada = []
for repeticion in range(0,numero_de_repeticiones):
x = mas_comunes[repeticion][1]
porentaje_de_iteraciones_total = float(x * 100.0000/ iteraciones_total)
porcentajes.append(porentaje_de_iteraciones_total)
iteracion_win = mas_comunes[repeticion][0]
iteracion_ganada.append(iteracion_win)table = PrettyTable(["iteracion_ganada", "porcentaje"])
i = 0
for repeticion in iteracion_ganada:table.add_row([iteracion_ganada[i], porcentajes[i]] ) i += 1print table
The result is something like this:
+------------------+-------------+ | iteracion_ganada | porcentajes | +------------------+-------------+ | 1 | 4.88 | | 2 | 4.54 | | 4 | 4.1 | | 3 | 3.98 | | 7 | 3.74 | | 5 | 3.72 | | 9 | 3.56 | | 6 | 3.26 | | 11 | 2.96 | | 10 | 2.88 | +------------------+-------------+God bless you and remember if we like what we work we can not call it work.