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 Functions

This lesson continues our exploration of functions. We’ll present a bit more Kotlin syntax, and spend time reinforcing what we’ve learned about functions. Let’s get started!

for in Loop
for in Loop

To get us warmed up and ready to go, let’s check out a new bit of Kotlin syntax! Remember how we started with this common while loop:

var values = intArrayOf(1, 9, 9)
var i = 0
while (i < values.size) {
// Do something with each value, like print it
println(i)
i++
}

and eventually arrived at this common for loop:

var values = intArrayOf(1, 9, 9)
for (i in values.indices) {
// Do something with each value, like print it
println(i)
}

Well, that for loop became so common that there’s an even simpler way to work through the values in array using Kotlin’s for in loop:

var values = intArrayOf(1, 9, 9)
for (i in values.indices) {
// Do something with each value
}

Indexed v. Non-Indexed for Loop
Indexed v. Non-Indexed for Loop

When you should use the indexed for (for (i in 0 until value)) and when for in? (Technically they are both for in loops, but the difference is whether the variable hold the index or a value from the array.) Here are some things to consider:

For many common array-processing tasks that we’ve encountered, the for in loop is a much better fit, since avoiding the extra index variable leads to a cleaner loop declaration and value access within the loop. For example, counting:

var values = intArrayOf(1, 2, 4)
// Indexed for
var count = 0
for (i in values.indices) { // Extra variable i
if (values[i] > 1) { // must use bracket notation
count++
}
}
println(count)
// for in
count = 0
for (value in values) { // No i needed for this problem
if (value > 1) { // No bracket notation!
count++
}
}
println(count)

Practice: Array All Pairs

Created By: Geoffrey Challen
/ Version: 2022.1.0

Write a method arrayAllPairs that returns whether a passed IntArray is composed entirely of adjacent pairs of the same value. For example, the array {1, 1, 2, 2} and the array {4, 4, -1, -1} are composed of adjacent pairs of the same element, but {2, 1, 1, 2} and {4, 4, -1, 0} are not. To be composed entirely of pairs of the same element, the array must contain an even number of elements. If the passed array is empty, you should return false.

You will need to construct your loop carefully to complete this problem! We suggest that you examine the array looking for a counterexample: meaning a pair of adjacent elements that do not have the same value.

Practice with Functions
Practice with Functions

Next let’s get some more practice with functions! Together we’ll write a method that determines when an IntArray is a palindrome array: meaning that the values it contains are the same forward and backward. For example, {1, 2, 4} is not an array palindrome, but {1, 0, 2, 0, 1} is!

Let’s go step by step and see how to approach constructing this method. First, let’s determine our method signature, and practice calling it on some sample inputs.

Next, let’s begin work on the body of the method. Our array access pattern here is a bit different than what we’ve seen previously, so let’s proceed carefully.

As a next step, let’s complete the job by adding the decision-making logic we need to determine if the passed array is an array palindrome. This should remind us a bit of the search pattern that we covered yesterday.

Finally, let’s make one small improvement to our code.

Practice: Array Range Sum

Created By: Geoffrey Challen
/ Version: 2022.1.0

Write a method named arrayRangeSum. It receives an IntArray and a positive Int range as parameters. You should return the sum of all the elements in the array that are between range and range * -1, non-inclusive.

For example, given the array {1, -4, 2, 24, -124} and the range 8, you would return -1: 1 + -4 + 2, since these are the values in the array strictly greater than -8 and strictly less than 8. Given the range 124, you should return 23: 1 + -4 + 2 + 24, since these are the values in the array strictly greater than -124 and less than 124.

Note that this problem is a great fit for the for in loop!

Homework: Array Min Max Sum

Created By: Geoffrey Challen
/ Version: 2022.8.0

Write a method named arrayMinMaxSum. It receives an IntArray and two Int values, min and max, in that order, as parameters. You should return the sum of all the elements in the array that are between the min the max, inclusive.

For example, given the array {1, -4, 2, 24, -124} and the min and max 1 and 24, you would return 27: 1 + 2 + 24, since these are the values in the array greater than or equal to 1 and less than or equal to 24. Note that the minimum may be greater than the maximum, in which case you should return 0. (But you don't need code to handle this specific case.)

Note that this problem is a great fit for the for in loop!

CS People: Ruchi Sanghvi
CS People: Ruchi Sanghvi

Early Facebook has a well-deserved reputation for being a male-dominated testosterone-driven workplace. So it may surprise you to find out that one of the software developers who was a core contributor to the first version of the News Feed—way back in 2006—was Ruchi Sanghvi.

She was also involved in Facebook’s response to the initial feedback on the News Feed. Users hated it! But Facebook kept it for one simple reason: Users were spending more time on the site. You should always keep this in mind when using Facebook, YouTube, TikTok, or any free site run by ad revenue. Their primary and sometimes only goal is for you to spend as much time as possible on their site. Regardless of whether that’s healthy or appropriate or useful to you.

Ruchi Sanghvi worked at several companies after leaving Facebook. In this video, she discusses some of what she learned along the way.

More Practice

Need more practice? Head over to the practice page.