Skip to content

Instantly share code, notes, and snippets.

@anztrax
Created January 26, 2025 10:22
Show Gist options
  • Save anztrax/4f946d84c9cab5086d04906af80993af to your computer and use it in GitHub Desktop.
Save anztrax/4f946d84c9cab5086d04906af80993af to your computer and use it in GitHub Desktop.
scratch file 2 kotlin
class FillInTheBlankQuestion {
val questionText: String = ""
val answer: String = ""
val difficulty: String = ""
}
class TrueOrFalseQuestion {
val questionText: String = ""
val answer: Boolean = false
val difficulty: String = ""
}
class NumericQuestion {
val questionText: String = ""
val answer: Int = 0
val difficulty: String = ""
}
object StudentProgress {
val total: Int = 10
var answered: Int = 3
}
enum class Difficulty {
EASY, MEDIUM, HARD
}
data class Question<T>(val questionText: String, val answer: T, val difficulty: Difficulty) {
}
class Quiz {
val question1 = Question<String>("Quoth the raven ___", "nevermore", Difficulty.MEDIUM)
val question2 = Question<Boolean>("The sky is green. True or false", false, Difficulty.EASY)
val question3 = Question<Int>("How many days are there between full moons?", 28, Difficulty.HARD)
companion object StudentProgress {
val total: Int = 10
var answered: Int = 3
}
}
var Quiz.StudentProgress.progressText: String
get() = "${answered} of ${total} answered"
set(value) {}
fun Quiz.StudentProgress.printProgressBar() {
repeat(Quiz.answered) {
print("▓")
}
repeat(Quiz.total - Quiz.answered) {
print("▒")
}
println()
println(Quiz.progressText)
}
interface ProgressPrintable {
val progressText: String;
fun printProgressBar()
}
class ProgressQuiz : ProgressPrintable {
val question1 = Question<String>("Quoth the raven ___", "nevermore", Difficulty.MEDIUM)
val question2 = Question<Boolean>("The sky is green. True or false", false, Difficulty.EASY)
val question3 = Question<Int>("How many days are there between full moons?", 28, Difficulty.HARD)
companion object StudentProgress {
val total: Int = 10
var answered: Int = 3
}
override val progressText: String
get() = "${answered} of ${total} answered"
override fun printProgressBar() {
repeat(Quiz.answered) {
print("▓")
}
repeat(Quiz.total - Quiz.answered) {
print("▒")
}
println()
println(progressText)
}
fun printQuiz() {
println(question1.answer)
println(question1.questionText)
println(question1.difficulty)
println()
question2.let {
println(it.questionText)
println(it.answer)
println(it.difficulty)
}
println()
question3.let {
println(it.questionText)
println(it.answer)
println(it.difficulty)
}
}
}
fun learnGeneric() {
println(Quiz.progressText)
Quiz.printProgressBar()
println("student progress : ${Quiz.StudentProgress.total} ${Quiz.StudentProgress.answered}")
println("with interface")
val progressQuiz1 = ProgressQuiz()
progressQuiz1.printProgressBar()
progressQuiz1.printQuiz()
// call and parameter without creating an object of class
ProgressQuiz().apply {
printQuiz()
}
}
fun learnArrayCollections() {
val rockPlanets = arrayOf<String>("Mercury", "Venus", "Earth", "Mars")
val gasPlanets = arrayOf<String>("Jupiter", "Saturn", "Uranus", "Neptune")
val solarSystems1 = rockPlanets + gasPlanets
for (item in solarSystems1) {
println(item)
}
solarSystems1[0] = "Plutonium"
println(solarSystems1[0])
}
fun learnListCollections() {
val solarSystems2 =
listOf("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
println("solarSystem2 size = (${solarSystems2.size})")
println(solarSystems2[2])
println(solarSystems2.get(3))
println(solarSystems2.indexOf("Earth"))
println(solarSystems2.indexOf("Plutonium"))
val mutableSolarSystem1 =
mutableListOf("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
mutableSolarSystem1.add("Pluto")
mutableSolarSystem1.add(3, "Theia")
mutableSolarSystem1[3] = "Future Moon"
mutableSolarSystem1.removeAt(9)
mutableSolarSystem1.remove("Future Moon")
println(mutableSolarSystem1.contains("Pluto"))
for (item in mutableSolarSystem1) {
println(item)
}
println("Future Moon" in mutableSolarSystem1)
}
fun learnSetsCollections() {
val solarSystem =
mutableSetOf("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
println("solarSystem Set size = ${solarSystem.size}")
solarSystem.add("Pluto")
println("solarSystem Set size = ${solarSystem.size}")
println(solarSystem.contains("Pluto"))
solarSystem.remove("Pluto")
println("solarSystem Set size = ${solarSystem.size}")
}
fun learnMapCollections() {
val solarSystems = mutableMapOf(
"Mercury" to 0,
"Venus" to 0,
"Earth" to 1,
"Mars" to 2,
"Jupiter" to 79,
"Saturn" to 82,
"Uranus" to 27,
"Neptune" to 14
)
println("SolarSystem size = ${solarSystems.size}")
solarSystems["Pluto"] = 5
println("SolarSystem size = ${solarSystems.size}")
println("Theia value = ${solarSystems.get("Theia")}")
solarSystems.remove("Pluto")
println("SolarSystem size = ${solarSystems.size}")
}
fun learnHighOrderFunctions() {
class Cookie(
val name: String,
val softBaked: Boolean,
val hasFilling: Boolean,
val price: Double
) {
}
val cookies = listOf(
Cookie(
name = "Chocolate Chip",
softBaked = false,
hasFilling = false,
price = 1.69
),
Cookie(
name = "Banana Walnut",
softBaked = true,
hasFilling = false,
price = 1.49
),
Cookie(
name = "Vanilla Creme",
softBaked = false,
hasFilling = true,
price = 1.59
),
Cookie(
name = "Chocolate Peanut Butter",
softBaked = false,
hasFilling = true,
price = 1.49
),
Cookie(
name = "Snickerdoodle",
softBaked = true,
hasFilling = false,
price = 1.39
),
Cookie(
name = "Blueberry Tart",
softBaked = true,
hasFilling = true,
price = 1.79
),
Cookie(
name = "Sugar and Sprinkles",
softBaked = false,
hasFilling = false,
price = 1.39
)
)
cookies.forEach {
println("Menu Item : ${it.name}")
}
println("full menu")
val fullMenu = cookies.map {
"${it.name} - ${it.price}"
}
println(fullMenu)
val softBakedMenu = cookies.filter {
it.softBaked
}
println("Soft Baked Menu : ${softBakedMenu}")
println("Soft cookies:")
softBakedMenu.forEach {
println("${it.name} - $${it.price}")
}
// group by
var groupedMenu = cookies.groupBy { it.softBaked }
var softBakedMenu2 = groupedMenu[true] ?: listOf()
var crunchyMenu = groupedMenu[false] ?: listOf()
println("Soft cookies :\n==============")
softBakedMenu2.forEach {
println("${it.name} - $${it.price}")
}
println("Cruncy cookies :\n==============")
crunchyMenu.forEach {
println("${it.name} - $${it.price}")
}
// fold
val totalPrice = cookies.fold(0.0) { acc, cookie ->
acc + cookie.price
}
println(totalPrice)
// sorted by
println("Alphabetical menu\n==============")
val alphabeticalMenu = cookies.sortedBy {
it.name
}
alphabeticalMenu.forEach {
println(it.name)
}
}
enum class Daypart {
MORNING,
AFTERNOON,
EVENING
}
data class Event(
var title: String,
var description: String? = null,
var daypart: Daypart,
var durationInMinutes: Int
)
val Event.durationOfEvent: String
get() = if (this.durationInMinutes < 60) {
"short"
} else {
"long"
}
fun challenge1_7() {
val event1 = Event(
title = "Wake up",
description = "Time to get up",
daypart = Daypart.MORNING,
durationInMinutes = 0
)
val event2 = Event(title = "Eat breakfast", daypart = Daypart.MORNING, durationInMinutes = 15)
val event3 =
Event(title = "Learn about Kotlin", daypart = Daypart.AFTERNOON, durationInMinutes = 30)
val event4 =
Event(title = "Practice Compose", daypart = Daypart.AFTERNOON, durationInMinutes = 60)
val event5 = Event(
title = "Watch latest DevBytes video",
daypart = Daypart.AFTERNOON,
durationInMinutes = 10
)
val event6 = Event(
title = "Check out latest Android Jetpack library",
daypart = Daypart.EVENING,
durationInMinutes = 45
)
var events = mutableListOf<Event>(
event1,
event2,
event3,
event4,
event5,
event6,
)
val shortEvents = events.filter {
it.durationInMinutes < 60
}
println("You have ${shortEvents.size} short events")
// group by to summarize
events.groupBy {
it.daypart
}.forEach { (daypart, event) ->
println("$daypart: ${event.size} events")
}
println("Last event of the day: ${events.last().title}");
println("Duration of first event of the day: ${events[0].durationOfEvent}")
}
fun main() {
challenge1_7()
println("High Order Functions\n============")
learnHighOrderFunctions()
println("==============")
println("Collections\n============")
learnMapCollections()
learnSetsCollections()
learnListCollections()
learnArrayCollections()
println("==============")
learnGeneric()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment