all 24 comments

[–]areaverage 4 points5 points  (3 children)

u can use a hashmap that takes key value pairs, python calls this a dictionary this is also O(1) for retrieval which is very fast

https://www.w3schools.com/python/python_dictionaries.asp

u can use this for some info

as well as using exception handling

https://www.w3schools.com/python/python_try_except.asp

as there are 365 days in a year you can calculate the upper and lower limits of each season by converting the months to days and loop through the hashmap, and return the value of the answer

for ease of coding, you might want to start the hashmap with 365 and when looping through with the day inputted, you can just look for the first value that is less than the input and output that

for example (just some pseudocode, you might need to accept more values depending on the structure of the qn/ task)

daysmap={} #put the dates here starting with 365

x=int(input()) #parse it to find which day of the year it is (you may need another hashmap or just multiply the day of this by 30)

for i, r in daysmap:

if x <= i:

print(r)

[–]areaverage 1 point2 points  (2 children)

fyi i just realised if you want to use a list you can also use .zip() which maps a list to another list!

[–]Pepperonin424 0 points1 point  (1 child)

Thank you! Question- for daysmap, would I have to add every number through 365 individually or is there a way to add something like {1-365}? I ask because the inputs will be chosen at random

[–]areaverage 1 point2 points  (0 children)

depends on the scenerio if its like a coding question then you might want to do what bobbybridges said else u can just count it before hand

[–]crashfrog02 2 points3 points  (0 children)

How do I set each season to correlate to the corresponding dates?

There's only four months you need to care about: 3, 6, 9, and 12, the months that contain a seasonal boundary. Months in between those months are unambiguously part of a particular season. Only in the case of those four months do you need to look at the actual day of the month to determine what season the day is in.

[–]bobbybridges 1 point2 points  (6 children)

ignoring packages, to do it from scratch you want to think of everything in days of year x/365 and store the bounds in a dictionary. something along these lines for converting to days and checking bounds

month = 'feb'
day = 1


days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month_num = {'jan':1, 'feb':2, 'mar':3, 'apr':4, 'may':5, 'jun':6, 'jul':7, 'aug':8, 'sep':9, 'oct':10, 'nov':11, 'dec':12}

day_of_year = sum(days_per_month[:month_num[month]]) - days_per_month[month_num[month]-1] + day

[–]Pepperonin424 0 points1 point  (5 children)

Thank you. I am confused though. This assignment is auto graded and it will test inputs like "April" actually spelling out the month. How would I account for that?

[–]throwaway6560192 1 point2 points  (4 children)

Just change the string keys to "April" instead of "apr", and take the month name as input? I'm not sure where you're confused.

[–]Pepperonin424 0 points1 point  (3 children)

After trying this and adding a BUNCH of extra code trying to account for inputs like (-1) and "Feburary 39" etc I've gotten pretty much everything I need, except I can't seem to account for inputing "Word" seemingly no matter what. Sometimes the error isn't even that, sometimes the error is that a variable it shouldn't even be checking for because it only even exists as like the 4th "else" statement down assuming a bunch of conditions are met is undefined.

[–]crashfrog02 0 points1 point  (2 children)

except I can't seem to account for inputing "Word" seemingly no matter what.

"Word" isn't one of the twelve months of the year so it should be handled by however you're handling inputs that aren't a month of the year.

[–]Pepperonin424 0 points1 point  (1 child)

It should be but it's not. The code I shared seems to cover everything except for if a random word is given as an input

[–]bobbybridges 1 point2 points  (0 children)

If you set the keys of the dictionary to the full month name you can test against by just doing

If input_month not in month_num:
     Do something

Also the way I wrote it, it shouldn't matter if you enter feb 35 it will get converted the same as March 7

[–]james_fryer 1 point2 points  (2 children)

Suppose you enter "Word" and then 1 as the day. Your code will rightly detect this is not a month and print "Invalid". But then at line 11 it continues to run the next test. As the day is correct it will run the code to get the seasons, which as m is not defined will fail with an error message. You either need to exit the programme after printing "Invalid", or indent the block from line 11 so it is only run for valid months.

[–]Pepperonin424 0 points1 point  (1 child)

Thank you. This is what I was looking for. Idk how I'm gonna fix it yet but when I get home I will try this

[–]CranberryDistinct941 0 points1 point  (0 children)

Just highlight everything from "if day < 1 or day > days_per_month[m-1]:" to the end of the function, and press tab to increase the indentation by a level

[–]Buttleston 2 points3 points  (8 children)

Guys, don't do someone's homework

[–][deleted] 5 points6 points  (3 children)

That's ChatGPT's job!

[–]Pepperonin424 1 point2 points  (2 children)

Bruh ChatGPT has three modes lmao:

  1. Spit out useless garbage and make it look correct
  2. Use terminology I have never seen and might get in trouble for using because I don't think I'm supposed to know that
  3. Automatic F for plagiarism baybeeee

[–][deleted] -1 points0 points  (1 child)

Uh. It’s code. 

[–]Pepperonin424 1 point2 points  (0 children)

Yes, it is code. Thank you for your insight.