Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added challenges solution #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 59 additions & 78 deletions 6-dictionaries/Morse Decoder/Morse.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// File Name: Morse.swift
// Author: Alex DiStasi
// Purpose: Encode and decode morse code messages


// Task Group: Setting Up

var englishText = "this is a secret message"
var secretMessage = ".... --- .-- -.. -.-- .--. .- .-. - -. . .-."
englishText=englishText.lowercased()
var secretMessage = ".... --- .-- -.. -.-- .--. .- .-. - -. . .-. .-.-.- .. .- -- -..-"

// Create a dictionary with letters as keys and morse code counterparts as values
var lettersToMorse: [String: String] = [
// Add your code below 🤫
var letterToMorse:[String:String]=[
"a": ".-",
"b": "-...",
"c": "-.-.",
Expand Down Expand Up @@ -37,89 +31,76 @@ var lettersToMorse: [String: String] = [
"y": "-.--",
"z": "--..",
".": ".-.-.-",
"!": "-.-.--",
"?": "..--..",
",": "--..--"
",": "--..--",
"!": "-.-.--"
]


// Encoding a Message

// Empty string that will store a morse code message
var morseText = ""

// Loop through each character in englishText
for element in englishText {
// Check if the value exists in the dictionary
if let morseChar = lettersToMorse["\(element)"]{
// Append the letter to morseText
// Append a space to morseText because each letter is separated by a single space in morse code.
morseText += morseChar + " "
var morseText=""
for element in englishText{
if let morseChar = letterToMorse["\(element)"]{
morseText+=morseChar+" "
}
else{
// Append 3 spaces to morseText because each word in a morse code message is separated by three spaces
morseText+=" "
}
morseText+=" "
}
}
print (morseText)


// Decoding a Message

var decodedMessage = ""
var currMorse = ""
// morseCodeArray will store individual morse code letters from secretMessage
var morseCodeArray = [String]()

// Loop through each character in secretMorse
for char in secretMessage {
// Check if char is not a space
if char != " " {
// Append the value of char to currMorse
print(morseText)
var decodedMessage=""
var morseCodeArray=[String]()
var currMorse=""
for char in secretMessage{
if char != " "{
currMorse.append(char)
}

// If the value of char is a space character, the code in the else statement will be executed
else {
// Use a switch statement to assemble characters into individual morse code letters
switch currMorse {
case "":
currMorse += " "
}
else{
switch currMorse{
case "":
currMorse+=" "
case " ":
// Append a space to morseCodeArray
morseCodeArray.append(" ")
currMorse = ""
default:
// Append the morse code letter to the array
currMorse=""
default:
morseCodeArray.append(currMorse)
// Reset the value of currMorse
currMorse = ""
currMorse=""
}

}
}
// Append the final value of currMorse to morseCodeArray
morseCodeArray.append(currMorse)

// Create an empty dictionary. This will hold morse code values as Keys and their english counter parts as Values
var morseToLetter: [String: String] = [:]

// Iterate through letterToMorse dictionary, add the keys as values and the values as keys to the morseToLetter dictionary
for (letter,morseChar) in lettersToMorse{
print(morseCodeArray)
var morseToLetter:[String:String]=[:]
for (letter,morseChar) in letterToMorse{
morseToLetter[morseChar]=letter
}

// Go through each element in morseCodeArray and find the text value via the morseToLetter dictionary
for morseValue in morseCodeArray {
// Check if the value exists in the morseToLetter dictionary
if let letterChar = morseToLetter[morseValue]{
//Append the values to decodedMessage
decodedMessage += letterChar
for morseValue in morseCodeArray{
if let letterChar=morseToLetter[morseValue]{
decodedMessage+=letterChar
}
else{
decodedMessage+=" "
}
//if it's not in the dictionary, it's probably a space
else {
// Add a space to decodedMessage
decodedMessage += " "
}
print(decodedMessage)
//flag to check if the letter is first letter of the sentence
var flag=1
var newDecodedMessage=""
for char in decodedMessage{
if char=="."||char=="?"||char=="!"{
flag=1
newDecodedMessage+="\(char)"
continue
}
if flag==1{
if char != " "{
print(char)
newDecodedMessage+=char.uppercased()
flag=0
}
else{
newDecodedMessage+=" "
}
}
else{
newDecodedMessage+="\(char)";
}
}
print (decodedMessage)
print(newDecodedMessage)