Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package murad
- data class Book(val title: String, val author: String)
- val library = sequenceOf(
- Book("The Great Gatsby", "F. Scott Fitzgerald"),
- Book("To Kill a Mockingbird", "Harper Lee"),
- Book("1984", "George Orwell"),
- Book("The Catcher in the Rye", "J.D. Salinger"),
- Book("The Lord of the Rings", "J.R.R. Tolkien"),
- Book("The Hobbit", "J.R.R. Tolkien"),
- Book("The Da Vinci Code", "Dan Brown"),
- Book("The Girl with the Dragon Tattoo", "Stieg Larsson"),
- Book("The Hunger Games", "Suzanne Collins"),
- Book("The Fault in Our Stars", "John Green")
- )
- fun main() {
- val uniqueAuthors = library.map { it.author }.toSet()
- println("Уникальные авторы:")
- uniqueAuthors.forEach{author -> println(author)}
- val author = "J.R.R. Tolkien"
- val booksByAuthor = library.filter { it.author == author }.toList()
- println("Книги от $author")
- booksByAuthor.forEach{book -> println(book)}
- }
- package murad
- data class Country(val name: String, val continent: String, val population: Long, val area: Double) {
- override fun toString(): String {
- return "Название: $name, Континент: $continent, Население: $population, Площадь: $area км^2"
- }
- }
- val countries = sequenceOf(
- Country("United States", "North America", 331002651, 9525067.0),
- Country("Canada", "North America", 37742154, 9984670.0),
- Country("China", "Asia", 1444216107, 9596961.0),
- Country("India", "Asia", 1380004385, 3287263.0),
- Country("Australia", "Australia/Oceania", 25696886, 7692024.0),
- Country("Brazil", "South America", 212559417, 8515767.0),
- Country("Russia", "Europe", 145934462, 17098242.0),
- Country("Germany", "Europe", 83783942, 357022.0),
- Country("Japan", "Asia", 126476461, 377973.0),
- Country("United Kingdom", "Europe", 67886011, 242495.0)
- )
- fun main() {
- val europeanCountries = countries.filter { it.continent == "Europe" }
- println("Европейский страны:")
- europeanCountries.forEach { country -> println(country.toString()) }
- println("\n\nЕсть ли в европе страны с население, более 50м человек: ${if (europeanCountries.any { it.population > 50000000 }) "Да" else "Нет"}")
- val largeCountries = countries.filter { it.area > 500000.0 }
- println("\n\nСтраны с площадью более 500 тыс км^2:")
- largeCountries.forEach { country -> println(country.toString()) }
- println("\n\nВсе ли Большие страны с населением меньше 100 млн: ${if (largeCountries.all { it.population < 100000000 }) "Да" else "Нет"}")
- }
- package murad
- data class SportsActivity(val participantName: String, val sportType: String, val duration: Int)
- val activities = sequenceOf(
- SportsActivity("Alice", "Running", 30),
- SportsActivity("Pavel", "Running", 40),
- SportsActivity("Bob", "Cycling", 45),
- SportsActivity("Charlie", "Swimming", 60),
- SportsActivity("Dave", "Gym", 75),
- SportsActivity("Eve", "Yoga", 45),
- SportsActivity("Frank", "Soccer", 90),
- SportsActivity("Grace", "Tennis", 60),
- SportsActivity("Hannah", "Basketball", 90),
- SportsActivity("Ivan", "Hiking", 120),
- SportsActivity("Jane", "Golf", 90)
- )
- fun main() {
- val activitiesString = activities.map { "Участник: ${it.participantName}, Вид спорта: ${it.sportType}, Длительность: ${it.duration} мин." }
- println("Исходные активности:")
- activitiesString.forEach { activity -> println(activity) }
- val activitiesBySportType = activities.groupBy { it.sportType }
- println("\nАктивности, сгруппированные по виду спорта:")
- activitiesBySportType.forEach { (sportType, activities) ->
- println("$sportType: ${activities.map { "Участник: ${it.participantName}, Вид спорта: ${it.sportType}, Длительность: ${it.duration} мин." }.joinToString(", ")}")
- }
- val averageDurationBySportType = activitiesBySportType.map { (sportType, activities) ->
- val averageDuration = activities.map { it.duration }.average().toInt()
- "Вид спорта: $sportType, Средняя длительность: $averageDuration мин."
- }.asSequence()
- println("\nСредняя длительность активности для каждого вида спорта:")
- averageDurationBySportType.forEach { println(it) }
- }
- package murad
- data class Student(val name: String, val age: Int, val averageGrade: Double, val missedClasses: Int)
- val students = sequenceOf(
- Student("Алексей", 20, 85.0, 2),
- Student("Борис", 21, 88.0, 0),
- Student("Василий", 19, 90.0, 3),
- Student("Григорий", 22, 87.0, 1),
- Student("Дмитрий", 20, 89.0, 0),
- Student("Евгений", 21, 91.0, 2),
- Student("Федор", 19, 92.0, 1),
- Student("Георгий", 22, 88.0, 3),
- Student("Иван", 20, 90.0, 0),
- Student("Константин", 21, 89.0, 2)
- )
- fun main() {
- val sortedStudents = students.sortedByDescending { it.averageGrade }
- println("Студенты отсортированы по среднему баллу в порядке убывания:")
- sortedStudents.forEach { println("${it.name}: ${it.averageGrade}") }
- val averageAge = sortedStudents.map { it.age }.average()
- println("Средний возраст студентов: $averageAge")
- val studentsOver20 = sortedStudents.takeWhile { it.age > 20 }
- println("Студенты старше 20 лет:")
- studentsOver20.forEach { println("${it.name}: ${it.age}") }
- val bestStudent = sortedStudents.find { it.averageGrade == sortedStudents.maxOf { student -> student.averageGrade } }
- println("Студент с максимальным средним баллом: ${bestStudent?.name} с средним баллом ${bestStudent?.averageGrade}")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement