This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <comment>" | |
exit 1 | |
fi | |
git add . | |
git commit -m "$1" | |
git push |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
symbols = "fj92hf!TGgq2^#" | |
# without using listcomp | |
codes = [] | |
for symbol in symbols: | |
codes.append(ord(symbol)) | |
print(codes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(readxl) | |
library(dplyr) | |
library(lubridate) | |
library(ggplot2) | |
library(sentimentr) | |
library(cowplot) | |
library(RColorBrewer) | |
library(wordcloud) | |
library(wordcloud2) | |
library(tidytext) |