Menu
Coddy logo textTech

Recap - Product List

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 72 of 93.

challenge icon

Challenge

Medium

Build a product inventory system by performing the following steps:

  1. Create an empty mutable list called products
  2. Read an integer n from input representing how many products to add
  3. Read n product names from input and add each one to the list
  4. Read one more string from input representing a product to search for

After building the list, print the following information on separate lines in this exact order:

  1. The total number of products in the list
  2. The first product in the list
  3. The last product in the list
  4. The index of the searched product (or -1 if not found)

Input format:

  • First line: an integer n
  • Next n lines: product names (strings)
  • Last line: the product name to search for

Input constraints: The number of products is at least 1; product names contain no newline.

Try it yourself

fun main() {
    // TODO: Create an empty mutable list called 'products'
    
    // Read the number of products to add
    val n = readLine()!!.toInt()
    
    // TODO: Read n product names and add each one to the list
    
    // Read the product name to search for
    val searchProduct = readLine()!!
    
    // TODO: Print the following on separate lines:
    // 1. The total number of products in the list
    // 2. The first product in the list
    // 3. The last product in the list
    // 4. The index of the searched product (or -1 if not found)
}

All lessons in Fundamentals

Practice on your own: Kotlin playground