Want to get involved? We're always looking for ideas and content for Weekly Challenges.
SUBMIT YOUR IDEAHaving done this type of thing for years in SAS, I wanted to try this one in pandas/python in the Python tool.
#################################
# List all non-standard packages to be imported by your
# script here (only missing packages will be installed)
# from ayx import Package
# #Package.installPackages(['pandas','numpy'])
#################################
from ayx import Alteryx
from datetime import datetime
from dateutil import parser
import pandas as pd
#################################
dfInput = Alteryx.read("#Input")
dfOutput = Alteryx.read("#Output")
expected_result = dfOutput.values.tolist()[0][0]
#################################
dfTinyTim = dfInput[(dfInput['Crane Name'] == "Tiny Tim") & (dfInput['Crane Status'] == "Leased")][['Start Date', 'End Date']]
#################################
dfMaximus = dfInput[(dfInput['Crane Name'] == "Maximus") & (dfInput['Crane Status'] == "Leased")][['Start Date', 'End Date']]
#################################
def start_end_list(s, e, mindt=datetime(2016,1,1), maxdt=datetime(2018,12,31)):
sdt = parser.parse(s)
edt = parser.parse(e)
# Limit dates to the 2016-2018 years
if edt >= mindt and sdt <= maxdt:
if sdt < mindt:
sdt = mindt
if edt > maxdt:
edt = maxdt
epoch = datetime(1970,1,1)
isdt = (sdt-epoch).days
iedt = (edt-epoch).days
return list(range(isdt,iedt+1))
else:
return []
#################################
tinyTimList = []
for r in dfTinyTim.values:
tinyTimList.extend(start_end_list(r[0], r[1]))
#################################
maximusList = []
for r in dfMaximus.values:
maximusList.extend(start_end_list(r[0], r[1]))
#################################
calculated_result = len(set(tinyTimList) & set(maximusList))
#################################
if expected_result == calculated_result:
print("SUCCESS!!! Found the correct number of days")
else:
print("ERROR!!! Try again")
#################################
dfOutput = pd.DataFrame({'Expected Result': [expected_result], 'Calculated Result': [calculated_result]})
#################################
Alteryx.write(dfOutput, 1)
My solution :