Created
October 6, 2021 20:50
-
-
Save leontabak/d63bb963649bde6d86dc586fb1575358 to your computer and use it in GitHub Desktop.
Suggestions that might help with the Playfair Cipher project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eonsahead | |
import com.sun.tools.javac.util.Pair | |
import scala.collection.mutable.Map | |
object AlphabetTable extends App { | |
// This program contains some functions that | |
// you might find useful in your effort to | |
// create a program that encrypts messages | |
// with the Playfair Cipher. | |
// Note: This code refers to a 5 x 5 array | |
// that contains all letters of the alphabet | |
// except J. However, the code does not actually | |
// create a 5 x 5 array. Instead, it creates... | |
// * a 1-dimensional array that contains the 25 | |
// letters | |
// * a map that contains 25 key/value pairs | |
// * functions that allow a translation between | |
// addresses in these data structures and | |
// addresses in a virtual 5 x 5 array | |
// Create an array that contains all uppercase | |
// letters except 'J'. | |
val letters = for (letter <- 'A' to 'Z' if letter != 'J') | |
yield letter | |
// Alternatively, filter an array that contains | |
// all 26 letters. | |
//val alphabet = letters.filter((letter) => letter != 'J') | |
println(letters) | |
// Can you find a way to shuffle letters? | |
// Given the index of an element of the | |
// 1-dimensional letters array, find the row | |
// and column in a virtual 5 x 5 2-dimensional table. | |
val indexToCoordinates = | |
(index: Int) => new Pair(index / 5, index % 5) | |
// Given the row and column of an element | |
// of a virtual 5 x 5 2-dimensional table, find its | |
// index. | |
// The index is the number we get if we start | |
// at the upper left corner of the table, move | |
// left to right and top to bottom, counting as | |
// we go, until we land on the element at the | |
// specified row and column. | |
val coordinatesToIndex = | |
(coordinates:Pair[Int,Int]) => | |
coordinates.fst * 5 + coordinates.snd | |
// Make a map that will allow easy retrieval | |
// of a letter's coordinates in a virtual 5 x 5 table. | |
val alphabet = Map[Char, Pair[Int,Int]]() | |
for( n <- 0 until letters.size) | |
alphabet(letters(n)) = indexToCoordinates(n) | |
// Define a function that finds the letter | |
// at a given row and column in the virtual 5 x 5 table. | |
val getLetter = (coordinates:Pair[Int,Int]) => | |
letters(coordinatesToIndex(coordinates)) | |
// Define a function that, given a letter, | |
// finds the letter's row and column in the | |
// virtual 5 x 5 table. | |
val getCoordinates = (letter:Char) => alphabet(letter) | |
for( key <- alphabet.keys) { | |
val coordinates = alphabet(key) | |
val row = coordinates.fst | |
val col = coordinates.snd | |
println( f"$key is at ($row, $col)" ) | |
} // for | |
} // end of AlphabetTable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment