Function encryption
Write a function encrypts plan test according to the code is given
Problem
Write a function encrypt (plaintext, code_table) that encrypts a given plaintext message using the given code table. For each letter in the plai ntext, we find the corresponding set of symbols.
If this is the first appearance of the letter in the message, then we assign the first symbol in the set, otherwise, we assign the next symbol after the most recently used symbol for this letter.
When there is more than one symbol available for a letter, then the first appearance of the letter should be encoded using the first symbol, the second appearance by the second symbol, and so on. Once all the symbols for that letter have been used, you should begin again from the first symbol.
• plaintext string of lowercase alphabetic letters.
• code_table list of list of integers, in which each list contains the symbols corresponding to letter at that position in the alphabet.
Returns a list of integers corresponding to the ciphertext of the given plaintext.
Assumptions:
• You can assume that plaintext contains only lowercase alphabetic letters.
• You can assume that code_table has been generated according to the definition of gen_code_tabte from Question 1.
CODE_TABLE = [[90,91,92,93,94,95,96,97], [98,99], [0,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]]
>>> print(encrypt('computer', CODE_TABLE))
[0, 48, 39, 56, 80, 71, 7, 59]
>>> print(encrypt('science', CODE_TABLE))
[65, 0, 26, 7, 41, 1, 8]
>>> print(encrypt('cabbagepatch', CODE_TABLE))
[0, 90, 98, 99, 91, 18, 7, 56, 92, 71, 1, 20]
>>> print(encrypt('bubble', CODE_TABLE))
[98, 80, 99, 98, 35, 7]
>>> print(encrypt('wowwhatawombat', CODE_TABLE))
[84, 48, 85, 84, 20, 90, 71, 91, 85, 49, 39, 98, 92, 72]
Source code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace encryptMe
{
class Program
{
static void Main(string[] args)
{
int[][] CODE_TABLE = new int[][] {
new int[] { 90,91,92,93,94,95,96,97 }, //A
new int[] { 98,99 },
new int[] { 0,1,2 },
new int[] { 3,4,5,6}, //D
new int[] { 7,8,9,10,11,12,13,14,15},
new int[] { 16,17},
new int[] { 18,19},
new int[] { 20,21,22,23,24,25},
new int[] { 26,27,28,29,30,31,32},
new int[] { 33},
new int[] { 34},
new int[] { 35,36,37,38},
new int[] { 39,40},
new int[] { 41,42,43,44,45,46,47},
new int[] { 48,49,50,51,52,53,54,55},
new int[] { 56,57},
new int[] { 58},
new int[] { 59,60,61,62,63,64},
new int[] { 65,66,67,68,69,70},
new int[] { 71,72,73,74,75,76,77,78,79},
new int[] { 80,81,82},
new int[] { 83},
new int[] { 84,85},
new int[] { 86},
new int[] { 87,88},
new int[] { 89},
};
string s = print("DeepDeep",CODE_TABLE);
Console.WriteLine(s);
Console.ReadKey();
}
private static string print(string p, int[][] baseArray)
{
char[] alpha = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
string vReturn = string.Empty;
int indexAlpha = 0;
for (int i = 0; i < p.Length; i++ )
{
indexAlpha = IndexOfInt(alpha, p[i]);
Debug.WriteLine("one character ... " + p[i] + " Index at " + indexAlpha.ToString());
string intrimValue = baseArray[indexAlpha][Occurance(i, p) % (baseArray[indexAlpha].Length)].ToString();
//string intrimValue = baseArray[indexAlpha][0].ToString();
Console.WriteLine(intrimValue);
vReturn += intrimValue + ",";
}
return vReturn;
}
static int Occurance(int index, string s)
{
int retunnValue = 0;
for (int i = 0; i <= index; i++)
{
if (s[index] == s[i])
retunnValue++;
}
return retunnValue-1;
}
static int IndexOfInt(char[] arr, char value)
{
for (int i = 0; i < arr.Length; i++)
{
if (char.ToUpper(value) == char.ToUpper(arr[i]))
return i;
}
return -1;
}
}
}
Comments
Post a Comment