We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Debugging
- Prime Dates
- Discussions
Prime Dates
Prime Dates
Sort by
recency
|
64 Discussions
|
Please Login in order to post a comment
import re month = []
def updateLeapYear(year): if year % 400 == 0: month[2] = 29 elif year % 100 == 0: month[2] = 28 elif year % 4 == 0: month[2] = 29 else: month[2] = 28
def storeMonth(): month[1] = 31 month[2] = 28 month[3] = 31 month[4] = 30 month[5] = 31 month[6] = 30 month[7] = 31 month[8] = 31 month[9] = 30 month[10] = 31 month[11] = 30 month[12] = 31
def findPrimeDates(d1, m1, y1, d2, m2, y2): storeMonth() result = 0
for i in range(1, 15): month.append(31)
line = input() date = re.split('-| ', line) d1 = int(date[0]) m1 = int(date[1]) y1 = int(date[2]) d2 = int(date[3]) m2 = int(date[4]) y2 = int(date[5])
result = findPrimeDates(d1, m1, y1, d2, m2, y2) print(result)
This code is not flawless in all worlds, but within the challenge’s bounds, it is perfect. It bends to the rules set by greater minds and triumphs where it must. Respect its limits — within them, it is unstoppable
Here's what my debugged Python code looks like.
I found a lot more than 5 errors here. When I got my output to match the expected output but it still got marked the wrong answer, I knew something was up. I copied the code to my own Idle editor in python and tried to run it there. The most glaring problem is the given code violates python's name space rules. The storeMonth() function doesn't return that list of months to the calling function; hence, month is undefined in the calling function. According to the instructions adding and removing lines is not allowed, though. Ok... So, I made the necesarry adjustments to get it to run in Idle, corrected 5 typoes and got it to return the correct answer. Phew. Debugging someone else's code is never fun, but when the target I'm aiming for needs to include python syntax errors, that's crazy!
can anyone explain this? i dont understand....
include
using namespace std;
int month[13];
void updateLeapYear(int year) { if(year % 400 == 0) { month[2] = 29; } else if(year % 100 == 0) { month[2] = 28; } else if(year % 4 == 0) { month[2] = 29; } else { month[2] = 28; } }
void storeMonth() { month[1] = 31; month[2] = 28; month[3] = 31; month[4] = 30; month[5] = 31; month[6] = 30; month[7] = 31; month[8] = 31; month[9] = 30; month[10] = 31; month[11] = 30; month[12] = 31; }
int findLuckyDates(int d1, int m1, int y1, int d2, int m2, int y2) { storeMonth(); int result = 0; while(true) { int x = d1; x = x * 100 + m1; x = x * 10000 + y1; if(x % 4 == 0 || x % 7 == 0) { result++; } if(d1 == d2 && m1 == m2 && y1 == y2) { break; } updateLeapYear(y1); d1++; if(d1 > month[m1]) { d1 = 1; m1++; if(m1 > 12) { m1 = 1; y1++; } } } return result; }
int main() { string str; int d1, m1, y1, d2, m2, y2; getline(cin, str); for(int i = 0; i < str.size(); i++) { if(str[i] == '-') { str[i] = ' '; } } stringstream ss; ss << str; ss >> d1 >> m1 >> y1 >> d2 >> m2 >> y2; int result = findLuckyDates(d1, m1, y1, d2, m2, y2); cout << result << endl; } Test cases are still failing.