-
Notifications
You must be signed in to change notification settings - Fork 1
/
palindrome.jl
executable file
·40 lines (38 loc) · 982 Bytes
/
palindrome.jl
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
function countFreqs(s)
freqDict = Dict()
for c in s
freqDict[c] = get(freqDict, c, 0) + 1
end
return freqDict
end
function buildPalindrome(evens, odds)
s = ""
for e in evens
for i in [1:div(e[2], 2)]
s = string(s, e[1])
end
end
single = ""
for e in odds
if e[2] > 1
for i in [1:div(e[2], 2)]
s = string(s, e[1])
end
else
single = e[1]
end
end
return string (s, single, reverse(s))
end
if ARGS[1] == reverse(ARGS[1])
println("$(ARGS[1]) is a palindrome")
else
freqDict = countFreqs(ARGS[1])
oddsDict = filter((e1, e2) -> e2 % 2 != 0, freqDict)
if length(oddsDict) > 1
println("$(ARGS[1]) cannot be made into a palindrome")
else
evensDict = filter((e1, e2) -> e2 % 2 == 0, freqDict)
println("A palindromic form of $(ARGS[1]) is $(buildPalindrome(evensDict, oddsDict))")
end
end