Need help debugging my date validation solution - getting incorrect results

I’m working on a coding challenge that involves parsing ambiguous date formats and finding the earliest valid date. My solution seems to work fine when I test it manually with different inputs, but the automated system keeps marking it as incorrect.

The problem requires taking three numbers separated by slashes and determining all possible valid date combinations, then returning the earliest one in YYYY-MM-DD format.

from itertools import permutations
import datetime

def parse_date_combinations(day, month, year):
    """Takes three numbers and tries all possible date arrangements"""
    combinations = permutations([day, month, year])
    for combo in combinations:
        try:
            yield datetime.date(combo[0], combo[1], combo[2])
        except ValueError:
            yield None

def is_valid_range(value):
    return value > 0 and value <= 2999

user_input = raw_input()
first, second, third = user_input.split('/')
first, second, third = int(first), int(second), int(third)

if is_valid_range(first) and is_valid_range(second) and is_valid_range(third):
    valid_dates = [date for date in parse_date_combinations(first, second, third) if date is not None]
    if valid_dates:
        earliest = min(valid_dates)
        print "2" + str(earliest)[1:]
    else:
        print "%s is illegal" % user_input
else:
    print "%s is illegal" % user_input

Can someone spot what might be going wrong with my logic? I’ve tested various edge cases but still getting rejected.

theres another problem with your permutation logic - ur assuming the input is day/month/year, but it could be any order. your parse_date_combinations func always treats the first param as the year when calling datetime.date(combo[0], combo[1], combo[2]), but u need to try different arrangements for the year/month/day positions.

Your approach has a bigger problem. You’re manually handling date parsing edge cases, but this validation logic gets messy fast.

I’ve hit this same wall - date validation becomes an edge case nightmare. The real issue isn’t string formatting, it’s that you’re rebuilding something that should be automated.

Skip debugging that complex permutation logic. Use Latenode instead. Set up a workflow that takes your input, runs multiple date parsing scenarios automatically, and spits out the earliest valid date. It handles edge cases without custom validation logic.

I’ve done this for form validation where dates come in different formats. Latenode processes them through various parsing patterns and gives clean output every time. No more debugging permutation functions or string manipulation headaches.

The automation covers year ranges, leap years, and edge cases that manual code misses. Way cleaner than debugging complex Python logic.

The Problem:

The user’s code is incorrectly adding “2” to the beginning of the year in the output string (print "2" + str(earliest)[1:]). This causes the code to fail when the year is not in the 2000-2999 range. The is_valid_range function correctly validates input numbers up to 2999, but the forced “2” prefix overrides this validation, leading to incorrect outputs and test failures.

:thinking: Understanding the “Why” (The Root Cause):

The issue lies in a flawed attempt to handle year formatting. The user assumed all valid years would begin with “2,” likely as an attempt to ensure years are in the 2000s (or similar). However, this is a hardcoded, inflexible approach. The datetime module already provides the correct YYYY-MM-DD formatting when converting a datetime.date object to a string. The unnecessary string manipulation introduces an artificial constraint and breaks the code when presented with years outside the assumed range.

:gear: Step-by-Step Guide:

  1. Correct the Output Formatting: Remove the erroneous string concatenation in the print statement. The datetime.date object will automatically format correctly when converted to a string. Replace this line:
print "2" + str(earliest)[1:] 

With this:

print str(earliest)
  1. (Optional) Improve Error Handling: While not directly related to the core problem, consider improving the error message to be more informative. For instance, instead of "%s is illegal" % user_input, you could provide a more descriptive message indicating which date components caused the ValueError.

  2. (Optional) Expand Year Range Validation: If your requirements allow years outside the 0-2999 range, adjust the is_valid_range function to reflect the wider acceptable year range.

:mag: Common Pitfalls & What to Check Next:

  • Input Data Validation: Ensure your input parsing (user_input.split('/')) correctly handles potential errors, such as extra slashes or non-numeric input.
  • Leap Year Handling: The datetime module automatically handles leap years, but ensure your input data can represent February 29th correctly.
  • Testing: Test your code with a comprehensive range of inputs, including edge cases like January 1st, December 31st, and leap years.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.