#Kenny Centeno #Y00409919 #COMP 2900 #Seccion: 72584 def binarySearch(alist, item): #defining the class an arraylist first = 0 #assigns 0 to the first element of the arraylist last = len(alist)-1 #sets the last element of the arraylist found = False #the value entered is false while first <= last and not found: midpoint = (first + last)//2 #while the value entered is less or equal to 0 and isn't false the next this instruction will be applied if alist[midpoint] == item: found = True else: if item < alist[midpoint]: last = midpoint-1 else: first = midpoint+1 return found #if the element isn't found in the arraylist then the program returns found testlist = [0 , 1, 2, 8, 13 , 17, 19, 32, 42,] #list with all the elements values assign print(binarySearch(testlist, 3)) #prints false because 3 isn't in one of the values print(binarySearch(testlist, 13)) #prints true because 13 is a value present in the lis #code from Problem Solving with Algorithms and Data Structures using Python
from SimpleNode import SimpleNode class SimpleLoopList(): def __init__(self): self.head = None # Master reference to the list header. def getBeforeTo(self, tar): aux = self.head # Auxiliary reference for scrolling through the list. while(aux.next != tar): aux = aux.next # Go to the next list item. return aux def getLast(self): aux = self.head while(aux.next != self.head): aux = aux.next return aux def addStack(self, value): new = SimpleNode() new.value = value if(self.head == None): self.head = new self.head.next = self.head # The header is linked to itself. else: last = self.getLast() # Get the last node in the list. new.next = self.head last.next = new # The last node in the list links to the new node. self.head = new # The header moves to the new node. def addTail(self, value): new = SimpleNode() new.value = value if(self.head == None): self.head = new self.head.next = self.head else: last = self.getLast() last.next = new new.next = self.head def search(self, value): aux = self.head while(aux.next != self.head): if(aux.value == value): break aux = aux.next if(aux.value == value): # Check that the auxiliary contains the target load. return aux else: return None def edit(self, old, new): target = self.search(old) # Gets the node containing a specified load. if(target != None): target.value = new # Updates the node payload. def insertBefore(self, tar, value): target = self.search(tar) if(target != None): if(target != self.head): new = SimpleNode() new.value = value bef = self.getBeforeTo(target) # Obtains the node immediately preceding the target node. bef.next = new # The previous node links to the new node. new.next = target else: self.addStack(value) def insertAfter(self, tar, value): target = self.search(tar) if(target != None): new = SimpleNode() new.value = value new.next = target.next target.next = new def delete(self, value): target = self.search(value) if(target != None): if(target == self.head): self.head = self.head.next # Save the header by moving it to the next node in the list. bef = self.getBeforeTo(target) bef.next = target.next target.next = None # Break the node link. del(target) # Deletes the node from memory. def print(self): aux = self.head while(True and aux != None): print(aux.value) aux = aux.next if(aux == self.head): break
mylist=['My','name','is','Nitin','Srivastava'] mylist1=[12,2,19,80] mylist.sort() print(mylist) mylist1.sort() print(mylist1) #Converting a list into a tuple mytuple=(mylist[0:2]) print(mytuple) mytuple1 =('This','is','another','tuple') # Note the difference a comma makes anothertuple=(3) anothertuple1=(3,) print(anothertuple*3) print(anothertuple1*3) #ADDITION in two lists mylist2=[9,10,11] mylist3=[11,12,13] mylist4=mylist2+mylist3 print(mylist4) mylist5=[] for elm in mylist2: x= mylist2.index(elm) mylist5.append(mylist2[x]+mylist3[x]) print(mylist5) #List Methods mylist6=['My','name','is','Nitin','Srivastava'] mylisttemp="Nitin" mylist6.extend(mylisttemp) print(mylist6) mylist6.append(mylisttemp) print(mylist6) print(mylist6[2:9]) print(mylist6[2:9:2]) #Zipping a=['One','Two','Three'] b=[1,2,3] c=zip(a,b) print(c) callers=[] for c in range(11): callers.append(c) while callers: print(callers.pop(0)) #Tuples packaging q=('o','n','e') r=('t','w','o') s=q+r print(s) #Unpackaging n,i,t,s,r,v=s print(n,i,t,s,r,i) joinedtuple=('one','two','three') x,y,z=joinedtuple print(x,y,z)
def testMethod(var1, *abc) : print(var1) for val in abc: print(val) testMethod(10, 20, 30, 40, 50) print(""); # Lambda testLambda = lambda arg1, arg2 : arg2 - arg1; print(testLambda(150,250));
# Hello World program in Python dict = { "key1":"val1", "key2":"val2", "key3":"val3", } dict['key4'] = 'val4' # dict1 = dict.copy(); # print(dict1); # seq = ('name', 'age') # dict2 = dict.fromkeys(seq) # print ("New Dictionary : %s" % str(dict2)) # dict2 = dict.fromkeys(('name1', 'age1'),[100,200]) # print ("New Dictionary : %s" % str(dict2)) print(dict.get('key21')); dict.setdefault('key5','val5') print(dict.items()); dict3 = { "key6":"val6", "key1":"val_1", "key7":"val7", } dict.update(dict3) print(dict);
import math import random # Игра со случайным игроком fNames = ["Alice" , "Mark" , "John" , "Victor"] sNames = ["Sancez" , "Iglesias" , "Wolf"] diffc = [ { "damage" : 10 , "name" : "easy"}, { "damage" : 15 , "name" : "normal"}, { "damage" : 30 , "name" : "hard"} ] class TOOLS: i = 12345 def get(self , name): return name**2 tool = TOOLS() x = fNames[random.randint(0,len(fNames)-1)] y = sNames[random.randint(0,len(sNames)-1)] z = random.randint(18,45) print("Hello , I'm" , x,y,"and me" , z , "ages ") num = random.randint(0,len(diffc)-1) diff = diffc[num]["damage"] difName = diffc[num]["name"] print(diff) print(difName) healt = 100 - ((z - 18)/27)*diff print( "Healt %.2f" % healt)
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more