Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
.classpath
.project
.settings/
target/
nbproject/*
.settings
runtime/*
*~
.DS_Store
4 changes: 4 additions & 0 deletions elixir_solution/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
27 changes: 27 additions & 0 deletions elixir_solution/.gitignore
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions elixir_solution/README.md
Original file line number Diff line number Diff line change
@@ -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).

15 changes: 15 additions & 0 deletions elixir_solution/lib/interview.ex
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions elixir_solution/mix.exs
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions elixir_solution/test/interview_test.exs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions elixir_solution/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
9 changes: 9 additions & 0 deletions java_solution/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.classpath
.project
.settings/
target/
nbproject/*
.settings
runtime/*
*~
.DS_Store
File renamed without changes.
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions java_solution/src/main/java/com/github/archarithms/App.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import com.github.archarithms.App;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* DateStringTests unit tests for simple App.
*/
Expand All @@ -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)));
}
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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")));
}
Expand All @@ -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")));
}
Expand All @@ -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));
}
}
15 changes: 0 additions & 15 deletions src/main/java/com/github/archarithms/App.java

This file was deleted.