Advertisement
Benjamin_Loison

TP entraînement Python MPSI

Aug 3rd, 2018
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name:        TP 1 - Ex 3
  3. # Purpose:     Training
  4. #
  5. # Author:      Benjamin Loison
  6. #
  7. # Created:     27/07/2018
  8. # Copyright:   (c) Benjamin Loison 2018
  9. # Licence:     Quote the author
  10. #-------------------------------------------------------------------------------
  11.  
  12. # 1
  13.  
  14. # for instance, 4 ^ 9 = 262 144 (many possibilities for Pierre) @LoveComputerPower
  15.  
  16. class Person:
  17.     def __init__(self, name, dices, sides):
  18.         self.name = name
  19.         self.dices = dices
  20.         self.sides = sides
  21.         self.minSum = dices * 1
  22.         self.maxSum = dices * sides
  23.         self.arraySize = self.maxSum - self.minSum
  24.         self.sums = [0] * (self.arraySize + 1)
  25.  
  26.     def getIndex(self, scoreSum):
  27.         return scoreSum - self.minSum
  28.  
  29.     def displayCouple(self, index):
  30.         print(str(index + self.minSum) + " " + str(self.sums[index]))
  31.  
  32. def computePossibilities(person, throwToDo = -1, scoreSum = 0):
  33.     if throwToDo == -1:
  34.         throwToDo = person.dices
  35.     if throwToDo != 0:
  36.         for side in range(1, person.sides + 1):
  37.             tmpSum = scoreSum + side
  38.             computePossibilities(person, throwToDo - 1, tmpSum)
  39.     else:
  40.         index = person.getIndex(scoreSum)
  41.         person.sums[index] = person.sums[index] + 1
  42.  
  43. def computeLists(person):
  44.     computePossibilities(person)
  45.  
  46. def displayList(person):
  47.     for index in range(0, person.arraySize + 1):
  48.         person.displayCouple(index)
  49.  
  50. pierre = Person("Pierre", 9, 4)
  51. colin = Person("Colin", 6, 6)
  52.  
  53. computeLists(pierre) # first (Pierre)
  54. ## displayList(pierre)
  55.  
  56. ## print()
  57.  
  58. computeLists(colin) # second (Colin)
  59. ## displayList(colin)
  60.  
  61. # 2
  62.  
  63. def proba_conda(Scolin, needPrint = True):
  64.     global pierre
  65.     sumAllPossibilitiesPierre = 0
  66.     sumPossibilitiesToWinPierre = 0
  67.     for index in range(0, pierre.arraySize + 1):
  68.         possibilities = pierre.sums[index]
  69.         sumAllPossibilitiesPierre += possibilities
  70.         if index + pierre.minSum > Scolin:
  71.             sumPossibilitiesToWinPierre += possibilities
  72.     pourcentage = (sumPossibilitiesToWinPierre / sumAllPossibilitiesPierre) * 100
  73.     if needPrint:
  74.         print(str(round(pourcentage, 4)) + " %")
  75.     else:
  76.         return pourcentage
  77.  
  78. ## proba_conda(20)
  79.  
  80. # 3
  81.  
  82. sumProbabilities = 0
  83. lowerBound = colin.minSum
  84. upperBound = colin.maxSum
  85. boundSize = upperBound - lowerBound
  86. for score in range(lowerBound, upperBound + 1):
  87.     sumProbabilities += proba_conda(score, False)
  88. print(sumProbabilities / boundSize)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement