def quickSM(L, n): if L.size > 1: t = L.head pivot = L.head.item smallList = lists.List() largeList = lists.List() t = t.next while t is not None: smallList, largeList = qSortHelper(t.item, smallList, largeList, pivot) t = t.next if (smallList.size + 1) == n: return pivot elif (smallList.size + 1) < n: return quickSM(largeList, 0) else: return quickSM(largeList, 0)
def mergeSort(L): #will take a list and break it down into two smaller lists and iterate the #smaller lists appedning the smaller item of both sorted lsits to the now #sorted list SL = lists.List() if L.size > 1: counter = 0 temp = L.head #split into 2 smaller lists ll = lists.List() rl = lists.List() while temp is not None: mergSplitter(ll, rl, counter, temp.item, L.size) counter = +1 temp = temp.next mergeSort(ll) mergeSort(rl) SL = merge(mergeSort(ll), mergeSort(rl))
def quickSort(L): #will take a list break that list into two samller with items greater #or less than the item at index 0 #will then concatinate the two lists after all recursive calls if L.size > 1: pivot = L.head.item l1 = lists.List() l2 = lists.List() #for every element in L temp = L.head.next while temp is not None: l1, l2 = qSortHelper(temp.item, l1, l2, pivot) temp = temp.next quickSort(l1) quickSort(l2) #lists.Append(l1,pivot) lists.Prepend(l2, pivot) lists.Concatenate(l1, l2) L.head = l1.head
def quickSortMotified(L, n): if L.size > 1: t = L.head rnk = rank01(L, n) pivot = L.head.item smallList = lists.List() largeList = lists.List() while t is not None: #print(t.item) smallList, largeList = qSortHelper(t.item, smallList, largeList, pivot) t = t.next if (pivot == rnk): # rank n is pivot return pivot elif (lists.Contains(smallList, rnk)): # rank n in small list nr = rank01(largeList, largeList.head.item) return quickSortMotified(largeList, nr) else: # rank n in large list nr = rank01(smallList, smallList.head.item) return quickSortMotified(smallList, nr)
def create_player(mage, x, y): #get players's attacks attacks = [] if mage['attacks']: for atk in mage['attacks']: attacks.append(adic.get_attack(atk)) #get monster's defense defense = [adic.get_defense(mage['defense'])] #note: armor, xp is hardcoded as zero creature_component = obcl.Creature( hp = mage['life'] - 10, #using MW values but adjusting for player's level mana = mage['mana'], channeling = mage['channeling'] - 4, #again, MW values adjusted. Player can regain over time armor = 0, xp = 0, attacks = attacks, defense = defense, alignment = 'player', death_function=obcl.player_death) defn.player = obcl.Object( x, y, traits=mage['traits'], properties = { 'name' : mage['name'], 'graphic' : '@', 'color' : libtcod.white, 'level' : 1, 'subtypes' : None, 'description' : 'This is you. If you die, the game is over.'}, blocks=True, creature=creature_component) defn.player.spellbook = lists.List() #defn.player.spellbook.contents = [] defn.training = mage['training'] defn.antitraining = mage['antitraining'] defn.player.creatures = [] #might change to allow some mages to start with a pet for spell in mage['spells']: new_spell = sdic.get_spell(spell) defn.player.spellbook.insert(new_spell, 'infinite') return defn.player
def merge(LL, RL): #will take two lists and create a single list of order items contatined #in the two smaller lists t1 = LL.head t2 = RL.head SL = lists.List() while t1 is not None or t2 is not None: if t2 == None or t1.item < t2.item: #if left list item small or right list is empty append lists.Append(SL, t1.item) t1 = t1.next else: lists.Append(SL, t2.item) t2 = t2.next return SL
smallList = lists.List() largeList = lists.List() t = t.next while t is not None: smallList, largeList = qSortHelper(t.item, smallList, largeList, pivot) t = t.next if (smallList.size + 1) == n: return pivot elif (smallList.size + 1) < n: return quickSM(largeList, 0) else: return quickSM(largeList, 0) L = lists.List() emptyList = lists.List() testList = lists.List() fillList(L) fillTList(testList) print('Original list: ', end=' ') #lists.Print(L) starttime = dt.now() bubbleSort(L) endtime = dt.now() - starttime print('it took ' + str(endtime) + ' to execute') #quickSort(L) #mergeSort(L) #print(quickSM(L,L.size/2))