Monday, January 23, 2017

Python Big List

Modules: modules are libraries in Python. Python ships with many standard library modules. Modules can be imported using the import statement. >>>import time >>> time.asctime() 'Mon Jan 23 12:25 2016' The asctime function form the time module returns the current time of the system as a string.

First name:

Last name:


if you click the "submit" button, the form data will be sent to a page called "action_page.php".

Python

----------------------------------------------------------------------------------


import heapq

class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0


   def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index += 1

   def pop( self):
        return heapq.heapop(self._queue)[-1]


------------------------------------------------------------------------------------ Heap: an untidy collection f thigns piled up haphazardly. Heapsort: implemented by pushing all values onto a heap and then popping off the smallest values one at a time. Python standard library ---> webbrowser --->(function) def open ---> time --> (function) def ctime and def sleep(suspend for a few seconds) These functions are abstract....


import heapq

class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0


   def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index += 1

   def pop( self):
        return heapq.heapop(self._queue)[-1]



self variable: represents the instance of the object itself; you have to declare it explicitly.  

a = A()
a.method_a('Sailor!')

__init__ roughly represents a constructor; 

when calling A() python creates an object for you, and passes it as the first parameter to the __init__ method.  A(24, 'Hello') will get passed as arguments--causing an exception to be raised.
 
Python Solutions


Here are the running times of some operations we might perform on the phone book, from best to worst:

    O(1) (worst case): Given the page that a business's name is on and the business name, find the phone number.

    O(1) (average case): Given the page that a person's name is on and their name, find the phone number.

    O(log n): Given a person's name, find the phone number by picking a random point about halfway through the part of the book you haven't searched yet, then checking to see whether the person's name is at that point. Then repeat the process about halfway through the part of the book where the person's name lies. (This is a binary search for a person's name.)

    O(n): Find all people whose phone numbers contain the digit "5".

    O(n): Given a phone number, find the person or business with that number.

    O(n log n): There was a mix-up at the printer's office, and our phone book had all its pages inserted in a random order. Fix the ordering so that it's correct by looking at the first name on each page and then putting that page in the appropriate spot in a new, empty phone book.

For the below examples, we're now at the printer's office. Phone books are waiting to be mailed to each resident or business, and there's a sticker on each phone book identifying where it should be mailed to. Every person or business gets one phone book.

    O(n log n): We want to personalize the phone book, so we're going to find each person or business's name in their designated copy, then circle their name in the book and write a short thank-you note for their patronage.

    O(n2): A mistake occurred at the office, and every entry in each of the phone books has an extra "0" at the end of the phone number. Take some white-out and remove each zero.

    O(n · n!): We're ready to load the phonebooks onto the shipping dock. Unfortunately, the robot that was supposed to load the books has gone haywire: it's putting the books onto the truck in a random order! Even worse, it loads all the books onto the truck, then checks to see if they're in the right order, and if not, it unloads them and starts over. (This is the dreaded bogo sort.)

    O(nn): You fix the robot so that it's loading things correctly. The next day, one of your co-workers plays a prank on you and wires the loading dock robot to the automated printing systems. Every time the robot goes to load an original book, the factory printer makes a duplicate run of all the phonebooks! Fortunately, the robot's bug-detection systems are sophisticated enough that the robot doesn't try printing even more copies when it encounters a duplicate book for loading, but it still has to load every original and duplicate book that's been printed.

Big O Notation

No comments:

Post a Comment