├── __init__.py ├── .gitignore ├── diff_operation_file_text ├── diff_operation_file_object ├── use_sys.py ├── pickling_text ├── hello_file.py ├── using_file_text ├── using_file.py ├── method.py ├── try_except.py ├── address-book_file ├── address_book_file ├── url.py ├── server_version.py ├── class_init.py ├── pickling.py ├── understand_static.py ├── finally.py ├── user_input.py ├── raising.py ├── README.md ├── static_nonstatic.py ├── application.py ├── cartesion.py ├── class_and_objects.py ├── inherit.py ├── diff_operation_file.py ├── producer_consumer.py ├── sockets.py ├── permutations.py ├── objvar.py ├── quicksort.py ├── port_scanner.py ├── sockets_server.py ├── anagrams.py ├── binarysearch.py ├── sockets_chat_server.py └── address-book.py /__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /diff_operation_file_text: -------------------------------------------------------------------------------- 1 | Writing in w mode 2 | -------------------------------------------------------------------------------- /diff_operation_file_object: -------------------------------------------------------------------------------- 1 | (lp0 2 | I1 3 | aI2 4 | aI3 5 | a. -------------------------------------------------------------------------------- /use_sys.py: -------------------------------------------------------------------------------- 1 | import sys 2 | for i in sys.argv: 3 | print i -------------------------------------------------------------------------------- /pickling_text: -------------------------------------------------------------------------------- 1 | (lp0 2 | S'mango' 3 | p1 4 | aS'apple' 5 | p2 6 | aS'banana' 7 | p3 8 | a. -------------------------------------------------------------------------------- /hello_file.py: -------------------------------------------------------------------------------- 1 | def hello(): 2 | print "hello" 3 | 4 | def hi(): 5 | print "hi" -------------------------------------------------------------------------------- /using_file_text: -------------------------------------------------------------------------------- 1 | Programming is fun 2 | when the work is done 3 | if you wanna make your work also fun 4 | use python 5 | -------------------------------------------------------------------------------- /using_file.py: -------------------------------------------------------------------------------- 1 | f=open("using_file_text") 2 | while True: 3 | text=f.readline() 4 | if(len(text)==0): 5 | break 6 | print text, 7 | f.close() -------------------------------------------------------------------------------- /method.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #filename method.py 3 | class Person: 4 | def say_hi(self): 5 | print "Hello, how are you?" 6 | p=Person() 7 | p.say_hi() -------------------------------------------------------------------------------- /try_except.py: -------------------------------------------------------------------------------- 1 | text="" 2 | try: 3 | text=input("Enter something") 4 | except EOFError: 5 | print "Eof" 6 | except KeyboardInterrupt: 7 | print "Keyboard interrupt" 8 | else: 9 | print text -------------------------------------------------------------------------------- /address-book_file: -------------------------------------------------------------------------------- 1 | (lp0 2 | (i__main__ 3 | Contact 4 | p1 5 | (dp2 6 | S'phone' 7 | p3 8 | I9632900904 9 | sS'name' 10 | p4 11 | S'akshar raaj' 12 | p5 13 | sS'email' 14 | p6 15 | S'akshar@agiliq.com' 16 | p7 17 | sba. -------------------------------------------------------------------------------- /address_book_file: -------------------------------------------------------------------------------- 1 | (lp0 2 | (i__main__ 3 | Contact 4 | p1 5 | (dp2 6 | S'phone' 7 | p3 8 | S'9949997612' 9 | p4 10 | sS'name' 11 | p5 12 | S'shabda raaj' 13 | p6 14 | sS'email' 15 | p7 16 | S'shabda@agiliq.com' 17 | p8 18 | sba. -------------------------------------------------------------------------------- /url.py: -------------------------------------------------------------------------------- 1 | import urllib2 2 | request=urllib2.Request('http://python.org') 3 | response=urllib2.urlopen(request) 4 | #response=urllib2.urlopen("http://python.org") 5 | html=response.read() 6 | file=open("url_fetch.txt",'w') 7 | file.write(html) 8 | print "completed" 9 | -------------------------------------------------------------------------------- /server_version.py: -------------------------------------------------------------------------------- 1 | import MySQLdb 2 | conn=MySQLdb.connect(host="localhost",user="root",passwd="hello",db="mysql") 3 | cursor=conn.cursor() 4 | cursor.execute("Select version();") 5 | row=cursor.fetchone() 6 | print "server version ",row[0] 7 | cursor.close() 8 | conn.close() 9 | -------------------------------------------------------------------------------- /class_init.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #filename class_init.py 3 | class Person: 4 | def __init__(self,name): 5 | self.name=name 6 | def say_hi(self): 7 | #print "Hello, I am "+self.name 8 | print ('Hello my name is {0}'.format(self.name)) 9 | Person("akshar").say_hi() -------------------------------------------------------------------------------- /pickling.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | """f=open("pickling_text","wb") 3 | shop_list=["mango","apple","banana"] 4 | pickle.dump(shop_list,f) 5 | del shop_list 6 | f.close()""" 7 | shop_list=[] 8 | print shop_list 9 | f=open("pickling_text","rb") 10 | shop_list=pickle.load(f) 11 | print shop_list -------------------------------------------------------------------------------- /understand_static.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | age=12 3 | def __init__(self,name): 4 | self.name=name 5 | def say_hi(self): 6 | print self.name, self.age 7 | def print_age(): 8 | print Person.age 9 | print_age=staticmethod(print_age) 10 | p=Person("def") 11 | p2=Person("newPerson") 12 | p.say_hi() 13 | p2.say_hi() 14 | Person.print_age() 15 | -------------------------------------------------------------------------------- /finally.py: -------------------------------------------------------------------------------- 1 | import time 2 | try: 3 | f=open("using_file_text","r") 4 | while True: 5 | text=f.readline() 6 | """if len(text)==0: 7 | break""" 8 | print text, 9 | time.sleep(1) 10 | except EOFError: 11 | print "eof" 12 | except KeyboardInterrupt: 13 | print "interrupt" 14 | finally: 15 | f.close() 16 | -------------------------------------------------------------------------------- /user_input.py: -------------------------------------------------------------------------------- 1 | def reverse(text): 2 | return text[::-1] 3 | 4 | def is_palindrome(text): 5 | text=text.replace("!","") 6 | text=text.replace("-","") 7 | text=text.replace(",","") 8 | text=text.lower() 9 | if text==reverse(text): 10 | return True 11 | else: 12 | return False 13 | 14 | text=input("Enter text\n") 15 | if is_palindrome(text): 16 | print "Palindrome" 17 | else: 18 | print "Not palindrome" 19 | -------------------------------------------------------------------------------- /raising.py: -------------------------------------------------------------------------------- 1 | #filename raising.py 2 | class ShortWordException(Exception): 3 | def __init__(self,length,atleast): 4 | Exception.__init__(self) 5 | self.length=length 6 | self.atleast=atleast 7 | 8 | try: 9 | text=input("Enter something\n") 10 | if len(text)<3: 11 | raise ShortWordException(len(text),3) 12 | except EOFError: 13 | print "eof" 14 | except ShortWordException as e: 15 | print "Length is {0}. It must be atleast {1}".format(e.length,e.atleast) 16 | else: 17 | print text 18 | 19 | 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Python-Programs 2 | =============== 3 | 4 | Contains some short programs. 5 | 6 | Some programs worth mentioning. 7 | 8 | * permutations.py: Finds all permutations of r elements out of given n elements. 9 | * cartesion.py: Finds the cartesion product. 10 | * sockets_server.py: A server using socket api provided by Python. Many concurrent clients can connect and talk with it. 11 | * producer_consumer.py: Solve producer consumer problem with Threads 12 | 13 | Sorting algorithms: 14 | ------------------- 15 | 16 | * Quicksort 17 | 18 | Search 19 | ---------- 20 | 21 | * Binary Search 22 | -------------------------------------------------------------------------------- /static_nonstatic.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | #Static variable 3 | count=0 4 | 5 | def __init__(self,name,age): 6 | self.name=name 7 | self.age=age 8 | print "Object created with "+self.name+" "+str(self.age) 9 | Person.increment_count() 10 | print "Count "+str(self.count) 11 | #Instance method 12 | def get_name(self): 13 | return self.name 14 | 15 | #Static method 16 | def increment_count(): 17 | Person.count=Person.count+1 18 | 19 | increment_count=staticmethod(increment_count) 20 | 21 | 22 | p1=Person('abc',12) 23 | p2=Person('def',16) -------------------------------------------------------------------------------- /application.py: -------------------------------------------------------------------------------- 1 | from wsgiref.simple_server import make_server 2 | 3 | def application(environ, start_response): 4 | path = environ.get('PATH_INFO') 5 | if path == '/': 6 | response_body = "Index" 7 | else: 8 | response_body = "Hello" 9 | status = "200 OK" 10 | #response_headers = [("Content-Length", str(len(response_body)))] 11 | response_headers = [] 12 | response_headers.append(('Content-Type', 'text/plain')) 13 | start_response(status, response_headers) 14 | return [response_body] 15 | 16 | httpd = make_server( 17 | '127.0.0.1', 8051, application) 18 | 19 | httpd.serve_forever() 20 | -------------------------------------------------------------------------------- /cartesion.py: -------------------------------------------------------------------------------- 1 | def cart(li): 2 | #In case an empty list is provided 3 | #eg : cart([]) 4 | if len(li) == 0: 5 | return [()] 6 | stack = [] 7 | if len(li) == 1: 8 | return [[each] for each in li[0]] 9 | else: 10 | for each in li[0]: 11 | results = cart(li[1:]) 12 | for each_result in results: 13 | each_result.insert(0, each) 14 | stack.append(each_result) 15 | return stack 16 | 17 | result = cart([['title1', 'title2'], ['body1', 'body2', 'body3'], ['url1', 'url2', 'url3']]) 18 | for combination in result: 19 | print combination 20 | -------------------------------------------------------------------------------- /class_and_objects.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #filename class_and_objects.py 3 | class Person: 4 | def __init__(self,name,age): 5 | self.name=name 6 | self.age=age 7 | 8 | def set_name(self,name): 9 | self.name=name 10 | 11 | def get_name(self): 12 | return self.name 13 | 14 | def get_age(self): 15 | return self.age 16 | 17 | """p=Person("akshar") 18 | p.set_name("shloka") 19 | print p.get_name()""" 20 | li=[] 21 | p1=Person("abc",12) 22 | p2=Person("def",15) 23 | li.append(p1) 24 | li.append(p2) 25 | p=li[0] 26 | print p.get_name()+" "+str(p.get_age()) 27 | 28 | -------------------------------------------------------------------------------- /inherit.py: -------------------------------------------------------------------------------- 1 | class SchoolMember: 2 | def __init__(self,name,age): 3 | self.name=name 4 | self.age=age 5 | print "Initializing school member {0}".format(self.name) 6 | 7 | def tell(self): 8 | print "Name: {0} Age: {1}".format(self.name,self.age) 9 | 10 | class Teacher(SchoolMember): 11 | def __init__(self,name,age,salary): 12 | SchoolMember.__init__(self,name,age) 13 | self.salary=salary 14 | print "Initialaizing teacher {0}".format(self.name) 15 | 16 | """def tell(self): 17 | SchoolMember.tell(self) 18 | print "{0} salary is {1}".format(self.name,self.salary)""" 19 | 20 | s=SchoolMember("abc",30) 21 | s.tell() 22 | t=Teacher("def",50,100000) 23 | t.tell() 24 | -------------------------------------------------------------------------------- /diff_operation_file.py: -------------------------------------------------------------------------------- 1 | """f=open("diff_operation_file_text","w") 2 | #text=f.read() 3 | text="" 4 | if(len(text)==0): 5 | print "No text" 6 | else: 7 | print text 8 | f.write("Writing in w mode\n") 9 | f=open("diff_operation_file_text","r") 10 | text=f.read() 11 | print text 12 | f.close()""" 13 | """import pickle 14 | f=open("diff_operation_file_object","w") 15 | li=[1,2,3] 16 | pickle.dump(li,f) 17 | f.close()""" 18 | import pickle 19 | import os 20 | f=open("diff_operation_file_object","r") 21 | isEmpty=os.path.getsize("diff_operation_file_object")==0 22 | """li2=[3,4,5] 23 | pickle.dump(li2,f) 24 | f.close()""" 25 | if not isEmpty: 26 | text=pickle.load(f) 27 | print text 28 | else: 29 | f=open("diff_operation_file_object","w") 30 | li=[1,2,3] 31 | pickle.dump(li,f) 32 | f.close() 33 | 34 | 35 | -------------------------------------------------------------------------------- /producer_consumer.py: -------------------------------------------------------------------------------- 1 | from threading import Thread 2 | import time 3 | import random 4 | from Queue import Queue 5 | 6 | queue = Queue(10) 7 | 8 | class ProducerThread(Thread): 9 | #Put limit on num of elements in queue 10 | def run(self): 11 | nums = range(5) 12 | global queue 13 | while True: 14 | num = random.choice(nums) 15 | queue.put(num) 16 | print "Produced", num 17 | time.sleep(random.random()) 18 | 19 | 20 | class ConsumerThread(Thread): 21 | def run(self): 22 | global queue 23 | while True: 24 | num = queue.get() 25 | queue.task_done() 26 | print "Consumed", num 27 | time.sleep(random.random()) 28 | 29 | 30 | ProducerThread().start() 31 | ConsumerThread().start() 32 | -------------------------------------------------------------------------------- /sockets.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | 4 | try: 5 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | except socket.error, msg: 7 | print "Failed to create socket" 8 | sys.exit() 9 | 10 | print "Socket created" 11 | 12 | host = 'www.google.com' 13 | port = 80 14 | 15 | try: 16 | remote_ip = socket.gethostbyname(host) 17 | except socket.gaierror: 18 | print "Could not resolve" 19 | sys.exit() 20 | 21 | print "IP address is " + remote_ip 22 | 23 | s.connect((remote_ip, port)) 24 | 25 | print 'Socket Connected to ' + host + ' on ipaddress ' + remote_ip 26 | 27 | message = "GET / HTTP/1.1\r\n\r\n" 28 | 29 | try: 30 | s.sendall(message) 31 | except socket.error: 32 | print "Send failed" 33 | sys.exit() 34 | 35 | print 'Message send successfully' 36 | 37 | reply = s.recv(4096) 38 | 39 | print reply 40 | 41 | s.close() 42 | -------------------------------------------------------------------------------- /permutations.py: -------------------------------------------------------------------------------- 1 | def permutation(li, r): 2 | stack = [] 3 | if r==1: 4 | stack = [[each] for each in li] 5 | return stack 6 | if r: 7 | r = r-1 8 | if len(li)==1: 9 | return [li] 10 | else: 11 | for i, each in enumerate(li): 12 | left_list = li[:i] 13 | right_list = li[i+1:] 14 | left_list = left_list + right_list 15 | result = [e for e in permutation(left_list, r)] 16 | for res in result: 17 | res.insert(0, each) 18 | stack.append(res) 19 | return stack 20 | 21 | def permutation_helper(li, r=None): 22 | if r and r>len(li): 23 | print("You can't arrange %d elements out of %d elements." % (r, len(li))) 24 | return 25 | else: 26 | return permutation(li, r) 27 | 28 | result = permutation_helper(['a', 'b', 'c', 'd', 'e'], 3) 29 | if result: 30 | print len(result) 31 | print result 32 | -------------------------------------------------------------------------------- /objvar.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #filename objvar.py 3 | class Robot: 4 | 5 | population=0 6 | def __init__(self,name): 7 | self.__name=name 8 | print "Initializing {0}".format(self.__name) 9 | Robot.population+=1 10 | 11 | def say_hi(self): 12 | print "Greetings, my master calls me {0}".format(self.__name) 13 | 14 | def __del__(self): 15 | print "{0} is being destroyed".format(self.__name) 16 | Robot.population-=1 17 | if(Robot.population==0): 18 | print ("{0} was the last one ".format(self.__name)) 19 | else: 20 | print ("There are still {0} robots left".format(Robot.population)) 21 | 22 | def how_many(): 23 | print "We have {0} robots left".format(Robot.population) 24 | 25 | how_many=staticmethod(how_many) 26 | 27 | r1=Robot("R1") 28 | r1.say_hi() 29 | Robot.how_many() 30 | r2=Robot("R2") 31 | r2.say_hi() 32 | Robot.how_many() 33 | #print r1.name 34 | del r1 35 | del r2 36 | Robot.how_many() 37 | 38 | -------------------------------------------------------------------------------- /quicksort.py: -------------------------------------------------------------------------------- 1 | def concatenate(left_list, pivot, right_list): 2 | left_list.extend(pivot) 3 | left_list.extend(right_list) 4 | return left_list 5 | 6 | def quicksort(li): 7 | if len(li)==1 or len(li)==0: 8 | return li 9 | pivot = li[0] 10 | left_list= [] 11 | right_list = [] 12 | for i in range(1, len(li)): 13 | if(li[i]<=pivot): 14 | left_list.append(li[i]) 15 | else: 16 | right_list.append(li[i]) 17 | return concatenate(quicksort(left_list), [pivot], quicksort(right_list)) 18 | 19 | def qs(li): 20 | return quicksort(li) 21 | 22 | import unittest 23 | import random 24 | class TestQuickSort(unittest.TestCase): 25 | def test_empty(self): 26 | print "Test ran" 27 | lst = [] 28 | ls = qs(lst) 29 | self.assertEqual(len(ls), 0) 30 | 31 | def test_one(self): 32 | lst = [1] 33 | ls = qs(lst) 34 | self.assertEqual(len(ls), 1) 35 | self.assertEqual(ls[0], 1) 36 | 37 | def test_equal(self): 38 | lst = [1,1] 39 | ls = qs(lst) 40 | self.assertEqual(len(ls), 2) 41 | self.assertEqual(ls[1], 1) 42 | 43 | def test_random(self): 44 | size = random.randint(0, 10) 45 | lst = [random.randint(0, 100) for i in range(size)] 46 | ls = qs(lst) 47 | self.assertEqual(len(ls), size) 48 | lst.sort() 49 | self.assertEqual(ls[0], lst[0]) 50 | 51 | if __name__=="__main__": 52 | unittest.main() 53 | -------------------------------------------------------------------------------- /port_scanner.py: -------------------------------------------------------------------------------- 1 | import socket #for sockets 2 | import sys #for exit 3 | from threading import Thread 4 | import threading 5 | 6 | print 'Socket Created' 7 | 8 | host = 'agiliq.com' 9 | ports = range(100) 10 | 11 | try: 12 | remote_ip = socket.gethostbyname( host ) 13 | 14 | except socket.gaierror: 15 | #could not resolve 16 | print 'Hostname could not be resolved. Exiting' 17 | sys.exit() 18 | 19 | print 'Ip address of ' + host + ' is ' + remote_ip 20 | 21 | class ConnectPortThread(Thread): 22 | def __init__(self, port): 23 | super(ConnectPortThread, self).__init__() 24 | self.port = port 25 | try: 26 | self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 27 | except socket.error, msg: 28 | print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1] 29 | sys.exit(); 30 | 31 | def run(self): 32 | try: 33 | self.s.connect((remote_ip , self.port)) 34 | print 'Socket Connected to ' + host + ' on port ' + str(self.port) 35 | except socket.error: 36 | pass 37 | 38 | def thread_count(): 39 | return threading.active_count() 40 | 41 | threads = [] 42 | for port in ports: 43 | #At some point num of thread reach a limit 44 | #and program gives an error 45 | #Make sure to never have more than 50 threads 46 | while True: 47 | if thread_count() < 50: 48 | break 49 | c = ConnectPortThread(port) 50 | threads.append(c) 51 | c.start() 52 | 53 | [thread.join() for thread in threads] 54 | -------------------------------------------------------------------------------- /sockets_server.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | import threading 4 | from Queue import Queue 5 | 6 | messages = Queue() 7 | 8 | class BroadCastThread(threading.Thread): 9 | 10 | def run(self): 11 | global messages 12 | while True: 13 | message, conn_addr = messages.get() 14 | for thread in threading.enumerate(): 15 | if isinstance(thread, ChatThread): 16 | if not conn_addr==thread.conn_addr: 17 | thread.conn.sendall(message) 18 | 19 | 20 | class ChatThread(threading.Thread): 21 | def __init__(self, conn, addr): 22 | super(ChatThread, self).__init__() 23 | self.conn = conn 24 | self.addr = addr 25 | print 'Connected with ' + addr[0] + ':' + str(addr[1]) 26 | self.conn_addr = addr[0] + ':' + str(addr[1]) 27 | 28 | def run(self): 29 | while True: 30 | global messages 31 | data = self.conn.recv(1024) 32 | if data=="\r\n": 33 | break 34 | messages.put((data, self.conn_addr)) 35 | self.conn.close() 36 | print "Conection closed with " + self.addr[0] + ":" + str(self.addr[1]) 37 | 38 | HOST = '' 39 | PORT = 8888 40 | 41 | try: 42 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 43 | except socket.error: 44 | print "Can't create socket" 45 | sys.exit() 46 | print "socket created" 47 | 48 | try: 49 | sock.bind((HOST, PORT)) 50 | except socket.error: 51 | print "Cant bind" 52 | sys.exit() 53 | print 'Socket bind complete' 54 | 55 | sock.listen(10) 56 | print 'Socket now listening' 57 | 58 | BroadCastThread().start() 59 | while True: 60 | conn, addr = sock.accept() 61 | ChatThread(conn, addr).start() 62 | -------------------------------------------------------------------------------- /anagrams.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | def check_anagrams(str1, str2): 4 | #Complexity of this algorithm seems O(NlogN) 5 | l1 = [] 6 | l2 = [] 7 | #O(N) 8 | for each in str1: 9 | l1.append(each) 10 | #O(N) 11 | for each in str2: 12 | l2.append(each) 13 | #O(NlogN) 14 | return sorted(l1) == sorted(l2) 15 | 16 | def check_anagrams_2(str1, str2): 17 | d1 = {} 18 | d2 = {} 19 | are_anagrams = True 20 | for each in str1: 21 | d1[each] = 1 if each not in d1 else d1[each]+1 22 | for each in str2: 23 | d2[each] = 1 if each not in d2 else d2[each]+1 24 | for k1, v1 in d1.iteritems(): 25 | if k1 not in d2.keys(): 26 | are_anagrams = False 27 | break 28 | v2 = d2.pop(k1) 29 | if not v1==v2: 30 | are_anagrams = False 31 | break 32 | if d2: 33 | are_anagrams = False 34 | return are_anagrams 35 | 36 | 37 | class TestAnagrams(unittest.TestCase): 38 | def test_anagrams(self): 39 | self.assertTrue(check_anagrams("debit card", "bad credit")) 40 | self.assertTrue(check_anagrams_2("debit card", "bad credit")) 41 | 42 | def test_nonagrams(self): 43 | self.assertFalse(check_anagrams("hello", "world")) 44 | self.assertFalse(check_anagrams_2("hello", "world")) 45 | 46 | def test_with_empty_strings(self): 47 | self.assertTrue(check_anagrams("", "")) 48 | self.assertTrue(check_anagrams_2("", "")) 49 | 50 | def test_with_one_empty_string(self): 51 | self.assertFalse(check_anagrams("", "nonempty")) 52 | self.assertFalse(check_anagrams_2("", "nonempty")) 53 | 54 | self.assertFalse(check_anagrams("nonempty", "")) 55 | self.assertFalse(check_anagrams_2("nonempty", "")) 56 | 57 | def test_with_spaces(self): 58 | self.assertFalse(check_anagrams("", " ")) 59 | self.assertFalse(check_anagrams_2("", " ")) 60 | 61 | if __name__ == "__main__": 62 | unittest.main() 63 | -------------------------------------------------------------------------------- /binarysearch.py: -------------------------------------------------------------------------------- 1 | """def binarysearch(slist, value, last_index): 2 | length = len(slist) 3 | if(length==0): 4 | return -1 5 | if(length==1): 6 | return 0 if slist[0]==value else -1 7 | index = length/2 8 | if(slist[index]==value): 9 | return last_index 10 | elif(value