Reputation: 138
I want to write a function to convert a String
into Phonetic Alphabets
For instance: "Hello World"
Should print:
(Hotel Echo Lima Lima Oscar Whiskey Oscar Romeo Lima Delta).
This is what I have done so far. But Xcode gives me an error
Usage of unresolved identifier reduce
and
Usage of unresolved identifier join
Anyone can help me with this as I am new to swift.
func catSomes<A>(xs:[A?]) -> [A] {
return reduce(xs, []) { acc, x in
x.map { acc + [$0] } ?? acc
}
}
let letters = [
"A" : "Alpha", "B" : "Bravo", "C" : "Charlie",
"D" : "Delta", "E" : "Echo", "F" : "Foxtrot",
"G" : "Golf", "H" : "Hotel", "I" : "India",
"J" : "Juliett","K" : "Kilo", "L" : "Lima",
"M" : "Mike", "N" : "November","O" : "Oscar",
"P" : "Papa", "Q" : "Quebec", "R" : "Romeo",
"S" : "Sierra", "T" : "Tango", "U" : "Uniform",
"V" : "Victor", "W" : "Whiskey", "X" : "X-ray",
"Y" : "Yankee", "Z" : "Zulu"]
func nato(str:String) -> String {
return join(" ", catSomes(map(str) { letters[String($0).uppercaseString] }))
}
Upvotes: 1
Views: 755
Reputation: 42977
To get your expected output this should be enough
func nato(str:String) -> String {
return str.characters.map{ letters["\($0)".uppercased()] ?? "" }.joined(separator: " ")
}
I did not understand the use of your catSomes
method.
Upvotes: 0
Reputation: 104
You can use this:
func nato(str:String) -> String {
var newString = ""
for c in str.characters {
c == " " ? newString.append("") : newString.append(letters[String(c).uppercased()]! + " ")
}
return newString
}
Upvotes: 1