am_dot_com

FP 2021-11-16

Nov 16th, 2021 (edited)
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.14 KB | None | 0 0
  1. #em8.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.     """
  26.    range (i:int)->list ---> [0, 1, ... , i-1]
  27.    """
  28.     listOfValidAddresses = range(iHowManyElementsInList)
  29.     #[0,1,2,3,4,5,6,7,8,9]
  30.     #linear
  31.     for iValidAddress in listOfValidAddresses:
  32.         someElement = pList[iValidAddress]
  33.         bMatchesSearchedElement = \
  34.             someElement == pEl #will be True when I found the searched pEl
  35.         if (bMatchesSearchedElement):
  36.             #return True #non-exaustive
  37.             listRet.append(iValidAddress) #appends to the end
  38.         #if
  39.     #for
  40.     return listRet
  41. #def searchInList
  42.  
  43. def randomEMBet(
  44.     piMin = DEFAULT_MIN,
  45.     piMax = DEFAULT_MAX,
  46.     piQ = DEFAULT_Q,
  47.     piStarMin = DEFAULT_STAR_MIN,
  48.     piStarMax = DEFAULT_STAR_MAX,
  49.     piQS = DEFAULT_QS
  50. ):
  51.     dictRandomBet = {}
  52.  
  53.     #TODO
  54.     dictRandomBet["nums"] = randomList(
  55.         DEFAULT_MIN,
  56.         DEFAULT_MAX,
  57.         DEFAULT_Q
  58.     ) #not sorted
  59.     dictRandomBet["stars"] = randomList(
  60.         DEFAULT_STAR_MIN,
  61.         DEFAULT_STAR_MAX,
  62.         DEFAULT_QS
  63.     ) #not sorted
  64.  
  65.     #sort!
  66.     bubbleSort(dictRandomBet["nums"])
  67.     bubbleSort(dictRandomBet["stars"])
  68.  
  69.     return dictRandomBet
  70. #def randomEMBet
  71.  
  72. import random
  73. def randomList(
  74.     piMin=1, #the min allowed value
  75.     piMax=1000, #the max allowed value
  76.     piQ=5, #the quantity of wanted values
  77.     pbAllowReps=False #allow repitiions? (defaults to NO)
  78. ):
  79.     iAmplitude = piMax-piMin+1 #[1,2] 2-1+1 = 2
  80.     if (pbAllowReps):
  81.         bIsPossible = True
  82.     else:
  83.         bIsPossible = piQ<=iAmplitude
  84.  
  85.     listRet = []
  86.     listRet = list()
  87.  
  88.     if (bIsPossible):
  89.         bListReady = False
  90.         while (not bListReady):
  91.             i = random.randint(piMin, piMax)
  92.             #without checking, directly
  93.             if (not pbAllowReps): #if not repetitions allowed
  94.                 # Understand this
  95.                 bNumberAlreadyInTheList = \
  96.                     len(searchInList(listRet, i))>=1 #call
  97.                 #insert in list if already NOT in list
  98.                 #to avoid repetitions
  99.                 if (not bNumberAlreadyInTheList):
  100.                     listRet.append(i)
  101.                 #if
  102.             #if
  103.             else: #repetitions allowed, number goes in without checking
  104.                 listRet.append(i)
  105.  
  106.  
  107.             bListReady = len(listRet)==piQ
  108.         #while
  109.     #if is possible to compute the requested list
  110.  
  111.     return listRet
  112. #def randomList
  113.  
  114. def bubbleSort (pCol, pbAscendingSort:bool=True):
  115.     iNumSwaps = 0
  116.     iNumWalks = 0
  117.     #while
  118.     bSorted = False
  119.     while(not bSorted):
  120.         """
  121.        Bubble Sort
  122.        will iterate from the first
  123.        to the **penultimate** address
  124.        """
  125.         listValidAddressesForBS = \
  126.             range(len(pCol)-1) #[0, len-1-1]
  127.  
  128.         bSwaps=False #reset bSwaps BEFORE each walk
  129.         for iAddress in listValidAddressesForBS:
  130.             if (pbAscendingSort):
  131.                 bAdjacentElementsOutOfOrder = \
  132.                     pCol[iAddress] > pCol[iAddress+1]
  133.             else:
  134.                 bAdjacentElementsOutOfOrder = \
  135.                     pCol[iAddress] < pCol[iAddress + 1]
  136.             if (bAdjacentElementsOutOfOrder):
  137.                 #swap / trocar elements
  138.                 iNumSwaps+=1
  139.                 """
  140.                temp = pCol[iAddress]
  141.                pCol[iAddress] = pCol[iAddress+1]
  142.                pCol[iAddress+1] = temp
  143.                """
  144.  
  145.                 #possible in Python, but not in other langs
  146.                 pCol[iAddress],pCol[iAddress+1] = \
  147.                     pCol[iAddress+1], pCol[iAddress]
  148.  
  149.                 bSwaps = True
  150.             #if
  151.         #for / walk ends
  152.         iNumWalks+=1
  153.  
  154.         #if no swaps were made during the walk over the col, then it is sorted
  155.         if (not bSwaps):
  156.             bSorted = True
  157.     return iNumSwaps, iNumWalks
  158. #def bubbleSort
  159.  
  160. ADDRESS_OF_SWAPS = 0
  161. def findMaxSwap(pListPerformances):
  162.     iAddressOfTheHardestSituation = None
  163.  
  164.     listValidAddresses = \
  165.         range(0, len(pListPerformances))
  166.  
  167.     for addr in listValidAddresses:
  168.         tuple2SW = pListPerformances[addr]
  169.         iSwaps = tuple2SW[ADDRESS_OF_SWAPS]
  170.         if (iAddressOfTheHardestSituation==None):
  171.             iAddressOfTheHardestSituation=addr
  172.         else:
  173.             currentWorst = pListPerformances[
  174.                 iAddressOfTheHardestSituation
  175.             ][0]
  176.             bChangeWorst = iSwaps>currentWorst
  177.             if(bChangeWorst):
  178.                 iAddressOfTheHardestSituation = addr
  179.         #else
  180.     #for
  181.     #return iAddressOfTheHardestSituation
  182.     return \
  183.     pListPerformances[iAddressOfTheHardestSituation]
  184. #def findMaxSwap
  185.  
  186. """
  187. tupleHardest = findMaxSwap(listPerformances)
  188. print ("Hardest situation: ", tupleHardest)
  189. """
  190.  
  191. """
  192. for i in range(10):
  193.    em = randomEMBet()
  194.    print (em)
  195. """
  196.  
  197. #tuple, list, dict, set
  198. def emClassify(pD1:dict, pD2:dict):
  199.     setN1 = set(pD1["nums"])
  200.     setN2 = set(pD2["nums"])
  201.     setIntersectNums = setN1.intersection(setN2)
  202.  
  203.     setS1 = set(pD1["stars"])
  204.     setS2 = set(pD2["stars"])
  205.     setIntersectStars = setS1.intersection(setS2)
  206.  
  207.     iNums = len(setIntersectNums)
  208.     iStars = len(setIntersectStars)
  209.     return (iNums, iStars)
  210. #def emClassify
  211.  
  212.  
  213. fav = {"nums":[1, 2, 3, 4, 5], "stars":[9, 10]}
  214. chave = {"nums":[10, 15, 20, 30, 31], "stars":[9, 10]}
  215.  
  216. res = emClassify(fav, chave)
  217. print (res)
  218.  
  219. """
  220. set rejects duplicates
  221. """
  222. set1 = set([10, 10, 11, "a", "a", True]) #rejects duplicates
  223. set2 = set(["a", "b", "c", True])
  224. union1 = set1.union(set2)
  225. intersect1 = set2.intersection(set1)
  226. print (set1)
  227. print (set2)
  228. print(union1)
  229. print(intersect1)
  230.  
Add Comment
Please, Sign In to add comment