
Last edited by Coffee table knight (2024-07-20 10:00:40)
CC: PIE |
You are here » CC: PIE » living room » flood #4 midsummer night's dream

Last edited by Coffee table knight (2024-07-20 10:00:40)
В Питере теперь знаю больше исторических мест чем в родной Москве, а так всегда - где живешь, там все пофиг
The Herald
I've been to Ermitage once. When my friend came to visit )
I know a lot of places in Moscow (a native Muscovite). And I know several attractions in St. Petersburg
Coffee table knight
soon you'll know all the places in Belgrad 
soon you'll know all the places in Belgrad
Dream of going to museums. While I visited the botanical garden and zoo. It was amazing!
I always considered Moscow grey and dull, but, in the hindsight, it might be because I never went exploring further than the route from home to school/work. But now I know the waterfront of Toronto almost intimately 
we bought a comic in serbian)))
I like how they call wonder woman: чудесна жена
42
How are you doing today?)
wonder woman: чудесна жена
if I ever have a wife that's how I'll call her
Lucifer
I have an interesting brain-wrecking task, so I'm good)
And you?
I have an interesting brain-wrecking task, so I'm good)
is it work related or one of your pet projects?
Lucifer
Work-related this time. In truth, it sounds rather simple, but when you start coding it, it gets quite complicated
42
well, you like a challenge )
Lucifer
I do. And I've always solved it (at least in broad strokes), but I cannot pin down how to write the recursion nicely here...
And you?
My leave starts in 15 minutes. I'm counting 
Lucifer
Lucky you!
I think I more or less did it!
const timeStart = '2023-05-12 00:00:00';
const timeEnd = '2024-07-15 23:59:59';const timeStartDate = new Date(timeStart);
let timeEndDate = new Date(timeEnd);
timeEndDate = new Date(timeEndDate.valueOf() + 1000)const periods = {
"year": [],
"month": [],
"week": [],
"day": [],
"hour": []
}function extractPeriods(timeStartDate, timeEndDate, period) {
let lastPeriodStart = new Date(timeEndDate);
lastPeriodStart = getFirstDayOfPeriod(lastPeriodStart, period)let prevPeriodStart = new Date(lastPeriodStart);
prevPeriodStart = onePeriodBack(prevPeriodStart, period);
let prev = new Date(prevPeriodStart);
while (timeStartDate < prevPeriodStart) {
periods[period].push(prevPeriodStart.toISOString())
prev = new Date(prevPeriodStart);
prevPeriodStart = onePeriodBack(prevPeriodStart, period);
}
if (timeStartDate < prev) {
return [
{
timeStartDate: timeStartDate,
timeEndDate: prev
},
{
timeStartDate: lastPeriodStart,
timeEndDate: timeEndDate
}
]
} else {
return [
{
timeStartDate: timeStartDate,
timeEndDate: timeEndDate
}
]
}
}function getFirstDayOfPeriod(date, period) {
switch (period) {
case "year":
date.setMonth(0, 1);
return date;
case "month":
date.setDate(1);
return date;
case "week":
const d = date.getDay()
date.setDate(date.getDate() - d);
return date;
case "day":
return date;
case "hour":
return date;
}
}function onePeriodBack(date, period) {
switch(period) {
case "year":
date.setFullYear(date.getFullYear() - 1)
return date;
case "month":
date.setMonth(date.getMonth() - 1);
return date;
case "week":
date = new Date(date.valueOf() - 604800000);
return date;
case "day":
date = new Date(date.valueOf() - 86400000);
return date;
case "hour":
date = new Date(date.valueOf() - 36000000);
return date;
}
}const timeIntervals = [ {
timeStartDate: timeStartDate,
timeEndDate: timeEndDate
}];
const periodNames = ['year', 'month', 'week', 'day', 'hour']function extractPeriodsRecursive(timeIntervals, periodNameIndex) {
timeIntervals.forEach((timeInterval) => {
const newTimeIntervals = extractPeriods(timeInterval['timeStartDate'], timeInterval['timeEndDate'], periodNames[periodNameIndex])
if (periodNameIndex < periodNames.length - 1) {
extractPeriodsRecursive(newTimeIntervals, periodNameIndex + 1)
}
})
}extractPeriodsRecursive(timeIntervals, 0);
Last edited by 42 (2024-07-22 22:00:31)
that's a lot of code
Faded Star
It's not as big as it looks, there are a couple of switches inside.
Basically, what it is supposed to do, given an arbitrary time period, it will split it into some calendar periods trying to prioritize the largest. So, if it's, say, a period of three years and some days, it will extract the full calendar years first, than, from the remaining time, it will extract all the full calendar months, than weeks, days and finally hours (my time periods don't go more granular than hours).
Faded Star
It's not as big as it looks, there are a couple of switches inside.Basically, what it is supposed to do, given an arbitrary time period, it will split it into some calendar periods trying to prioritize the largest. So, if it's, say, a period of three years and some days, it will extract the full calendar years first, than, from the remaining time, it will extract all the full calendar months, than weeks, days and finally hours (my time periods don't go more granular than hours).
Sounds formidable even though hard for me to grasp as of now
Keep up the good work, it does not sound easy in the slightest
Faded Star
This actually supposed to be the easiest part of the whole data processing module I've been developing
But this time sorting algorithm is a little b*tch, it's true
Why is the code similar to English? Is it a python that speaks Rudyard Kipling's language?
Coffee table knight
Why would not it be? The code consists of functions, memory blocks (variables) and operations. All these things need names, and ideally those names should be eloquent enough that, simply by looking at the name, you would guess what the function does. So yes, the names of the functions, variables and everything else always make perfect sense in English
42
How do you know which words the progamming language recognizes in code and which ones it doesn’t? Who gives it a list of English words to recognize? And how to replenish a dictionary?
Coffee table knight
The compiler (the program) does not care about the words. All those words are names and needed only for humans' benefit. For the compiler they are simply names. Like "Jack" does not have any meaning on its own and simply points to a person, the same way calculateDates() does not mean anything for the compiler, it simply indicates which chunk of code to execute
42
I mean, how do you know what to say in code, so the computer can do what you want?
For example, how do you know what to write exactly this?
const timeStart = '2023-05-12 00:00:00';
const timeEnd = '2024-07-15 23:59:59';const timeStartDate = new Date(timeStart);
let timeEndDate = new Date(timeEnd);
timeEndDate = new Date(timeEndDate.valueOf() + 1000)
How do you know that the computer will understand you? Have you learned all these commands? Is there a dictionary of all the commands in the world? Who’s filling in the dictionary? The developers of the programming language?
Coffee table knight
Yes. It's called "the programming language syntax". It is slightly different for every language although most of the languages are similar enough. Each language has specific rules for how you need to structure your commands and a set of predefined, the most commonly used functions (a very large set of thousands of functions usually). You do not have to know them all, of course, but the most common you memorize fast enough.
In a way, it is like learning another language
Last edited by 42 (2024-07-22 23:15:48)
42
Wow, thank you, it's interesting!
Good morning 
Lucifer, hello!
New puzzles are waiting for you.
You are here » CC: PIE » living room » flood #4 midsummer night's dream