/
ch-2.kts
executable file
·50 lines (45 loc) · 1.34 KB
/
ch-2.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env kotlin
/*
* You are given a list of passenger details in the form “9999999999A1122”,
* where 9 denotes the phone number, A the gender, 1 the age and 2 the seat
* number. Write a script to return the count of all senior citizens (age >=
* 60).
*/
data class Passenger(
val phone: String,
val gender: String,
val age: Int,
val seat: String,
) {
companion object {
private val DETAILS_REGEX = Regex(
"""
(?<phone>\d+)
(?<gender>\p{Alpha})
(?<age>\d{2})
(?<seat>\d{2})
""",
RegexOption.COMMENTS,
)
operator fun invoke(details: String): Passenger {
val match = DETAILS_REGEX.matchEntire(details)
?: throw IllegalArgumentException("Invalid details $details")
val (phone, gender, age, seat) = match.destructured
return Passenger(phone, gender, age.toInt(), seat)
}
}
}
fun passengers(vararg details: String) =
details.map { Passenger(it) }
fun seniorCitizens(passengers: List<Passenger>) =
passengers.filter { it.age >= 60 }
println(
seniorCitizens(
passengers("7868190130M7522", "5303914400F9211", "9273338290F4010"),
).size,
)
println(
seniorCitizens(
passengers("1313579440F2036", "2921522980M5644"),
).size,
)