Skip to content

Instantly share code, notes, and snippets.

@sungiven
sungiven / wordcloud.r
Created April 24, 2024 18:28
Create wordclouds from tweets
library(readxl)
library(dplyr)
library(lubridate)
library(ggplot2)
library(sentimentr)
library(cowplot)
library(RColorBrewer)
library(wordcloud)
library(wordcloud2)
library(tidytext)
@sungiven
sungiven / double_duty_tuples.py
Created February 25, 2024 10:08
tuples as records and immutable lists
#!/usr/bin/env python3
# tuples as records & immutable lists
## TUPLES AS RECORDS ##
lax_coordinates = (33.9425, -118.408056)
city, year, pop, chg, area = ("Tokyo", 2003, 32_450, 0.66, 8014)
traveler_ids = [("USA", "31195855"), ("BRA", "CE342567"), ("ESP", "XDA205856")]
# the % formatting operator understands tuples and treats each item as a separate
@sungiven
sungiven / gen_exps_init.py
Created February 10, 2024 09:35
genexp init vs listcomp
#!/usr/bin/env python3
symbols = "$¢£¥€¤"
output = tuple(ord(symbol) for symbol in symbols)
print(output)
# If the generator expression is the single argument in a function call, there is no
# need to duplicate the enclosing parentheses.
import array
@sungiven
sungiven / cartesian_product.py
Created January 27, 2024 18:54
Cartesian product of card ranks & suits
#!/usr/bin/env python3
card_ranks = ["A", "K", "Q"]
card_suits = ["♠", "♥", "♦", "♣"]
# cartesian product of cards, arranged by ranks
cards = [(rank, suit) for rank in card_ranks for suit in card_suits]
print(cards)
# cartesian product of cards, arranged by suits
@sungiven
sungiven / listcomp_vs_mapfilter.py
Last active January 27, 2024 18:31
listcomp list vs filter/map composition
#!/usr/bin/env python3
symbols = "fj#@2$¢£¥€¤92f^x"
over_ascii = [ord(s) for s in symbols if ord(s) > 127]
print(over_ascii)
# Using filter/map composition to make the same list created by the listcomp looks
# so much more complex.
@sungiven
sungiven / local_scope_in_comps_genexps.py
Created January 27, 2024 10:29
local scope in comps and gen exps
#!/usr/bin/env python3
x = "EGG"
codes = [ord(x) for x in x]
print(codes)
# output: [69, 71, 71]
print(x)
# x is not clobbered, still bound to 'EGG'
@sungiven
sungiven / listcomp.py
Created January 27, 2024 09:22
listcomp comparison
#!/usr/bin/env python3
symbols = "fj92hf!TGgq2^#"
# without using listcomp
codes = []
for symbol in symbols:
codes.append(ord(symbol))
print(codes)
@sungiven
sungiven / gush.sh
Last active January 20, 2024 09:01
gush.sh
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <comment>"
exit 1
fi
git add .
git commit -m "$1"
git push
import math
class Vector:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
"""Returns the string representation of the obj for inspection."""
return f"Vector({self.x!r}, {self.y!r})"
import collections
Card = collections.namedtuple("Card", ["rank", "suit"])
# `collections.namedtuple` is used to construct a simple class to represent individual cards.
# We can use `namedtuple` to build classes of objects that are just bundles of attributes
# with no custom methods, like a database record.
class FrenchDeck:
ranks = [str(n) for n in range(2, 11)] + list("JQKA")