commit d9cd489a0edb4da3dcc81cfe3bb63b90bbcf13e5 Author: BackIsBachus Date: Wed May 5 15:34:09 2021 +0200 Initial commit for WordsSorting diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a8542d --- /dev/null +++ b/.gitignore @@ -0,0 +1,362 @@ +## 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/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# 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 +nunit-*.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/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# 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 + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# 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 +# NuGet Symbol Packages +*.snupkg +# 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/ + +# 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 +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).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/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a3ed8b0 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Words Sorting + +Words Sorting is a little project done as part of a coding test exercise. + +This is a little CLI tool developped in Visual C++ for Windows and that requires Boost 1.76.0 to compile. + +## Features + +This tool takes a list of words in a file as en input, sorts them in alphabetical order and returns them in an `out.txt` file. It is also possible to ask the tool to use a reverse ordering and/or to delete duplicates. + +## Usage + +The usage is very simple you can use this as a command line input. + +```WordsSorting --reverse --unique /path/to/input.txt``` + +This tool also has a `--help` option to remind you how it works. + +## Posible evolutions/limitations + +Because of the recommended time frame here are possible improvements or limitations for this projects +* Integrate a unit testing suite: this was not done because I have never integrated a unit test suite in a brand new project like this so it would have taken me too much time +* Add an option to specify an output file instead of just using the currently hardcoded `out.txt` +* Define a way to filter/validate in input "words" after qualifying what counts as a word and what processing is allowed + * What to do with leading/trailing spaces? + * Are we supporting only a single word per line or are we accepting small sentences? +* Gracefully support non-ascii/non-basic characters: this was done for simplicity but currently there is no graceful support for accents (for example consider that `é` is right after `e` and not far at the end of the alphabet) +* Gracefully support other alphabets: only the latin alphabets with basic encoding is currently supported +* Improve the repository setup as I am fairly certian this is not the celanest way to present a C++ project but it is not something I do oftent at all on my time \ No newline at end of file diff --git a/WordsSorting.sln b/WordsSorting.sln new file mode 100644 index 0000000..0ec99a8 --- /dev/null +++ b/WordsSorting.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31205.134 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WordsSorting", "WordsSorting\WordsSorting.vcxproj", "{E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Debug|x64.ActiveCfg = Debug|Win32 + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Debug|x64.Build.0 = Debug|Win32 + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Debug|x86.ActiveCfg = Release|Win32 + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Debug|x86.Build.0 = Release|Win32 + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Release|x64.ActiveCfg = Release|x64 + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Release|x64.Build.0 = Release|x64 + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Release|x86.ActiveCfg = Release|Win32 + {E0B8ADE8-E356-44D8-A6B9-D26E957E1D02}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6506F224-5D46-4ED9-9ADF-72B506B53C5C} + EndGlobalSection +EndGlobal diff --git a/WordsSorting/FileTools.cpp b/WordsSorting/FileTools.cpp new file mode 100644 index 0000000..5dc3449 --- /dev/null +++ b/WordsSorting/FileTools.cpp @@ -0,0 +1,39 @@ +#include "FileTools.h" + +#include +#include + +void FileTools::ExtractWordsFromFile(const string& iFilePath, vector& oWords) +{ + ifstream wordList; + wordList.open(iFilePath); + if (wordList.is_open()) { + string word; + while (getline(wordList, word)) { + // TODO basic checks on the words to validate/sanitise the input + if (!word.empty()) { + oWords.push_back(word); + } + } + wordList.close(); + } +} + +void FileTools::SaveRseult(const vector& iWords, const string& iPath, bool isReverse) +{ + fstream fileOut; + fileOut.open(iPath, ios_base::out); + if (fileOut.is_open()) { + if (isReverse) { + for (auto wordsIt = iWords.rbegin(); wordsIt != iWords.rend(); ++wordsIt) { + fileOut << *wordsIt << "\n"; + } + } + else { + for (auto wordsIt = iWords.begin(); wordsIt != iWords.end(); ++wordsIt) { + fileOut << *wordsIt << "\n"; + } + } + } + fileOut.close(); +} diff --git a/WordsSorting/FileTools.h b/WordsSorting/FileTools.h new file mode 100644 index 0000000..b383f3f --- /dev/null +++ b/WordsSorting/FileTools.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +using namespace std; + +class FileTools +{ +public: + /* + * brief: Read the file provided and store each word/line in the output + */ + static void ExtractWordsFromFile(const string& iFilePath, vector& oWords); + + /* + * brief: Save the porcessed words to an output file + */ + static void SaveRseult(const vector& iWords, const string& iPath, bool isReverse); +}; + diff --git a/WordsSorting/Options.h b/WordsSorting/Options.h new file mode 100644 index 0000000..ba55901 --- /dev/null +++ b/WordsSorting/Options.h @@ -0,0 +1,12 @@ +#pragma once + +#include +using namespace std; + +// List of hardcoded options strings +struct Options { + const std::string kHelp = "help"; + const std::string kReverse = "reverse"; + const std::string kUnique = "unique"; + const std::string kInput = "input"; +}; \ No newline at end of file diff --git a/WordsSorting/WordProcessing.cpp b/WordsSorting/WordProcessing.cpp new file mode 100644 index 0000000..dda0e4a --- /dev/null +++ b/WordsSorting/WordProcessing.cpp @@ -0,0 +1,43 @@ +#include "WordProcessing.h" + +#include +#include + +// Comparator struct for alphabetical sorting +// TODO support non ASCII characters in a graceful manner (accents, other alphabets, etc) +struct AlphbeticalStringOrdering +{ + bool operator()(string iLeft, string iRight) const + { + + string lowerLeft = iLeft, lowerRight = iRight; + transform(lowerLeft.begin(), lowerLeft.end(), lowerLeft.begin(), ::tolower); + transform(lowerRight.begin(), lowerRight.end(), lowerRight.begin(), ::tolower); + + /* If the strings are the same in case a case insensitive comparisonthen we can keep the default/basic + * because we will only compare the case of the letters + * If they are different in case insensitive we use this as our basis + * because we do not want to risk having a string starting with a 'Z' before a word starting with an 'a' + */ + bool leftSmaller = iLeft < iRight; + if (lowerLeft != lowerRight) + { + leftSmaller = lowerLeft < lowerRight; + } + + return leftSmaller; + } + +}; + +void WordProcessing::ProcessWords(bool isUnique, vector& ioWords) +{ + std::function sorter = AlphbeticalStringOrdering(); + sort(ioWords.begin(), ioWords.end(), sorter); + + if (isUnique) + { + auto eraseIt = unique(ioWords.begin(), ioWords.end()); + ioWords.erase(eraseIt, ioWords.end()); + } +} diff --git a/WordsSorting/WordProcessing.h b/WordsSorting/WordProcessing.h new file mode 100644 index 0000000..4b6cc0c --- /dev/null +++ b/WordsSorting/WordProcessing.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +using namespace std; + +class WordProcessing +{ +public: + /* + * brief: Sort and apply the possible unicity option to the words + */ + static void ProcessWords(bool isUnique, vector& ioWords); +}; + diff --git a/WordsSorting/WordsSorting.cpp b/WordsSorting/WordsSorting.cpp new file mode 100644 index 0000000..11eb5fa --- /dev/null +++ b/WordsSorting/WordsSorting.cpp @@ -0,0 +1,83 @@ +#include "FileTools.h" +#include "Options.h" +#include "WordProcessing.h" + +#include +namespace po = boost::program_options; + +#include +#include +using namespace std; + +int main(int ac, char* av[]) +{ + try { + Options options; + + // Visible options that are possible to use + po::options_description visible("Allowed options"); + visible.add_options() + (options.kHelp.c_str(), "produce help message") + (options.kReverse.c_str(), "sort word list in reverse alphbetical order") + (options.kUnique.c_str(), "delete duplicates from the word list"); + + // Hidden parameter for the source file + po::options_description hidden("Hidden options"); + hidden.add_options() + (options.kInput.c_str(), po::value(), "input file"); + + po::options_description cmdline_options; + cmdline_options.add(visible).add(hidden); + + // Only expect 1 filename for the hidden parameter + po::positional_options_description p; + p.add(options.kInput.c_str(), 1); + + po::variables_map vm; + po::store(po::command_line_parser(ac, av). + options(cmdline_options).positional(p).run(), vm); + po::notify(vm); + + // Only display the help if asked + if (vm.count(options.kHelp)) { + cout << "Usage: WordsSorting [OPTIONS] FILE\n" + << "Sort the list of words provided in the input file\n" + << "Example: WordsSorting --reverse test.txt\n\n" + << visible << "\n"; + return 0; + } + + // Input file is required + string inputFile; + if (vm.count(options.kInput)) { + inputFile = vm[options.kInput].as(); + } + else { + cerr << "Input file missing\n"; + return 2; + } + + // Setting up the flags for the proccessing + const bool isReverse = vm.count(options.kReverse); + const bool isUnique = vm.count(options.kUnique); + + vector words; + + FileTools::ExtractWordsFromFile(inputFile, words); + WordProcessing::ProcessWords(isUnique, words); + // TODO have an output parameter + FileTools::SaveRseult(words, "out.txt", isReverse); + + } + catch (exception& e) { + cerr << "error: " << e.what() << "\n"; + cout << "Use --help to see how to use this tool\n"; + return 1; + } + catch (...) { + cerr << "Exception of unknown type!\n"; + } + + return 0; + +} \ No newline at end of file diff --git a/WordsSorting/WordsSorting.vcxproj b/WordsSorting/WordsSorting.vcxproj new file mode 100644 index 0000000..94ea0c9 --- /dev/null +++ b/WordsSorting/WordsSorting.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {e0b8ade8-e356-44d8-a6b9-d26e957e1d02} + WordsSorting + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp17 + D:\Dev\libs\boost_1_76_0 + + + Console + true + D:\Dev\libs\boost_1_76_0\stage\lib;D:\Dev\libs\boost_1_76_0\bin.v2\libs;D:\Dev\libs\boost_1_76_0\libs;%(AdditionalLibraryDirectories) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + stdcpp17 + D:\Dev\libs\boost_1_76_0 + + + Console + true + true + true + D:\Dev\libs\boost_1_76_0\stage\lib;D:\Dev\libs\boost_1_76_0\bin.v2\libs;D:\Dev\libs\boost_1_76_0\libs;%(AdditionalLibraryDirectories) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WordsSorting/WordsSorting.vcxproj.filters b/WordsSorting/WordsSorting.vcxproj.filters new file mode 100644 index 0000000..18b0dda --- /dev/null +++ b/WordsSorting/WordsSorting.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file