Reiterating on Caesar and Iteration
Completed
Encryption vs Encoding
Cryptography in History
Revisit Info Theory Concept
About Control Flow
Today
Revisit Iteration
Revisit Caesar Cipher
Upcoming
Portfolio Discussion
Quiz
Previously, we already talked about Control Flow and the three main types ( there are others, but these are the main concepts). If you have forgotten them, rewatch this short video on them.
Iteration isn't just a process for code, it's also a process in how we design things. After you define your problem, you can immediately begin the process of learning through iteration and adaptation. In this video, Professor Matt Andrews from Harvard, demonstrates the process of how you learn through iteration to ultimately solve your problem.
Iteration & Algorithm Design
When we looked at Morse Code, we used a little bit of iteration. We also used it when creating the Birthday Card mini-project; in case a user entered invalid input.
Let's look at how we can use iteration (loops) related to the Caesar Cipher program we had made a while back (using the string.replace method).
First, let's talk about it, then we'll look at how we'd code it. In doing so, we'll be able to add in a user-defined key as well.
Iteration & the Caesar Cipher - Version 2
Here is one way we could code a Caesar Cipher using iteration. This is not a great way, but it is one way to do it.
Notice, on line 5, we are asking the user for a value that we will use as a key. This is stored in a variable we've named key.
On line 10, I've created a variable called count. Count will be used to keep track of how many times the loop runs, by increasing its value by 1 each time. Once the value of count is no longer less than the value of key, then we know the loop has run the number of times we want.
If this doesn't make any sense to you, then you may need to watch me walkthrough the design of it.
Iteration & Mirror Writing
We can also use a loop to create a simple transpositional cipher. Do you remember what a transpositional cipher is? Substitution swaps the letters with other values or symbols, whereas the transpositional cipher rearranges the message to obscure the meaning.
Can you figure out how to combine the Mirror Cipher with the Caesar Cipher?
When you input clear text into the program, it should return text that has both been shifted by a value (Caesar), and then also written backward (Mirror Writing).