KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • map-reduce-filter : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Companion Objects : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Immutability : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Practice with Strings

Welcome back! Our next lesson is entirely focused on one problem: encryption.

We’re going to modify the normal lesson flow. We’ll start with the homework problem at the top. If you’d like to just go at on your own, go for it! And, if you’d like a bit of help, we’ll break it down piece-by-piece below.

Let’s get to it!

Homework: Rot 13 Encryption

Created By: Geoffrey Challen
/ Version: 2020.9.0

Encryption is an ancient practice of trying to conceal information by scrambling it. Modern encryption techniques are incredibly strong and mathematically sound. But in the past, simpler and more primitive methods were used.

Let's implement a form of encryption known as a Caesar Cipher, sometimes also known as Rot-13 encryption. (Rot for rotation, and 13 for one amount that you might rotate.) Here is how it works. Given a String and an amount to rotate, we replace each character in the String with a new character determined by rotating the original character in a given array. For example, given the String array "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", "ABC" rotated 3 would be "DEF", and rotated -1 would be " AB". (Note the space at the end of the character array.)

Declare and implement a function called encrypt that, given a String and an Int amount, returns the passed String "encrypted" by rotating it the given amount. ("Encrypted" is in scare quotes because this is not by any means a strong method of encryption!) If the passed String is null you should return null. Note that rotation may be negative, which will require some additional care.

fun encrypt(
input: String?,
rotation: Int,
): String? {
val characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
return input
}

Rot-13 Part 0: Understanding the Problem
Rot-13 Part 0: Understanding the Problem

Let’s break down this problem into smaller pieces, and spend a few moments just orienting ourselves and figuring out what to do. We won’t write test cases yet, and instead save them for the smaller pieces that we’re about to create.

// Breaking Down Rot-13

Rot-13 Part 1: Character Mapping
Rot-13 Part 1: Character Mapping

Now that we have a sense of what the different pieces are, let’s look at one of the core challenges: remapping each character. We’ll also write some simple test cases for our helper method.

// Remapping Each Character

Rot-13 Part 2: Breaking Down the String
Rot-13 Part 2: Breaking Down the String

At this point we’ve identified how to remap individual characters. Next we need to review how to break the input String into individual characters.

// Breaking a String Into Characters

Rot-13 Part 3: Putting it All Together
Rot-13 Part 3: Putting it All Together

Now that we have our building blocks, let’s integrate everything together!

// Putting it All Together

Homework: Rot 13 Encryption

Created By: Geoffrey Challen
/ Version: 2020.9.0

Encryption is an ancient practice of trying to conceal information by scrambling it. Modern encryption techniques are incredibly strong and mathematically sound. But in the past, simpler and more primitive methods were used.

Let's implement a form of encryption known as a Caesar Cipher, sometimes also known as Rot-13 encryption. (Rot for rotation, and 13 for one amount that you might rotate.) Here is how it works. Given a String and an amount to rotate, we replace each character in the String with a new character determined by rotating the original character in a given array. For example, given the String array "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", "ABC" rotated 3 would be "DEF", and rotated -1 would be " AB". (Note the space at the end of the character array.)

Declare and implement a function called encrypt that, given a String and an Int amount, returns the passed String "encrypted" by rotating it the given amount. ("Encrypted" is in scare quotes because this is not by any means a strong method of encryption!) If the passed String is null you should return null. Note that rotation may be negative, which will require some additional care.

fun encrypt(
input: String?,
rotation: Int,
): String? {
val characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
return input
}

Rotate Right
Rotate Right

If you are enjoying Strings, rotation, and modular arithmetic, and haven’t had enough yet—here is a practice problem that you might enjoy!

Created By: Geoffrey Challen
/ Version: 2020.9.1

This problem combines Strings, functions, and arrays. Super fun!

Write a function called rotateRight that takes a nullable String as its first argument and a non-negative Int as its second argument and rotates the String by the given number of characters. Here's what we mean by rotate:

  • CS125 rotated right by 1 becomes 5CS12
  • CS125 rotated right by 2 becomes 25CS1
  • CS125 rotated right by 3 becomes 125CS
  • CS125 rotated right by 4 becomes S125C
  • CS125 rotated right by 5 becomes CS125
  • CS125 rotated right by 8 becomes 125CS

And so on. Notice how characters rotated off the right end of the String wrap around to the left.

This problem is tricky! Here are a few hints:

  1. You will want to use the remainder operator to perform modular arithmetic.
  2. You will probably want to convert the String to an array of characters before you begin.
  3. You can convert an array of characters characters back into a String like this: String(characters).
  4. You can also solve this problem quite elegantly using substring.

If the passed String argument is null, you should return null. Good luck and have fun!

CS People: Shafi Goldwasser
CS People: Shafi Goldwasser

Remarkably few women have won the Turing Award, the highest award given for contributions to computer science. (Often considered the Nobel Prize of computing.) Shafi Goldwasser is one of them.

She received the award in 2012 “for transformative work that laid the complexity-theoretic foundations for the science of cryptography and in the process pioneered new methods for efficient verification of mathematical proofs in complexity theory.” Her work underlies the foundations of our modern data society, including algorithms that you use every day when you chat, browse, shop, and engage online. Here’s a short (if somewhat poorly done) official video describing her contributions:

More Practice

Need more practice? Head over to the practice page.