Complete most of the content up to the Higher Order Functions section
This commit is contained in:
180
Exercises/Practical 1/Practicle 1.kt
Normal file
180
Exercises/Practical 1/Practicle 1.kt
Normal file
@@ -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..<exponent) {
|
||||||
|
result *= base
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Int.isPrime(): Boolean {
|
||||||
|
if (this == 0) return false
|
||||||
|
var tmpVar: Int = this
|
||||||
|
if (this < 0) tmpVar *= -1
|
||||||
|
|
||||||
|
for (loop in 2..tmpVar / 2) {
|
||||||
|
if (tmpVar % loop == 0) return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun splitList(list: List<Int>): Array<List<Int>> {
|
||||||
|
val newList1: MutableList<Int> = mutableListOf<Int>()
|
||||||
|
val newList2: MutableList<Int> = mutableListOf<Int>()
|
||||||
|
|
||||||
|
for (loop in 0..<list.size) {
|
||||||
|
if (loop % 2 == 0) newList1.add(list[loop])
|
||||||
|
else newList2.add(list[loop])
|
||||||
|
}
|
||||||
|
|
||||||
|
return arrayOf(newList1, newList2)
|
||||||
|
}
|
||||||
|
|
||||||
|
class Rectangle(var width: Double, var height: Double) {
|
||||||
|
init {
|
||||||
|
println("Created new Rectangle with a width of $width and a height of $height")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun area(): Double {
|
||||||
|
return width * height
|
||||||
|
}
|
||||||
|
|
||||||
|
fun perimeter(): Double {
|
||||||
|
return width * 2 + height * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isSquare(): Boolean {
|
||||||
|
return width == height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
// 1
|
||||||
|
//println("Hello, world!!!")
|
||||||
|
|
||||||
|
// 2
|
||||||
|
//var myFloat: Float = 10f
|
||||||
|
//myFloat *= 5
|
||||||
|
//println(myFloat)
|
||||||
|
|
||||||
|
// 3
|
||||||
|
//val myInt1: Int = (1..10).random()
|
||||||
|
//val myInt2: Int = (1..10).random()
|
||||||
|
//val myInt3: Int = myInt1 / myInt2
|
||||||
|
//println(myInt1.toString() + " / " + myInt2.toString() + " = " + myInt3.toString())
|
||||||
|
|
||||||
|
// 4
|
||||||
|
//val myString: String = "My String"
|
||||||
|
//println(myString.uppercase().reversed())
|
||||||
|
|
||||||
|
// 5
|
||||||
|
//val myInt1: Int = 2
|
||||||
|
//val myInt2: Int = 2
|
||||||
|
//val myInt3: Int = 4
|
||||||
|
//val myInt4: Int = 4
|
||||||
|
//if ((myInt1 == myInt2 || myInt3 == myInt4) && myInt3 > 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<Int> = arrayOf(1, 2, 3, 4, 5)
|
||||||
|
//val myStringArray: Array<String> = 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<Int> = arrayOf(5, 10, 5)
|
||||||
|
//var myResult: Int = 0
|
||||||
|
//myArray.forEach { myResult += it }
|
||||||
|
//println(myResult)
|
||||||
|
|
||||||
|
// 13
|
||||||
|
//val myArray: Array<Int> = arrayOf(5, 10, 2)
|
||||||
|
//val myMutableList: MutableList<Int> = 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<Int> = listOf<Int>(1, 4, 6, 7, 8)
|
||||||
|
//val myArray: Array<List<Int>> = splitList(myList)
|
||||||
|
//for (item in myArray) {
|
||||||
|
// for (subItem in item) println(subItem)
|
||||||
|
//}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user