My coding works, but just to double check... - "Parsing Numbers from Complex Strings, Part Two"

Screen Link: Learn data science with Python and R projects

My Code:

def process_date(date):
    if "-" in date:
        splited_date = date.split("-")
        average_date = (int(splited_date[0]) + int(splited_date[1])) / 2
        final_date = round(average_date)
    else:
        final_date = int(date)
    
    return final_date

What Dataquest answer for code is:

def process_date(date):
    if "-" in date:
        split_date = date.split("-")
        date_one = split_date[0]
        date_two = split_date[1]       
        date = (int(date_one) + int(date_two)) / 2
        date = round(date)
    else:
        date = int(date)
    return date

I was creating the code for process_date function and my answer was different from Dataquest’s answer.
(Sorry Dataquest for not following your instruction! I just wanted to try :sweat_smile:)
I would like to know if my answer is still usable or not safe to use(meaning, it can cause error in the future or something).
Things I would like to confirm in my code are followings;

  • Is it ok to create calculation right after “date.split(”-")"?
    ⇨I wanted to make the coding short, so I cut defining part (Dq code: date_one = split_date[0]&date_two = split_date[1])

  • Is it ok to assign different variable? (in dq they assign back to “date” at the end)
    ⇨I wanted to create new variable (we had "def process_date(date)"and at the end I assigned final data as “return final_date” instead of “return date”) so that we can differ before calculation variable and after calculation variable. Please let me know if that causes error in later on.

Sorry for asking so much details and silly question! Im still at the beginner level…
Thank you for your help in advance!

Hey @kanae.iwase!

Never apologize for trying to figure out a problem yourself instead of using a prewritten solution!

From what I can tell, your code should work perfectly. To answer your specific questions

  1. Yes, this works. You never need to declare variables. You should do it because it helps in understanding your code. But it still works to not declare the variable.

  2. No problem with this. As long as you returned the same thing that they returned, you’re good.

Hope it helps!

2 Likes

Thank you very much! I feel more confident now :smiling_face: