Last active
October 25, 2024 19:46
-
-
Save PlugFox/cf41859c3c03fb8ee4ae1e8fd1de81f1 to your computer and use it in GitHub Desktop.
Сортировка чисел от 10 до 100 по их русскому текстовому представлению
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Сортировка чисел от 10 до 100 по их русскому текстовому представлению | |
* https://gist.github.com/PlugFox/cf41859c3c03fb8ee4ae1e8fd1de81f1 | |
* https://dartpad.dev?id=cf41859c3c03fb8ee4ae1e8fd1de81f1 | |
* Mike Matiunin <[email protected]>, 25 October 2024 | |
*/ | |
const units = [ | |
'ноль', | |
'один', | |
'два', | |
'три', | |
'четыре', | |
'пять', | |
'шесть', | |
'семь', | |
'восемь', | |
'девять', | |
]; | |
const tens = [ | |
'', // 0..9 - покрываются units | |
'десять', | |
'двадцать', | |
'тридцать', | |
'сорок', | |
'пятьдесят', | |
'шестьдесят', | |
'семьдесят', | |
'восемьдесят', | |
'девяносто', | |
]; | |
// Преобразование числа в текст | |
String n2w(int number) => switch (number) { | |
<= 9 => units[number], | |
11 => 'одиннадцать', | |
12 => 'двенадцать', | |
13 => 'тринадцать', | |
14 => 'четырнадцать', | |
15 => 'пятнадцать', | |
16 => 'шестнадцать', | |
17 => 'семнадцать', | |
18 => 'восемнадцать', | |
19 => 'девятнадцать', | |
< 100 when number % 10 == 0 => tens[number ~/ 10], | |
< 100 => '${tens[number ~/ 10]} ${units[number % 10]}', | |
100 => 'сто', | |
_ => 'число вне диапазона', | |
}; | |
void main() => print([ | |
for (int i = 0; i <= 100; i++) (i, n2w(i)), | |
]..sort((a, b) => a.$2.compareTo(b.$2))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment