Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Saturday, November 9, 2013

Quick Sort

# -*- coding:utf-8 -*-
#!/usr/bin/python2
"""
===================================================
Merge Sort
Shijie Xu <xushijie520@gmail.com>
Created on Mon, Nov 9, 2013
===================================================
"""
L = [3, 5, 7, 8, 1, 9, 2, 4, 6, 123, 12, 19, 123123]

def QuickSort(m):
    if len(m) < 2:
        return m
    else:
        target = m.pop(0)
        L1 = []
        L2 = []
        for item in m:
            if item <= target:
                L1.append(item)
            else:
                L2.append(item)
        L1.append(target)
        L = QuickSort(L1) + QuickSort(L2)
        return L

result = QuickSort(L)
print result

Merge Sort

# -*- coding:utf-8 -*-
#!/usr/bin/python2
"""
=====================================================
Merge Sort
Shijie Xu <xushijie520@gmail.com>
Created on Mon, Nov 9, 2013
=====================================================
"""
L = [7, 10, 5, 8, 1, 3, 2, 4, 6, 9]
def merge(x, y):
    L = []
    while (len(x) > 0) and (len(y) > 0):
        if x[0] < y[0]:
            L.append(x[0])
            x.pop(0)
        else:
            L.append(y[0])
            y.pop(0)
    L += x if len(x) > 0 else y
    return L

def msort(m):
    if len(m) < 2:
        return m
    else:
        mid = len(m) // 2
        L1 = msort(m[:mid])
        L2 = msort(m[mid:])
        l_result = merge(L1, L2)
        return l_result

reuslt = msort(L)
print reuslt

Sunday, October 6, 2013

Seattle

This is a Learning Journal of the Seattle in CS program in New York University.

1) Local Test Mechanism
python repy.py restrictions.test <Your repy files> 
2) Login to the Seattle
  1. python seash.py
  2. !> loadkeys <username> # load the the key information of the user
  3. !> as <username> # login as the user
  4. <username>@ !>
3) Useful Instruction in Seattle
    a) browse 
        # Browse the available resources in the Seattle.
    b) on browsegood 
        # Use the resources which are working in good condition
    c) run <Your repy files in your local path>
        # Run the program
    d) show log
        # Show the result of what the program have executed
    e) list
        # List the information of the vessel that you own or control
4) Specific Functions & Variables in Seattle
   You can refer to the webpage:
        https://seattle.poly.edu/wiki/RepyApi#waitforconnlocaliplocalportfunction
    mycontext: It is a dictionary type provided for the global variables
        eg: mycontext['global_variables'] = 'This is a global varible'
    settimer(waittime, function, arg): Set a timer that when it expires will start a new thread to call a function with a set of arguments. The thread is charged to your problem when you set the timer instead of when the timer fires. This function returns a timer handle that may be used to cancel the timer before the thread is started.

Monday, February 25, 2013

My First Crawler

import urllib2
from pyquery import PyQuery as pq

url = "http://api.jquery.com/category/selectors/"
result = urllib2.urlopen(url).read().decode("utf8")
q = pq(result)
result_of_bookmarks = q('a[rel="bookmark"]')
bookmarks = result_of_bookmarks.map(lambda i, e: pq(e).text())
'''
for item in bookmarks:
    print item.encode('utf8')

'''

result_of_sumaries = q('div[class="entry-summary"]')
sumaries = result_of_sumaries.map(lambda i, e: pq(e).text())
'''
for item in sumaries:
    print item.encode('utf8')
'''
Query = {}
for i in range(len(bookmarks)):
    Query[bookmarks[i]] = sumaries[i]

Sunday, February 24, 2013

Ubuntu Sublime Python Env


1) Change Environment Path
Find the file named "Python.Sublime-build":
{
    "cmd": ["python", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Change the Python path to yours in the Environment like:
{
"cmd": ["/home/hiddenghost/pyenv/bin/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

2) Python Project Configuration Example
With the project name --> "*.sublime-project"
{
"folders":
    [
        {
            "path": "/home/hiddenghost/Documents/hiddenghost"
        }
    ],
    "build_systems":
    [
        {
       "name": "Run Tests",
        "env":
        {
        "PYTHONPATH": "/home/hiddenghost/pyenv/bin/python:/home/hiddenghost/pyenv/lib/python2.7/site-packages"
        },
       "working_dir": "/home/hiddenghost/Documents/hiddenghost",
       "cmd": ["/home/hiddenghost/pyenv/bin/python", "$file"],
       "selector": "source.python"
   }
    ]
}