Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #em8.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
- """
- range (i:int)->list ---> [0, 1, ... , i-1]
- """
- 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) #appends to the end
- #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=1, #the min allowed value
- piMax=1000, #the max allowed value
- piQ=5, #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
- def bubbleSort (pCol, pbAscendingSort:bool=True):
- bSorted = False
- while(not bSorted):
- """
- Bubble Sort
- will iterate from the first
- to the **penultimate** address
- """
- listValidAddressesForBS = \
- range(len(pCol)-1) #[0, len-1-1]
- bSwaps=False #reset bSwaps BEFORE each walk
- for iAddress in listValidAddressesForBS:
- if (pbAscendingSort):
- bAdjacentElementsOutOfOrder = \
- pCol[iAddress] > pCol[iAddress+1]
- else:
- bAdjacentElementsOutOfOrder = \
- pCol[iAddress] < pCol[iAddress + 1]
- if (bAdjacentElementsOutOfOrder):
- #swap / trocar elements
- temp = pCol[iAddress]
- pCol[iAddress] = pCol[iAddress+1]
- pCol[iAddress+1] = temp
- #possible in Python, but not in other langs
- #pCol[iAddress],pCol[iAddress+1] = pCol[iAddress+1], pCol[iAddress]
- bSwaps = True
- #if
- #for
- #if no swaps were made during the walk over the col, then it is sorted
- if (not bSwaps):
- bSorted = True
- #while
- #def bubbleSort
- """
- for iTestNumber in range(100):
- rbet = randomEMBet()
- print (rbet)
- #for
- """
- iListCounter=1
- i=0
- while (i<500):
- print("List number",i+1)
- print("List number", iListCounter)
- umaLista = randomList(piQ=9) #named params
- print("ANTES do BS: ", umaLista)
- bubbleSort(umaLista)
- print("DEPOIS do BS: ", umaLista)
- i+=1
- iListCounter+=1
- for i in range(500):
- print("List number",i+500+1)
- print("List number", iListCounter)
- umaLista = randomList(piQ=9) #named params
- print ("ANTES do BS: ", umaLista)
- #bubbleSort(umaLista, pbAscendingSort=False)
- bubbleSort(umaLista, False)
- print ("DEPOIS do BS: ", umaLista)
- iListCounter+=1
- #TP2M
- #1000 testes a bubble sort ASC com lista aleatoria
- #TP2M
- #500 primeiros testes para ordenação ASC
- #500 segundos testes para ordenação DESC
Add Comment
Please, Sign In to add comment