generated from justuser-31/code_projects_template
Выгрузка всех файлов
This commit is contained in:
parent
22475e1eb5
commit
7853f66d43
3
STEP1_Basics/1_Hello_world/main.kt
Normal file
3
STEP1_Basics/1_Hello_world/main.kt
Normal file
@ -0,0 +1,3 @@
|
||||
fun main() {
|
||||
println("Hello World!")
|
||||
}
|
25
STEP1_Basics/2_Arithmetic/main.kt
Normal file
25
STEP1_Basics/2_Arithmetic/main.kt
Normal file
@ -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())
|
||||
}
|
16
STEP1_Basics/3_Formatting/main.kt
Normal file
16
STEP1_Basics/3_Formatting/main.kt
Normal file
@ -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())
|
||||
}
|
32
STEP1_Basics/4_Double_array/main.kt
Normal file
32
STEP1_Basics/4_Double_array/main.kt
Normal file
@ -0,0 +1,32 @@
|
||||
fun main() {
|
||||
val matrix: Array<Array<Int>> = Array(10) { Array<Int>(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()
|
||||
}
|
||||
}
|
48
STEP1_Basics/5_Recursion/main.kt
Normal file
48
STEP1_Basics/5_Recursion/main.kt
Normal file
@ -0,0 +1,48 @@
|
||||
fun rmin(ll: Array<Int>, 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<Int> = 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())
|
||||
}
|
50
STEP1_Basics/6_Files/main.kt
Normal file
50
STEP1_Basics/6_Files/main.kt
Normal file
@ -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<Int>> = 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<Int>> = Array(2) { Array(2) {0} }
|
||||
|
||||
val lines: List<String> = file.readLines()
|
||||
var first = true
|
||||
for (i in lines) {
|
||||
if (first) {
|
||||
// Convert List<String> -> List<Int> -> Array<Int>
|
||||
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())
|
||||
|
||||
}
|
36
STEP1_Basics/7_JSON/build.gradle.kts
Normal file
36
STEP1_Basics/7_JSON/build.gradle.kts
Normal file
@ -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<JavaExec>("runWithInput") {
|
||||
group = "application"
|
||||
mainClass.set("org.example.MainKt")
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
standardInput = System.`in` // 👈 enables readln() or readLine()
|
||||
}
|
64
STEP1_Basics/7_JSON/main.kt
Normal file
64
STEP1_Basics/7_JSON/main.kt
Normal file
@ -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<Student>(rawData)
|
||||
|
||||
var averageBest: Float = 0f
|
||||
var averageBestI: Int = 0
|
||||
var stud_80_plus: Array<Student> = arrayOf()
|
||||
var averBal: Float = 0f;
|
||||
if (json != null) {
|
||||
var el: Student;
|
||||
for (i in 0..<json.size) {
|
||||
//println(i.name)
|
||||
el = json.get(i)
|
||||
averBal = ((el.grades.math + el.grades.history + el.grades.physics)/3).toFloat()
|
||||
if (averBal > 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} ")
|
||||
}
|
||||
}
|
48
STEP1_Basics/8_Threads/main.kt
Normal file
48
STEP1_Basics/8_Threads/main.kt
Normal file
@ -0,0 +1,48 @@
|
||||
package org.example
|
||||
|
||||
var sum: Int = 0
|
||||
var lock: Int = -1
|
||||
var workers: Array<Int> = 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<Thread> = arrayOf()
|
||||
var firstThread: Boolean = true
|
||||
for (i in 0..<threadNums) {
|
||||
if (firstThread) {
|
||||
lock = i
|
||||
firstThread = false
|
||||
}
|
||||
threadArray += WorkerThread(i)
|
||||
workers += i
|
||||
}
|
||||
for (i in threadArray) {
|
||||
i.start()
|
||||
}
|
||||
}
|
34
STEP1_Basics/9_Network/Client.kt
Normal file
34
STEP1_Basics/9_Network/Client.kt
Normal file
@ -0,0 +1,34 @@
|
||||
package org.client
|
||||
|
||||
import com.beust.klaxon.Klaxon
|
||||
import org.server.Person
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.PrintWriter
|
||||
import java.net.Socket
|
||||
|
||||
data class Person(
|
||||
val name: String,
|
||||
val age: Int,
|
||||
val city: String
|
||||
)
|
||||
|
||||
const val serverPort: Int = 12345
|
||||
const val rawJson = """{
|
||||
"name": "Иван Иванов",
|
||||
"age": 25,
|
||||
"city": "Москва"
|
||||
}"""
|
||||
val json = Klaxon().parse<Person>(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()
|
||||
}
|
45
STEP1_Basics/9_Network/Main.kt
Normal file
45
STEP1_Basics/9_Network/Main.kt
Normal file
@ -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<Person>(rawReceived)
|
||||
println("""
|
||||
|
||||
Parsed data:
|
||||
name: ${json?.name}
|
||||
age: ${json?.age}
|
||||
city: ${json?.city}
|
||||
|
||||
""".trimIndent())
|
||||
|
||||
val response = "OK"
|
||||
writer.println(response)
|
||||
|
||||
client.close()
|
||||
}
|
||||
}
|
42
STEP1_Basics/9_Network/build.gradle.kts
Normal file
42
STEP1_Basics/9_Network/build.gradle.kts
Normal file
@ -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<JavaExec>("runWithInput") {
|
||||
group = "application"
|
||||
mainClass.set("org.server.MainKt")
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
standardInput = System.`in` // 👈 enables readln() or readLine()
|
||||
}
|
||||
tasks.register<JavaExec>("runWithInputClient") {
|
||||
group = "application"
|
||||
mainClass.set("org.client.ClientKt")
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
standardInput = System.`in` // 👈 enables readln() or readLine()
|
||||
}
|
Loading…
Reference in New Issue
Block a user