Advertisement
selebry

dasdas

Dec 14th, 2023
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.05 KB | None | 0 0
  1. package murad
  2.  
  3. data class Book(val title: String, val author: String)
  4. val library = sequenceOf(
  5.     Book("The Great Gatsby", "F. Scott Fitzgerald"),
  6.     Book("To Kill a Mockingbird", "Harper Lee"),
  7.     Book("1984", "George Orwell"),
  8.     Book("The Catcher in the Rye", "J.D. Salinger"),
  9.     Book("The Lord of the Rings", "J.R.R. Tolkien"),
  10.     Book("The Hobbit", "J.R.R. Tolkien"),
  11.     Book("The Da Vinci Code", "Dan Brown"),
  12.     Book("The Girl with the Dragon Tattoo", "Stieg Larsson"),
  13.     Book("The Hunger Games", "Suzanne Collins"),
  14.     Book("The Fault in Our Stars", "John Green")
  15. )
  16.  
  17.  
  18. fun main() {
  19.     val uniqueAuthors = library.map { it.author }.toSet()
  20.     println("Уникальные авторы:")
  21.     uniqueAuthors.forEach{author -> println(author)}
  22.  
  23.     val author = "J.R.R. Tolkien"
  24.     val booksByAuthor = library.filter { it.author == author }.toList()
  25.     println("Книги от $author")
  26.     booksByAuthor.forEach{book -> println(book)}
  27.  
  28. }
  29.  
  30.  
  31.  
  32. package murad
  33. data class Country(val name: String, val continent: String, val population: Long, val area: Double) {
  34.     override fun toString(): String {
  35.         return "Название: $name, Континент: $continent, Население: $population, Площадь: $area км^2"
  36.     }
  37. }
  38.  
  39. val countries = sequenceOf(
  40.     Country("United States", "North America", 331002651, 9525067.0),
  41.     Country("Canada", "North America", 37742154, 9984670.0),
  42.     Country("China", "Asia", 1444216107, 9596961.0),
  43.     Country("India", "Asia", 1380004385, 3287263.0),
  44.     Country("Australia", "Australia/Oceania", 25696886, 7692024.0),
  45.     Country("Brazil", "South America", 212559417, 8515767.0),
  46.     Country("Russia", "Europe", 145934462, 17098242.0),
  47.     Country("Germany", "Europe", 83783942, 357022.0),
  48.     Country("Japan", "Asia", 126476461, 377973.0),
  49.     Country("United Kingdom", "Europe", 67886011, 242495.0)
  50. )
  51.  
  52. fun main() {
  53.         val europeanCountries = countries.filter { it.continent == "Europe" }
  54.         println("Европейский страны:")
  55.         europeanCountries.forEach { country -> println(country.toString()) }
  56.  
  57.         println("\n\nЕсть ли в европе страны с население, более 50м человек: ${if (europeanCountries.any { it.population > 50000000 }) "Да" else "Нет"}")
  58.  
  59.         val largeCountries = countries.filter { it.area > 500000.0 }
  60.         println("\n\nСтраны с площадью более 500 тыс км^2:")
  61.         largeCountries.forEach { country -> println(country.toString()) }
  62.  
  63.         println("\n\nВсе ли Большие страны с населением меньше 100 млн: ${if (largeCountries.all { it.population < 100000000 }) "Да" else "Нет"}")
  64. }
  65.  
  66. package murad
  67.  
  68. data class SportsActivity(val participantName: String, val sportType: String, val duration: Int)
  69.  
  70. val activities = sequenceOf(
  71.     SportsActivity("Alice", "Running", 30),
  72.     SportsActivity("Pavel", "Running", 40),
  73.     SportsActivity("Bob", "Cycling", 45),
  74.     SportsActivity("Charlie", "Swimming", 60),
  75.     SportsActivity("Dave", "Gym", 75),
  76.     SportsActivity("Eve", "Yoga", 45),
  77.     SportsActivity("Frank", "Soccer", 90),
  78.     SportsActivity("Grace", "Tennis", 60),
  79.     SportsActivity("Hannah", "Basketball", 90),
  80.     SportsActivity("Ivan", "Hiking", 120),
  81.     SportsActivity("Jane", "Golf", 90)
  82. )
  83.  
  84. fun main() {
  85.     val activitiesString = activities.map { "Участник: ${it.participantName}, Вид спорта: ${it.sportType}, Длительность: ${it.duration} мин." }
  86.     println("Исходные активности:")
  87.     activitiesString.forEach { activity -> println(activity) }
  88.  
  89.     val activitiesBySportType = activities.groupBy { it.sportType }
  90.     println("\nАктивности, сгруппированные по виду спорта:")
  91.     activitiesBySportType.forEach { (sportType, activities) ->
  92.         println("$sportType: ${activities.map { "Участник: ${it.participantName}, Вид спорта: ${it.sportType}, Длительность: ${it.duration} мин." }.joinToString(", ")}")
  93.     }
  94.  
  95.     val averageDurationBySportType = activitiesBySportType.map { (sportType, activities) ->
  96.         val averageDuration = activities.map { it.duration }.average().toInt()
  97.         "Вид спорта: $sportType, Средняя длительность: $averageDuration мин."
  98.     }.asSequence()
  99.     println("\nСредняя длительность активности для каждого вида спорта:")
  100.     averageDurationBySportType.forEach { println(it) }
  101. }
  102.  
  103.  
  104. package murad
  105.  
  106. data class Student(val name: String, val age: Int, val averageGrade: Double, val missedClasses: Int)
  107. val students = sequenceOf(
  108.     Student("Алексей", 20, 85.0, 2),
  109.     Student("Борис", 21, 88.0, 0),
  110.     Student("Василий", 19, 90.0, 3),
  111.     Student("Григорий", 22, 87.0, 1),
  112.     Student("Дмитрий", 20, 89.0, 0),
  113.     Student("Евгений", 21, 91.0, 2),
  114.     Student("Федор", 19, 92.0, 1),
  115.     Student("Георгий", 22, 88.0, 3),
  116.     Student("Иван", 20, 90.0, 0),
  117.     Student("Константин", 21, 89.0, 2)
  118. )
  119. fun main() {
  120.  
  121.  
  122.     val sortedStudents = students.sortedByDescending { it.averageGrade }
  123.     println("Студенты отсортированы по среднему баллу в порядке убывания:")
  124.     sortedStudents.forEach { println("${it.name}: ${it.averageGrade}") }
  125.  
  126.     val averageAge = sortedStudents.map { it.age }.average()
  127.     println("Средний возраст студентов: $averageAge")
  128.  
  129.     val studentsOver20 = sortedStudents.takeWhile { it.age > 20 }
  130.     println("Студенты старше 20 лет:")
  131.     studentsOver20.forEach { println("${it.name}: ${it.age}") }
  132.  
  133.     val bestStudent = sortedStudents.find { it.averageGrade == sortedStudents.maxOf { student -> student.averageGrade } }
  134.     println("Студент с максимальным средним баллом: ${bestStudent?.name} с средним баллом ${bestStudent?.averageGrade}")
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement