Skip to content

Instantly share code, notes, and snippets.

View nimish-mehta's full-sized avatar

Nimish Mehta nimish-mehta

View GitHub Profile
import itertools
def getAllSentences(input_data):
"""
take cartesian cross product of list and join to form the sentences to get all sentences.
Assumption: The order for sentences is that for every element in ith list create all sentences from i-1..0th list
i.e. if list = [[1,2],[3,4],[5]]
order of sentences = [135,145,235,245]
"""
return [" ".join(sentence) for sentence in list(itertools.product(*input_data))]
defmodule SetMember do
defstruct val: 0, lt: 0, rt: 0, children: []
end
defmodule Test do
# function header for defaults
def assign_counts(set_member, count\\1)
# no child just assign counts and return last used count
@nimish-mehta
nimish-mehta / config.exs
Last active March 25, 2020 13:56
Configure Logging to File in Phoenix
# Configures Elixir's Logger to log to file
# ensure https://github.com/onkel-dirtus/logger_file_backend
# is installed in deps of the project in mix.exs
# reuses the original phoenix logging format.
config :logger, backends: [{LoggerFileBackend, :request_log}],
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
# Keep a seperate log file for each env.
# logs are stored in the root directory of the application
@nimish-mehta
nimish-mehta / red_black_tree.ex
Created May 25, 2016 10:37
Red black tree in elixir
defmodule RedBlackTree do
# elixir translation of https://gist.github.com/mjn/2648040
# stucture of node: {Key, Value, Color, Smaller, Bigger}
# tree = nil
# tree = RedBlackTree.insert(tree, 2, 3)
# IO.inspect tree
# tree = RedBlackTree.insert(tree, 3, 4)
# IO.inspect tree
# tree = RedBlackTree.insert(tree, 6, 9)
# IO.inspect tree
@nimish-mehta
nimish-mehta / column_alias.ex
Last active January 6, 2017 06:38
Alias elixir ecto columns from db columns, by using a separate virtual field per column for reading and writing
# Define a macro to alias columns and collect them
defmodule ColumnAlias do
defmacro __using__(_) do
quote do
Module.register_attribute(__MODULE__, :column_aliases, accumulate: true)
import ColumnAlias, only: [alias_field: 3]
@before_compile ColumnAlias
end
end
@nimish-mehta
nimish-mehta / anonymous_factorial.ex
Created December 20, 2016 13:19
Recursively calling anonymous functions in elixir
factorial = fn (x) ->
calc_factorial = fn
(_, 0) -> 1
(calc_fn, n) -> n * calc_fn.(calc_fn, n - 1)
end
calc_factorial.(calc_factorial, x)
end
IO.inspect factorial.(10)