Cheatsheet: Data Structures
    Array / List
    Map / Dict
    Set
    Tuple
    Struct / Data Class
    Stacks
    Other Data Structures
    Collection Conversions
Cheatsheet: Control Flow
Cheatsheet: Class

Polyglot CheatSheet - Operators

Updated: 2021-12-10

Python

Swap

a, b = b, a

TreeNode Definition

class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None

If You Forget Function Name

Use dir() to check the function names:

>>> dir([])
[... 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Or pretty print:

>>> import pprint; pprint.pprint(dir([]))
[ ...
  'append',
  'clear',
  'copy',
  'count',
  'extend',
  'index',
  'insert',
  'pop',
  'remove',
  'reverse',
  'sort']

All and ANY

ALL: if every one is truthy

>>> all([1, "abc", [False]])
True
>>> all([1, 0, 1])
False

ANY: if any one is truthy

>>> any([1, 0, 0])
True
>>> any(["", 0, 0])
False

Conversions

string to char array

>>> s = "asdf"
>>> [c for c in s]
['a', 's', 'd', 'f']

int to char

>>> chr(97)
'a'

char to int

>>> ord('a')
97

int to binary

>>> format(14, 'b')
'1110'

int to hex

>>> '%#x' % 255, '%x' % 255, '%X' % 255
('0xff', 'ff', 'FF')
>>> format(255, '#x'), format(255, 'x'), format(255, 'X')
('0xff', 'ff', 'FF')
>>> f'{255:#x}', f'{255:x}', f'{255:X}'
('0xff', 'ff', 'FF')