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
- ) #not sorted
- dictRandomBet["stars"] = randomList(
- DEFAULT_STAR_MIN,
- DEFAULT_STAR_MAX,
- DEFAULT_QS
- ) #not sorted
- #sort!
- bubbleSort(dictRandomBet["nums"])
- bubbleSort(dictRandomBet["stars"])
- 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):
- iNumSwaps = 0
- iNumWalks = 0
- #while
- 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
- iNumSwaps+=1
- """
- 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 / walk ends
- iNumWalks+=1
- #if no swaps were made during the walk over the col, then it is sorted
- if (not bSwaps):
- bSorted = True
- return iNumSwaps, iNumWalks
- #def bubbleSort
- ADDRESS_OF_SWAPS = 0
- def findMaxSwap(pListPerformances):
- iAddressOfTheHardestSituation = None
- listValidAddresses = \
- range(0, len(pListPerformances))
- for addr in listValidAddresses:
- tuple2SW = pListPerformances[addr]
- iSwaps = tuple2SW[ADDRESS_OF_SWAPS]
- if (iAddressOfTheHardestSituation==None):
- iAddressOfTheHardestSituation=addr
- else:
- currentWorst = pListPerformances[
- iAddressOfTheHardestSituation
- ][0]
- bChangeWorst = iSwaps>currentWorst
- if(bChangeWorst):
- iAddressOfTheHardestSituation = addr
- #else
- #for
- #return iAddressOfTheHardestSituation
- return \
- pListPerformances[iAddressOfTheHardestSituation]
- #def findMaxSwap
- """
- tupleHardest = findMaxSwap(listPerformances)
- print ("Hardest situation: ", tupleHardest)
- """
- """
- for i in range(10):
- em = randomEMBet()
- print (em)
- """
- #tuple, list, dict, set
- def emClassify(pD1:dict, pD2:dict):
- setN1 = set(pD1["nums"])
- setN2 = set(pD2["nums"])
- setIntersectNums = setN1.intersection(setN2)
- setS1 = set(pD1["stars"])
- setS2 = set(pD2["stars"])
- setIntersectStars = setS1.intersection(setS2)
- iNums = len(setIntersectNums)
- iStars = len(setIntersectStars)
- return (iNums, iStars)
- #def emClassify
- fav = {"nums":[1, 2, 3, 4, 5], "stars":[9, 10]}
- chave = {"nums":[10, 15, 20, 30, 31], "stars":[9, 10]}
- res = emClassify(fav, chave)
- print (res)
- """
- set rejects duplicates
- """
- set1 = set([10, 10, 11, "a", "a", True]) #rejects duplicates
- set2 = set(["a", "b", "c", True])
- union1 = set1.union(set2)
- intersect1 = set2.intersection(set1)
- print (set1)
- print (set2)
- print(union1)
- print(intersect1)
Add Comment
Please, Sign In to add comment