Skip to content

Instantly share code, notes, and snippets.

Symmetric Matrix

A matrix is symmetric if the first row is the same as the first column, the second row is the same as the second column etc… Write a function to determine whether a matrix is symmetric.

Example input and output:

print symmetric([[1, 2, 3],
                [2, 3, 4],
                [3, 4, 1]])
>>> True
def split_power_2(x):
""" Splits a number up into powers of 2, returning a list of powers.
"""
split_powers = []
i = 1
while i <= x:
if i & x: # 1 or 0 -> evaluate as True/False
split_powers.append(i)
i <<= 1
return split_powers
@SteadBytes
SteadBytes / binary_search_insert.py
Created October 17, 2017 10:32
Binary Search Insert
def binary_search_insert(target, seq):
"""Finds position to insert an item into a **descending** sorted list
Args:
target : value to find insert position of
seq : descending sorted list
Returns:
int: Index of position to insert target to maintain sort
"""
l_index = 0
@SteadBytes
SteadBytes / reverse_int.py
Last active October 19, 2017 16:30
Mathematical and String-based methods for reversing the digits of an integer
import timeit
# reverse the digits of an integer i.e 1234 -> 4321
# String method turns out to be faster! (timer code if if __name__=='main' section)
def reverse_int_math(x):
"""Reverses the digits of an integer mathematically
Args:
x (int): integer to reverse
Returns:
@SteadBytes
SteadBytes / int_to_bin.py
Created November 8, 2017 07:31
algorithm for converting integer to binary string (python3)
def int_to_bin(x):
if x == 0: return "0"
s = ''
while x:
if x & 1 == 1:
s = "1" + s
else:
s = "0" + s
x //= 2
return s
@SteadBytes
SteadBytes / is_prime.py
Created November 12, 2017 08:46
Few methods for testing primality, with timing for comparison
import timeit
import math
def is_prime(n):
if n <=1:
return False
elif n <=3:
return True
elif n % 2 ==0 or n%3 == 0:
return False
i = 5
@SteadBytes
SteadBytes / factorial.py
Last active November 17, 2017 18:30
Timing comparisons for recursive and iterative factorial functions
import timeit
# Add a memoization method?
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
@SteadBytes
SteadBytes / timeit_boilerplate.py
Last active December 11, 2017 09:01
Boilerplate code for using the timeit module within a python module
import timeit
def func(x):
pass
def func_2():
pass
def wrapper(func, *args, **kwargs):
def wrapper():
@SteadBytes
SteadBytes / multiple_value_order.py
Created December 24, 2017 08:20
Order by multiple values
lis = [(18, 4), (19, 4), (14, 3), (15, 3), (31, 3), (25,4)]
# sort by tuple[1], then tuple[0]
print(sorted(lis, key=lambda x: (x[1], x[0])))
# >>> [(14, 3), (15, 3), (31, 3), (18, 4), (19, 4), (25, 4)]
# also use for max()/min()
print(max(lis, key=lambda x: (x[1], x[0])))
# >>> (25, 4)
print(min(lis, key=lambda x: (x[1], x[0])))
# >>> (14, 3)
@SteadBytes
SteadBytes / django_url_viewnames.py
Created March 9, 2018 08:56
Programatically retrieve Django View names from urlconfs
from django.urls import URLResolver, URLPattern, include
from django.conf import settings
from importlib import import_module
def view_names_from_urlpatterns(urlpatterns, names=None):
""" Get all view names specified by a set of given urlpatterns
# import some urlpatterns
>>> from myapp.urls import urlpatterns