|
| 1 | +alphabets = ['A','B','C','D','E','F','G','H', |
| 2 | + 'I','J','K','L','M','N','O','P', |
| 3 | + 'Q','R','S','T','U','V','W','X','Y','Z'] # series of alphabets in order |
| 4 | + |
| 5 | +def find_index(letter): |
| 6 | + '''Returns the index of the alphabet in the series of alphabet''' |
| 7 | + return alphabets.index(letter) |
| 8 | + |
| 9 | +def code_cyclic(index, letter): |
| 10 | + '''Cyclic transform of letters based on the index of the letter in the word and |
| 11 | + that in the series of alphabets in order |
| 12 | + Arguments (Parameters, I guess): |
| 13 | + _____________ |
| 14 | + index - index of the letter in the word (type : int) |
| 15 | + letter - the letter to b coded (type : str)''' |
| 16 | + # The formula used here is : 26 * (index of the letter in word - 1) + (index of the |
| 17 | + # letter in the alphabets) |
| 18 | + # I had to use a separate variable 'index' to avoid the confusion the program gets when |
| 19 | + # choosing the index of the letter (eg, in the case of "HELLO", there are 2 'l' 's and this will cause confusion |
| 20 | + # This problem has been solved in the upcoming function. |
| 21 | + return 26 * index + (find_index(letter) + 1) |
| 22 | + |
| 23 | +def code_word(word): |
| 24 | + '''codes each word with an array of numbers''' |
| 25 | + number_code = [] |
| 26 | + i = 0 |
| 27 | + for letter in word: |
| 28 | + number_code.append(code_cyclic(i, letter)) |
| 29 | + i += 1 |
| 30 | + return number_code |
| 31 | + |
| 32 | +def code_sentence(sentence): |
| 33 | + '''codes the given sentence into an array of numbers''' |
| 34 | + sentence_number_coded = [] |
| 35 | + for word in sentence.split(): |
| 36 | + sentence_number_coded.append(code_word(word)) |
| 37 | + return sentence_number_coded |
| 38 | + |
| 39 | +def code_sentence_to_numeric_string(sentence): |
| 40 | + '''codes the given sentence into an array of numbers, each array transfored into a |
| 41 | + numeric string''' |
| 42 | + sentence_number_coded = [] |
| 43 | + for word in sentence.split(): |
| 44 | + string = "" |
| 45 | + for number in code_word(word): |
| 46 | + string += str(number) + "-" |
| 47 | + sentence_number_coded.append(string) |
| 48 | + sentence_number_coded.append("<space>") |
| 49 | + converted_string = str().join(sentence_number_coded) |
| 50 | + # hope the <space> won't taunt you or disturb you... :) |
| 51 | + return converted_string |
0 commit comments