content stringlengths 42 6.51k |
|---|
def find_largest_keys(root, k):
"""
Question 15.4: Find k largest keys in bst
"""
largest = []
stack = []
done = False
while not done and len(largest) < k:
if root:
stack.append(root)
root = root.right
else:
if len(stack):
... |
def GetFullPartitionSize(partition, metadata):
"""Get the size of the partition including metadata/reserved space in bytes.
The partition only has to be bigger for raw NAND devices. Formula:
- Add UBI per-block metadata (2 pages) if partition is UBI
- Round up to erase block size
- Add UBI per-partition meta... |
def calculate_iteration_count(total_recs, max_rec):
"""
Given the total number of records and the maximum records allowed in a granule,
calculates the number of iterations required to traverse the entire array in chunks of size max_rec
@param total_recs The total number of records
@param max_rec The... |
def check_name_in_string(name, string):
"""
Check whether the name string is a substring of another string (i.e. wikipedia title)
"""
return int(name.lower() in string) |
def trapezoid_area(base_minor, base_major, height):
"""Returns the area of a trapezoid"""
# You have to code here
# REMEMBER: Tests first!!!
return ((base_major+base_minor) / 2 ) * height |
def calculate_receptive_field(kernel_size, n_layers, stride, dilations=[]):
""" Given model parameters, calcualte the receptive field.
Args:
model (obj): model object.
"""
if len(dilations) == 0:
dilations = [1 for n in range(n_layers)]
receptive_field = kernel_size
for n i... |
def _nullable_list_intersection(list_1, list_2):
"""
Returns the intersection of 2 nullable lists.
Parameters
----------
list_1 : `None` or `list` of ``DiscordEntity``
First list.
list_2 : `None` or `list` of ``DiscordEntity``
First list.
Returns
-------
int... |
def insert_underscores(num: str) -> str:
"""Given a number, insert underscore every 3 digit after decimal dot
"""
# add underscore before .
if "." in num:
idx = num.index(".")-3
else:
idx = len(num) - 3
while idx > 0:
num = num[:idx] + "_" +num[idx:]
idx -= 3
... |
def label2string(labels):
"""
List of labels to a comma-saperated string.
"""
if labels is not None:
for _i in labels:
if "," in _i:
raise ValueError(
"The labels must not contain a comma as it is used "
"as the separator for th... |
def sanity_check(vars, cons):
""" Check all variables participate in some constraint """
v_con = []
for c in cons:
for x in cons[c]['scope']:
if x not in v_con:
v_con.append(x)
for v in vars:
if v not in v_con:
return False
return True |
def find_first_slice_value(slices, key):
"""For a list of slices, get the first value for a certain key."""
for s in slices:
if key in s and s[key] is not None:
return s[key]
return None |
def onBoard(top, left=0):
"""Simplifies a lot of logic to tell if the coords are within the board"""
return 0 <= top <= 9 and 0 <= left <= 9 |
def rax_clb_node_to_dict(obj):
"""Function to convert a CLB Node object to a dict"""
if not obj:
return {}
node = obj.to_dict()
node['id'] = obj.id
node['weight'] = obj.weight
return node |
def make_airline_link(carrier_names):
"""Creates a string for an airline's website.
>>> carrier_names = []
>>> make_airline_link(carrier_names)
[]
>>> carrier_names = [[u'Alaska', u'Airlines', u'Inc.'], [u'United', u'Airlines', u'', u'Inc.']]
>>> make_airline_link(carrier_names)
[u'alaskaa... |
def flatten(l):
"""Flattens list l.
:type l: list
:arg l: list to flatten
:rtype: list
:return: flattened list
"""
return [item for sublist in l for item in sublist] |
def polarity(num):
"""
Returns the polarity of the given number.
"""
if num > 0:
return 1
if num < 0:
return -1
return 0 |
def seconds_to_datetime(seconds):
"""
Updates the information from seconds to 00:00:00 time
:param seconds:
:return:
"""
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
h, m, s = int(h), int(m), int(s)
if h < 0:
h = m = s = 0
if h < 10:
h = "0" + str(h)
els... |
def get_localized_name(name):
"""Returns the localizedName from the name object"""
locale = "{}_{}".format(
name["preferredLocale"]["language"],
name["preferredLocale"]["country"]
)
return name['localized'].get(locale, '') |
def l1_distance(x1, y1, x2, y2):
""" Returns l1 (manhattan) distance."""
return abs(x1-x2) + abs(y1-y2) |
def sign(x):
"""
Devuelve el signo de x (0 se interpreta con signo positivo)
x>=0 --> +1
x <0 --> -1
"""
if x<0:
return -1
elif x>=0:
return +1 |
def parse_unknown_options(args):
"""
Args:
args:
"""
warning_message = 'Please provide args with a proper ' \
'identifier as the key and the following structure: ' \
'--custom_argument="value"'
assert all(a.startswith('--') for a in args), warning... |
def removeInvertedPaths(mpDict):
"""
Find the number of paths of this type joining
the nodes in the sample
:param mpDict: dict {str: [int, bool]},
key, str - name of the metapath
value, [int, bool] - which matrix file to use, and
whether to use the transpose (inverse path)
:return: m... |
def compose_remove_description(document: dict) -> str:
"""
Compose a change description for removing an OTU.
:param document: the OTU document that is being removed
:return: a change description
"""
name = document["name"]
abbreviation = document.get("abbreviation")
description = f"Re... |
def is_valid_method_param(met):
""" Checks if method is compatible with GROMACS """
methods = ['linkage', 'jarvis-patrick', 'monte-carlo', 'diagonalization', 'gromos']
return met in methods |
def get_size_and_path(line):
"""From a 'ls -l' line, return columns 4 (size) and 8 (path)."""
cols = line.split()
size, path = (int(cols[4]), cols[8])
return size, path |
def parse_coordinate(coordinate):
"""
Args:
coordinate (str):
Example:
>>> parse_coordinate('chr1:153500000-153501000, chr1:153540000-153542000')
['chr1', 153500000, 153501000, 153540000, 153542000]
Return:
A list [chromosome] + [coordinate of four corners]
"""
... |
def _expand_prefix(all_names, prefix):
"""Expand the given prefix into real entity names.
Args:
all_names: A list of all entity names.
prefix: A prefix string.
Returns:
A list of entity names that the pattern expands to.
"""
return [name for name in all_names if name.startswith(prefix)] |
def miller_rabin(n):
""" primality Test
if n < 3,825,123,056,546,413,051, it is enough to test
a = 2, 3, 5, 7, 11, 13, 17, 19, and 23.
Complexity: O(log^3 n)
"""
if n == 2: return True
if n <= 1 or not n&1: return False
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
d = n -... |
def parse_batch_parse_statement_forwarder_id_url(json):
"""
Extract the statement forwarder ids from a batch of statement forwarders.
:param json: JSON text to parse.
:type json: list(dict(str, dict(str, str)))
:return: List of ids from statement forwarders.
:rtype: list(dict(str, str))
"""... |
def checkPatch_index_inImages_naive(Index_check_patch,Num_patches_perImage,num_col,num_row):
"""check the index of patches and the size of images"""
aaa = int(Index_check_patch/Num_patches_perImage)
nn = Index_check_patch%Num_patches_perImage
bbb = int(nn/num_col)
ccc = nn%num_col
# aaa = np.wher... |
def searching(value, liste):
""" filter the items that math the search term """
list1 = []
for item in liste:
if value.lower() in item.lower():
list1.append(item)
return list1 |
def str_int_dpid(str_dpid):
"""Return stringified int version, of int or hex DPID from YAML."""
str_dpid = str(str_dpid)
if str_dpid.startswith('0x'):
return str(int(str_dpid, 16))
else:
return str(int(str_dpid)) |
def underscore_join( strlis ):
"""Uses an underscore to join a list of strings into a long string."""
return '_'.join( [s for s in strlis if s is not None and len(s)>0] ) |
def return_calibration_type(model_name):
"""
Returns metadata for the calibration dataset for specified model
Parameters
----------
model: string
String of model name.
Returns
-------
dict
Metadata on the dataset. A dict is returned with parameters CO2, H2O, or mixed set to True or False.
True indicat... |
def get_filter_constant(interval_distance, track_size):
"""Given a track size and distance, determine the minimum allowable
split duration.
"""
# Determine constant of impossible time for 400m off of distance.
if interval_distance <= 200:
constant = 20
elif interval_distance > 200 and in... |
def IsValidTimezone(timezone):
"""
Checks the validity of a timezone string value:
- checks whether the timezone is in the pytz common_timezones list
- assumes the timezone to be valid if the pytz module is not available
"""
try:
import pytz
return timezone in pytz.common_timezones
... |
def type_error_message(func_name: str, expected: str, got: str) -> str:
"""Return an error message for function func_name returning type got,
where the correct return type is expected."""
return ('{0} should return a {1}, but returned {2}' +
'.').format(func_name, expected, got) |
def excstr(e):
"""Return a string for the exception.
The string will be in the format that Python normally outputs
in interactive shells and such:
<ExceptionName>: <message>
AttributeError: 'object' object has no attribute 'bar'
Neither str(e) nor repr(e) do that.
"""
if e is No... |
def _check_geo_type_suffix(x):
""" Checks if `geo_type` suffix contains an `int` """
try:
return int(x)
except:
raise ValueError(f"`geo_type` suffix: '{x}' cannot be parsed as `int`.") |
def build_jrn(**kwargs):
"""
Composes a journal file of journal blocks.
:param kwargs: journal blocks
:return:
"""
jrn_text = "\n".join(kwargs.values())
return jrn_text |
def point_in_rectangle(point, rect_top_left, rect_sides):
"""
Checks if point is in rectangle
Parameters
----------
point : (float, float)
(x,y) coordinates of point
rect_top_left : (float, float)
(x,y) coordinates of rectangle top left corner
rect_sides : (float, float)
... |
def f(x):
"""
Function x^3 - x -2
Example function.
"""
return x**3. - x - 2 |
def add(vec_1, vec_2):
"""
This function performs vector addition. This is a good place
to play around with different collection types (list, tuple, set...),
:param vec_1: a subscriptable collection of length 3
:param vec_2: a subscriptable collection of length 3
:return vec_3: a subscriptable ... |
def _format_result(result):
"""Format result into string for templating."""
# do not include decimal if it's 100
if result == 100:
return "100"
return "{:.1f}".format(result) |
def convert(list):
""" Converts list to categories. """
categories = dict()
for dict_row in list:
try:
categories[tuple(dict_row[0][0])][dict_row[1]].append(dict_row[2][0])
except KeyError:
try: categories[tuple(dict_row[0][0])][dict_row[1]] = [dict_row[2][0]]
... |
def fibonacci_comprehension(limit):
"""fibonacci sequence using a list comprehension."""
sequence = [0, 1]
[sequence.append(sequence[i] + sequence[i-1]) for i in range(1, limit)]
return sequence[-1] |
def constant_increment_growth_rule(increment, level):
"""
The number of samples in the 1D quadrature rule where number of of points
grow by a fixed constant at each level.
Parameters
----------
level : integer
The level of the quadrature rule
Return
------
num_samples_1d : ... |
def unwrap_hash( signals ):
"""
unwrap all signals in a hash array and return a copy
"""
# make a copy
list_of_unwrapped_signals = signals.copy()
# create a new list of signals
for key, signal in list_of_unwrapped_signals.items():
list_of_unwrapped_signals[key] = signal.unwrap... |
def prep_msg(msg):
""" Prepare a string to be sent as a message """
msg += '\0'
return msg.encode('utf-8') |
def binarySearch(arr, val):
"""
array values must be sorted
"""
left = 0
right = len(arr) - 1
half = (left + right) // 2
while arr[half] != val:
if val < arr[half]:
right = half - 1
else:
left = half + 1
half = (left + right) // 2
if arr[ha... |
def cles(lessers, greaters):
"""
# code from https://github.com/ajschumacher/cles
# explanation from https://janhove.github.io/reporting/2016/11/16/common-language-effect-sizes
the probability that a score sampled at random from one distribution will be greater than a score sampled from some other distr... |
def length(tree):
"""
Count the total number of the tree nodes.
:param tree: a tree node
:return: the total of nodes in the subtree
"""
if tree:
n_nodes = length(tree.left)
n_nodes += length(tree.right)
n_nodes += 1
return n_nodes
return 0 |
def usi32_to_si32(val):
"""
Quick hack to read the signed val of an unsigned int (Image loads all ints from bytes as unsigned ints)
:param val:
:return:
"""
bits = 32
if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
val = val - (1 << bits) ... |
def boundary_check(string, start_idx, end_idx):
"""
Check that given character indexes into given string
align with token boundaries.
"""
start_clause = (
(start_idx == 0) or
string[start_idx-1] in (" ", ",")
)
end_clause = (
(end_idx == len(string)) or
strin... |
def triangular2(cycle):
"""
triangular2 for cyclic LR. https://arxiv.org/abs/1506.01186
"""
return 1.0 / (2.**(cycle - 1)) |
def build_search_query(query, page, per_page) -> dict:
"""
Build the multi-search query for Elasticsearch.
:param str query:
:param int page:
:param int per_page:
"""
search_query = {
"query": {"multi_match": {"query": query, "fields": ["*"]}},
"from": (page - 1) * per_page,
... |
def encoding_description(encoding):
"""
Function to create a meaningful description that will be added as variable attribute into the netcdf file.
Input:
------
encoding: dict
Dictionary with the encoding specifications for each variable.
"""
labels = {
32001: "BLOSC",
... |
def split_first_level(dependency):
"""
Internal function to split nested dependencies.
:param dependency:
:return:
"""
dependencies = []
beginning = 0
level = 0
for i in range(len(dependency)):
if dependency[i] == ',' and level == 0:
dependencies.append(dependency... |
def handle_status_update(loan_id, new_status):
"""Handles a status update for an order."""
# lookup order in your system using loan_id
# set order status to new_status
return '' |
def sort_and_cut_by_cluster(row, N, fractions):
"""Pick the top N items from a cluster.
This function returns the top N * fractions[cluster] items
Args:
row (rdd object): row has the form
((user, cluster), iterator_over((user, cluster, item, rating)))
N (int): number of items d... |
def add_auth_response(appreq=None, auth_obj=None):
"""Called after init_actingweb() if add_response was set to False, and now responses should be added."""
if not appreq or not auth_obj:
return False
appreq.response.set_status(auth_obj.response['code'], auth_obj.response['text'])
if auth_obj.res... |
def FWHMeff2FWHMgeom(FWHMeff):
"""
Convert FWHMeff to FWHMgeom.
This conversion was calculated by Bo Xin and Zeljko Ivezic
(and will be in an update on the LSE-40 and overview papers).
Parameters
----------
FWHMeff: float
the single-gaussian equivalent FWHM value, appropriate for ca... |
def is_truthy(data):
"""
Returns True if the data is a truthy value, False otherwise.
"""
string = str(data).lower()
return string in ['true', '1', 'f'] |
def n_tasks(dec_num):
"""
Takes a decimal number as input and returns the number of ones in the
binary representation.
This translates to the number of tasks being done by an organism with a
phenotype represented as a decimal number.
"""
bitstring = ""
try:
bitstring = dec_num[2:... |
def check_if_elements_is_empty(json_object):
"""
Checks to see that there are elements in the json object
:param json_object:
:return: bool false if not empty, true if empty
"""
try:
if len(json_object) > 0:
is_empty = len(json_object['elements']) == 0
else:
... |
def _configure_title_plotly(title, font_size, color="black"):
"""Helper function for plot_surf with plotly engine.
This function configures the title if provided.
"""
if title is None:
return dict()
return {"text": title,
"font": {"size": font_size,
"color":... |
def choices_to_list(choices):
"""Transforms choices dict to an ordered string list.
Arguments:
choices -- the dict containing available choices
"""
return list(map(str, sorted(choices.keys(), key=lambda v: v if type(v) == int else -1))) |
def allocz(size):
"""Alloc zeros with range"""
return [0 for _ in range(size)] |
def _build_underline(data: str, md: bool = False, emoji: bool = False) -> str:
"""An internal function to return a rough estimate of an underline"""
text_len = len(data)
add = 3
if md and not emoji:
add = 0
elif emoji and not md:
add = 7
elif emoji and md:
add = 1
ret... |
def complexns_core(strin):
"""
Improved conversion of string representation of complex numbers to float
"""
try:
return complex(strin)
except ValueError:
pass
# Do the simple fixes
tmp = strin.replace(' ', '').replace('*','')
tmp = tmp.lower().replace('i','j')
... |
def gray_code(n):
"""Finds Gray Code of n!"""
return n ^ (n >> 1) |
def update_fluent_cached_urls(item, dry_run=False):
"""
Regenerate the cached URLs for an item's translations. This is a fiddly
business: we use "hidden" methods instead of the public ones to avoid
unnecessary and unwanted slug changes to ensure uniqueness, the logic for
which doesn't work with our ... |
def factorial(n):
"""
factorial: calculate factorial n!
:param n: input number
:return: n!
"""
fact=1
for i in range(1,n+1):
fact*=i
return fact |
def split(formula):
"""
>>> split('CNNCB')
('CNN', 'NCB')
>>> split('NNCB')
('NNC', 'CB')
>>> split('NCB')
('NC', 'CB')
"""
left = formula[:len(formula) // 2 + 1]
right = formula[len(left) - 1:]
return left, right |
def load_mapeval_runtimes(map_times_path):
"""
read the runtimes out of map_times.txt, assuming first line is a header
"""
try:
map_times_dict = {}
with open(map_times_path) as map_times_file:
lines = [line for line in map_times_file]
for line in lines[1:]:
... |
def delimiter_check(line):
"""
Determines what delimiter is used in given text file.
Parameters:
-line: str
line from text file
Returns:
-delim: str
delimiter used in text file
Notes:
Recognizes only ",", "\t", and ":" delimiters.
"""
if ',' in line:
delim = ','
elif '\t' in lin... |
def closestMatch(value, _set):
"""Returns an element of the set that is closest to the supplied value"""
a = 0
element = _set[0]
while(a<len(_set)):
if((_set[a]-value)*(_set[a]-value)<(element-value)*(element-value)):
element=_set[a]
a = a + 1
return element |
def p_value(y, l2):
"""
returns p-value for each response based on l2
:param y: The value for which the p-value is to be computed
:param l2: The list of values on which the p-value calculation is based
:return: The calculated p-value
"""
return (len([x for x in l2 if x >= y]) + 1) / (len(l2)... |
def draw_progressbar(value, total, width=40):
"""Visualize progess with a progress bar.
:param value:
The current progress as a fraction of total.
:type value:
int
:param total:
The maximum value that 'value' may obtain.
:type total:
int
:param width:
The... |
def safe_compare(val1, val2):
"""
Compares two strings to each other.
The time taken is independent of the number of characters that match.
:param val1: First string for comparison.
:type val1: :class:`str`
:param val2: Second string for comparison.
:type val2: :class:`str`
:returns: ... |
def color_match_threshold(pxl, ink, thresh):
""" Returns True if both colors match, within threshhold
"""
diff = [0, 0, 0]
diff[0] = abs(pxl[0] - ink[0])
diff[1] = abs(pxl[1] - ink[1])
diff[2] = abs(pxl[2] - ink[2])
#print diff
if(diff[0]+diff[1]+diff[2]) <= thresh:
return True
return False |
def stringListToFloat(stringList):
"""Converts a list with strings into a list with floats."""
return [float(singleFloatResult) for singleFloatResult in stringList] |
def parse_struct_stat(stats):
"""Parse the structure of stats values in influencer data in order
to return a pandas-compatible object.
"""
data = dict()
for stat in stats:
for item in stats[stat]:
for metric in item['counts']:
data.setdefault(metric, dict())
... |
def get_probes(probes):
"""Returns a list of probes"""
probe_list = ['a', 'b', 'c', 'd', 'e']
thprobe_list = ['tha', 'thb', 'thc', 'thd', 'the']
ans_list = []
if not isinstance(probes, (list, tuple)):
probes = [probes]
for p in probes:
p = p.lower()
if p == '*':
... |
def lib_ext(shared):
"""Returns the appropriate library extension based on the shared flag"""
return '.a' if not shared else '.so' |
def observation(s, breaks, matrix, memo):
"""
Returns an observation using memoization, based on the parameter s.
"""
b = len(breaks)
if b == 0 or s <= breaks[0]:
return s * matrix[0]
for i in range(1, b + 1):
if i == b or s <= breaks[i]:
j = (i - 1) * 2
r... |
def rgb_to_hex(r: float, g: float, b: float) -> int:
"""Convert the color from RGB coordinates to hexadecimal."""
return int(r * 255) << 16 | int(g * 255) << 8 | int(b * 255) |
def power_law_vband_norm(x, slope):
"""Power law normalised at 0.55 microns (V band)."""
return (x / 0.55) ** slope |
def format_float(value: float, precision: int = 4) -> str:
"""
Formats a float value to a specific precision.
:param value: The float value to format.
:param precision: The number of decimal places to use.
:return: A string containing the formatted float.
"""
return f'{value:.{precision}f}' |
def conv_if_neg(x):
"""returns abs of an angle if it's negative"""
#sin(-x) = -sin(x)
if x<0:
return abs(x), True
return x,False |
def assemble_pair(label, value):
"""Assemble a 'pair' Celery Script node (for use as a body item)."""
return {
'kind': 'pair',
'args': {'label': label, 'value': value}} |
def remove_unused_samples(music):
"""
Remove samples that are never used
Depends: to_mod_format
"""
# Get a list of samples actually referenced (each entry is unique)
referenced_samples = []
for chan in music['mod']['channels']:
for sample_idx in chan:
if sample_idx not in referenced_samples:
reference... |
def list_results(query_results):
"""
keys :: [key]
= all possible keys. (Not all results will contain all keys).
results :: [dict]
= list of results
"""
keys = query_results['head']['vars']
results = query_results['results']['bindings']
return keys, results |
def get_qualified_name(names):
"""
``get_qualified_name`` gets a qualified name for the provided name list.
:param names: name list to qualify
:type names: list(str)
:return: a qualified name
:rtype: str
:Example:
>>> type, name = demangle_ms(Architecture["x86_64"], "?testf@Foobar@@SA?AW4foo@1@W421@@Z")
>>... |
def levenshtein_distance(s1, s2):
"""
The minimum amount of edits needed to make s2 into s1.
Args:
s1: string
s2: string
Returns:
int
"""
if len(s1) > len(s2):
s1, s2 = s2, s1
distances = range(len(s1) + 1)
for index2, char2 in enumerate(s2):
... |
def filter_credit_score(credit_score, bank_list):
"""Filters the bank list by the mininim allowed credit score set by the bank.
Args:
credit_score (int): The applicant's credit score.
bank_list (list of lists): The available bank loans.
Returns:
A list of qualifying bank lo... |
def deserialize_datetime(string):
"""Deserializes string to datetime.
The string should be in iso8601 datetime format.
:param string: str.
:type string: str
:return: datetime.
:rtype: datetime
"""
try:
from dateutil.parser import parse
return parse(string)
except ImportError:
... |
def format_vault_encrypted_secret(vault_encrypted_secret):
""" returns a prettier vault secret """
# Clean up spacing on the ansible vault line
vault_encrypted_secret = vault_encrypted_secret.replace(" $ANSIBLE_VAULT", " $ANSIBLE_VAULT")
return vault_encrypted_secret |
def char_collect_all(s, c):
"""
Find all appearances of char in string
:param s: haystack
:param c: needle
:return: list of indices
"""
start = 0
res = []
clen = len(c)
while True:
start = s.find(c, start)
if start == -1: break
res.append(start)
st... |
def task9(a: int) -> int:
"""
Function that computes the value of a+aa+aaa+aaaa with a given digit as the value of a
Input: digit as integer
Output: number as integer
"""
pierwsza = a
druga = int(str(a)*2)
trzecia = int(str(a) * 3)
czwarta = int(str(a) * 4)
wynik = pierwsza + dr... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.