diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..20522cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,346 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ +# ASP.NET Core default setup: bower directory is configured as wwwroot/lib/ and bower restore is true +**/wwwroot/lib/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- Backup*.rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ \ No newline at end of file diff --git a/MorseCode.sln b/MorseCode.sln new file mode 100644 index 0000000..a7416d6 --- /dev/null +++ b/MorseCode.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.421 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MorseCode", "MorseCode\MorseCode.csproj", "{57067FA1-7108-4AAA-AC18-450BE9D3C037}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MorseCodeLibrary", "MorseCodeLibrary\MorseCodeLibrary.csproj", "{CD3E0A58-5339-42EE-8369-AF24066DBA77}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MorseCodeTest", "MorseCodeTest\MorseCodeTest.csproj", "{9176F838-9B9B-4C9E-A6C3-70344CCC849C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {57067FA1-7108-4AAA-AC18-450BE9D3C037}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57067FA1-7108-4AAA-AC18-450BE9D3C037}.Debug|Any CPU.Build.0 = Debug|Any CPU + {57067FA1-7108-4AAA-AC18-450BE9D3C037}.Release|Any CPU.ActiveCfg = Release|Any CPU + {57067FA1-7108-4AAA-AC18-450BE9D3C037}.Release|Any CPU.Build.0 = Release|Any CPU + {CD3E0A58-5339-42EE-8369-AF24066DBA77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD3E0A58-5339-42EE-8369-AF24066DBA77}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD3E0A58-5339-42EE-8369-AF24066DBA77}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD3E0A58-5339-42EE-8369-AF24066DBA77}.Release|Any CPU.Build.0 = Release|Any CPU + {9176F838-9B9B-4C9E-A6C3-70344CCC849C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9176F838-9B9B-4C9E-A6C3-70344CCC849C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9176F838-9B9B-4C9E-A6C3-70344CCC849C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9176F838-9B9B-4C9E-A6C3-70344CCC849C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {51EA4905-EB17-43E2-9F91-0C9B5B7A47CE} + EndGlobalSection +EndGlobal diff --git a/MorseCode/MorseCode.csproj b/MorseCode/MorseCode.csproj new file mode 100644 index 0000000..935faad --- /dev/null +++ b/MorseCode/MorseCode.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp2.2 + + + + + + + diff --git a/MorseCode/MorseCodeDriver.cs b/MorseCode/MorseCodeDriver.cs new file mode 100644 index 0000000..7711818 --- /dev/null +++ b/MorseCode/MorseCodeDriver.cs @@ -0,0 +1,32 @@ +using MorseCodeLibrary; +using System; +using System.IO; + +namespace MorseCode { + + class MorseCodeDriver { + + static void Main(string[] args) { + if (ValidArgs(args)) { + string filePath = args[0]; + ITranslator translator = new MorseCodeTranslator(); + Console.WriteLine("Translating file: " + filePath); + Console.WriteLine("Any invalid characters will be denoted by '!'\n"); + Console.WriteLine(translator.Translate(filePath)); + } + } + + private static bool ValidArgs(string[] args) { + bool valid = true; + if (args.Length != 1) { + Console.WriteLine("Error: Must have only one argument specifying a valid file path"); + valid = false; + } else if (!File.Exists(args[0])) { + Console.WriteLine("Error: Could not read file " + args[0]); + valid = false; + } + + return valid; + } + } +} diff --git a/MorseCode/Properties/launchSettings.json b/MorseCode/Properties/launchSettings.json new file mode 100644 index 0000000..19e8119 --- /dev/null +++ b/MorseCode/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "MorseCode": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/MorseCodeLibrary/ITranslator.cs b/MorseCodeLibrary/ITranslator.cs new file mode 100644 index 0000000..0942806 --- /dev/null +++ b/MorseCodeLibrary/ITranslator.cs @@ -0,0 +1,19 @@ +namespace MorseCodeLibrary { + + public interface ITranslator { + + /// + /// Translates a file with the object's method of translation. + /// + /// The file to translate + /// A string containing the translation + string Translate(string inputFilePath); + + /// + /// Translates a string with the object's method of translation. + /// + /// The line to translate + /// The translated line + string TranslateLine(string line); + } +} diff --git a/MorseCodeLibrary/MorseCodeLibrary.csproj b/MorseCodeLibrary/MorseCodeLibrary.csproj new file mode 100644 index 0000000..3154d95 --- /dev/null +++ b/MorseCodeLibrary/MorseCodeLibrary.csproj @@ -0,0 +1,7 @@ + + + + netcoreapp2.2 + + + diff --git a/MorseCodeLibrary/MorseCodeTranslator.cs b/MorseCodeLibrary/MorseCodeTranslator.cs new file mode 100644 index 0000000..a472dad --- /dev/null +++ b/MorseCodeLibrary/MorseCodeTranslator.cs @@ -0,0 +1,86 @@ +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace MorseCodeLibrary { + + public class MorseCodeTranslator : ITranslator { + private IDictionary dictionary = new Dictionary() { + { ".-", "a" } + ,{ "-...", "b" } + ,{ "-.-.", "c" } + ,{ "-..", "d" } + ,{ ".", "e" } + ,{ "..-.", "f" } + ,{ "--.", "g" } + ,{ "....", "h" } + ,{ "..", "i" } + ,{ ".---", "j" } + ,{ "-.-", "k" } + ,{ ".-..", "l" } + ,{ "--", "m" } + ,{ "-.", "n" } + ,{ "---", "o" } + ,{ ".--.", "p" } + ,{ "--.-", "q" } + ,{ ".-.", "r" } + ,{ "...", "s" } + ,{ "-", "t" } + ,{ "..-", "u" } + ,{ "...-", "v" } + ,{ ".--", "w" } + ,{ "-..-", "x" } + ,{ "-.--", "y" } + ,{ "--..", "z" } + ,{ ".----", "1" } + ,{ "..---", "2" } + ,{ "...--", "3" } + ,{ "....-", "4" } + ,{ ".....", "5" } + ,{ "-....", "6" } + ,{ "--...", "7" } + ,{ "---..", "8" } + ,{ "----.", "9" } + ,{ "-----", "0" } + ,{ ",", " "} + }; + + /// + /// Translates a file containing morse code. + /// + /// The file to translate + /// A string containing the translation + public string Translate(string inputFilePath) { + StringBuilder sb = new StringBuilder(); + + var lines = File.ReadLines(inputFilePath); + foreach (var line in lines) { + string translatedLine = TranslateLine(line); + sb.Append(translatedLine); + sb.AppendLine(); + } + + return sb.ToString(); + } + + /// + /// Returns the translation of an individual line of morse code. + /// + /// The line to translate + /// The translated line + public string TranslateLine(string line) { + StringBuilder sb = new StringBuilder(); + + // Replace two breaks in a row with a comma in between, signifying a space + string formattedLine = line.Trim().Replace("||||", "||,||"); + + string[] letters = formattedLine.Split("||"); + foreach (string letter in letters) { + string value = dictionary.ContainsKey(letter) ? dictionary[letter] : "!"; + sb.Append(value); + } + + return sb.ToString(); + } + } +} diff --git a/MorseCodeTest/MorseCodeTest.cs b/MorseCodeTest/MorseCodeTest.cs new file mode 100644 index 0000000..5a6b88d --- /dev/null +++ b/MorseCodeTest/MorseCodeTest.cs @@ -0,0 +1,91 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MorseCodeLibrary; +using System; + +namespace MorseCodeTest { + + [TestClass] + public class MorseCodeTest { + static ITranslator translator; + + [AssemblyInitialize] + public static void AssemblyInit(TestContext context) { + translator = new MorseCodeTranslator(); + } + + [TestMethod] + public void Test_Alphabet_A_Through_H() { + string input = ".-||-...||-.-.||-..||.||..-.||--.||...."; + string output = translator.TranslateLine(input); + + Assert.AreEqual("abcdefgh", output); + } + + [TestMethod] + public void Test_Alphabet_I_Through_P() { + string input = "..||.---||-.-||.-..||--||-.||---||.--."; + string output = translator.TranslateLine(input); + + Assert.AreEqual("ijklmnop", output); + } + + [TestMethod] + public void Test_Alphabet_Q_Through_Z() { + string input = "--.-||.-.||...||-||..-||...-||.--||-..-||-.--||--.."; + string output = translator.TranslateLine(input); + + Assert.AreEqual("qrstuvwxyz", output); + } + + [TestMethod] + public void Test_Numerals_One_Through_Five() { + string input = ".----||..---||...--||....-||....."; + string output = translator.TranslateLine(input); + + Assert.AreEqual("12345", output); + } + + [TestMethod] + public void Test_Numerals_Six_Through_Zero() { + string input = "-....||--...||---..||----.||-----"; + string output = translator.TranslateLine(input); + + Assert.AreEqual("67890", output); + } + + [TestMethod] + public void Test_String_Dog() { + string input = "-..||---||--."; + string output = translator.TranslateLine(input); + + Assert.AreEqual("dog", output); + } + + [TestMethod] + public void Test_String_Hello_World() { + string input = "....||.||.-..||.-..||---||||.--||---||.-.||.-..||-.."; + string output = translator.TranslateLine(input); + Console.WriteLine(output); + + Assert.AreEqual("hello world", output); + } + + [TestMethod] + public void Test_String_Non_Morse() { + string input = "This is not morse code"; + string output = translator.TranslateLine(input); + Console.WriteLine(output); + + Assert.AreEqual("!", output); + } + + [TestMethod] + public void Test_String_Invalid_Morse() { + string input = "....--.-.||-..||---||--.|| fs"; + string output = translator.TranslateLine(input); + Console.WriteLine(output); + + Assert.AreEqual("!dog!", output); + } + } +} diff --git a/MorseCodeTest/MorseCodeTest.csproj b/MorseCodeTest/MorseCodeTest.csproj new file mode 100644 index 0000000..54b037b --- /dev/null +++ b/MorseCodeTest/MorseCodeTest.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp2.2 + + false + + + + + + + + + + + + + diff --git a/README.md b/README.md index 605e027..709fdec 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,45 @@ +# Morse Code + +By [Joshua Zinkovsky](mailto:joshua.zky@gmail.com) + +## Instructions + +1. Navigate to [repo](https://github.com/Josh961/MorseCode.git) +2. Clone locally using + `git clone https://github.com/Josh961/MorseCode.git` +3. Open `MorseCode.sln` +4. Right click the `MorseCode` project -> properties +5. Click debug and enter the path of the input file as an argument - application will not run otherwise +6. `ctrl+f5` to run the application +7. Application should display results and any errors in translating a character + +## Troubleshooting + +The application was cloned and tested on multiple machines and should work fine. However, if there +are any problems, please see the following points: + +1. Application is running on .NET Core 2.2 and user must have the SDK installed +2. If any build errors occur, try rebuilding the entire solution + +## Discussion + +I used the following technologies: C# and MSTest + +The solution is split up into three different projects. +1. `MorseCode` contains the driver program. +2. `MorseCodeLibrary` contains the `ITranslator` interface to implement future possible methods of translation. +3. `MorseCodeTest` contains the unit test file. + +## Requirements + +The program accepts a flat file as an input. It reads the file using the proper morse code characters, and outputs their +translation in English. + +#### Tests + +Run tests within visual studio using shortcut `ctrl+r a` + + Morse Code ==========