Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.
Merged
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
2 changes: 1 addition & 1 deletion w1/breaking_records.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule HackerRank.W1.BreakingRecords do

## Example

iex> breaking_records([3, 4, 21, 36, 10, 28, 35, 5, 24, 42])
iex> BreakingRecords.challenge([3, 4, 21, 36, 10, 28, 35, 5, 24, 42])
[4, 0]
"""
@spec challenge([integer()]) :: [integer()]
Expand Down
1 change: 1 addition & 0 deletions w1/breaking_records_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule HackerRank.W1.BreakingRecordsTests do
use ExUnit.Case
alias HackerRank.Test.Helper
alias HackerRank.W1.BreakingRecords
doctest HackerRank.W1.BreakingRecords

@pattern ~w"w1/tc/4_*.txt"

Expand Down
195 changes: 195 additions & 0 deletions w1/camel_case.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
defmodule HackerRank.W1.CamelCase do
@moduledoc """
Camel Case – Challenge 5, Week 1.

[HackerRank Problem – Camel Case](https://www.hackerrank.com/challenges/three-month-preparation-kit-camel-case/problem)
"""

@doc """
Creates or splits Java-style CamelCase variable, method, and class names.

## Parameters

- `token` (string): A semicolon-separated string specifying the operation and target string.
Format:
- `C` = combine, `S` = separate
- `M` = method, `C` = class, `V` = variable

## Returns

- A string converted to or from CamelCase based on the specified configuration.

## Examples

iex> CamelCase.challenge("C;V;can of coke")
"canOfCoke"

iex> CamelCase.challenge("S;M;sweetTea()")
"sweet tea"

iex> CamelCase.challenge("C;C;mirror")
"Mirror"

iex> CamelCase.challenge("C;M;santa claus")
"santaClaus()"
"""

@spec challenge(String.t()) :: String.t()
def challenge(token) do
# split the token up
[o, v, t] = String.split(token, ";")

# pass it off to the specific operation
case [o, v] do
["C", "M"] -> combine_method(t)
["C", "V"] -> combine_variable(t)
["C", "C"] -> combine_class(t)
["S", "M"] -> split_method(t)
["S", "V"] -> split_variable(t)
["S", "C"] -> split_class(t)
end
end

@doc """
Combines a string into class name

## Parameters
- `t`: a string to convert

## Returns
- The formatted string

## Example

iex> CamelCase.combine_class("mirror")
"Mirror"
"""
@spec combine_class(String.t()) :: String.t()
def combine_class(t) do
t
|> String.split()
|> Enum.map(&String.capitalize/1)
|> Enum.join()
end

@doc """
Combines a string into method name

## Parameters
- `t`: a string to convert

## Returns
- The formatted string

## Example

iex> CamelCase.combine_method("santa claus")
"santaClaus()"
"""
@spec combine_method(String.t()) :: String.t()
def combine_method(t) do
# split up the first from the rest
[first | rest] = String.split(t)

# downcase the first, capitalise the rest and join it
name =
[String.downcase(first) | Enum.map(rest, &String.capitalize/1)]
|> Enum.join()

# whack a set of parenthesis on the end
name <> "()"
end

@doc """
Combines a string into variable name

## Parameters
- `t`: a string to convert

## Returns
- The formatted string

## Example

iex> CamelCase.combine_variable("can of coke")
"canOfCoke"
"""
@spec combine_variable(String.t()) :: String.t()
def combine_variable(t) do
# split up the first from the rest
[first | rest] = String.split(t)

# downcase the first, capitalise the rest and join it
[String.downcase(first) | Enum.map(rest, &String.capitalize/1)]
|> Enum.join()
end
@doc """
Splits a string from a class name

## Parameters
- `t`: a string to convert

## Returns
- The formatted string

## Example

iex> CamelCase.split_class("sweetTea")
"sweet tea"
"""
@spec split_class(String.t()) :: String.t()
def split_class(t) do
t
|> split_camel_case()
|> Enum.map(&String.downcase/1)
|> Enum.join(" ")
end
@doc """
Splits a string from method name

## Parameters
- `t`: a string to convert

## Returns
- The formatted string

## Example

iex> CamelCase.split_method("sweetTea()")
"sweet tea"
"""
@spec split_method(String.t()) :: String.t()
def split_method(t) do
t
|> split_camel_case()
|> Enum.map(&String.downcase/1)
|> Enum.join(" ")
|> strip_parens()
end

@doc """
Splits a string from variable name

## Parameters
- `t`: a string to convert

## Returns
- The formatted string

## Example

iex> CamelCase.split_variable("epsonPrinter")
"epson printer"
"""
@spec split_variable(String.t()) :: String.t()
def split_variable(t) do
t
|> split_camel_case()
|> Enum.map(&String.downcase/1)
|> Enum.join(" ")
end

defp split_camel_case(t), do: Regex.scan(~r/[a-z]+|[A-Z][a-z]*/, t) |> List.flatten |> Enum.map(&String.trim/1)
defp strip_parens(t), do: String.replace(t, "()", "")

end
4 changes: 2 additions & 2 deletions w1/camel_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def camel_case(token: str) -> str:
return result


# HackerRank made you write all of the runner for this one as well.
# HackerRank made you write this in the style of a weird runner.
# that doesn't seem all that 'basic'
if __name__ == '__main__' and PRINT:
for line in sys.stdin.readlines():
for line in sys.stdin.readlines():
camel_case(line.strip())
20 changes: 20 additions & 0 deletions w1/camel_case_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule HackerRank.W1.CamelCaseTests do
use ExUnit.Case
alias HackerRank.Test.Helper
alias HackerRank.W1.CamelCase
doctest HackerRank.W1.CamelCase

@pattern "w1/tc/5_*txt"

@tag :unit
test "run the tests" do
Helper.test_all(@pattern, &parse_args/1, &parse_expected/1 )
|> Enum.flat_map(fn [inputs, outputs] -> Enum.zip(inputs, outputs) end)
|> Enum.each(fn {args, expected} ->
assert CamelCase.challenge(args) == expected
end)
end

def parse_args(raw), do: raw
def parse_expected(raw), do: raw
end
2 changes: 1 addition & 1 deletion w1/mini_max_sum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule HackerRank.W1.MiniMaxSum do

## Examples

iex> mini_max_sum([1, 2, 3, 4, 5])
iex> MiniMaxSum.challenge([1, 2, 3, 4, 5])
[10, 14]
"""
@spec challenge([integer()]) :: [integer()]
Expand Down
1 change: 1 addition & 0 deletions w1/mini_max_sum_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule HackerRank.W1.MiniMaxSumTests do
use ExUnit.Case
alias HackerRank.Test.Helper
alias HackerRank.W1.MiniMaxSum
doctest HackerRank.W1.MiniMaxSum

@pattern "w1/tc/2_*.txt"

Expand Down
1 change: 1 addition & 0 deletions w1/plus_minus_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule HackerRank.W1.PlusMinusTest do
use ExUnit.Case
alias HackerRank.Test.Helper
alias HackerRank.W1.PlusMinus
doctest HackerRank.W1.PlusMinus

@pattern "w1/tc/1_*.txt"

Expand Down