Weekly Challenge

Solve the challenge, share your solution and summit the ranks of our Community!
New content is available in Academy! You may need to clear your browser cache for an optimal viewing experience

Challenge #192: Crane Leasing

Highlighted
Atom

My solution:

 

 

Highlighted
Asteroid
Spoiler

Kale_Malcom_0-1575562849787.png

 

Highlighted
Alteryx Partner
 
Highlighted
Meteor
Spoiler
LiD_0-1575573049498.png
Highlighted
Meteor
Spoiler
Weekly Challenge 192.PNG
Highlighted
Fireball

Having done this type of thing for years in SAS, I wanted to try this one in pandas/python in the Python tool.

 

 

Spoiler
#################################
# 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)

 

 

Highlighted
Alteryx
Alteryx

My Solution.

 

Highlighted
Asteroid

My solution :

Spoiler
solution.png

Highlighted
Meteoroid
 
Highlighted
Spoiler
Spoiler
Here is my solution:
Spoiler
 

EwanHarris_0-1575642362065.png