Ad
Fundamentals
Code
Diff
  • def numberprint(x):
        n = ''.join(map(str, range(1,x)))
        return int(f'{n}{x}{n[::-1]}')
    • def numberprint(x):
    • nor = 0
    • final = ''
    • while nor < x:
    • nor += 1
    • final += str(nor)
    • while nor > 1:
    • nor -= 1
    • final += str(nor)
    • return int(final)
    • n = ''.join(map(str, range(1,x)))
    • return int(f'{n}{x}{n[::-1]}')

My answer suggests that the meaning of life regards spending your life with a loved one.

Code
Diff
  • from statistics import mean
    
    LIVES   = 1
    SPAN    = 72
    MONTHS  = 12
    DAYS    = 7
    COMPANY = 2 #3's a crowd
    
    def meaning_of_life_is(ing=range(LIVES,SPAN//MONTHS*DAYS*COMPANY)):
        return '{}'.format(mean(ing), 'of', 'life')
    • def meaning_of_life_is():
    • return """
    • go crazy with your imagination and return anything you like.
    • strings, numbers, ... just don't return None.
    • may the most creative answer win
    • """
    • from statistics import mean
    • LIVES = 1
    • SPAN = 72
    • MONTHS = 12
    • DAYS = 7
    • COMPANY = 2 #3's a crowd
    • def meaning_of_life_is(ing=range(LIVES,SPAN//MONTHS*DAYS*COMPANY)):
    • return '{}'.format(mean(ing), 'of', 'life')
Fundamentals
Strings
Algorithms

A palindrome is when a series of characters are the same backwards and forwards. The most simple way to determine if something is a palindrome is to reverse the series of characters and check for equality with the original.

Code
Diff
  • palindrome=lambda w:(t:=str(w))==t[::-1]
    • palindrome=lambda w:str(w)==str(w)[::-1]
    • palindrome=lambda w:(t:=str(w))==t[::-1]
Code
Diff
  • def remove(i, v):
        sv = set(v)
        return list(filter(lambda x: not x in sv, i))
    • def remove(integers, values):
    • value_set = set(values)
    • res = []
    • for x in integers:
    • if x not in value_set:
    • res += [x]
    • return res
    • def remove(i, v):
    • sv = set(v)
    • return list(filter(lambda x: not x in sv, i))
Code
Diff
  • from re import findall as fa
    
    # This can all be done on one line. I expanded it to be easier to read.
    def to_camel_case(text:str, fw=False) -> str:
        out = ''
        for w in fa(r'([a-zA-Z0-9]*)', text):
            out = f'{out}{(w, w.capitalize())[fw]}'
            fw  = True
        return out
    • def to_camel_case(text: str):
    • split_chars = "-_"
    • # loop over each character above
    • for split_char in split_chars:
    • split_parts = text.split(split_char)
    • # skip if there was nothing to split on
    • if len(split_parts) == 1:
    • continue
    • parts = []
    • # break up the string in to it's parts
    • for i, item in enumerate(split_parts):
    • # save the first part but don't change the case of the first letter
    • if i == 0:
    • parts.append(item)
    • continue
    • parts.append(f"{item[0].upper()}{item[1:]}")
    • # join the parts and overwrite the text variable for the next loop
    • text = "".join(parts)
    • return text
    • from re import findall as fa
    • # This can all be done on one line. I expanded it to be easier to read.
    • def to_camel_case(text:str, fw=False) -> str:
    • out = ''
    • for w in fa(r'([a-zA-Z0-9]*)', text):
    • out = f'{out}{(w, w.capitalize())[fw]}'
    • fw = True
    • return out

The point of this function is to handle situations where a value is compared against a series of consistently decreasing or increasing values, in a branchless way.

For example - Let's assume you had a bunch of grades and you wanted to return the letter representation of those grades. The most generic solution would be to write a stream of conditional statements, each returning the appropriate grade letter. That certainly would work, but it would not be branchless.

Instead, every condition is checked, and the sum of initial consecutive True conditions is used as the index for an iterable of choices to return from. The trickiest part of this is making sure that once aFalse condition is encountered no remaining conditions will be recognized as True. To make it even more dynamic it should also be possible to determine if each condition is not True.

Note that AND NOT is not the same thing as NAND. It may be functionally the same (maybe), but it is not logically the same. This is evidenced by sum. sum is adding all the True statements together which is base 10 AND logic.

Essentially, using the functions "NOT" (_) argument will result in this logic:

value AND (NOT next value) AND (NOT next value)...

'''
BRANCHLESS AND/AND NOT CONDITIONS - OysterShucker
returns a value from a list/tuple indexed by the sum of initial consecutive True conditions
    val    : the comparator
    op     : operator to use
    comp   : list/tuple of contrasts or comparisons = (`if`,`and`,`and`,...)
    choices: list/tuple of choices to return from   = (`else`,`if`,`and`,`and`,...)
    _      : reverse condition value (not)
'''
def br_and(val, op, comp, choices, _=0):
    return (l:=1, choices[sum(l:=l*(op(val, cval)^_) for cval in comp)])[-1]