am_dot_com

FP 20211109

Nov 9th, 2021 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #em1.py
  2. #gerarApostaAleatoria
  3. #randomEMBet
  4. DEFAULT_MIN = 1
  5. DEFAULT_MAX = 50
  6. DEFAULT_Q = 5
  7. DEFAULT_STAR_MIN = 1
  8. DEFAULT_STAR_MAX = 10
  9. DEFAULT_QS = 2
  10.  
  11. """
  12. rewrite searchInList, so that it returns
  13. a list of all the positions where pEl
  14. exists
  15. If pEl does NOT exist at all, return
  16. the empty list []
  17. """
  18. def searchInList(
  19.     pList,
  20.     pEl
  21. )->list:
  22.     listRet = [] #a data structure to take note of all the addresses where pEl is to be found
  23.     #linear search
  24.     iHowManyElementsInList = len(pList) #e.g. 10
  25.     listOfValidAddresses = range(iHowManyElementsInList)
  26.     #[0,1,2,3,4,5,6,7,8,9]
  27.     #linear
  28.     for iValidAddress in listOfValidAddresses:
  29.         someElement = pList[iValidAddress]
  30.         bMatchesSearchedElement = \
  31.             someElement == pEl #will be True when I found the searched pEl
  32.         if (bMatchesSearchedElement):
  33.             #return True #non-exaustive
  34.             listRet.append(iValidAddress)
  35.         #if
  36.     #for
  37.     return listRet
  38. #def searchInList
  39.  
  40.  
  41. def randomEMBet(
  42.     piMin = DEFAULT_MIN,
  43.     piMax = DEFAULT_MAX,
  44.     piQ = DEFAULT_Q,
  45.     piStarMin = DEFAULT_STAR_MIN,
  46.     piStarMax = DEFAULT_STAR_MAX,
  47.     piQS = DEFAULT_QS
  48. ):
  49.     dictRandomBet = {}
  50.  
  51.     #TODO
  52.     dictRandomBet["nums"] = randomList(
  53.         DEFAULT_MIN,
  54.         DEFAULT_MAX,
  55.         DEFAULT_Q
  56.     )
  57.     dictRandomBet["stars"] = randomList(
  58.         DEFAULT_STAR_MIN,
  59.         DEFAULT_STAR_MAX,
  60.         DEFAULT_QS
  61.     )
  62.  
  63.     return dictRandomBet
  64. #def randomEMBet
  65.  
  66. import random
  67. def randomList(
  68.     piMin, #the min allowed value
  69.     piMax, #the max allowed value
  70.     piQ, #the quantity of wanted values
  71.     pbAllowReps=False #allow repitiions? (defaults to NO)
  72. ):
  73.     iAmplitude = piMax-piMin+1 #[1,2] 2-1+1 = 2
  74.     if (pbAllowReps):
  75.         bIsPossible = True
  76.     else:
  77.         bIsPossible = piQ<=iAmplitude
  78.  
  79.     listRet = []
  80.     listRet = list()
  81.  
  82.     if (bIsPossible):
  83.         bListReady = False
  84.         while (not bListReady):
  85.             i = random.randint(piMin, piMax)
  86.             #without checking, directly
  87.             if (not pbAllowReps): #if not repetitions allowed
  88.                 # Understand this
  89.                 bNumberAlreadyInTheList = \
  90.                     len(searchInList(listRet, i))>=1 #call
  91.                 #insert in list if already NOT in list
  92.                 #to avoid repetitions
  93.                 if (not bNumberAlreadyInTheList):
  94.                     listRet.append(i)
  95.                 #if
  96.             #if
  97.             else: #repetitions allowed, number goes in without checking
  98.                 listRet.append(i)
  99.             bListReady = len(listRet)==piQ
  100.         #while
  101.     #if is possible to compute the requested list
  102.  
  103.     return listRet
  104. #def randomList
  105.  
  106. for iTestNumber in range(100):
  107.     rbet = randomEMBet()
  108.     print (rbet)
  109. #for
Advertisement
Add Comment
Please, Sign In to add comment