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..9e5c324 --- /dev/null +++ b/elixir_solution/lib/interview.ex @@ -0,0 +1,15 @@ +defmodule Interview do + 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 convertUnixToDateString(seconds \\ DateTime.to_unix(DateTime.now!("Etc/UTC"))) + when is_integer(seconds) do + seconds + |> DateTime.from_unix!() + |> Calendar.strftime("%B %-d, %Y") + 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..f122b42 --- /dev/null +++ b/elixir_solution/test/interview_test.exs @@ -0,0 +1,41 @@ +defmodule InterviewTest do + use ExUnit.Case + + 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 + + test "defaults to today" do + today = Calendar.strftime(DateTime.now!("Etc/UTC"), "%B %-d, %Y") + assert Interview.convertUnixToDateString() == today + end + 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/java_solution/src/main/java/com/github/archarithms/App.java b/java_solution/src/main/java/com/github/archarithms/App.java new file mode 100644 index 0000000..b67a4a2 --- /dev/null +++ b/java_solution/src/main/java/com/github/archarithms/App.java @@ -0,0 +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) throws Exception + { + 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) throws Exception + { + 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/src/test/java/com/github/archarithms/test/DateStringTests.java b/java_solution/src/test/java/com/github/archarithms/test/DateStringTests.java similarity index 59% 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..24b4bff 100644 --- a/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. */ @@ -21,7 +24,7 @@ public class DateStringTests * Test the testConvertUnixToDateString method */ @Test - public void testConvertUnixToDateString() { + public void testConvertUnixToDateString() throws Exception { String testStr = "July 4, 2017"; assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L))); } @@ -30,7 +33,17 @@ public void testConvertUnixToDateString() { * Test the testNullCase method */ @Test - public void testNullCase() { + 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/src/test/java/com/github/archarithms/test/TitleCaseTests.java b/java_solution/src/test/java/com/github/archarithms/test/TitleCaseTests.java similarity index 55% 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..0dd1302 100644 --- a/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,8 +39,25 @@ 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"))); } + + /** + * Test the string provided in the instruction file + */ + @Test + 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)); + } } diff --git a/src/main/java/com/github/archarithms/App.java b/src/main/java/com/github/archarithms/App.java deleted file mode 100644 index dcb6723..0000000 --- a/src/main/java/com/github/archarithms/App.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.archarithms; - - -public class App -{ - public static String convertToTitleCase(final String inpStr) - { - return inpStr; - } - - public static String convertUnixToDateString(final Long inpUnixSeconds) - { - return String.valueOf(inpUnixSeconds); - } -}