-
Notifications
You must be signed in to change notification settings - Fork 1
/
palindrome.dart
executable file
·64 lines (56 loc) · 1.35 KB
/
palindrome.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
import 'dart:io';
var palindrome = "";
var freq = new Map();
Map countLetters(String string) {
for (var letter in string.split('')) {
if (freq.containsKey(letter)) {
++freq[letter];
} else {
freq[letter] = 1;
}
}
return freq;
}
Bool isPalindrome(String string) {
return (string == string.split('').reversed.join(''))
? true
: false;
}
Bool canBePalindrome(Map freq) {
var numberOfOdds = 0;
for (var amount in freq.values) {
if (amount % 2 != 0) {
++numberOfOdds;
}
}
return (numberOfOdds <= 1)
? true
: false;
}
void findOdd(Symbol k, Num v) {
if (v % 2 != 0) {
palindrome = palindrome + k.toString();
freq[k] -= 1;
}
}
String combineParts(Symbol k, Num v) {
palindrome = palindrome + (k * (v / 2).floor());
palindrome = (k * (v / 2).floor()) + palindrome;
}
String createPalindrome(Map freq) {
freq.forEach((k, v) => findOdd(k, v));
freq.forEach((k, v) => combineParts(k, v));
return palindrome;
}
void main() {
stdout.writeln("Enter a word or phrase");
String input = stdin.readLineSync();
freq = countLetters(input);
if (isPalindrome(input)) {
print("$input is already a palindrome.");
} else if (!canBePalindrome(freq)) {
print("$input cannot be made into a palindrome.");
} else {
print("Palindrome of $input: ${createPalindrome(freq)}");
}
}