-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create weekday(date) function #50
Conversation
# weekday with date_time | ||
test_eval 'weekday("2014-03-21T21:03")' '5' | ||
test_eval 'weekday("2015-03-21T21:03")' '6' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! it also works!
Should i add the changes to the Makefile? it doesn't seem to make sense for me since that it's version controlled since it's generated by cmake |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job!
|
string_type locales[8] = {"en", "nb", "pt-BR", "es-ES", "fr-FR", "de-DE", "zh-CN", "th-TH"}; | ||
|
||
for (int i = 0; i < 8; i++) { | ||
if(locale == locales[i]) { | ||
ret = localized_weekdays[i][week_day]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could use a map here to achieve O(1). int index = locales[locale]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i know, but map lookup is only amortized O(1). with n=8, you can see the difference is infinitesimal (in this print array method wins by 4ms but it's basically the same, i ran it a lot of times to check
therefore, i don't think the inclusion of the lib is warrented.
scripts used:
string_method
#include <iostream>
#include <string>
using namespace std;
int main(){
string localized_weekdays[8][7] = {
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
{"Søndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag"},
{"Domingo", "Segunda-Feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"},
{"Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sabado"},
{"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"},
{"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"},
{"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"},
{"วันอาทิตย์", "วันจันทร์", "วันอังคาร", "วันพุธ", "วันพฤหัสบดี", "วันศุกร์", "วันเสาร์"}
};
string locales[8] = {"en", "nb", "pt-BR", "es-ES", "fr-FR", "de-DE", "zh-CN", "th-TH"};
string locale, ret;
int week_day;
while(cin >> locale >> week_day){
for (int i = 0; i < 8; i++) {
if(locale == locales[i]) {
ret = localized_weekdays[i][week_day];
}
}
cout << ret << endl;
}
}
map method
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
map<string, vector<string>> localized_weekdays = {
{"en", {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}},
{"nb", {"Søndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag"}},
{"pt-BR", {"Domingo", "Segunda-Feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"}},
{"es-ES", {"Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sabado"}},
{"fr-FR", {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"}},
{"de-DE", {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}},
{"zh-CN", {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}},
{"th-TH", {"วันอาทิตย์", "วันจันทร์", "วันอังคาร", "วันพุธ", "วันพฤหัสบดี", "วันศุกร์", "วันเสาร์"}}
};
string locale, ret;
int week_day;
while(cin >> locale >> week_day){
ret = localized_weekdays[locale][week_day];
cout << ret << endl;
}
}
timer.sh
#!/bin/bash
# Run the array method 1 million times
echo "Running the array method 1 million times"
start_time1=$(date +%s%N) # Record start time
./array_method < input.txt > /dev/null
end_time1=$(date +%s%N) # Record end time
total_time1=$(((end_time1 - start_time1)/1000000))
echo "Time taken for array_method: $total_time1 miliseconds"
# Run the map method 1 million times
echo "Running the map method 1 million times"
start_time2=$(date +%s%N) # Record start time
./map_method < input.txt > /dev/null
end_time2=$(date +%s%N) # Record end time
total_time2=$(((end_time2 - start_time2)/1000000))
echo "Time taken for map method: $total_time2 miliseconds"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice bench! Lets keep it simple than!
if (a_iArgc < 1) { | ||
throw ParserError(ErrorContext(ecTOO_FEW_PARAMS, GetExprPos(), GetIdent())); | ||
} else if (a_iArgc > 2) { | ||
throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmm! We can write variable arity functions! This will make our case strategy simpler!
test_eval 'weekday("2016-03-21", "zh-CN")' '"星期一"' | ||
test_eval 'weekday("2017-03-21", "fr-FR")' '"Mardi"' | ||
test_eval 'weekday("2018-03-21", "th-TH")' '"วันพุธ"' | ||
test_eval 'weekday("2019-03-21", "pt-BR")' '"Quinta-feira"' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing tests with unknow locale and invalid date
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will add!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, it seems like this tests.sh does not test for thrown errors. i think tests like these are added on the parsec gem, like this
this test.sh assumes that errors won't be thrown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@niltonvasques tests for thrown errors are done in parsec layer, can we merge this and proceed to parsec PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible guys @EduardoRSeifert and @Marinofull, to catch crashs on bash: See
https://medium.com/@dirk.avery/the-bash-trap-trap-ce6083f36700
But looking parsec code, seems that it is also possible to get the syntax error:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we have tests in the parsec gem that check for syntax error, as i said in my previous comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats not the reason that I posted the parsec link here. In my link you can see that is possible to collect the syntax error from the libnativemath output, thus you can also write tests here on the bash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In bash you can also catch crashs/fatals as you can see in the first link.
test_eval 'weekday("2019-03-21", "invalidlocale")' 'The chosen locale is not supported' | ||
test_eval 'weekday("2024-32-21")' 'Invalid format on the parameter(s). Please use two "yyyy-mm-dd" for dates OR two "yyyy-mm-ddTHH:MM" for date_times' | ||
test_eval 'weekday("2024-09-17", "en", "one too many params")' 'Too many parameters passed to function' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
voila, @niltonvasques
@@ -111,6 +111,8 @@ MUP_NAMESPACE_START | |||
m_vErrMsg[ecADD_HOURS_DATE] = _T("The first parameter could not be converted to a date. Please use the format: \"yyyy-mm-dd\""); | |||
m_vErrMsg[ecADD_HOURS_DATETIME] = _T("The first parameter could not be converted to a date time. Please use the format: \"yyyy-mm-ddTHH:MM\""); | |||
m_vErrMsg[ecINVALID_TYPES_MATCH] = _T("Both values of the default(x, y) function should have the same type"); | |||
m_vErrMsg[ecUKNOWN_LOCALE] = _T("The chosen locale is not supported"); | |||
m_vErrMsg[ecINVALID_TIME_FORMAT] = _T("Invalid time format on parameter(s). Please use the \"HH:MM:SS\" format."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without this error message the program was filing because of it missing, which would stop the tests script from getting the error message. it was not added in the PR that used it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love tests because their incredible ability to highlight these corner cases. 👏
Create the weekday(date/datetime) function that takes in a date and returns the number of days since sunday(returns the day of the week in int form, 0 = sunday, 6 = saturday).
Make support these languages as option argument, otherwise return the numbers [0-6]:
Tests Passed !