hey guys, looking for a little help with my code. its for an assignment using python.
Write a function called occurrences. This function states the weekday that that day and month
fell most and least frequently on. The function accepts four (4) integer arguments:
a. day represents a day of a month and can range between 1 and 31
b. month represents a month of a year and can range between 1 and 12
c. start represents the first year of the period being considered
d. end represents the year after the last of the period being considered
For example, if we wanted to know the weekdays that January 1st fell most and least frequently
on during the 20th century (Jan 1901 to Dec 2000), we would call o ccurences with the
following arguments,
>>> occurences(1,1,1901,2000)
That date falls most often on a Wednesday and least often on a
Saturday within the given period
I also previously defined a function that would tell me what day a certain date falls under, so I’ve incorporated that into the code:
Write a function called zeller that accepts three(3) arguments: day, month and year. z eller
returns the day of the week that that date fell on as a string. For example,
>>> zeller(1,1,2015)
‘Thursday’
Here’s what I have so far
def zeller(day,month,year):
#Accept a date and return the day of the week in which the date occured
#Dictionary for days of the week
day_names=dict()
day_names[0]="Sunday"
day_names[1]="Monday"
day_names[2]="Tuesday"
day_names[3]="Wednesday"
day_names[4]="Thursday"
day_names[5]="Friday"
day_names[6]="Saturday"
#Make modifications for the formula to interpret the months correctly
if month==1 or month==2:
month=month+10
year=year-1
else:
month=month-2
#set up the formula
A=month
B=day
C=int(year%100)
D=int(year/100)
#compute the day using the formula
W=int((13*A-1)/5)
X=int(C/4)
Y=int(D/4)
Z=int(W+X+Y+B+C-2*D)
R=int(Z%7)
#Output answer
return day_names[R]
def occurences(day,month,start,end):
#Accept a day of the month, a month and two years. Return the weekday that the day of the month and month fell most and least frequently on.
#Make a list for all the days that the date given falls under
days=[]
for i in range(start,end):
days.append(zeller(day,month,i))
return max(days),min(days)
the issue here is that it isn’t returning the minimum value from the list and I’m entirely stumped as to why. One thought that crossed my mind is that it could be an issue with indices as I used a dictionary for zeller, but even then shouldn’t it return the element that appears the least amount of times in the list? the maximum seems correct since it gives me the same day as in the example(wednesday).
I’m not sure what you mean by “it isn’t returning the minimum value from the list”. What list? And what isn’t returning the value?
Give some sample input and output. Put some debug statements between each step in the equation to print out what is happening so you can trace it easier.
I’ve never done python before but it seems pretty easy to read in general.
Also a few pointers/questions for you.
Why are you adding 10 to month when it’s Jan or Feb? Why are you subtracting otherwise? Is that just part of what is needed to make the equation work?
Use meaningful variable names. Why are you doing A=month when you could just throw month into the equation? What is “W” and “X” and “Y” and “Z”? What do they represent? Name them what they mean instead of arbitrary letters with no meaning.
Use line breaks, it makes your code more readable. I’d break that zeller function up into multiple functions to make it easier to read and maintain as well.
If its Jan or Feb, you rewind time to the previous year? Thats weird…
Look at the entire algorithm. Its either subtracting 2 months from the month, or if that creates an invalid months (for months 1 and 2), it adds 10 instead to get the same thing. So Jan becomes November, Feb Becomes December, March becomes January… though why theres a 2 month offset makes no logical sense?
As to your problem, if MAX works but MIN doesn’t, theres a chance your algorithm is just wrong. What is the value for each of the 7 days in your dictionary?
The algorithm used to determine the day that any given date will be on is Zeller’s congruence. When he created the algorithm March was the start of the year instead of January. In order for the algorithm to give the correct day you have to modify the variable to take these differences into account. The issue with my code isn’t the Zeller function, it’s occurrences.
I thought that maybe the algorithm wasn’t correct but that isn’t the case. I printed the list and counted myself, and it just isn’t returning the value that appears the least amount of times in the list.
As for the variable names A,B etc they were the ones that we were given to use.
purebeast elaborate a bit on line breaks? I’m literally one month into this course. I suppose my code does look sloppy to veterans.
Cross, I just mean to add more spaces between your “logical blocks of code” just to make it easier to read, rather than your code looking like 1 long paragraph. Make it look like 3 or 4 “smaller” paragraphs by adding in an extra return character after the logical section breaks.
Looks like Pythons Min/Max doesn’t work for String Values. Your array seems to be full of strings (‘Monday’, ‘Tuesday’, etc) and Min/Max only works on numeric values, not for the “Most Common Occuring” and “Least Common Occuring” values
You’ll need to come up with a better method to use. I would suggest using a dictionary for days, instead of an array, and update the dictionary so that everytime you get a day back as a result, you increase the counter by 1 for that key value. Then you could use Min/Max to get the dictionary record with the most/least occurances
Your decompiler does not seem up to the task. I don’t know how much you can do about that other than get a better one or just rewrite the parts it didn’t translate.
lol is that C#, see I don’t know what I’m doing lol.
I’ve tried JetBrains dotPeek and ILSpy. DotPeek produced less errors.
Now that I write this, I didn’t check to see if ILSpy produced the same errors as well.
Sounds like that is the problem here heh. Not sure what you expect anyone here to help you with.
You aren’t supposed to be able to just decompile any old executable and recompile it like it’s being compiled from the source code. That’s just not how it works.