-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome_number_test.dart
82 lines (66 loc) · 1.93 KB
/
palindrome_number_test.dart
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import 'package:test/test.dart';
bool palindromeNumber(int value) {
if (value.isNegative) return false;
var tens = 10;
while (tens < value) tens *= 10;
tens = tens ~/ 10;
while (value > 10) {
var lastDigit = value % 10;
var firstDigit = value ~/ tens;
if (lastDigit != firstDigit) return false;
value -= firstDigit * tens;
value = value ~/ 10;
tens = tens ~/ 100;
}
return true;
}
bool palindromeNumberList(int value) {
if (value.isNegative) return false;
final list = _convert(value);
for (int i = 0; i < (list.length ~/ 2); i++) {
if (list[i] != list[list.length - 1 - i]) return false;
}
return true;
}
List<int> _convert(int value) {
final list = <int>[];
while (value > 0) {
list.add(value % 10);
value = value ~/ 10;
}
return list;
}
bool palindromeNumberReverted(int value) {
if (value.isNegative) return false;
if (value == 0) return true;
if (value % 10 == 0) return false;
var reverted = 0, iter = value;
while (iter > 0) {
reverted = reverted * 10;
reverted += iter % 10;
iter = iter ~/ 10;
}
return reverted == value;
}
bool palindromeNumberStrings(int value) => '$value'.isPalindrome;
extension on String {
// There are obviously better ways to code this, but hey, I already did
// something similar for a different challenge: valid_palindrome_test.
bool get isPalindrome => this == split('').reversed.join('');
}
void main() {
group('palindromeNumber', () {
test('some examples...', () {
expect(palindromeNumber(-454), false);
expect(palindromeNumber(454), true);
expect(palindromeNumber(0), true);
expect(palindromeNumber(1), true);
expect(palindromeNumber(5), true);
expect(palindromeNumber(9292929), true);
expect(palindromeNumber(1221), true);
expect(palindromeNumber(141141), true);
expect(palindromeNumber(141), true);
expect(palindromeNumber(454), true);
});
});
}