From 7853f66d432330fdd390bcb180fd40ed7b0786cf Mon Sep 17 00:00:00 2001 From: justuser-31 Date: Sat, 3 May 2025 11:48:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B2=D1=81=D0=B5=D1=85=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STEP1_Basics/1_Hello_world/main.kt | 3 ++ STEP1_Basics/2_Arithmetic/main.kt | 25 ++++++++++ STEP1_Basics/3_Formatting/main.kt | 16 +++++++ STEP1_Basics/4_Double_array/main.kt | 32 +++++++++++++ STEP1_Basics/5_Recursion/main.kt | 48 +++++++++++++++++++ STEP1_Basics/6_Files/main.kt | 50 +++++++++++++++++++ STEP1_Basics/7_JSON/build.gradle.kts | 36 ++++++++++++++ STEP1_Basics/7_JSON/main.kt | 64 +++++++++++++++++++++++++ STEP1_Basics/8_Threads/main.kt | 48 +++++++++++++++++++ STEP1_Basics/9_Network/Client.kt | 34 +++++++++++++ STEP1_Basics/9_Network/Main.kt | 45 +++++++++++++++++ STEP1_Basics/9_Network/build.gradle.kts | 42 ++++++++++++++++ 12 files changed, 443 insertions(+) create mode 100644 STEP1_Basics/1_Hello_world/main.kt create mode 100644 STEP1_Basics/2_Arithmetic/main.kt create mode 100644 STEP1_Basics/3_Formatting/main.kt create mode 100644 STEP1_Basics/4_Double_array/main.kt create mode 100644 STEP1_Basics/5_Recursion/main.kt create mode 100644 STEP1_Basics/6_Files/main.kt create mode 100644 STEP1_Basics/7_JSON/build.gradle.kts create mode 100644 STEP1_Basics/7_JSON/main.kt create mode 100644 STEP1_Basics/8_Threads/main.kt create mode 100644 STEP1_Basics/9_Network/Client.kt create mode 100644 STEP1_Basics/9_Network/Main.kt create mode 100644 STEP1_Basics/9_Network/build.gradle.kts diff --git a/STEP1_Basics/1_Hello_world/main.kt b/STEP1_Basics/1_Hello_world/main.kt new file mode 100644 index 0000000..07bc6cd --- /dev/null +++ b/STEP1_Basics/1_Hello_world/main.kt @@ -0,0 +1,3 @@ +fun main() { + println("Hello World!") +} diff --git a/STEP1_Basics/2_Arithmetic/main.kt b/STEP1_Basics/2_Arithmetic/main.kt new file mode 100644 index 0000000..9e51c39 --- /dev/null +++ b/STEP1_Basics/2_Arithmetic/main.kt @@ -0,0 +1,25 @@ +import kotlin.math.pow + +fun main() { + print("Enter x: "); val x: Float = readln().toFloat() + print("Enter y: "); val y: Float = readln().toFloat() + + val sum: Float = x + y + val sub: Float = x - y + val div: Float = x / y + val mult: Float = x * y + val pow: Float = x.pow(y) + val sqrt: Float = x.pow(1 / y) + + println(""" + Result: + --------------------- + sum: $sum + sub: $sub + div: $div + mult: $mult + pow: $pow + sqrt: $sqrt + --------------------- + """.trimIndent()) +} diff --git a/STEP1_Basics/3_Formatting/main.kt b/STEP1_Basics/3_Formatting/main.kt new file mode 100644 index 0000000..77a48ef --- /dev/null +++ b/STEP1_Basics/3_Formatting/main.kt @@ -0,0 +1,16 @@ +fun main() { + print("Enter day of the month: "); val day: Int = readln().toInt() + print("Enter name of the month: "); val month: String = readln() + print("Enter year: "); val year: Int = readln().toInt() + + val yyyy_mm_dd = "$year-$month-$day" + val dd_mm_yyyy = "$day-$month-$year" + + println(""" + Result: + ----------------------- + yyyy_mm_dd: $yyyy_mm_dd + dd_mm_yyyy: $dd_mm_yyyy + ----------------------- + """.trimIndent()) +} diff --git a/STEP1_Basics/4_Double_array/main.kt b/STEP1_Basics/4_Double_array/main.kt new file mode 100644 index 0000000..42b7970 --- /dev/null +++ b/STEP1_Basics/4_Double_array/main.kt @@ -0,0 +1,32 @@ +fun main() { + val matrix: Array> = Array(10) { Array(10) { 0 } } + //matrix[0][0] = 1 + // Fill from left-top to right-top + for (i in 0..9) { + matrix[i][0] = i + } + // Fill from left-top to left-bottom + for (i in 0..9) { + matrix[0][i] = i + } + + // Fill all other + for (x in 1..9) { + for (y in 1..9) { + matrix[x][y] = x * y + } + } + + // Get output + for (x in 0..9) { + for (y in 0..9) { + if (matrix[x][y] >= 10) { + print("${matrix[x][y]} ") + } + else { + print("${matrix[x][y]} ") + } + } + println() + } +} diff --git a/STEP1_Basics/5_Recursion/main.kt b/STEP1_Basics/5_Recursion/main.kt new file mode 100644 index 0000000..5b721d0 --- /dev/null +++ b/STEP1_Basics/5_Recursion/main.kt @@ -0,0 +1,48 @@ +fun rmin(ll: Array, i: Int? = null): Int { + if (i == null) { + return rmin(ll, ll.size - 1) + } + else { + val cur = ll[i] + if (i > 0) { + val next: Int = rmin(ll, i - 1) + if (next < cur) { + return next + } + else { + return cur + } + } + else { + return cur + } + } +} + +fun main() { + val ll: Array = Array(10) {0 * 10} + // Gen random numbers + for (i in 0..9) { + ll[i] = (0..100).random() + } + + var bll: String = "" + var first: Boolean = true + for (i in 0..9) { + if (first) { + bll += "${ll[i]}" + first = false + } + else { + bll += ", ${ll[i]}" + } + } + + // Print out + println(""" + Original list: + $bll + + Min: ${rmin(ll)} + """.trimIndent()) +} diff --git a/STEP1_Basics/6_Files/main.kt b/STEP1_Basics/6_Files/main.kt new file mode 100644 index 0000000..e80b57a --- /dev/null +++ b/STEP1_Basics/6_Files/main.kt @@ -0,0 +1,50 @@ +import java.io.File + +fun main() { + print("Enter a: "); val a = readln().toInt() + print("Enter b: "); val b = readln().toInt() + + // Create matrix + var matrix: Array> = Array(2) { Array(2) {0} } + matrix[0][0] = a ; matrix[1][0] = b + matrix[0][1] = a + 1; matrix[1][1] = b + 1 + + // Make strings + var str1: String = "${matrix[0][0]} ${matrix[0][1]}" + var str2: String = "${matrix[1][0]} ${matrix[1][1]}" + + // Write result + val file = File("file") + file.writeText(""" + $str1 + $str2 + """.trimIndent()) + + // Read result + var new_matrix: Array> = Array(2) { Array(2) {0} } + + val lines: List = file.readLines() + var first = true + for (i in lines) { + if (first) { + // Convert List -> List -> Array + new_matrix[0] = i.split(" ").map { it.toInt() }.toTypedArray() + first = false + } + else { + new_matrix[1] = i.split(" ").map { it.toInt() }.toTypedArray() + } + } + + // Get output + println(""" + Original matrix: + ${matrix[0][0]} ${matrix[0][1]} + ${matrix[1][0]} ${matrix[1][1]} + + Readed matrix: + ${new_matrix[0][0]} ${new_matrix[0][1]} + ${new_matrix[1][0]} ${new_matrix[1][1]} + """.trimIndent()) + +} diff --git a/STEP1_Basics/7_JSON/build.gradle.kts b/STEP1_Basics/7_JSON/build.gradle.kts new file mode 100644 index 0000000..2eff40b --- /dev/null +++ b/STEP1_Basics/7_JSON/build.gradle.kts @@ -0,0 +1,36 @@ +plugins { + kotlin("jvm") version "2.1.10" + application +} + +group = "org.example" +version = "1.0-SNAPSHOT" + +repositories { + mavenCentral() +} + +dependencies { + testImplementation(kotlin("test")) + implementation("com.beust:klaxon:5.5") +} + +tasks.test { + useJUnitPlatform() +} + +kotlin { + jvmToolchain(17) +} + +// "run" task +application { + mainClass.set("org.example.MainKt") // Replace with your actual main class +} + +tasks.register("runWithInput") { + group = "application" + mainClass.set("org.example.MainKt") + classpath = sourceSets["main"].runtimeClasspath + standardInput = System.`in` // 👈 enables readln() or readLine() +} diff --git a/STEP1_Basics/7_JSON/main.kt b/STEP1_Basics/7_JSON/main.kt new file mode 100644 index 0000000..8b766be --- /dev/null +++ b/STEP1_Basics/7_JSON/main.kt @@ -0,0 +1,64 @@ +package org.example +import com.beust.klaxon.* +import java.io.File +import java.lang.ProcessBuilder.* +import java.util.concurrent.TimeUnit + +data class Grades( + val math: Int, + val physics: Int, + val history: Int +) + +data class Student( + val name: String, + val age: Int, + val grades: Grades +) + +fun String.runCommand(workingDir: File) { + ProcessBuilder(*split(" ").toTypedArray()) + .directory(workingDir) + .redirectOutput(Redirect.INHERIT) + .redirectError(Redirect.INHERIT) + .start() + .waitFor(60, TimeUnit.MINUTES) +} + +fun main() { + val file = File("src/main/resources/data.json") + var rawData = file.readText() + var json = Klaxon().parseArray(rawData) + + var averageBest: Float = 0f + var averageBestI: Int = 0 + var stud_80_plus: Array = arrayOf() + var averBal: Float = 0f; + if (json != null) { + var el: Student; + for (i in 0.. averageBest) { + averageBest = averBal + averageBestI = i + } + if (el.grades.math > 80) { + stud_80_plus += el + } + } + } + + val aver = json?.get(averageBestI) + println(""" + Student with best average score: + ${aver?.name} ${aver?.age} ${aver?.grades?.math} ${aver?.grades?.physics} ${aver?.grades?.history} + + """.trimIndent()) + + println("Students with 80+ math scores:") + for (i in stud_80_plus) { + println("${i.name} ${i.age} ${i.grades.math} ${i.grades.physics} ${i.grades.history} ") + } +} diff --git a/STEP1_Basics/8_Threads/main.kt b/STEP1_Basics/8_Threads/main.kt new file mode 100644 index 0000000..263a4ab --- /dev/null +++ b/STEP1_Basics/8_Threads/main.kt @@ -0,0 +1,48 @@ +package org.example + +var sum: Int = 0 +var lock: Int = -1 +var workers: Array = arrayOf() + +class WorkerThread(private val threadI: Int): Thread() { + public override fun run() { + for (i in 1..3) { + // Wait until unlocked + while (lock != threadI) { + Thread.sleep(100) + } + sum += 1 + println("T$threadI | $sum") + Thread.sleep(500) + // Pass execution to the next thread + // Check if we not last + if (workers.size > 1) { + // Check if thread last in array + if (workers.indexOf(threadI) == (workers.size - 1)) { + lock = 0 + } + else { + lock = workers.indexOf(threadI) + 1 + } + } + } + } +} + +fun main() { + val threadNums = 3; + + var threadArray: Array = arrayOf() + var firstThread: Boolean = true + for (i in 0..(rawJson) + +fun main() { + val socket = Socket("localhost", serverPort) + val writer = PrintWriter(socket.getOutputStream(), true) + val reader = BufferedReader(InputStreamReader(socket.getInputStream())) + + writer.println(Klaxon().toJsonString(json)) + val response = reader.readLine() + println("Received from server: $response") + + socket.close() +} diff --git a/STEP1_Basics/9_Network/Main.kt b/STEP1_Basics/9_Network/Main.kt new file mode 100644 index 0000000..43777e0 --- /dev/null +++ b/STEP1_Basics/9_Network/Main.kt @@ -0,0 +1,45 @@ +package org.server + +import com.beust.klaxon.Klaxon +import java.io.BufferedReader +import java.io.InputStreamReader +import java.io.PrintWriter +import java.net.ServerSocket + +data class Person( + val name: String, + val age: Int, + val city: String +) + +const val serverPort: Int = 12345 + +fun main() { + val server = ServerSocket(serverPort) + println("Server started on port $serverPort...") + + while (true) { + val client = server.accept() + println("Client connected: ${client.inetAddress.hostAddress}:${client.port}") + + val reader = BufferedReader(InputStreamReader(client.getInputStream())) + val writer = PrintWriter(client.getOutputStream(), true) + + val rawReceived = reader.readLine() + //println("Received from client: $received") + val json = Klaxon().parse(rawReceived) + println(""" + + Parsed data: + name: ${json?.name} + age: ${json?.age} + city: ${json?.city} + + """.trimIndent()) + + val response = "OK" + writer.println(response) + + client.close() + } +} diff --git a/STEP1_Basics/9_Network/build.gradle.kts b/STEP1_Basics/9_Network/build.gradle.kts new file mode 100644 index 0000000..716f735 --- /dev/null +++ b/STEP1_Basics/9_Network/build.gradle.kts @@ -0,0 +1,42 @@ +plugins { + kotlin("jvm") version "2.1.10" + application +} + +group = "org.example" +version = "1.0-SNAPSHOT" + +repositories { + mavenCentral() +} + +dependencies { + testImplementation(kotlin("test")) + implementation("com.beust:klaxon:5.5") +} + +tasks.test { + useJUnitPlatform() +} + +kotlin { + jvmToolchain(17) +} + +// "run" task +application { + mainClass.set("org.example.MainKt") // Replace with your actual main class +} + +tasks.register("runWithInput") { + group = "application" + mainClass.set("org.server.MainKt") + classpath = sourceSets["main"].runtimeClasspath + standardInput = System.`in` // 👈 enables readln() or readLine() +} +tasks.register("runWithInputClient") { + group = "application" + mainClass.set("org.client.ClientKt") + classpath = sourceSets["main"].runtimeClasspath + standardInput = System.`in` // 👈 enables readln() or readLine() +}