Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #em1.py
- #gerarApostaAleatoria
- #randomEMBet
- DEFAULT_MIN = 1
- DEFAULT_MAX = 50
- DEFAULT_Q = 5
- DEFAULT_STAR_MIN = 1
- DEFAULT_STAR_MAX = 10
- DEFAULT_QS = 2
- """
- rewrite searchInList, so that it returns
- a list of all the positions where pEl
- exists
- If pEl does NOT exist at all, return
- the empty list []
- """
- def searchInList(
- pList,
- pEl
- )->list:
- listRet = [] #a data structure to take note of all the addresses where pEl is to be found
- #linear search
- iHowManyElementsInList = len(pList) #e.g. 10
- listOfValidAddresses = range(iHowManyElementsInList)
- #[0,1,2,3,4,5,6,7,8,9]
- #linear
- for iValidAddress in listOfValidAddresses:
- someElement = pList[iValidAddress]
- bMatchesSearchedElement = \
- someElement == pEl #will be True when I found the searched pEl
- if (bMatchesSearchedElement):
- #return True #non-exaustive
- listRet.append(iValidAddress)
- #if
- #for
- return listRet
- #def searchInList
- def randomEMBet(
- piMin = DEFAULT_MIN,
- piMax = DEFAULT_MAX,
- piQ = DEFAULT_Q,
- piStarMin = DEFAULT_STAR_MIN,
- piStarMax = DEFAULT_STAR_MAX,
- piQS = DEFAULT_QS
- ):
- dictRandomBet = {}
- #TODO
- dictRandomBet["nums"] = randomList(
- DEFAULT_MIN,
- DEFAULT_MAX,
- DEFAULT_Q
- )
- dictRandomBet["stars"] = randomList(
- DEFAULT_STAR_MIN,
- DEFAULT_STAR_MAX,
- DEFAULT_QS
- )
- return dictRandomBet
- #def randomEMBet
- import random
- def randomList(
- piMin, #the min allowed value
- piMax, #the max allowed value
- piQ, #the quantity of wanted values
- pbAllowReps=False #allow repitiions? (defaults to NO)
- ):
- iAmplitude = piMax-piMin+1 #[1,2] 2-1+1 = 2
- if (pbAllowReps):
- bIsPossible = True
- else:
- bIsPossible = piQ<=iAmplitude
- listRet = []
- listRet = list()
- if (bIsPossible):
- bListReady = False
- while (not bListReady):
- i = random.randint(piMin, piMax)
- #without checking, directly
- if (not pbAllowReps): #if not repetitions allowed
- # Understand this
- bNumberAlreadyInTheList = \
- len(searchInList(listRet, i))>=1 #call
- #insert in list if already NOT in list
- #to avoid repetitions
- if (not bNumberAlreadyInTheList):
- listRet.append(i)
- #if
- #if
- else: #repetitions allowed, number goes in without checking
- listRet.append(i)
- bListReady = len(listRet)==piQ
- #while
- #if is possible to compute the requested list
- return listRet
- #def randomList
- for iTestNumber in range(100):
- rbet = randomEMBet()
- print (rbet)
- #for
Advertisement
Add Comment
Please, Sign In to add comment