-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathDecimal_To_Any.dart
More file actions
71 lines (63 loc) · 1.67 KB
/
Decimal_To_Any.dart
File metadata and controls
71 lines (63 loc) · 1.67 KB
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
//Convert a Decimal Number to Any Other Representation
//https://en.wikipedia.org/wiki/Positional_notation#Base_conversion
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';
String decimalToAny(int value, int base) {
var ALPHABET_VALUES = {
10: 'A',
11: 'B',
12: 'C',
13: 'D',
14: 'E',
15: 'F',
16: 'G',
17: 'H',
18: 'I',
19: 'J',
20: 'K',
21: 'L',
22: 'M',
23: 'N',
24: 'O',
25: 'P',
26: 'Q',
27: 'R',
28: 'S',
29: 'T',
30: 'U',
31: 'V',
32: 'W',
33: 'X',
34: 'Y',
35: 'Z',
};
if (value == 0) return "0";
if (base < 2 || base > 36) throw FormatException("Base not supported!");
bool negative = false;
if (value < 0) {
negative = true;
value *= -1;
}
String output = "";
while (value > 0) {
int remainder = value % base;
value = value ~/ base;
output =
(remainder < 10
? remainder.toString()
: ALPHABET_VALUES[remainder] ?? '0') +
output;
}
return negative ? '-' + output : output;
}
void main() {
test('test case 1', () => expect(decimalToAny(0, 2), '0'));
test('test case 2', () => expect(decimalToAny(5, 4), '11'));
test('test case 3', () => expect(decimalToAny(20, 3), '202'));
test('test case 4', () => expect(decimalToAny(-58, 16), '-3A'));
test('test case 5', () => expect(decimalToAny(243, 17), 'E5'));
test('test case 6', () => expect(decimalToAny(34923, 36), 'QY3'));
test('test case 7', () => expect(decimalToAny(10, 11), 'A'));
test('test case 8', () => expect(decimalToAny(-16, 16), '-10'));
test('test case 9', () => expect(decimalToAny(36, 36), '10'));
}