Skip to content

Instantly share code, notes, and snippets.

View rosemichaele's full-sized avatar
👋

Michael Rose rosemichaele

👋
View GitHub Profile
@rosemichaele
rosemichaele / fast_inv_sqrt.py
Last active August 30, 2017 19:28
I came across the fast inverse square root algorithm after reading about it on Quora, and I decided to take a stab at implementing it in Python 3.
from struct import pack, unpack
def inv_sqrt(n: float) -> float:
n_half = n / 2
n_hex = hex(unpack('>l', pack('>f', n))[0])
magic_num = '0x5f3759df'
guess_int = int(magic_num, 16) - (int(n_hex, 16) >> 1)
guess_float = unpack('>f', pack('>l', guess_int))[0]
better_guess = guess_float*(1.5 - n_half*guess_float**2)
return better_guess
@rosemichaele
rosemichaele / calkin_wilf_sequence.py
Last active May 7, 2021 20:57
Use a generator function for the Calkin-Wilf sequence to determine where in the sequence a given rational number lies.
def calkin_wilf_sequence(number):
def fractions():
from fractions import Fraction
i = Fraction(1/1)
while True:
yield [i.numerator, i.denominator]
i = Fraction(1 / (2*int(i) - i + 1))
gen = fractions()
res = 0
@rosemichaele
rosemichaele / n_queens.py
Created September 6, 2017 14:19
An adaptation of the eight queens puzzle for any positive integer n > 3.
from itertools import permutations, dropwhile
def n_queens(n: int) -> list:
"""An adaptation of the eight queens puzzle for any positive integer n > 3.
:param - n: a positive integer representing the number of rows, columns, and queens to solve for.
:return - queen_squares: a list of 2-tuples representing the (0-based) squares to place the queens on, such that no
queen is attacked / guarded by another."""
if n < 4:
return ["Do it yourself."]
@rosemichaele
rosemichaele / timing.py
Created September 6, 2017 14:20
Timing function intended to be used as a decorator.
def timing(f):
from time import time
def timed(*args, **kwargs):
start = time()
r = f(*args, **kwargs)
end = time()
print("Time elapsed: {} seconds".format(end-start))
return r
return timed
@rosemichaele
rosemichaele / xml_parsed_to_list.py
Last active September 7, 2017 16:30
Parse input XML string to list of tags(attributes) accounting for number of parent tags w/ preceding dashes.
from xml.parsers.expat import ParserCreate, ExpatError, errors
from functools import partial
def start_element(name, attrs, out, depth):
depth.append(1)
if out.get(name, None) == None:
out[name] = {}
out[name]["attributes"] = set()
out[name]["loc"] = len(out) - 1
out[name]["depth"] = sum(depth)
@rosemichaele
rosemichaele / clear_json_values.py
Last active September 7, 2017 18:25
Remove and return field values in JSON formatted text.
import json
from collections import OrderedDict
def replace_values(d):
for k in d:
if isinstance(d[k], dict):
d[k] = replace_values(d[k])
elif isinstance(d[k], float):
d[k] = int()
else:
@rosemichaele
rosemichaele / parse_directory_paths.py
Last active November 29, 2017 18:56
Parses an input list of directory paths and returns a list of strings with information about the paths: See https://codefights.com/fight/iM6TDRMYJcbrTv23j/ for more details.
from collections import OrderedDict
def parse_directory_paths(calls):
calls_dict = OrderedDict()
paths = []
for c in calls:
s = c.split("/")[1:]
paths.append(s)
for p in paths:
if p[0] not in calls_dict:
calls_dict[p[0]] = {}
@rosemichaele
rosemichaele / uiautomator2_scroll_logs.txt
Created August 28, 2020 19:08
UiAutomator scrollIntoView debug logs
2020-08-28 17:01:28:921 - [HTTP] --> POST /wd/hub/session {"desiredCapabilities":{"automationName":"UiAutomator2","deviceName":"Android","platformName":"Android","appiumVersion":"1.17.0","newCommandTimeout":0,"orientation":"PORTRAIT","browserstack.is_hub_canary":"false","acceptSslCert":false,"detected_language":"appium/ruby_lib_core/3.11.0 (selenium/3.142.7 (ruby macosx))","new_bucketing":true,"device":"samsung galaxy s9","sessionName":"Android Event Type Location Details: Physical location appears in location section of Event Type details","buildName":"August 2020 Test Development","projectName":"Test Project","platform":"ANDROID","os_version":"8.0","chromeOptions":{"w3c":false},"browserstack.minOSVersion":"6.0","appPackage":"com.calendly.app.dev","appActivity":"com.calendly.app.splash.SplashActivity","bundleID":"com.calendly.app.dev","bundleId":"com.calendly.app.dev","nativeWebScreenshot":true,"version":"","mobile":{"browser":"mobile","version":"Samsung Galaxy S9-8.0"},"browserstack.appDownloadTimeout":"90"