From 766c34a9258624d65f050210b0a9a47755c8990e291a59560fb214a1ac63ad09 Mon Sep 17 00:00:00 2001 From: snap Date: Wed, 18 Feb 2026 00:46:51 +0000 Subject: [PATCH] Complete most of the content up to the Higher Order Functions section --- Exercises/Practical 1/Practicle 1.kt | 180 +++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 Exercises/Practical 1/Practicle 1.kt diff --git a/Exercises/Practical 1/Practicle 1.kt b/Exercises/Practical 1/Practicle 1.kt new file mode 100644 index 0000000..9532d5a --- /dev/null +++ b/Exercises/Practical 1/Practicle 1.kt @@ -0,0 +1,180 @@ +import com.sun.org.apache.xpath.internal.operations.Bool + +/** + * You can edit, run, and share this code. + * play.kotlinlang.org + */ +fun String.isPalindrome(): Boolean { + var treatedString: String = String() + this.forEach { char -> + if (char.isLetterOrDigit()) treatedString += char.uppercase() + } + + return treatedString == treatedString.reversed() +} + +fun calculateVolumeOfSphere(radius: Double): Double { + return (4.0 / 3.0) * Math.PI * (radius * radius * radius) +} + +fun ageMessage(age: Int) { + when (age) { + in Int.MIN_VALUE..<0 -> println("Unborn") + in 0..11 -> println("Young Kid") + in 12..17 -> println("Teenager") + 18 -> println("18 Year Old") + in 19..21 -> println("Young Adult") + in 22..64 -> println("Adult") + in 65..Int.MAX_VALUE -> println("Elderly") + } +} + +fun powerCalculator(base: Int, exponent: Int): Float { + var result: Float = base.toFloat() + + if (base == 0) return 0f + else if (exponent == 0) return 1f + + if (exponent < 0) { + for (loop in exponent..<-1) { + result *= base + } + result = 1 / result + } else { + for (loop in 1..): Array> { + val newList1: MutableList = mutableListOf() + val newList2: MutableList = mutableListOf() + + for (loop in 0.. myInt2) println("Either the first two integers or the last two integers are equal, and the 3rd integer is larger that the 2nd.") + + // 6 + //val myPalindrome: String = "Satan, oscillate my metallic sonatas." + //println(myPalindrome.isPalindrome()) + + // 7 + //println(calculateVolumeOfSphere(2.0)) + + // 8 + //val myIntArray: Array = arrayOf(1, 2, 3, 4, 5) + //val myStringArray: Array = arrayOf("One", "Two", "Three", "Four", "Five") + //myIntArray.forEach { println(it) } + //myStringArray.forEach { println(it) } + + // 9 + //val myString: String = "Hello" + //val loopAmount: Int = 5 + //var currentLoop: Int = 0 + //while (currentLoop < loopAmount) { + // println(myString) + // currentLoop++ + //} + + // 10 + //for (number in 1..42) println(number) + + // 11 + //(65..90).forEach { println(it.toChar()) } + + // 12 + //val myArray: Array = arrayOf(5, 10, 5) + //var myResult: Int = 0 + //myArray.forEach { myResult += it } + //println(myResult) + + // 13 + //val myArray: Array = arrayOf(5, 10, 2) + //val myMutableList: MutableList = mutableListOf() + //for (entry in myArray.reversed()) myMutableList += entry + //myMutableList.forEach { println(it) } + + // 14 + //ageMessage(14) + + // 15 + //println(powerCalculator(-2, 3)) + + // 16 + //println(887.isPrime()) + + // 17 + //val myRectangle: Rectangle = Rectangle(5.0, 10.0) + //println(myRectangle.area()) + //println(myRectangle.perimeter()) + //println(myRectangle.isSquare()) + + // 18 + //val myList: List = listOf(1, 4, 6, 7, 8) + //val myArray: Array> = splitList(myList) + //for (item in myArray) { + // for (subItem in item) println(subItem) + //} +}