Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
nathan-cruz77 / group_equal.py
Last active December 13, 2023 02:48
Group consecutive equal items of iterable
# Group equal consecutive items of iterable.
#
#
# Sample usage:
#
# >>> group_equal('aaabbcd')
# [['a', 'a', 'a'], ['b', 'b'], ['c'], ['d']]
#
def group_equal(iterable):
subgroups = []
@nathan-cruz77
nathan-cruz77 / email_url.py
Created May 15, 2019 12:43
Decode Vanhack's tracking system url (so I can actually use their email links)
import sys
import base64
from urllib.parse import urlparse
from urllib.parse import parse_qs
url = urlparse(sys.argv[-1])
query = parse_qs(url.query)
encoded_url = query['url'][0].encode('utf8')
@nathan-cruz77
nathan-cruz77 / grouper.py
Last active December 9, 2023 18:31
Group each N elements of the iterable.
from itertools import zip_longest
# Group each `n` elements of the iterable, filling with `fillvalue` if needed.
#
#
# Sample usage:
#
# >>> list(grouper([1, 2, 3, 4, 5, 6], 2))
# [(1, 2), (3, 4), (5, 6)]
#
@nathan-cruz77
nathan-cruz77 / sliding_window.py
Last active December 9, 2023 18:41
Sliding window for iterables.
from itertools import islice
# Slides through iterable each `n` elements.
#
# Sample usage:
#
# >>> list(sliding_window([1, 2, 3, 4]))
# [(1, 2), (2, 3), (3, 4)]
#
# >>> list(sliding_window([1, 2, 3]))
@nathan-cruz77
nathan-cruz77 / string_converter.ex
Created January 25, 2019 16:23
Convert strings to ascii.
defmodule StringConverter do
@doc """
Returns an ascii representation of the given string.
## Examples
iex> StringConverter.asciify("língüiça")
"linguica"
@nathan-cruz77
nathan-cruz77 / parallel.ex
Created November 15, 2018 15:53
Utility functions to ease working with parallel processes in elixir.
defmodule Parallel do
@moduledoc """
Utility functions to ease working with parallel processes.
"""
@doc """
Equivalent to `Enum.map/2` but each `function` call is made inside its own
process.
"""
def map(enumerable, function) do
@nathan-cruz77
nathan-cruz77 / text_to_whatsapp.js
Last active August 26, 2022 02:08
Whatsapp text formatting
// Regex to format text to whatsapp format
function whatsToHtml(msg) {
if (!msg) return;
return msg
.replace(/~(~*[^~\n]+~*)~/g, '<del>$1</del>')
.replace(/_(_*[^_\n]+_*)_/g, '<em>$1</em>')
.replace(/\*(\**[^*\n]+\**)\*/g, '<strong>$1</strong>');
}
@nathan-cruz77
nathan-cruz77 / calculator.ex
Created August 26, 2018 18:25
Extract operation from string and executes it
# Sample usage:
#
# iex(1)> Calculator.calculate('123 + 27')
# 150
# iex(2)> Calculator.calculate('123 - 27')
# 96
# iex(3)> Calculator.calculate('123 * 27')
# 3321
# iex(4)> Calculator.calculate('123 / 27')
# 4.555555555555555
# Return stirng representation of given array of building sizes (with overall height of 9)
#
# Sample usage:
#
# >>> print(buildings('252'))
#
#
#
#
# #
@nathan-cruz77
nathan-cruz77 / total_flex.html
Created August 3, 2018 14:29
Evenly spaced centralized divs with flexbox
<div class="container full--size">
<div class="square first"></div>
<div class="square second"></div>
<div class="square third"></div>
</div>
<style>
.container {
display: flex;