Python Sorting Algorithms

1. bubbleSort

def bubbleSort(arr):
    for i in range(1, len(arr)):
        for j in range(0, len(arr)-i):
            if arr[j] > arr[j+1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

2. radixSort

def radix(arr):

    digit = 0
    max_digit = 1
    max_value = max(arr)
    while 10**max_digit < max_value:
        max_digit = max_digit + 1

    while digit < max_digit:
        temp = [[] for i in range(10)]
        for i in arr:
            t = int((i/10**digit)%10)
            temp[t].append(i)

        coll = []
        for bucket in temp:
            for i in bucket:
                coll.append(i)

        arr = coll
        digit = digit + 1

    return arr

3. selectionSort

def selectionSort(arr):
    for i in range(len(arr) - 1):
        minIndex = i
        for j in range(i + 1, len(arr)):
            if arr[j] < arr[minIndex]:
                minIndex = j
        # exchange when i is not the min
        if i != minIndex:
            arr[i], arr[minIndex] = arr[minIndex], arr[i]
    return arr

4. insertionSort

def insertionSort(arr):
    for i in range(len(arr)):
        preIndex = i-1
        current = arr[i]
        while preIndex >= 0 and arr[preIndex] > current:
            arr[preIndex+1] = arr[preIndex]
            preIndex-=1
        arr[preIndex+1] = current
    return arr

5. shellSort

def shellSort(arr):
    import math
    gap=1
    while(gap < len(arr)/3):
        gap = gap*3+1
    while gap > 0:
        for i in range(gap,len(arr)):
            temp = arr[i]
            j = i-gap
            while j >=0 and arr[j] > temp:
                arr[j+gap]=arr[j]
                j-=gap
            arr[j+gap] = temp
        gap = math.floor(gap/3)
    return arr

6. mergeSort

def mergeSort(arr):
    import math
    if(len(arr)<2):
        return arr
    middle = math.floor(len(arr)/2)
    left, right = arr[0:middle], arr[middle:]
    return merge(mergeSort(left), mergeSort(right))

def merge(left,right):
    result = []
    while left and right:
        if left[0] <= right[0]:
            result.append(left.pop(0));
        else:
            result.append(right.pop(0));
    while left:
        result.append(left.pop(0));
    while right:
        result.append(right.pop(0));
    return result

7. quickSort

def quickSort(arr, left=None, right=None):
    left = 0 if not isinstance(left,(int, float)) else left
    right = len(arr)-1 if not isinstance(right,(int, float)) else right
    if left < right:
        partitionIndex = partition(arr, left, right)
        quickSort(arr, left, partitionIndex-1)
        quickSort(arr, partitionIndex+1, right)
    return arr

def partition(arr, left, right):
    pivot = left
    index = pivot+1
    i = index
    while  i <= right:
        if arr[i] < arr[pivot]:
            swap(arr, i, index)
            index+=1
        i+=1
    swap(arr,pivot,index-1)
    return index-1

def swap(arr, i, j):
    arr[i], arr[j] = arr[j], arr[i]

Publish your own python project on PyPi.org

  1. create a simple project:
    Directory of C:\Hans\packaging_tutorial
C:\Hans\packaging_tutorial>tree /F
Folder PATH listing for volume Windows7_OS
Volume serial number is 006F0074 54AA:C963
C:.
│ LICENSE
│ README.md
│ setup.py
│
└─list_difference
__init__.py

C:\Hans\packaging_tutorial>type setup.py

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="list_difference_zhuby1973", # Replace with your own username
    version="0.0.2",
    author="Hans Zhu",
    author_email="zhuby1973@gmail.com",
    description="A package to find difference between two list",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

C:\Hans\packaging_tutorial>type README.md

Example Package
This is a simple example package.
  1. Generating distribution archives
    Make sure you have the latest versions of setuptools and wheel installed:
    python -m pip install –user –upgrade setuptools wheel
    Now run this command from the same directory where setup.py is located:
    python setup.py sdist bdist_wheel
    This command should output a lot of text and once completed should generate two files in the dist directory:
    Directory of C:\Hans\packaging_tutorial\dist

05/28/2020 06:31 PM .
05/28/2020 06:31 PM ..
05/28/2020 06:31 PM 2,626 list_difference_zhuby1973-0.0.1-py3-none-
any.whl
05/28/2020 06:31 PM 1,304 list_difference_zhuby1973-0.0.1.tar.gz
2 File(s) 3,930 bytes

  1. Uploading the distribution archives
    you need register an account on pypi.org first!
    You’ll need to install Twine:
    python -m pip install –user –upgrade twine
    Once installed, run Twine to upload all of the archives under dist:
    python -m twine upload –repository pypi dist/*
    You will be prompted for a username and password.
  2. Installing your newly uploaded package
    You can use pip to install your package and verify that it works.
    python -m pip install –index-url https://pypi.org/simple/ –no-deps list_difference_zhuby1973
    Make sure to specify your username in the package name!
    pip should install the package from Test PyPI and the output should look something like this:
    Collecting list_difference_zhuby1973
    Downloading list_difference_zhuby1973-0.0.1-py3-none-any.whl (2.6 kB)
    Installing collected packages: list-difference-zhuby1973
    Successfully installed list-difference-zhuby1973-0.0.1
    You can test that it was installed correctly by importing the package. Run the Python interpreter (make sure you’re still in your virtualenv):

C:> python
and from the interpreter shell import the package: import list_difference

Congratulations, you’ve packaged and distributed a Python project!

Blueprint validation

#!/usr/bin/python
import os
import sys
import yaml
import json
from termcolor import colored
bp_dir = sys.argv[1]
cwd = os.getcwd()
bppath = os.path.join(cwd, bp_dir)
inpath = os.path.join(bppath, 'input')
y_file = os.path.join(bppath, 'blueprint.yaml')
def get_diff(list1, list2):
   difference = set(list1).symmetric_difference(set(list2))
   list_difference = list(difference)
   if len(list_difference) == 0:
       print("input.json parameters are matching with blueprint.yaml")
   else:
       print('Found miss match between YAML and JSON, please check below parameters:')
       print(colored(list_difference, 'red'))
def yaml_json_compare(yaml_file, json_file):
with open(yaml_file, 'r') as stream:
   try:
       dict = yaml.load(stream, Loader=yaml.FullLoader)
       for key, value in dict.items():
           if key == 'inputs':
               dict2 = value
               aList = list(dict2)
               #print(aList)
               with open(json_file) as f:
                   try:
                       data = json.load(f)
                       bList = list(data)
                       print(json_file + " is valid")
                       get_diff(aList, bList)
                   except ValueError as exc:
                       print(json_file + " is invalid")
   except yaml.YAMLError as exc:
       print(exc)
       print("blueprint.yaml is invalid")
def walk(dirname):
   for name in os.listdir(dirname):
       path = os.path.join(dirname, name)
       if os.path.isfile(path):
           yaml_json_compare(y_file, path)
       else:
           walk(path)
if __name__ == '__main__':
   walk(inpath)
here is the output:
(venv) lindas-MacBook-Air:hello linda$ python3 write_excel.py DEMO
/Users/linda/PycharmProjects/hello/DEMO/input/SIT/inputs.json is valid
Found miss match between YAML and JSON, please check below parameters:
['vm_size']
/Users/linda/PycharmProjects/hello/DEMO/input/DEV/inputs.json is valid
Found miss match between YAML and JSON, please check below parameters:
['cost1', 'cost']
/Users/linda/PycharmProjects/hello/DEMO/input/UAT/inputs.json is valid
input.json parameters are matching with blueprint.yaml

Develop web service with NodeJS

npm init –yes
npm i express
npm i -g nodemon
npm install –save express@4.16.3 mysql@2.15.0 body-parser@1.18.2
you can get version info from https://www.npmjs.com/package/body-parser
https://www.npmjs.com/package/mysql

C:\Hans\first-app>nodemon index.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter rs
[nodemon] watching path(s): .
[nodemon] watching extensions: js,mjs,json
[nodemon] starting node index.js
Listening on port 3000
[nodemon] restarting due to changes…
[nodemon] starting node index.js
Listening on port 3000

C:\Hans\first-app>set PORT=5000 (on Linux use export PORT=5000)

check records on http://localhost:3000/api/courses
use postman to POST data

Here is the code:

const express = require('express');
const app = express();

app.use(express.json());

const courses = [
    { id: 1, name: 'course1' },
    { id: 2, name: 'course2' },
    { id: 3, name: 'course3' },
];

app.get('/', (req, res) => {
    res.send('Hello World!!!');
});

app.get('/api/courses', (req, res) => {
    res.send(courses);
});


app.post('/api/courses', (req, res) => {
    const course = {
        id: courses.length + 1,
        name: req.body.name
    };
    courses.push(course);
    res.send(course);
});


app.get('/api/courses/:id', (req, res) => {
   let course = courses.find(c => c.id === parseInt(req.params.id));
   if (!course) res.status(404).send('the id not found');
   res.send(course)
});

//port
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}....`))