Thursday, May 5, 2016

Python access Clipbaord

First of all you need to install the clipboard package.
You can do this with pip.


->pip --install clipboard

I use it with python 3 but it will also work with version 2.
import clipboard #Import the package


To get datas from clipboard simply call:
clipboard.paste()



And to write them to the clipboard call:
clipboard.copy("Your Argument")



Python solve Algorithm

# author:           Marcin Cherek
# python version:   3.4
# Date:             5 of Mai 2016
# language:         English
# Title:            solve Algorithm python implementation
import decimal
import logging

logging.basicConfig()
logger= logging.getLogger(__name__)
logger.setLevel(logging.INFO)

#Prepares the the calculation
#this method splits the Operators
#from the Numbers
def _prepare(calculation):
        for i in ["+","-","*","/"]:
                calculation=calculation.replace(i,";"+i+";")
        return calculation.split(";")

#Replaces the sub calculation
#with the Result of it
def _replaceRes(deb,result,zähler):
        deb[zähler-1]=""
        deb[zähler]=""
        deb[zähler+1]=result
        return deb

#Normal calculation of two
#Numebers and one Operator
def _calc(z1,z2,op):
        if(op=="+"):return z1+z2
        elif(op=="-"):return z1-z2
        elif(op=="*"):return z1*z2
        elif(op=="/"):return z1/z2
        
#Shrinks the Array
#It removes the empty elements
def _popArr(deb):
        p=0
        while p<len(deb):
            if deb[p]=="":
                deb.pop(p)
                continue
            p+=1
        return deb

#Most important method
#It iterates the calculation
#and corrdinates the subcalcs with the
#results of it
def _solve_simple(deb,op):
        result=0;zähler=0
        for i in deb:
            if i==op:
                logger.info(deb)
                result=_calc(decimal.Decimal(deb[zähler-1]),decimal.Decimal(deb[zähler+1]),op)
                _replaceRes(deb,result,zähler)
                result=0
            zähler+=1
        deb = _popArr(deb)
        return deb

#Calls the simple Alg
#First of course */ and after that +-
def solve(calculation):
        calculation = _prepare(calculation)
        for i in ["*","/","+","-"]:
                calculation=_solve_simple(calculation,i)
        return decimal.Decimal(calculation[len(calculation)-1])

Tuesday, May 3, 2016

Programming Books


https://drive.google.com/open?id=0B18_gfM_Pmu-bEpyVU8wOTVPbjQ

  • Clean Code
  • code complete 2nd edition
  • Effective Java
  • HeadFirstDesignPatterns
  • Java Performance Tuning
  • Java Performance the definitive Guide
  • Python High Performance Programming
  • Software Architecture Design Pattern
  • Working Effectively with Legacy Code

Monday, May 2, 2016

KMP Algorithm in Python

# author:           Marcin Cherek
# python version:   2.7.11
# Date:             2 of Mai 2016
# language:         English
# Title:            KMP Algorithm python implementation

#Build the Prefix table
def lspTable(pattern):
    lsp = [0] * len(pattern)
    lsp[0] = 0
    for i in range(1,len(pattern)):
        j = int(lsp[i-1])
        while(j>0 and pattern[i]!=pattern[j]):
            j=int(lsp[j-1])
        if(pattern[i] == pattern[j]):
            j+=1
        lsp[i]=j
    return lsp

def kpm_search(pattern, text):
    lsp = lspTable(pattern)
    j = 0
    for i in range(0,len(text)):
        while( j>0 and text[i] != pattern[j]):
            j = lsp[j-1]
        if(text[i]==pattern[j]):
            j+=1
            if(j==len(pattern)):
               return i-(j-1) #returns the position
    return -1 #Not found

Saturday, April 30, 2016

Quicksort in Python

# author:           Marcin Cherek
# python version:   2.7.11
# Date:             12th of April 2016
# language:         English
# Title:            Quicksort Algorithm python implementation


# divide conquer combine
# First we choose a element to compare (pivot)
# Then we put all smaller elements in a list and
# all bigger elements in a list.
# The elements that equals the pivot goes to the pivotList.
# This method is recursive so we repeat it until they are no
# more smaller or greater elements then the pivot.
# At the end we return te more + pivotList + less.
def quicksort(aList):
    return __quickSort(aList)

def __quickSort(aList):
    less = []
    pivotList =[]
    more =  []
    if(len(aList)>0):
        pivot = aList[0]
        for i in aList:
            if i < pivot:
                less.append(i)
            elif i > pivot:
                more.append(i)
            else:
                pivotList.append(i)
        less = __quickSort(less)
        more = __quickSort(more)
    return more + pivotList + less

Mergesort in Python


# author:           Marcin Cherek
# python version:   2.7.11
# Date:             12th of April 2016
# language:         English
# Title:            Mergesort Algorithm python implementation


# divide conquer combine
# First we split te List and then we sort the parts.
# At the end we combine the parts together to one Solution
# We split until it is not more possible to split. Then we compare
# the splited parts.
# We return all sorted Parts combined in one List
def merge_sort(aList):
    middle = len(aList) /2
    leftPart = aList[:middle]
    rightPart = aList[middle:]
    if(middle >0):
        leftPart = merge_sort(leftPart)
        rightPart = merge_sort(rightPart)
    return list(__merge(leftPart,rightPart))


def __merge(leftPart,rightPart):
    sortedL = []
    left_index, right_index = 0,0
    while left_index < len(leftPart) and right_index <len(rightPart):
        if( leftPart[left_index] >= rightPart[right_index]):
            sortedL.append(leftPart[left_index])
            left_index +=1
        else:
            sortedL.append(rightPart[right_index])
            right_index +=1
    if leftPart:
        sortedL.extend(leftPart[left_index:]) #Extend adds a Lists content
    if rightPart:
        sortedL.extend(rightPart[right_index:])
    return sortedL

Heapsort in Python

# author:           Marcin Cherek
# python version:   2.7.11
# Date:             12th of April 2016
# language:         English
# Title:            Heapsort Algorithm python implementation

def heapsort(aList):
    return __heapsort(aList)

# PUBLIC: HeapSort
# We build a heap with the rules
# To build a heap with start a the index of the leastparent
# and iterate to the root Element index of 0. step -1
# In the Second step we flat the Heap to an Array. We start at the
# last nodes Index and and Iterate to zero with step -1.
# In each repeat we swap the root Element with the last Element and
# rebuild our Heap with the rules. This leads to an shrinking Heap.
# The last repeat is when we have a Heap with only 2 Nodes.
def __heapsort(aList):
    # List to Heap
    length = len(aList) - 1
    #We start at the index with the least parent because our down method need childnodes.
    for i in range(abs(length / 2), -1, -1):
        __down(aList, i, length)
    # Heap to List
    for i in range(length, 0, -1):
        #swaps the last element with the rootElement
        __swap(aList, 0, i)
        #restore Heap rules for our smaller getting heap
        __down(aList, 0, i - 1)
    return aList


# PRIVATE: Down
# First we calculate the Children. Left = 2n and Right = 2n+1
# Then we look if the children exists and if they are bigger than
# the parent we swap the elements.
def __down(aList, current, heapSize):
    leftC = 2 * current
    rightC = leftC + 1
    if (leftC <= heapSize and rightC > heapSize):
        #Just one Child on the left site
        if (aList[leftC] < aList[current]):
            __swap(aList, leftC, current)
    else:
        if (rightC <= heapSize):
            #There are two children and it's possible that they are also
            #Parent's
            child = 0
            if (aList[leftC] < aList[rightC]):
                child = leftC
            else:
                child = rightC
            if (aList[child] < aList[current]):
                __swap(aList, current, child)
                __down(aList, child, heapSize)

#PRIVATE: SWAP
# Swap means to exchange a node with his child
# idx_one and idx_two are the indexes
def __swap(aList, idx_one, idx_two):
    #logger.info(("SWAP:", aList[idx_one], aList[idx_two]))
    tmp = aList[idx_one]
    aList[idx_one] = aList[idx_two]
    aList[idx_two] = tmp


if __name__ == "__main__":
    print("RESULT:"+str(heapsort([2, 3, 21, 56, 53, 2, 1, 6, 2, 3, 2, 4, 5, 6, 5, 5, 3, 3, 4, 100])))