Skip to content

Instantly share code, notes, and snippets.

@lrhache
Created January 8, 2016 15:37
Show Gist options
  • Save lrhache/63a28e5a1276f0730a5a to your computer and use it in GitHub Desktop.
Save lrhache/63a28e5a1276f0730a5a to your computer and use it in GitHub Desktop.
Convert weekdays as bit representation
weekdays = ('Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday', )
def convert_choice(*args):
"""Convert weekdays representation to 127 bits
Example:
>> # Monday, Tuesday, Friday, Sunday
>> bw.convert(1, 1, 0, 0, 1, 0, 1)
83
>> # Monday, Tuesday
>> bw.convert(True, True)
3
"""
bits = sum([2**i for i, a in enumerate(args) if a])
return bits
def choice_indexes(choice):
return (i for i in range(0, 7) if 2**i & choice)
def choice_as_string(choice):
indexes = choice_indexes(choice)
return (weekdays[i] for i in indexes)
def is_day_in_choice(day, choice):
indexes = choice_indexes(choice)
if day in indexes:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment