Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Exercise https://old.reddit.com/r/dailyprogrammer/comments/cn6gz5/20190807_challenge_380_intermediate_smooshed/
- public static string Smalpha(string Smore)
- {
- string alphabet = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."; //
- string[] morse = alphabet.Split(' '); //Morse alphabet declaration
- bool[] UsedLetters = new bool[morse.Length]; //A bool with all possible letters used.False stands for not used. Default declaration for bool in C# is false. We go through every letter by using the i int data variable in the loop that is in getWord function
- string Output = getWord(Smore, UsedLetters); //Output declaration
- string getWord(string smore, bool[] usedLetters)
- {
- string output = ""; //assigns a value, so I don't get an error
- bool foundletter = false; //Confirmation if the letter has been found, if a letter has not wabeen found, it returns that this path has no good answers
- for (int i = 0; i < morse.Length; i++)
- {
- if (morse[i].Length <= smore.Length && morse[i] == smore.Substring(0, morse[i].Length) && usedLetters[i] == false)
- {
- char letter = (char)(i + 97); //Converts ASCII values to letters
- output = letter.ToString(); // Char -> to string CAN BE IMPROVED
- if (morse[i].Length < smore.Length) //Checks if there are still more smooshed morse code left after the current letter
- {
- bool[] tempUsedLettersClone = (bool[])usedLetters.Clone(); //Creates a temporary array CLONE with the used letter bool array
- tempUsedLettersClone[i] = true; //modifies the current letter as USED
- string temp = getWord(smore.Substring(morse[i].Length), tempUsedLettersClone); // if TRUE, does recursion (repeats the step)
- if (temp != "-404") //Checks if the returned 'letter' is not -404 (stands for 404 letter not found), IF TRUE, adds the letter, and confirms a letter has been found and stopping the loop
- {
- output += temp;
- foundletter = true;
- break;
- }
- }
- else if (morse[i].Length == smore.Length) //Just some protection, for dumbness, when it comes to the last letter, so the upper IF-STATEMENT won't do any mistakes.
- {
- foundletter = true;
- break;
- }
- }
- }
- if (foundletter == false) //Returns -404 string in case no letter has been found for protection saying that this path has no letters that work wit htehsmooshed morse code.
- return "-404";
- else
- return output; //If the upper if-statement was false, returns the curren letter which means everything is OK
- }
- return Output;
- }
Add Comment
Please, Sign In to add comment