-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpalindrome.nim
executable file
·49 lines (42 loc) · 1.21 KB
/
palindrome.nim
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
import os
import tables
import strutils
# Graciously stolen from Nim's Wikipedia page
proc reverse(s: string): string =
result = ""
for i in countdown(high(s), 0):
result.add s[i]
proc buildPalindrome(s: string, odds: CountTable[char], evens: CountTable[char]) =
var
buildStr = ""
single = ""
for k,v in evens:
buildStr.add repeat(k, v div 2)
for k,v in odds:
if v > 1:
buildStr.add repeat(k, v div 2)
else:
single.add k
buildStr = buildStr & single & reverse(buildStr)
echo "A palindromic form of ", s, " is ", buildStr
proc checkForIfPossible(s: string) =
var counts = initCountTable[char]()
for c in s:
counts.inc(c)
var
oddCounts = initCountTable[char]()
evenCounts = initCountTable[char]()
for k,v in counts.pairs:
if v mod 2 != 0:
oddCounts.inc(k, v)
else:
evenCounts.inc(k, v)
if oddCounts.len > 1:
echo s, " cannot be made into a palindrome"
else:
buildPalindrome(s, oddCounts, evenCounts)
let input = paramStr(1)
if input == reverse(input):
echo input, " is a palindrome"
else:
checkForIfPossible(input)