My answer suggests that the meaning of life regards spending your life with a loved one.
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')
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.
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 abovefor split_char in split_chars:split_parts = text.split(split_char)# skip if there was nothing to split onif len(split_parts) == 1:continueparts = []# break up the string in to it's partsfor i, item in enumerate(split_parts):# save the first part but don't change the case of the first letterif i == 0:parts.append(item)continueparts.append(f"{item[0].upper()}{item[1:]}")# join the parts and overwrite the text variable for the next looptext = "".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]
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
from operator import le, gt
from statistics import mean
# test.assert_equals(actual, expected, [optional] message)
@test.describe('Branchless Accumulative AND/AND NOT Conditions')
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(br_and(50/1.80**2, le, (30,25,18.5), ('Obese', 'Overweight', 'Normal', 'Underweight')), 'Underweight')
test.assert_equals(br_and(mean((95, 90, 93))//1, gt, (59,69,79,89), ('F','D','C','B','A')), 'A')