JavaScript中最频繁的周期问题
做最好的研究,我找不到JavaScript的答案, 但是我发现该解决方案在Python中正常工作
import datetime
def most_frequent_days(year):
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
most = sorted(list(set([datetime.date(year, 1, 1).weekday() ,datetime.date(year, 12, 31).weekday()])))
return [days[i] for i in most]
print(most_frequent_days(212))
,现在试图将其转换为JavaScript,您需要知道Python(我知道如何与Python打交道)现在想到的最好的事情
const mostFrequentDays = year => {
function getDayOfWeek(date) {
const dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek];
}
//let days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]
let most = [getDayOfWeek(`${year}-1-1`), getDayOfWeek(`${year}-12-31`)].sort()
let mySet = [...new Set(most)]
//let sorted = mySet;
let i = 0;
let data = [];
while (i < mySet.length) {
data = [ ...data, mySet[i] ]
i++;
}
return data;
};
,但是当我尝试尝试时,使用我构建的功能 MostFrequentdays(212)
我没有获得预期的输出
**您将作为整数给您一年。返回那年本周最常见的一天。从星期一开始(例如['星期一','星期二']开始,应按照一周几天的订单进行排序。 **
测试
it("should handle basic tests", () => {
assert.deepEqual(mostFrequentDays(2427), ["Friday"]);
assert.deepEqual(mostFrequentDays(2185), ["Saturday"]);
assert.deepEqual(mostFrequentDays(1084), ["Tuesday", "Wednesday"]);
assert.deepEqual(mostFrequentDays(1167), ["Sunday"]);
assert.deepEqual(mostFrequentDays(1216), ["Friday", "Saturday"]);
assert.deepEqual(mostFrequentDays(1492), ["Friday", "Saturday"]);
assert.deepEqual(mostFrequentDays(1770), ["Monday"]);
assert.deepEqual(mostFrequentDays(1785), ["Saturday"]);
assert.deepEqual(mostFrequentDays(212), ["Wednesday", "Thursday"]);
});
很多都可以使用
当我将python代码转换为JS以外的JS时, 是该解决方案的许多Python版本,但是我使用的是简单的版本,而在Python中,该代码可以工作,因为Python中的DateTime在类似212之类的日期工作。 或者,也许如果您知道最初不是来自Python的更好的版本,那么您将如何为JS实施解决方案
,谢谢, 此致。
Doing the best research, I couldn't find the answer in JavaScript,
But I found this solution that works perfectly in python
import datetime
def most_frequent_days(year):
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
most = sorted(list(set([datetime.date(year, 1, 1).weekday() ,datetime.date(year, 12, 31).weekday()])))
return [days[i] for i in most]
print(most_frequent_days(212))
Now trying to convert this into JavaScript, you need to know python (I know how to deal with python) the best thing I come up with
const mostFrequentDays = year => {
function getDayOfWeek(date) {
const dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek];
}
//let days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]
let most = [getDayOfWeek(`${year}-1-1`), getDayOfWeek(`${year}-12-31`)].sort()
let mySet = [...new Set(most)]
//let sorted = mySet;
let i = 0;
let data = [];
while (i < mySet.length) {
data = [ ...data, mySet[i] ]
i++;
}
return data;
};
Now this works in JS, however when I try to use the function I builtmostFrequentDays(212)
I don't get the expected output
**You are given a year as an integer. Return the most frequent day(s) of the week in that year. The resulting list of days should be sorted by the order of days in week starting from Monday (e. g. ['Monday', 'Tuesday']). **
Tests
it("should handle basic tests", () => {
assert.deepEqual(mostFrequentDays(2427), ["Friday"]);
assert.deepEqual(mostFrequentDays(2185), ["Saturday"]);
assert.deepEqual(mostFrequentDays(1084), ["Tuesday", "Wednesday"]);
assert.deepEqual(mostFrequentDays(1167), ["Sunday"]);
assert.deepEqual(mostFrequentDays(1216), ["Friday", "Saturday"]);
assert.deepEqual(mostFrequentDays(1492), ["Friday", "Saturday"]);
assert.deepEqual(mostFrequentDays(1770), ["Monday"]);
assert.deepEqual(mostFrequentDays(1785), ["Saturday"]);
assert.deepEqual(mostFrequentDays(212), ["Wednesday", "Thursday"]);
});
A lot of them works when I convert the python code to JS except the last one because the JS Date doesn't work on year 212
Please help me make a function that can pass all the tests, there are a lot of python versions of the solution but the one I used is the easy one, and in python, that code works because the DateTime in python work on a date like 212.
Or maybe if you know a better version that's not originally from python, how would you implement a solution for JS
Thank you,
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要定义“不起作用”。
非闰年有 365 天,即 52 周加一天。因此,无论哪一天是一年中的第一天,都会出现 53 次,其他所有日子都会出现 52 次。因此,出现频率最高的只是 1 月 1 日这一工作日。
对于闰年,第一天和第二天将出现 53 次,其他日期出现 52 次。例如,2018 年 1 月 1 日是星期一,12 月 31 日也是星期一,因此星期一有 53 次,其他日期有 52 次。 2020 年 1 月 1 日是星期三,是闰年,12 月 31 日是星期四,因此有 53 个星期三和星期四,以及其余 52 个。
当然,随机的日期范围会让生活变得更加困难。
实现上述功能的函数是:
另一种算法(PHP 代码正在执行的操作)是检查 1 月 1 日和 12 月 31 日是否是同一天。如果是,当天就返回。如果没有,请两天返回。
唯一需要的排序是闰年从星期日开始的位置。默认返回是[星期日、星期一],但在该特定情况下要求是[星期一、星期日]。
因此,如果 1 月 1 日是星期日(第 0 天),则反转日期列表,例如
You need to define "doesn't work".
A non–leap year has 365 days, which is 52 weeks plus one day. So whichever day is the first day of the year will have 53 occurrences and all the others will have 52. So the most frequent is just the weekday of 1 January.
For a leap year, the first and second day will have 53 occurrences and the others 52. e.g. 1 Jan 2018 was Monday, as was 31 December, so there were 53 Mondays and 52 of the others. 1 Jan 2020 was Wednesday and a leap year, 31 Dec was Thursday so 53 Wednesdays and Thursdays and 52 of the rest.
Of course a random range of dates will make life a little more difficult.
A function to implement the above is:
Another algorithm (which is what the PHP code is doing) is to check if 1 Jan and 31 Dec are the same day. If they are, return that day. If not, return both days.
The only sorting that's required is where a leap year starts on Sunday. The default return is [Sunday, Monday] but the requirement is for [Monday, Sunday] in that particular case.
So if 1 Jan is a Sunday (day 0) reverse the day list, e.g.