Skip to content

Instantly share code, notes, and snippets.

View rpivo's full-sized avatar

Ryan Pivovar rpivo

  • New York City, NY
View GitHub Profile
@rpivo
rpivo / index.md
Last active January 13, 2021 15:53
The XOR Operator in Python

The XOR Operator in Python

As in JavaScript, we can use the ^ symbol for XOR (exclusive or) operations.

print(
  1 ^ 2,  # 3
  5 ^ 1,  # 4
  10 ^ 2, # 8
 1 ^ 1, # 0
@rpivo
rpivo / index.md
Created January 17, 2021 21:06
Getting the Last Item in a List in Python

Getting the Last Item in a List in Python

You can get the last item in a list by using -1 as the index.

l = [1,2,3,4,5]

print(l[-1]) # 5
@rpivo
rpivo / index.md
Last active January 18, 2021 19:03
Flipping a Value Between 0 and 1 in Python

Flipping a Value Between 0 and 1 in Python

You can convert a 1 to its boolean opposite (and vice versa) using int(not bool(n)). This converts n to a boolean value using bool(), uses the not keyword to get the boolean opposite of this value, and then converts this value to an integer with int().

print(
  int(not bool(1)), # 0
  int(not bool(0)), # 1
)
@rpivo
rpivo / index.md
Last active January 18, 2021 19:10
Two Ways to Reverse a List in Python

Two Ways to Reverse a List in Python

An easy way to reverse a list in Python is to use the reverse() list function:

l = [1, 2, 3]
l.reverse()
print(l) # [3, 2, 1]
@rpivo
rpivo / index.md
Last active January 18, 2021 21:25
Toggling State in JavaScript

Toggling State in JavaScript

Toggle between two values using a function:

function toggle(v) {
  return {
    off: 'on',
 on: 'off',
@rpivo
rpivo / index.md
Last active January 19, 2021 01:25
A Simple CloudFormation Template

A Simple CloudFormation Template

Basic keys:

AWSTemplateFormatVersion: "2010-09-09" # AWS Template Format Version will always be 2010-09-09 until AWS updates the format version
Description: a sample template # Description: string
Resources: # list of varioues types of resources, including Lambda functions & layers, EC2 instances, S3 buckets, IAM execution roles, etc
  SomeResource: # user-given ID (name) for the resource
    Type: "AWS::EC2::Instance" # type of the resource. See AWS resource and property types reference in references
    Properties: # properties that are unique to the resource type. See AWS resource and property types reference in references for a list of available properties on each resource type
@rpivo
rpivo / index.md
Last active January 21, 2021 00:09
Creating a Linked List in TypeScript

Creating a Linked List with TypeScript Classes

The snippet below implements a ListNode and a LinkedList class. The LinkedList can be constructed using an array of numbers, and although it doesn't allow for modification after creation, this could be set up.

There are a lot of JavaScript linked list examples online that require the linked list to be manually constructed, but this implementation just needs to ingest an array of numbers in order to construct the linked list.

This LinkedList class exposes head and tail properties. It could be set up to be traversed from either end.

class ListNode {
@rpivo
rpivo / index.md
Last active January 19, 2021 19:48
TypeScript Parameter Properties

TypeScript Parameter Properties

Rather than declaring class properties up front like this...

class ListNode {
  public val: number
  public next: ListNode | null

 constructor(val?: number, next?: ListNode | null) {
@rpivo
rpivo / index.md
Last active January 23, 2021 16:46
Calculating Triangular Numbers

Calculating Triangular Numbers

Sometimes you'll need to calculate what's called a triangular number. You'll know you have a triangular number on your hands when you have a number (say 5), and you want to get the sum of all positive integers up to and including the number itself (1 + 2 + 3 + 4 + 5).

As an example, maybe you want to find all repeating as well as singular character sequences in a string.

aaaa
@rpivo
rpivo / index.md
Last active February 7, 2021 22:09
Using reduce in Python

Using reduce in Python

from functools import reduce

print(
  reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
)

# 15