From 2145dd3f5a734a69cbf36737d61b45d05ff05b7f Mon Sep 17 00:00:00 2001 From: Daniel Klatt Date: Sat, 20 Feb 2021 10:54:54 -0600 Subject: [PATCH 1/4] Split project into elixir and java solutions Moved existing java files to java_solution, and added elixir files into elixir_solution --- .gitignore | 8 ------ elixir_solution/.formatter.exs | 4 +++ elixir_solution/.gitignore | 27 ++++++++++++++++++ elixir_solution/README.md | 21 ++++++++++++++ elixir_solution/lib/interview.ex | 18 ++++++++++++ elixir_solution/mix.exs | 28 +++++++++++++++++++ elixir_solution/test/interview_test.exs | 8 ++++++ elixir_solution/test/test_helper.exs | 1 + java_solution/.gitignore | 9 ++++++ README.md => java_solution/README.md | 0 .../codingChallenge.md | 0 pom.xml => java_solution/pom.xml | 0 .../main/java/com/github/archarithms/App.java | 5 ++++ .../archarithms/test/DateStringTests.java | 18 ++++++------ .../archarithms/test/TitleCaseTests.java | 9 ++++++ 15 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 elixir_solution/.formatter.exs create mode 100644 elixir_solution/.gitignore create mode 100644 elixir_solution/README.md create mode 100644 elixir_solution/lib/interview.ex create mode 100644 elixir_solution/mix.exs create mode 100644 elixir_solution/test/interview_test.exs create mode 100644 elixir_solution/test/test_helper.exs create mode 100644 java_solution/.gitignore rename README.md => java_solution/README.md (100%) rename codingChallenge.md => java_solution/codingChallenge.md (100%) rename pom.xml => java_solution/pom.xml (100%) rename {src => java_solution/src}/main/java/com/github/archarithms/App.java (64%) rename {src => java_solution/src}/test/java/com/github/archarithms/test/DateStringTests.java (60%) rename {src => java_solution/src}/test/java/com/github/archarithms/test/TitleCaseTests.java (79%) diff --git a/.gitignore b/.gitignore index 773c56d..e43b0f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1 @@ -.classpath -.project -.settings/ -target/ -nbproject/* -.settings -runtime/* -*~ .DS_Store diff --git a/elixir_solution/.formatter.exs b/elixir_solution/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir_solution/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir_solution/.gitignore b/elixir_solution/.gitignore new file mode 100644 index 0000000..90b807e --- /dev/null +++ b/elixir_solution/.gitignore @@ -0,0 +1,27 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +interview-*.tar + + +# Temporary files for e.g. tests +/tmp diff --git a/elixir_solution/README.md b/elixir_solution/README.md new file mode 100644 index 0000000..d804d13 --- /dev/null +++ b/elixir_solution/README.md @@ -0,0 +1,21 @@ +# Interview + +The elixir way of solving the interview problems in the java coding challenge + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `interview` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:interview, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at [https://hexdocs.pm/interview](https://hexdocs.pm/interview). + diff --git a/elixir_solution/lib/interview.ex b/elixir_solution/lib/interview.ex new file mode 100644 index 0000000..7c9bbae --- /dev/null +++ b/elixir_solution/lib/interview.ex @@ -0,0 +1,18 @@ +defmodule Interview do + @moduledoc """ + Documentation for `Interview`. + """ + + @doc """ + Hello world. + + ## Examples + + iex> Interview.hello() + :world + + """ + def hello do + :world + end +end diff --git a/elixir_solution/mix.exs b/elixir_solution/mix.exs new file mode 100644 index 0000000..ad058d4 --- /dev/null +++ b/elixir_solution/mix.exs @@ -0,0 +1,28 @@ +defmodule Interview.MixProject do + use Mix.Project + + def project do + [ + app: :interview, + version: "0.1.0", + elixir: "~> 1.11", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/elixir_solution/test/interview_test.exs b/elixir_solution/test/interview_test.exs new file mode 100644 index 0000000..310f2e6 --- /dev/null +++ b/elixir_solution/test/interview_test.exs @@ -0,0 +1,8 @@ +defmodule InterviewTest do + use ExUnit.Case + doctest Interview + + test "greets the world" do + assert Interview.hello() == :world + end +end diff --git a/elixir_solution/test/test_helper.exs b/elixir_solution/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/elixir_solution/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/java_solution/.gitignore b/java_solution/.gitignore new file mode 100644 index 0000000..773c56d --- /dev/null +++ b/java_solution/.gitignore @@ -0,0 +1,9 @@ +.classpath +.project +.settings/ +target/ +nbproject/* +.settings +runtime/* +*~ +.DS_Store diff --git a/README.md b/java_solution/README.md similarity index 100% rename from README.md rename to java_solution/README.md diff --git a/codingChallenge.md b/java_solution/codingChallenge.md similarity index 100% rename from codingChallenge.md rename to java_solution/codingChallenge.md diff --git a/pom.xml b/java_solution/pom.xml similarity index 100% rename from pom.xml rename to java_solution/pom.xml diff --git a/src/main/java/com/github/archarithms/App.java b/java_solution/src/main/java/com/github/archarithms/App.java similarity index 64% rename from src/main/java/com/github/archarithms/App.java rename to java_solution/src/main/java/com/github/archarithms/App.java index dcb6723..564372c 100644 --- a/src/main/java/com/github/archarithms/App.java +++ b/java_solution/src/main/java/com/github/archarithms/App.java @@ -5,6 +5,11 @@ public class App { public static String convertToTitleCase(final String inpStr) { + /** + * Convert all non-alphanumeric characters to spaces + * Remove leading and trailing whitespace + * Capitalize each word + */ return inpStr; } diff --git a/src/test/java/com/github/archarithms/test/DateStringTests.java b/java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java similarity index 60% rename from src/test/java/com/github/archarithms/test/DateStringTests.java rename to java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java index 04e9943..506515d 100644 --- a/src/test/java/com/github/archarithms/test/DateStringTests.java +++ b/java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java @@ -20,17 +20,17 @@ public class DateStringTests /** * Test the testConvertUnixToDateString method */ - @Test - public void testConvertUnixToDateString() { - String testStr = "July 4, 2017"; - assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L))); - } + // @Test + // public void testConvertUnixToDateString() { + // String testStr = "July 4, 2017"; + // assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L))); + // } /** * Test the testNullCase method */ - @Test - public void testNullCase() { - assertThrows(Exception.class, () -> App.convertUnixToDateString(null)); - } + // @Test + // public void testNullCase() { + // assertThrows(Exception.class, () -> App.convertUnixToDateString(null)); + // } } diff --git a/src/test/java/com/github/archarithms/test/TitleCaseTests.java b/java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java similarity index 79% rename from src/test/java/com/github/archarithms/test/TitleCaseTests.java rename to java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java index bd3bb2d..3e626f1 100644 --- a/src/test/java/com/github/archarithms/test/TitleCaseTests.java +++ b/java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java @@ -42,4 +42,13 @@ public void testOtherChars() { String testStr = "Truth Track"; assertTrue(testStr.equals(App.convertToTitleCase("TRUTH-TRACK"))); } + + /** + * Test the string provided in the instruction file + */ + @Test + public void testInstructionString() { + String testStr = "Truth Track"; + assertTrue(testStr.equals(App.convertToTitleCase("CASE-THREE_extra[chars]///"))); + } } From b3ad6a926c4d5d7a486f6335834b4d3d3c896832 Mon Sep 17 00:00:00 2001 From: Daniel Klatt Date: Sat, 20 Feb 2021 16:52:02 -0600 Subject: [PATCH 2/4] Elixir Solution Added functions and tests for the interview problems presented in the instructions --- elixir_solution/lib/interview.ex | 24 ++++++++--------- elixir_solution/test/interview_test.exs | 34 ++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/elixir_solution/lib/interview.ex b/elixir_solution/lib/interview.ex index 7c9bbae..d162de3 100644 --- a/elixir_solution/lib/interview.ex +++ b/elixir_solution/lib/interview.ex @@ -1,18 +1,14 @@ defmodule Interview do - @moduledoc """ - Documentation for `Interview`. - """ - - @doc """ - Hello world. - - ## Examples - - iex> Interview.hello() - :world + def convertToTitleCase(string) when is_binary(string) do + string + |> String.split(~r/[^[:alnum:]]/u, trim: true) + |> Enum.map(&String.capitalize/1) + |> Enum.join(" ") + end - """ - def hello do - :world + def convertUnixToDateString(seconds) when is_integer(seconds) do + seconds + |> DateTime.from_unix!() + |> Calendar.strftime("%B %-d, %Y") end end diff --git a/elixir_solution/test/interview_test.exs b/elixir_solution/test/interview_test.exs index 310f2e6..d5c1d42 100644 --- a/elixir_solution/test/interview_test.exs +++ b/elixir_solution/test/interview_test.exs @@ -1,8 +1,36 @@ defmodule InterviewTest do use ExUnit.Case - doctest Interview - test "greets the world" do - assert Interview.hello() == :world + describe "Title Case Tests" do + test "basic string" do + assert Interview.convertToTitleCase("TITLE_CASE") == "Title Case" + end + + test "properly handles numbers" do + assert Interview.convertToTitleCase("NUMBER_3") == "Number 3" + end + + test "properly handles hyphens" do + assert Interview.convertToTitleCase("TRUTH-TRACK") == "Truth Track" + end + + test "extra test case from instruction file" do + assert Interview.convertToTitleCase("CASE-THREE_extra[chars]///") == + "Case Three Extra Chars" + end + + test "throws exception when passed null" do + assert_raise FunctionClauseError, fn -> Interview.convertToTitleCase(nil) end + end + end + + describe "Date String Tests" do + test "test date converstion" do + assert Interview.convertUnixToDateString(1_499_144_400) == "July 4, 2017" + end + + test "throws exception when passed null" do + assert_raise FunctionClauseError, fn -> Interview.convertUnixToDateString(nil) end + end end end From a31e4548b8a1be195f2b56bc199656cae870ea35 Mon Sep 17 00:00:00 2001 From: Daniel Klatt Date: Sun, 21 Feb 2021 11:25:34 -0600 Subject: [PATCH 3/4] Java Solution Added functions and tests to complete the Java solution of the coding challenge --- .../main/java/com/github/archarithms/App.java | 45 +++++++++++++++---- .../archarithms/test/DateStringTests.java | 31 +++++++++---- .../archarithms/test/TitleCaseTests.java | 19 +++++--- 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/java_solution/src/main/java/com/github/archarithms/App.java b/java_solution/src/main/java/com/github/archarithms/App.java index 564372c..b67a4a2 100644 --- a/java_solution/src/main/java/com/github/archarithms/App.java +++ b/java_solution/src/main/java/com/github/archarithms/App.java @@ -1,20 +1,47 @@ package com.github.archarithms; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; public class App { - public static String convertToTitleCase(final String inpStr) + public static String convertToTitleCase(final String inpStr) throws Exception { - /** - * Convert all non-alphanumeric characters to spaces - * Remove leading and trailing whitespace - * Capitalize each word - */ - return inpStr; + if (inpStr == null) { + throw new Exception(); + } + + String[] words = inpStr.split("[^a-zA-Z0-9]"); + for (int i = 0; i < words.length; i++) { + String word = words[i]; + String head = word.substring(0, 1); + String tail = word.substring(1); + words[i] = head.toUpperCase() + tail.toLowerCase(); + } + return String.join(" ", words); + } + + public static String convertUnixToDateString() throws Exception + { + Long seconds = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); + + return convertUnixToDateString(seconds); } - public static String convertUnixToDateString(final Long inpUnixSeconds) + public static String convertUnixToDateString(final Long inpUnixSeconds) throws Exception { - return String.valueOf(inpUnixSeconds); + if (inpUnixSeconds == null) { + throw new Exception(); + } + + LocalDateTime date = LocalDateTime.ofEpochSecond(inpUnixSeconds, 0, ZoneOffset.UTC); + DateTimeFormatter formatter = patternedFormatter(); + + return date.format(formatter); + } + + public static DateTimeFormatter patternedFormatter() { + return DateTimeFormatter.ofPattern("MMMM d, yyyy"); } } diff --git a/java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java b/java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java index 506515d..24b4bff 100644 --- a/java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java +++ b/java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java @@ -7,6 +7,9 @@ import com.github.archarithms.App; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + /** * DateStringTests unit tests for simple App. */ @@ -20,17 +23,27 @@ public class DateStringTests /** * Test the testConvertUnixToDateString method */ - // @Test - // public void testConvertUnixToDateString() { - // String testStr = "July 4, 2017"; - // assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L))); - // } + @Test + public void testConvertUnixToDateString() throws Exception { + String testStr = "July 4, 2017"; + assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L))); + } /** * Test the testNullCase method */ - // @Test - // public void testNullCase() { - // assertThrows(Exception.class, () -> App.convertUnixToDateString(null)); - // } + @Test + public void testNullCase() throws Exception { + assertThrows(Exception.class, () -> App.convertUnixToDateString(null)); + } + + /** + * Test the testDefaultCase method + */ + @Test + public void testDefaultCase() throws Exception { + LocalDateTime date = LocalDateTime.now(); + DateTimeFormatter formatter = App.patternedFormatter(); + assertTrue(date.format(formatter).equals(App.convertUnixToDateString())); + } } diff --git a/java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java b/java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java index 3e626f1..0dd1302 100644 --- a/java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java +++ b/java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java @@ -1,6 +1,7 @@ package com.github.archarithms.test; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -20,7 +21,7 @@ public class TitleCaseTests * Test the testConvertToTitleCase method */ @Test - public void testConvertToTitleCase() { + public void testConvertToTitleCase() throws Exception { String testStr = "Title Case"; assertTrue(testStr.equals(App.convertToTitleCase("TITLE_CASE"))); } @@ -29,7 +30,7 @@ public void testConvertToTitleCase() { * Test the testNumbers method */ @Test - public void testNumbers() { + public void testNumbers() throws Exception { String testStr = "Number 3"; assertTrue(testStr.equals(App.convertToTitleCase("NUMBER_3"))); } @@ -38,7 +39,7 @@ public void testNumbers() { * Test the testOtherChars method */ @Test - public void testOtherChars() { + public void testOtherChars() throws Exception { String testStr = "Truth Track"; assertTrue(testStr.equals(App.convertToTitleCase("TRUTH-TRACK"))); } @@ -47,8 +48,16 @@ public void testOtherChars() { * Test the string provided in the instruction file */ @Test - public void testInstructionString() { - String testStr = "Truth Track"; + public void testInstructionString() throws Exception { + String testStr = "Case Three Extra Chars"; assertTrue(testStr.equals(App.convertToTitleCase("CASE-THREE_extra[chars]///"))); } + + /** + * Test the testNullCase method + */ + @Test + public void testNullCase() throws Exception { + assertThrows(Exception.class, () -> App.convertToTitleCase(null)); + } } From 18499ff2e749397eedfb1c31d2b30a24229f9b0e Mon Sep 17 00:00:00 2001 From: Daniel Klatt Date: Sun, 21 Feb 2021 11:46:58 -0600 Subject: [PATCH 4/4] Added Elixir Default Forgot to add default date of today to Elixir version of solution --- elixir_solution/lib/interview.ex | 3 ++- elixir_solution/test/interview_test.exs | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/elixir_solution/lib/interview.ex b/elixir_solution/lib/interview.ex index d162de3..9e5c324 100644 --- a/elixir_solution/lib/interview.ex +++ b/elixir_solution/lib/interview.ex @@ -6,7 +6,8 @@ defmodule Interview do |> Enum.join(" ") end - def convertUnixToDateString(seconds) when is_integer(seconds) do + def convertUnixToDateString(seconds \\ DateTime.to_unix(DateTime.now!("Etc/UTC"))) + when is_integer(seconds) do seconds |> DateTime.from_unix!() |> Calendar.strftime("%B %-d, %Y") diff --git a/elixir_solution/test/interview_test.exs b/elixir_solution/test/interview_test.exs index d5c1d42..f122b42 100644 --- a/elixir_solution/test/interview_test.exs +++ b/elixir_solution/test/interview_test.exs @@ -32,5 +32,10 @@ defmodule InterviewTest do test "throws exception when passed null" do assert_raise FunctionClauseError, fn -> Interview.convertUnixToDateString(nil) end end + + test "defaults to today" do + today = Calendar.strftime(DateTime.now!("Etc/UTC"), "%B %-d, %Y") + assert Interview.convertUnixToDateString() == today + end end end