am_dot_com

FP 2021-11-15

Nov 15th, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 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. )
  59. dictRandomBet["stars"] = randomList(
  60. DEFAULT_STAR_MIN,
  61. DEFAULT_STAR_MAX,
  62. DEFAULT_QS
  63. )
  64.  
  65. return dictRandomBet
  66. #def randomEMBet
  67.  
  68. import random
  69. def randomList(
  70. piMin=1, #the min allowed value
  71. piMax=1000, #the max allowed value
  72. piQ=5, #the quantity of wanted values
  73. pbAllowReps=False #allow repitiions? (defaults to NO)
  74. ):
  75. iAmplitude = piMax-piMin+1 #[1,2] 2-1+1 = 2
  76. if (pbAllowReps):
  77. bIsPossible = True
  78. else:
  79. bIsPossible = piQ<=iAmplitude
  80.  
  81. listRet = []
  82. listRet = list()
  83.  
  84. if (bIsPossible):
  85. bListReady = False
  86. while (not bListReady):
  87. i = random.randint(piMin, piMax)
  88. #without checking, directly
  89. if (not pbAllowReps): #if not repetitions allowed
  90. # Understand this
  91. bNumberAlreadyInTheList = \
  92. len(searchInList(listRet, i))>=1 #call
  93. #insert in list if already NOT in list
  94. #to avoid repetitions
  95. if (not bNumberAlreadyInTheList):
  96. listRet.append(i)
  97. #if
  98. #if
  99. else: #repetitions allowed, number goes in without checking
  100. listRet.append(i)
  101.  
  102.  
  103. bListReady = len(listRet)==piQ
  104. #while
  105. #if is possible to compute the requested list
  106.  
  107. return listRet
  108. #def randomList
  109.  
  110. def bubbleSort (pCol, pbAscendingSort:bool=True):
  111. bSorted = False
  112. while(not bSorted):
  113. """
  114. Bubble Sort
  115. will iterate from the first
  116. to the **penultimate** address
  117. """
  118. listValidAddressesForBS = \
  119. range(len(pCol)-1) #[0, len-1-1]
  120.  
  121. bSwaps=False #reset bSwaps BEFORE each walk
  122. for iAddress in listValidAddressesForBS:
  123. if (pbAscendingSort):
  124. bAdjacentElementsOutOfOrder = \
  125. pCol[iAddress] > pCol[iAddress+1]
  126. else:
  127. bAdjacentElementsOutOfOrder = \
  128. pCol[iAddress] < pCol[iAddress + 1]
  129. if (bAdjacentElementsOutOfOrder):
  130. #swap / trocar elements
  131. temp = pCol[iAddress]
  132. pCol[iAddress] = pCol[iAddress+1]
  133. pCol[iAddress+1] = temp
  134.  
  135. #possible in Python, but not in other langs
  136. #pCol[iAddress],pCol[iAddress+1] = pCol[iAddress+1], pCol[iAddress]
  137.  
  138. bSwaps = True
  139. #if
  140. #for
  141. #if no swaps were made during the walk over the col, then it is sorted
  142. if (not bSwaps):
  143. bSorted = True
  144. #while
  145. #def bubbleSort
  146.  
  147. """
  148. for iTestNumber in range(100):
  149. rbet = randomEMBet()
  150. print (rbet)
  151. #for
  152. """
  153. iListCounter=1
  154. i=0
  155. while (i<500):
  156. print("List number",i+1)
  157. print("List number", iListCounter)
  158. umaLista = randomList(piQ=9) #named params
  159. print("ANTES do BS: ", umaLista)
  160. bubbleSort(umaLista)
  161. print("DEPOIS do BS: ", umaLista)
  162. i+=1
  163. iListCounter+=1
  164.  
  165. for i in range(500):
  166. print("List number",i+500+1)
  167. print("List number", iListCounter)
  168. umaLista = randomList(piQ=9) #named params
  169. print ("ANTES do BS: ", umaLista)
  170. #bubbleSort(umaLista, pbAscendingSort=False)
  171. bubbleSort(umaLista, False)
  172. print ("DEPOIS do BS: ", umaLista)
  173. iListCounter+=1
  174.  
  175. #TP2M
  176. #1000 testes a bubble sort ASC com lista aleatoria
  177. #TP2M
  178. #500 primeiros testes para ordenação ASC
  179. #500 segundos testes para ordenação DESC
Add Comment
Please, Sign In to add comment