Recap - Product List
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 72 of 93.
Challenge
MediumBuild a product inventory system by performing the following steps:
- Create an empty mutable list called
products - Read an integer
nfrom input representing how many products to add - Read
nproduct names from input and add each one to the list - 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:
- The total number of products in the list
- The first product in the list
- The last product in the list
- The index of the searched product (or
-1if not found)
Input format:
- First line: an integer
n - Next
nlines: 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
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic Input12Lists Basics
List vs MutableListAccessing ElementsModifying ListsList MethodsPair And TripleRecap - Product ListRecap - Reversed ListPractice on your own: Kotlin playground