Skip to content

Instantly share code, notes, and snippets.

View uxjp's full-sized avatar
🛠️
making it work

JP uxjp

🛠️
making it work
View GitHub Profile
@uxjp
uxjp / 001-README.md
Created August 8, 2022 11:35 — forked from leandronsp/001-README.md
OOP in Bash script

OOP in Bash script

Wait, what?

Inspired by this awesome article.

Prelude about OOP

According to wikipedia, OOP is a programming paradigm or technique based on the concept of "objects". The object structure contain data and behaviour.

Data is the object's state, which should be isolated and must be private.

@uxjp
uxjp / 1_read_from_input.ex
Last active September 20, 2022 02:03
How to read from STDIN in Elixir (for HackerRank) by uxjp
defmodule Solution do
input_as_int = elem(Integer.parse(IO.gets(nil)), 0)
1..input_as_int |> Enum.each(fn _ -> IO.puts "Hello World" end)
end
@uxjp
uxjp / sum_inputs.ex
Created September 9, 2022 21:50
Hacker hank scanning 2 integers from STDIN, calling a function, returning a value, and printing it to STDOUT.
defmodule Solution do
a = IO.gets(nil) |> String.trim |> String.to_integer
b = IO.gets(nil) |> String.trim |> String.to_integer
a + b |> IO.puts
end
defmodule Solution do
[head | tail] = IO.read(:all) |> String.split
nTimes = head |> String.to_integer
tail
|> Enum.each(
fn x -> Range.new(1, nTimes)
|> Enum.each(fn _ -> IO.puts(x)
end)
end)
@uxjp
uxjp / 1_filter_list.ex
Last active September 22, 2022 02:26
Filter list Elixir
defmodule Solution do
[max | tail ] = IO.read(:all)
|> String.split
|> Enum.map(&String.to_integer/1) #everything is already integer after this
tail
|> Enum.filter(fn x -> x < max end)
|> Enum.each(&IO.puts/1)
end
@uxjp
uxjp / 1_filter_list_positions.ex
Last active September 22, 2022 11:57
Elixir filter positions in a list
defmodule Solution do
IO.read(:all)
|> String.split
|> Enum.drop_every(2)
|> Enum.each(&IO.puts/1)
end
defmodule Solution do
IO.read(:all)
|> String.to_integer
|> (&(Range.new(1, &1))).()
|> Enum.to_list
|> IO.inspect
end
@uxjp
uxjp / 1_revert_list.ex
Last active September 25, 2022 02:15
Revert a List Elixir
defmodule ListUtil do
def invert([head | tail], acc) do
invert(tail, [head | acc])
end
def invert([], acc) do
acc
end
end
@uxjp
uxjp / 0_sum_odd_indexes_in_a_list.ex
Last active September 25, 2022 03:25
Sum_odd_indexes_in_a_list
# I miss interpreted the function, but the outcome was cool
defmodule ListUtil do
def su(list) do
sum(list, 0)
end
def sum([head | tail], acc) do
sum(tail, head + acc)
end
@uxjp
uxjp / list_length.ex
Created September 25, 2022 03:40
fp-list-length without libs
defmodule Arr do
def count(arr) do
c(arr, 0)
end
def c([_ | tail], acc) do
c(tail, acc + 1)
end
def c([], acc) do