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

Algorithms and Strings

fun bestCourse(courses: Array<String>): String {
for (course in courses) {
if (course.contains("124")) {
return course
}
}
return ""
}
println(bestCourse(arrayOf("CS 124", "ECE 110")))
println(bestCourse(arrayOf("STAT 107", "CS 105")))

This lesson is a lot of fun. We’ll integrate what we’ve learned recently about Strings, algorithms, and then functions. And then we’ll get some practice approaching a few new problems using Strings.

Debugging Challenge
Debugging Challenge

But let’s start with some debugging practice!

Odds and Ends
Odds and Ends

But first, we have a few new things to cover.

Type Conversions
Type Conversions

When we started working with variables and Kotlin’s eight basic types, we observed that there were certain types of assignments that would fail. For example:

var i = 10 // Inferred as Int
i = 10.8 // Can't assign a Double literal to an Int variable

Unlike some other languages, Kotlin will not automatically convert a value even if you might think it could be done safely:

var i = 10.5 // Inferred as Double
i = 20 // Can't assign an Int literal to a Double variable

So let’s look at how we can force Kotlin to perform certain type changes when necessary.

var d = 10.8
var i = 10

Strings Are Immutable
Strings Are Immutable

One of the important things to understand about Kotlin’s Strings is that none of the methods that we can call on them change that String. Instead, they always return a new String that may be modified in some way. Let’s see that in action:

var first = "I'm a string!"

Practice: String Reverse

Created By: Geoffrey Challen
/ Version: 2020.9.0

Let's get some more practice working with Strings: an incredibly useful data type for working with text.

Write a function called reverse. It should accept a single String argument and return that String, reversed! There are several ways to approach this problem—have fun! And maybe take a good look at the documentation before you write too much code...

Array<> and arrayOf
Array<> and arrayOf

We’ll be working with arrays of Strings in this lesson. Given that previously, when we used an array of Ints we declared it as IntArray and initialized it using intArrayOf, and when we used an array of Doubles we declared it as DoubleArray and initialized it using doubleArrayOf, you might assume that an array of Strings would be a StringArray and intialized using stringArrayOf:

var array: StringArray = stringArrayOf("Test", "Me")

But not quite! Instead, the actual syntax is a bit different:

var array: Array<String> = arrayOf("Test", "Me")

On the left we see the type Array<String>, which indicates that this is an array that contains Strings. The type of the contents of the array is inside the angle brackets: <String>. On the right we see the generic array initializer for Kotlin, which you can use to create arrays of any type.

As usual, we don’t need to specify the type explicitly on the left:

var array = arrayOf("Test", "Me")

How does Kotlin know what type of array it is? Based on the arguments to arrayOf! Since they are all Strings, the type is inferred as Array<String>.

For now we’ll work with these arrays without worrying too much about what’s going on under the hood. Just be careful with arrayOf, since if you mix the types of the arguments things won’t work out as expected:

// This is an array of Strings, since all arguments to arrayOf are String literals
var strings = arrayOf("Test", "Me")
println(strings[0].length)
// This is not an array of Strings...
var problems = arrayOf("Test", 4)
println(problems[0].length)

String Algorithms
String Algorithms

Now let’s have some fun and write a few new algorithms that work on Strings!

String Character Search
String Character Search

First, let’s try and write a function that determines if a String contains a particular character. We’ll sketch our our algorithm first, explore some potentially useful String methods, and construct and test a solution.

// Write a function that searches a `String` for a character

Structured String Parsing
Structured String Parsing

Frequently when computers work with text data, we are processing data that was itself created by a machine. Or set up for machines to easily process.

One example is data stored in comma-separated-value format. Let’s say we wanted to track how many people were tested for some random respiratory illness each day. We might save that data in a String that looked like this:

2020-09-02,5402
2020-09-03,12042
2020-09-04,4637
2020-09-05,89054
2020-09-06,2033
2020-09-07,10238
2020-09-08,76452
2020-09-09,8902

(Note that this is not real data!)

Let’s experiment with how we might work with this kind of data. We’ll use some existing String methods and a few new ones. We’ll also use another unfamiliar method: String.toInt, which allows us to convert a String to an Int.

var data = """2020-09-02,5402
2020-09-03,12042
2020-09-04,4637
2020-09-05,89054
2020-09-06,2033
2020-09-07,10238
2020-09-08,76452
2020-09-09,8902"""
println(data)

Practice: String Flip Halves

Created By: Geoffrey Challen
/ Version: 2021.8.0

Write a method name flipHalves that returns a passed String rearranged as follows. You should move the second half of the string to the front, and the first half to the back. So given the String "CS" you would return "SC", and given the String "testme" you would return "tmetes". However, if the length of the String is odd the middle character should remain in place. So given the String "CS124" you would return "241CS". If the passed String is empty, just return the empty String.

You will definitely want to review the substring String method, and also spend some time experimenting with it before you begin crafting your solution. You also may want to consider odd and even length Strings separately.

Homework: Hawaiian Ws

Created By: Geoffrey Challen
/ Version: 2022.7.0

Words from languages that we are unfamiliar with can be difficult to pronounce correctly. Phonetic pronunciation guides can help make them more accessible to us. In this problem we'll complete one part of a pronunciation guide for words in the Hawaiian language.

Consonants in the Hawaiian language are pronounced similarly to the English versions. The only exception is 'w', which is pronounced as a "v" if it follows an 'i' or 'e' and pronounced as "w" otherwise.

Complete a method named rewriteW which, given a String, rewrites all the "w"s according to the rule above. For example, given the String "wahine" you would return "wahine", since the "w" is not preceded by an 'i' or 'e'. Given the String "iwa" you would return "iva", since the "w" is preceded by an 'i'. You should treat upper and lowercase characters equivalently, and your return String should be all lowercase.

CS People: Joy Buolamwini
CS People: Joy Buolamwini

Joy Buolamwini is a self-described poet of code who, through her art, voice, and research, is drawing attention to important social implications of the widespread use of artificial intelligence. Watch her speak for herself, in words both beautiful and powerful:

If you have a few more minutes, watch her TED Talk on algorithmic bias—what she calls “the coded gaze”.

More Practice

Need more practice? Head over to the practice page.