No matter how much I re-read and try to solve this question I simply cannot wrap my head around it. I don't understand how to make my code translate the needed text without hardcoding the entire thing.
Here is the question:
Rosetta Stone Programming challenge description: The Rosetta stone is an ancient decree written into stone that was used by archeologists to translate between Greek and Ancient Egyptian languages.
Write a program to decode messages using a Rosetta for translation. The "Rosetta" will be a series of character sets paired with English words or characters. If you translate the encrypted message correctly, you will find the hidden message.
HINT: Spaces that are not part of a fragment must be preserved
Input: Multiple lines of input will be given. The first line of input will be the message to translate to English.
All other lines of input will be the Rosetta needed to translate. The fragments of each pair will be separated by a pipe character, one pair per line of input.
For example:
Hola Mundo
Mundo|World
Lunes|Monday
Hola|Hello
Translates:
Hola Mundo
into:
Hello World
Output: The translated text, which will be a real English phrase
Test 1
Test Input
Hola Mundo
Mundo|World
Lunes|Monday
Hola|Hello
Expected Output
Hello World
Test 2 Test Input
mieux vaut prévenir que guérir
merci|thank you
que|than
malade|sick
mieux|better
guérir|to heal
chien|dog
vaut prévenir|to prevent
beurre|butter
s'il vous plaît|please
Expected Output
better to prevent than to heal
Test 3 Test Input
5748494348 574159 544845 57494E44 424C4F5753
41|A
42|B
43|C
44|D
45|E
46|F
47|G
48|H
49|I
4A|J
4B|K
4C|L
4D|M
4E|N
4F|O
50|P
51|Q
52|R
53|S
54|T
55|U
56|V
57|W
58|X
59|Y
5A|Z
Expected Output
WHICH WAY THE WIND BLOWS
Here is my current code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
public static class Program {
static void Main()
{
using (StreamReader reader = new StreamReader(Console.OpenStandardInput()))
while (!reader.EndOfStream)
{
string input = reader.ReadLine();
string output = RosettaStoneTranslate(input);
Console.WriteLine(output);
}
}
static string RosettaStoneTranslate(string input)
{
string[] lines = input.Split('\n');
string message =lines[0];
//parse Rosetta Stone pairs.
Dictionary<string, string> rosettaDict = new Dictionary<string, string>();
for (int i = 1; i < lines.Length - 1; i++)
{
string[] pair = lines[i].Trim().Split('|');
rosettaDict[pair[0]] = pair[i];
}
string[] fragments = message.Split(' ');
List<string> translatedFragments = new List<string>();
foreach (string fragment in fragments)
{
if (rosettaDict.TryGetValue(fragment, out string translation))
{
translatedFragments.Add(translation);
}
else
{
translatedFragments.Add(fragment);
}
}
//reconstruct translated message.
string translatedMessage = string.Join(" ", translatedFragments);
return translatedMessage;
}
}
Please could some explain to me the process of how to find a solution!
You have a few issues. One, you need to build a list of lines before calling
RosettaStoneTranslate.Here's the
Mainmethod that does that:Then there's a small change to the start of
RosettaStoneTranslateas we're sending in the full list instead of a single string.And finally, you had a small bug in the
forloop. You neededlines.Lengthrather thanlines.Length - 1.It now works.
It is important to note that "Test 3" relies on pairing characters to do the replacement.
The whole groups, such as
544845, do not appear in your dictionary, and a simple search as replace approach likelines.Skip(1).Select(x => x.Split('|')).Aggregate(lines.First(), (a, x) => a.Replace(x[0], x[1]));does not honour the character boundaries. The44in the middle of544845shouldn't be replaced.