From c5b21a37698050a8680b44b6e58d0f9efa5d2c08 Mon Sep 17 00:00:00 2001 From: alrweashd Date: Fri, 14 Feb 2025 01:26:25 +0300 Subject: [PATCH 1/3] 1 to 3 --- .idea/jpa-buddy.xml | 6 ++++++ .idea/kotlinc.xml | 6 +----- .idea/misc.xml | 3 +++ src/Main.kt | 41 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 .idea/jpa-buddy.xml diff --git a/.idea/jpa-buddy.xml b/.idea/jpa-buddy.xml new file mode 100644 index 0000000..966d5f5 --- /dev/null +++ b/.idea/jpa-buddy.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 8226e53..36b41ec 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,10 +1,6 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 03f397c..f8ed9c1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,4 +3,7 @@ + + \ No newline at end of file diff --git a/src/Main.kt b/src/Main.kt index a7a5e00..0685006 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,3 +1,40 @@ fun main() { - println("Hello World!") -} \ No newline at end of file + + //Part 1 + val numbers = mutableListOf(1,2,3,4,5,6,7,8,9,10) + val numbersDoubled = numbers.map{it*2}; + println(numbersDoubled) + + //Part 2 + val names = mutableListOf("Alice", "Bob", "Amir", "Charlie", "Annie", "David"); + println(filterWithA("alice")) + println(filterNames(names,filterWithA)) + + //Part 3 + val fruits = mutableListOf("apple", "banana", "kiwi", "strawberry", "grape") + println(sortedList(fruits,findWordLength)) + + //Part 4 + + +} + +//For part 2 +fun filterNames(list:List, filteringFun: (String)->(Boolean)): List{ + return list.filter{filteringFun(it)} + +} + + +val filterWithA: (String) -> Boolean = { + it -> it.startsWith("A") +} + + +//for part 3 +fun sortedList(list:List, filteringFun: (String) -> Int): List{ + return list.sortedByDescending(filteringFun) +} +val findWordLength: (String) -> Int = { + it -> it.length +} From 46be8552ce3757f1b3fc1cb176110d29c75a19d0 Mon Sep 17 00:00:00 2001 From: alrweashd Date: Fri, 14 Feb 2025 15:22:08 +0300 Subject: [PATCH 2/3] part 4 --- src/Main.kt | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Main.kt b/src/Main.kt index 0685006..7438b70 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,3 +1,5 @@ +import javax.swing.text.StyledEditorKit.BoldAction + fun main() { //Part 1 @@ -15,6 +17,20 @@ fun main() { println(sortedList(fruits,findWordLength)) //Part 4 + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it > 5 }) + //or + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),biggerThanFive)) + + + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it % 2 == 0 }) + //or + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),isEven)) + + + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) { it % 3 == 0 }) + //or + println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),multiplesOfThree)) + } @@ -38,3 +54,17 @@ fun sortedList(list:List, filteringFun: (String) -> Int): List{ val findWordLength: (String) -> Int = { it -> it.length } + +//for part 4 +fun customFilter(list: List, filteringFun: (Int) -> Boolean): List{ + return list.filter(filteringFun) +} +val biggerThanFive: (Int) -> Boolean = { + it -> it > 5 +} +val isEven: (Int) -> Boolean = { + it -> it % 2 == 0 +} +val multiplesOfThree: (Int) -> Boolean = { + it -> it % 3 == 0 +} From f4eec67c569a1aa5f0b13e78a2ee8b1b9414f7f4 Mon Sep 17 00:00:00 2001 From: alrweashd Date: Fri, 14 Feb 2025 17:22:31 +0300 Subject: [PATCH 3/3] part 5 --- src/Main.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Main.kt b/src/Main.kt index 7438b70..d788a8e 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -32,6 +32,9 @@ fun main() { println(customFilter(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),multiplesOfThree)) + //Part 5 + println(processNumbers(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),lambdaNotEven,lambdaSquare)); + } @@ -68,3 +71,15 @@ val isEven: (Int) -> Boolean = { val multiplesOfThree: (Int) -> Boolean = { it -> it % 3 == 0 } + +//for part 5 +//takes two lambdas funs as paramters with List of int and returns a list of int +fun processNumbers(list:List,lambdaNotEven: (Int)->Boolean, lambdaSquare: (Int) -> Int): List{ + return list.filter(lambdaNotEven).map(lambdaSquare); +} +val lambdaNotEven: (Int) -> Boolean = { + it -> it % 2 != 0 +} +val lambdaSquare: (Int) -> Int = { + it -> it * it +} \ No newline at end of file