From 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Thu, 2 Jul 2026 11:55:17 -0700 Subject: Init --- Homework/cs4700/kotlin/Lists.kt | 154 +++++++++++++++++++++ Homework/cs4700/kotlin/META-INF/main.kotlin_module | Bin 0 -> 50 bytes Homework/cs4700/kotlin/Main.kt | 85 ++++++++++++ Homework/cs4700/kotlin/lists.jar | Bin 0 -> 4697257 bytes .../cs4700/kotlin/lists/ListsKt$compose$1.class | Bin 0 -> 1342 bytes .../kotlin/lists/ListsKt$composeList$1.class | Bin 0 -> 1032 bytes .../kotlin/lists/ListsKt$composeList$2$1.class | Bin 0 -> 1311 bytes Homework/cs4700/kotlin/lists/ListsKt$main$1.class | Bin 0 -> 1468 bytes .../cs4700/kotlin/lists/ListsKt$main$f$1.class | Bin 0 -> 1377 bytes .../cs4700/kotlin/lists/ListsKt$main$f$2.class | Bin 0 -> 1377 bytes Homework/cs4700/kotlin/lists/ListsKt.class | Bin 0 -> 9880 bytes Homework/cs4700/kotlin/lists/MainKt$main$1.class | Bin 0 -> 1423 bytes Homework/cs4700/kotlin/lists/MainKt$main$2.class | Bin 0 -> 1423 bytes Homework/cs4700/kotlin/lists/MainKt$main$3.class | Bin 0 -> 1423 bytes .../kotlin/lists/MainKt$main$functionList$1.class | Bin 0 -> 1353 bytes .../kotlin/lists/MainKt$main$functionList$2.class | Bin 0 -> 1353 bytes .../kotlin/lists/MainKt$main$functionList$3.class | Bin 0 -> 1353 bytes Homework/cs4700/kotlin/lists/MainKt.class | Bin 0 -> 5079 bytes Homework/cs4700/kotlin/main.jar | Bin 0 -> 4701863 bytes 19 files changed, 239 insertions(+) create mode 100644 Homework/cs4700/kotlin/Lists.kt create mode 100644 Homework/cs4700/kotlin/META-INF/main.kotlin_module create mode 100644 Homework/cs4700/kotlin/Main.kt create mode 100644 Homework/cs4700/kotlin/lists.jar create mode 100644 Homework/cs4700/kotlin/lists/ListsKt$compose$1.class create mode 100644 Homework/cs4700/kotlin/lists/ListsKt$composeList$1.class create mode 100644 Homework/cs4700/kotlin/lists/ListsKt$composeList$2$1.class create mode 100644 Homework/cs4700/kotlin/lists/ListsKt$main$1.class create mode 100644 Homework/cs4700/kotlin/lists/ListsKt$main$f$1.class create mode 100644 Homework/cs4700/kotlin/lists/ListsKt$main$f$2.class create mode 100644 Homework/cs4700/kotlin/lists/ListsKt.class create mode 100644 Homework/cs4700/kotlin/lists/MainKt$main$1.class create mode 100644 Homework/cs4700/kotlin/lists/MainKt$main$2.class create mode 100644 Homework/cs4700/kotlin/lists/MainKt$main$3.class create mode 100644 Homework/cs4700/kotlin/lists/MainKt$main$functionList$1.class create mode 100644 Homework/cs4700/kotlin/lists/MainKt$main$functionList$2.class create mode 100644 Homework/cs4700/kotlin/lists/MainKt$main$functionList$3.class create mode 100644 Homework/cs4700/kotlin/lists/MainKt.class create mode 100644 Homework/cs4700/kotlin/main.jar (limited to 'Homework/cs4700/kotlin') diff --git a/Homework/cs4700/kotlin/Lists.kt b/Homework/cs4700/kotlin/Lists.kt new file mode 100644 index 0000000..2839b8c --- /dev/null +++ b/Homework/cs4700/kotlin/Lists.kt @@ -0,0 +1,154 @@ +// Do not remove or rename the package +package lists + +/* +* The following functions are helper functions that I am providing +*/ + +/* +* Extend the List class with a "tail" getter to get the tail of a list. +* Below is an example of how you would use tail +* val a = listOf(1,2,3) +* val t = a.tail +* println("tail of $a is $t") // prints [2,3] +*/ +val List.tail: List +get() = drop(1) + +/* +* Extend the List class with a "head" getter to get the head of a list. +* Below is an example of how you would use head +* val a = listOf(1,2,3) +* val h = a.head +* println("head of $a is $h") // prints 1 +*/ +val List.head: T +get() = first() + +/* +* The isPrime function takes as input an Int +* x : an Int object to test +* and returns a Boolean +* true if x is a prime +* false if x is not a prime +*/ +fun isPrime(x : Int) : Boolean { + if (x == 1) + return false + for (i in 2..(x-1)) { + if (x % i == 0) { + return false + } + } + return true +} + +/* The compose function takes as input +* f - A function that takes as input a value of type T and returns a value of type T +* g - A function that takes as input a value of type T and returns a value of type T +* and returns as output the composition of the functions +* f(g(x)) +*/ +fun compose(f: (T)->T, g:(T) -> T) : (T) -> T = { f(g(it)) } + +/* Be sure to document + your functions + describing inputs and outputs and what the function does + */ + +/** + * @param limit is the upper bound of the list of natural numbers + * @return a list of natural numbers up to limit + */ +fun countingNumbers(limit : Int?) : List? { + if (limit == null) + return null + return (1..limit).map { it } +} + +/** + * @param limit is the upper bound of the list of even numbers + * @return a list of even numbers up to limit + */ +fun evenNumbers(limit : Int?) : List? { + return countingNumbers(limit)?.filter( { it % 2 == 0 } ) +} + +/** + * @param limit is the upper bound of the list of prime numbers + * @return a list of prime numbers up to limit + */ +fun primeNumbers(limit : Int?) : List? { + return countingNumbers(limit)?.filter( { isPrime(it) } ) +} + +/** + * @param a is a null-safe list of elements of "Comparable" items + * @param b is a null-safe list of elements of "Comparable" items + * @return a sorted merge list of the elements of a and b + */ +fun> merge(a: List?, b: List?) : List? { + if (a == null || b == null) + return null + + val merged = mutableListOf() + + var aInd = 0 + var bInd = 0 + + while (aInd < a.size && bInd < b.size) { + if (a[aInd] < b[bInd]) { + merged.add(a[aInd]); + aInd++; + continue; + } + merged.add(b[bInd]); + bInd++; + } + + if (aInd < a.size) + merged += a.subList(aInd, a.size) + + else if (bInd < b.size) + merged += b.subList(bInd, b.size) + + return merged +} + +/** + * @param a is a null-safe list of elements + * @return a list of "sublists" where result[i] = a[0..i] + */ +fun subLists(a: List?) : List>? { + if (a == null) + return null + return (1..(a.size)).map( { a.subList(0, it) } ) +} + +/** + * @param a is a null-safe list of lists of elements + * @return the number of elements total in each sublist + */ +fun countElements(a: List? >?) : Int? { + if (a == null) + return a + return a.filter({ it != null }).fold(0, { acc, x -> acc + x!!.size }) +} + +/** + * @param a is a null-safe list of lists of elements + * @return the number of elements total in each sublist + */ +fun listApply(f: (T, T) -> T, a: List>?): List? { + if (a == null) + return null + return a.map( { l: List -> l.foldIndexed( l[0], { index, acc, arg -> if (index > 0) f(acc, arg) else arg } ) } ); +} + +/** + * @param a is a list of functions + * @return a function that the composition of the list of functions (from right to left) + */ +fun composeList(a: List<(T) -> T>): (T) -> T { + return a.foldRight({ x -> x }, { acc, f: (T) -> T -> { x -> f(acc(x)) } }); +} diff --git a/Homework/cs4700/kotlin/META-INF/main.kotlin_module b/Homework/cs4700/kotlin/META-INF/main.kotlin_module new file mode 100644 index 0000000..49be1bf Binary files /dev/null and b/Homework/cs4700/kotlin/META-INF/main.kotlin_module differ diff --git a/Homework/cs4700/kotlin/Main.kt b/Homework/cs4700/kotlin/Main.kt new file mode 100644 index 0000000..0fafdf5 --- /dev/null +++ b/Homework/cs4700/kotlin/Main.kt @@ -0,0 +1,85 @@ +/** + * The main part of the lists package consists of functions to test the functions you code, you may modify however you like as I will use my own Main.kt to test + */ +package lists + +// Functions for composeList testing +fun add1(x: Int) : Int = x + 1 +fun add2(x: Int) : Int = x + 2 + +// Functions for listApply testing +fun add(x : Int, y : Int) : Int { + return x + y +} + +fun main(args: Array) { + println("Start testing") + // Comment out tests you don't use + /* This is a multi-line + comment + */ + val n = 12 + val zero = 0 + val nAsNull = null + + // countingNumbers tests + val countingNumbersUpToN = countingNumbers(n) + println("countingNumbers to $n are $countingNumbersUpToN") + val countingNumbersZero = countingNumbers(zero) + println("countingNumbers to $zero are $countingNumbersZero") + val countingNumbersNull = countingNumbers(nAsNull) + println("countingNumbers to $nAsNull are $countingNumbersNull") + println() + + // evenNumbers tests + val evenNumbersUpToN = evenNumbers(n) + println("evenNumbers to $n are $evenNumbersUpToN") + val evenNumbersZero = evenNumbers(zero) + println("evenNumbers to $zero are $evenNumbersZero") + val evenNumbersNull = evenNumbers(nAsNull) + println("evenNumbers to $nAsNull are $evenNumbersNull") + println() + + // primeNumbers tests + val primesUpToN = primeNumbers(n) + println("primeNumbers to $n are $primesUpToN") + val primesUpToZero = primeNumbers(zero) + println("primeNumbers to $zero are $primesUpToZero") + val primeNumbersNull = primeNumbers(nAsNull) + println("primeNumbers to $nAsNull are $primeNumbersNull") + println() + + // subLists tests + println("subLists of $primesUpToN are ${subLists(primesUpToN)}") + println("subLists of $evenNumbersZero are ${subLists(evenNumbersZero)}") + println("subLists of $evenNumbersNull are ${subLists(evenNumbersNull)}") + println() + + // countElements tests + println("countElements of subLists of $primesUpToN are ${countElements(subLists(primesUpToN))}") + println("countElements of subLists of even numbers to $zero are ${countElements(subLists(evenNumbersZero))}") + println("countElements of subLists of prime numbers to $nAsNull are ${countElements(subLists(primeNumbersNull))}") + val tempList = listOf(evenNumbersUpToN,evenNumbersZero,evenNumbersNull) + println("countElements of $tempList is ${countElements(tempList)}") + println() + + // merge tests + println("merge of $primesUpToN and $countingNumbersUpToN is ${merge(primesUpToN,countingNumbersUpToN)}") + println("merge of $primesUpToN and $countingNumbersZero is ${merge(primesUpToN,countingNumbersZero)}") + println("merge of $primesUpToN and $countingNumbersNull is ${merge(primesUpToN,countingNumbersNull)}") + println() + + // listApply tests + println("listApply of ::add to $countingNumbersUpToN is ${listApply(::add,subLists(countingNumbersUpToN))}") + println("listApply of ::add to $countingNumbersZero is ${listApply(::add,subLists(countingNumbersZero))}") + println("listApply of ::add to $countingNumbersNull is ${listApply(::add,subLists(countingNumbersNull))}") + println() + + // composeList test + val functionList = listOf(::add1,::add2,::add1) + val composedFunction = composeList(functionList) + var result = composedFunction(n) + println("composedFunction of [::add1,::add2,::add1] applied to $n is $result") + +} + diff --git a/Homework/cs4700/kotlin/lists.jar b/Homework/cs4700/kotlin/lists.jar new file mode 100644 index 0000000..dcfdc9c Binary files /dev/null and b/Homework/cs4700/kotlin/lists.jar differ diff --git a/Homework/cs4700/kotlin/lists/ListsKt$compose$1.class b/Homework/cs4700/kotlin/lists/ListsKt$compose$1.class new file mode 100644 index 0000000..fdb4c14 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/ListsKt$compose$1.class differ diff --git a/Homework/cs4700/kotlin/lists/ListsKt$composeList$1.class b/Homework/cs4700/kotlin/lists/ListsKt$composeList$1.class new file mode 100644 index 0000000..aa16793 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/ListsKt$composeList$1.class differ diff --git a/Homework/cs4700/kotlin/lists/ListsKt$composeList$2$1.class b/Homework/cs4700/kotlin/lists/ListsKt$composeList$2$1.class new file mode 100644 index 0000000..8847d9b Binary files /dev/null and b/Homework/cs4700/kotlin/lists/ListsKt$composeList$2$1.class differ diff --git a/Homework/cs4700/kotlin/lists/ListsKt$main$1.class b/Homework/cs4700/kotlin/lists/ListsKt$main$1.class new file mode 100644 index 0000000..9771e62 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/ListsKt$main$1.class differ diff --git a/Homework/cs4700/kotlin/lists/ListsKt$main$f$1.class b/Homework/cs4700/kotlin/lists/ListsKt$main$f$1.class new file mode 100644 index 0000000..23c2833 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/ListsKt$main$f$1.class differ diff --git a/Homework/cs4700/kotlin/lists/ListsKt$main$f$2.class b/Homework/cs4700/kotlin/lists/ListsKt$main$f$2.class new file mode 100644 index 0000000..1e1ac49 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/ListsKt$main$f$2.class differ diff --git a/Homework/cs4700/kotlin/lists/ListsKt.class b/Homework/cs4700/kotlin/lists/ListsKt.class new file mode 100644 index 0000000..a31cea6 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/ListsKt.class differ diff --git a/Homework/cs4700/kotlin/lists/MainKt$main$1.class b/Homework/cs4700/kotlin/lists/MainKt$main$1.class new file mode 100644 index 0000000..2197f8f Binary files /dev/null and b/Homework/cs4700/kotlin/lists/MainKt$main$1.class differ diff --git a/Homework/cs4700/kotlin/lists/MainKt$main$2.class b/Homework/cs4700/kotlin/lists/MainKt$main$2.class new file mode 100644 index 0000000..3dbb957 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/MainKt$main$2.class differ diff --git a/Homework/cs4700/kotlin/lists/MainKt$main$3.class b/Homework/cs4700/kotlin/lists/MainKt$main$3.class new file mode 100644 index 0000000..5936baf Binary files /dev/null and b/Homework/cs4700/kotlin/lists/MainKt$main$3.class differ diff --git a/Homework/cs4700/kotlin/lists/MainKt$main$functionList$1.class b/Homework/cs4700/kotlin/lists/MainKt$main$functionList$1.class new file mode 100644 index 0000000..b3c4244 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/MainKt$main$functionList$1.class differ diff --git a/Homework/cs4700/kotlin/lists/MainKt$main$functionList$2.class b/Homework/cs4700/kotlin/lists/MainKt$main$functionList$2.class new file mode 100644 index 0000000..8b0602a Binary files /dev/null and b/Homework/cs4700/kotlin/lists/MainKt$main$functionList$2.class differ diff --git a/Homework/cs4700/kotlin/lists/MainKt$main$functionList$3.class b/Homework/cs4700/kotlin/lists/MainKt$main$functionList$3.class new file mode 100644 index 0000000..4e4dfc0 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/MainKt$main$functionList$3.class differ diff --git a/Homework/cs4700/kotlin/lists/MainKt.class b/Homework/cs4700/kotlin/lists/MainKt.class new file mode 100644 index 0000000..3989e42 Binary files /dev/null and b/Homework/cs4700/kotlin/lists/MainKt.class differ diff --git a/Homework/cs4700/kotlin/main.jar b/Homework/cs4700/kotlin/main.jar new file mode 100644 index 0000000..96faa22 Binary files /dev/null and b/Homework/cs4700/kotlin/main.jar differ -- cgit v1.3