Normantas

r/DailyProgrammer Challenge #380 Intermediate Solution

Dec 27th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. //Exercise https://old.reddit.com/r/dailyprogrammer/comments/cn6gz5/20190807_challenge_380_intermediate_smooshed/
  2. public static string Smalpha(string Smore)
  3.         {
  4.             string alphabet = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."; //
  5.             string[] morse = alphabet.Split(' '); //Morse alphabet declaration
  6.             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
  7.             string Output = getWord(Smore, UsedLetters); //Output declaration
  8.             string getWord(string smore, bool[] usedLetters)
  9.             {
  10.                 string output = ""; //assigns a value, so I don't get an error
  11.                 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
  12.                 for (int i = 0; i < morse.Length; i++)
  13.                 {
  14.                     if (morse[i].Length <= smore.Length && morse[i] == smore.Substring(0, morse[i].Length) && usedLetters[i] == false)
  15.                     {
  16.                         char letter = (char)(i + 97); //Converts ASCII values to letters
  17.                         output = letter.ToString(); // Char -> to string CAN BE IMPROVED
  18.                         if (morse[i].Length < smore.Length) //Checks if there are still more smooshed morse code left after the current letter
  19.                         {
  20.                             bool[] tempUsedLettersClone = (bool[])usedLetters.Clone(); //Creates a temporary array CLONE with the used letter bool array
  21.                             tempUsedLettersClone[i] = true; //modifies the current letter as USED
  22.  
  23.                             string temp = getWord(smore.Substring(morse[i].Length), tempUsedLettersClone); // if TRUE, does recursion (repeats the step)
  24.                             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
  25.                             {
  26.                                 output += temp;
  27.                                 foundletter = true;
  28.                                 break;
  29.                             }
  30.  
  31.                         }
  32.                         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.
  33.                         {
  34.                             foundletter = true;
  35.                             break;
  36.                         }
  37.                     }
  38.                 }
  39.                 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.
  40.                     return "-404";
  41.                 else
  42.                     return output; //If the upper if-statement was false, returns the curren letter which means everything is OK
  43.             }
  44.             return Output;
  45.         }
Add Comment
Please, Sign In to add comment