Skip to content

Instantly share code, notes, and snippets.

@atatsu
Created November 29, 2016 05:14
Show Gist options
  • Save atatsu/cb6205b44809308e3e3d0063bc1f1f48 to your computer and use it in GitHub Desktop.
Save atatsu/cb6205b44809308e3e3d0063bc1f1f48 to your computer and use it in GitHub Desktop.
color of the day
#!/usr/bin/env python
# py3
"""
$ ./color_of_the_day.py -h
usage: color_of_the_day.py [-h] [--full-summary] days_in_future
Determines car color for future date.
positional arguments:
days_in_future Number of days in the future to determine color
optional arguments:
-h, --help show this help message and exit
--full-summary If supplied will also print all days leading up to the
supplied day
"""
import argparse
import sys
COLORS = [
'Red',
'Orange',
'Yellow',
'Green',
'Blue',
'Purple',
'Black',
]
def color_of_the_day(days_in_future):
"""
Through mystical and oft-forbidden incantations the car color
being manufactured on a given day in the future is divined.
Args:
days_in_future (int) - Number of days in the future to
predict the production color
Returns:
str - The production color for the given day in the future
"""
# if the future date is low enough (within the first five days)
# nothing special needs to be done so just take the color
# straight-up
if days_in_future < 5:
return COLORS[days_in_future]
# If dividing by a full week leaves us with with a remainder of
# 5 or 6 we know we're on a weekend
if days_in_future % 7 in (5, 6):
return 'No Color'
# If we made it this far we know that we're past a week,
# we just need to know by how many (so that we can subtract
# a corresponding number of weekend days)
days_offset = days_in_future // 7 * 2
adjusted = days_in_future - days_offset
# Before we can return a color, though, we need to know whether
# our colors need to roll over (if the target day in the adjusted
# future exceeds the length of the colors list).
if adjusted < len(COLORS):
# No need to roll over so just return as-is.
return COLORS[adjusted]
# Rollover needed, adjust accordingly.
adjusted = adjusted % len(COLORS)
return COLORS[adjusted]
def main():
# This stuff is housed in a function so my PEP8 linter
# doesn't yell at me :(
# Stupid simple arg parsing so why not.
parser = argparse.ArgumentParser(
description='Determines car color for future date.'
)
parser.add_argument(
'days_in_future',
type=int,
help='Number of days in the future to determine color'
)
parser.add_argument(
'--full-summary',
dest='full_summary',
action='store_true',
help=('If supplied will also print all days '
'leading up to the supplied day')
)
args = parser.parse_args()
if not args.full_summary:
color = color_of_the_day(args.days_in_future)
print(color)
sys.exit(0)
# add 1 so the actual target day is included in the summary
for day in range(args.days_in_future + 1):
color = color_of_the_day(day)
print(color)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment