aboutsummaryrefslogtreecommitdiff
path: root/challenge-162/eric-cheung/python/ch-2.py
blob: d0029088028137a5b0e64ad1a22a7efaebfcef36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
nMaxElem = 25
nDiv = 5
arrKey = []

def getKeyTable(strWord):
    nCount = 0
    
    for nLoop in range(0, len(strWord)):
        strLetter = strWord[nLoop].upper()

        if strLetter == " ":
            continue
        
        if not strLetter in arrKey:
            arrKey.append(strLetter)
            nCount = nCount + 1

    nIndex = 65
    nLoop = nCount
    while nLoop < nMaxElem:
        strLetter = chr(nIndex)

        if strLetter == "I" or strLetter == "J":
            if not "I" in arrKey and not "J" in arrKey:
                arrKey.append(strLetter)
                nLoop = nLoop + 1
        elif not strLetter in arrKey:
            arrKey.append(strLetter)
            nLoop = nLoop + 1

        nIndex = nIndex + 1

def getRowColFromKey(strChar):

    nIndx = -1
    if strChar == "I" or strChar == "J":
        if "I" in arrKey:
            nIndx = arrKey.index("I")
        else:
            nIndx = arrKey.index("J")
    else:
        nIndx = arrKey.index(strChar)

    nRowIndx = int (nIndx / nDiv)
    nColIndx = nIndx % nDiv

    return [nRowIndx, nColIndx]


def getRefineNewMsg(strWord, bIsEncrypt):

    strNuWord = ""
    for nLoop in range(0, len(strWord)):
        strLetter = strWord[nLoop].upper()

        if strLetter == " ":
            continue

        if nLoop == 0:
            strNuWord = strLetter
        elif strLetter == strWord[nLoop - 1].upper():
            if bIsEncrypt:
                strNuWord = strNuWord + "X" + strLetter
            else:
                strNuWord = strNuWord + strLetter
        else:
            strNuWord = strNuWord + strLetter

    return strNuWord

def getEncryptDecryptPair(strLetterPair, bIsEncrypt):

    nRowNum_01, nColNum_01 = getRowColFromKey(strLetterPair[0])
    nRowNum_02, nColNum_02 = getRowColFromKey(strLetterPair[1])

    if nRowNum_01 == nRowNum_02 and nColNum_01 == nColNum_02:

        return strLetterPair

    if nRowNum_01 != nRowNum_02 and nColNum_01 != nColNum_02:

        nMinCol = min(nColNum_01, nColNum_02)
        nMaxCol = max(nColNum_01, nColNum_02)
        
        nRowNum_Nu_01 = nRowNum_01
        nRowNum_Nu_02 = nRowNum_02

        nColNum_Nu_01 = nMaxCol - nColNum_01 + nMinCol
        nColNum_Nu_02 = nMaxCol - nColNum_02 + nMinCol
        
    elif nRowNum_01 == nRowNum_02:
        
        nColDiff = 1
        
        nRowNum_Nu_01 = nRowNum_01
        nRowNum_Nu_02 = nRowNum_02
        
        if bIsEncrypt:
            nColNum_Nu_01 = nColNum_01 + nColDiff if nColNum_01 + nColDiff < nDiv else nColNum_01 + nColDiff - nDiv
            nColNum_Nu_02 = nColNum_02 + nColDiff if nColNum_02 + nColDiff < nDiv else nColNum_02 + nColDiff - nDiv
        else:
            nColNum_Nu_01 = nColNum_01 - nColDiff if nColNum_01 - nColDiff > -1 else nColNum_01 - nColDiff + nDiv
            nColNum_Nu_02 = nColNum_02 - nColDiff if nColNum_02 - nColDiff > -1 else nColNum_02 - nColDiff + nDiv
    else:

        nRowDiff = 1

        if bIsEncrypt:
            nRowNum_Nu_01 = nRowNum_01 + nRowDiff if nRowNum_01 + nRowDiff < nDiv else nRowNum_01 + nRowDiff - nDiv
            nRowNum_Nu_02 = nRowNum_02 + nRowDiff if nRowNum_02 + nRowDiff < nDiv else nRowNum_02 + nRowDiff - nDiv
        else:
            nRowNum_Nu_01 = nRowNum_01 - nRowDiff if nRowNum_01 - nRowDiff > -1 else nRowNum_01 - nRowDiff + nDiv
            nRowNum_Nu_02 = nRowNum_02 - nRowDiff if nRowNum_02 - nRowDiff > -1 else nRowNum_02 - nRowDiff + nDiv

        nColNum_Nu_01 = nColNum_01
        nColNum_Nu_02 = nColNum_02

    return arrKey[nRowNum_Nu_01 * nDiv + nColNum_Nu_01] + arrKey[nRowNum_Nu_02 * nDiv + nColNum_Nu_02]


def getEncryptDecrytMsg(strWord, bIsEncrypt):

    strEncryWord = ""
    for nLoop in range(0, len(strWord)):
        if nLoop % 2 == 1:
            continue

        ## print (strWord[nLoop:nLoop + 2] + "-" + getEncryptDecryptPair(strWord[nLoop:nLoop + 2], bIsEncrypt))
        strEncryWord = strEncryWord + getEncryptDecryptPair(strWord[nLoop:nLoop + 2], bIsEncrypt)

    return strEncryWord.lower()


def EncryptDecryptPart(strKeyWord, strEncryptWord, bIsEncrypt):

    getKeyTable(strKeyWord)

    return getEncryptDecrytMsg(getRefineNewMsg(strEncryptWord, bIsEncrypt), bIsEncrypt)

## print (EncryptDecryptPart("playfair example", "hide the gold in the tree stump", True))
print (EncryptDecryptPart("perl and raku", "siderwrdulfipaarkcrw", False))