Program python untuk mengurutkan kata tanpa fungsi sort(sorting word without sort function)

Berikut program python untuk mengurutkan kata tanpa fungsi pada python versi 2 dan versi 3

coding di versi 2
n=int(raw_input())
arr=[[]]*n
def inputKata(arr,n):
    for i in range(n):
        kata=raw_input()
        arr[i]=[0]*len(kata)
        for j in range(len(arr[i])):
            arr[i][j]=kata[j]
    return arr

def terPanjang(arr):
    panjang=len(arr[0])
    for i in range(1,len(arr)):
        if(panjang<len(arr[i])):
           panjang=len(arr[i])
    return panjang

def samaKan(arr,n):
    for i in range(len(arr)):
        bN=n-len(arr[i])+1
        arr[i]+=[str(len(arr[i]))]*bN

def bubbleSort(arr,x):   
    for i in range(len(arr)-1):
        for j in range(i+1, len(arr)):
            if(ord(arr[j][x]) < ord(arr[i][x])):
                temp = arr[i]
                arr[i] = arr[j]
                arr[j] = temp
    return arr

def tampil(arr):
    for i in range(len(arr[0])-1,-1,-1):
        bubbleSort(arr,i);
    for i in range(len(arr)):
        n=len(arr[i])
        for j in range(int(arr[i][n-1])):
            print arr[i][j],
        print
inputKata(arr,n)
samaKan(arr,terPanjang(arr))
tampil(arr)

coding di versi 3
n=int(input())
arr=[[]]*n
def inputKata(arr,n):
    for i in range(n):
        kata=raw_input()
        arr[i]=[0]*len(kata)
        for j in range(len(arr[i])):
            arr[i][j]=kata[j]
    return arr

def terPanjang(arr):
    panjang=len(arr[0])
    for i in range(1,len(arr)):
        if(panjang<len(arr[i])):
           panjang=len(arr[i])
    return panjang

def samaKan(arr,n):
    for i in range(len(arr)):
        bN=n-len(arr[i])+1
        arr[i]+=[str(len(arr[i]))]*bN

def bubbleSort(arr,x):   
    for i in range(len(arr)-1):
        for j in range(i+1, len(arr)):
            if(ord(arr[j][x]) < ord(arr[i][x])):
                temp = arr[i]
                arr[i] = arr[j]
                arr[j] = temp
    return arr

def tampil(arr):
    for i in range(len(arr[0])-1,-1,-1):
        bubbleSort(arr,i);
    for i in range(len(arr)):
        n=len(arr[i])
        for j in range(int(arr[i][n-1])):
            print(arr[i][j],end="")
        print()
inputKata(arr,n)
samaKan(arr,terPanjang(arr))
tampil(arr)

bona

Tidak ada komentar:

Posting Komentar

Instagram