Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Challenge: sort weekdays in week-order (elegantly and efficiently)

by LanX (Sage)
on Jul 21, 2022 at 14:27 UTC ( #11145634=note: print w/replies, xml ) Need Help??


in reply to Challenge: sort weekdays in week-order (elegantly and efficiently)

> is there a way without using that %order?

ehm ... you need this order information somehow.

So is your question

  • if this order could be taken from some Date or Time module like DateTime
  • or if there is some obfuscated way to calculate it like with the Doomsday algorithm
?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Side note: The week starts traditionally on Sunday. At least for the church.

  • Comment on Re: Challenge: sort weekdays in week-order (elegantly and efficiently)

Replies are listed 'Best First'.
Re^2: Challenge: sort weekdays in week-order (elegantly and efficiently)
by bliako (Monsignor) on Jul 21, 2022 at 14:50 UTC

    The latter: some kind of algorithm or rather a transform of each weekday into a new name which is alphabetically sorted right (as per the %order or whatever calendar order). For example, transform each weekday by removing the 1st letter, then their cmp order is correct. OK this does not work. But I was looking for this kind of transform. Of course I can try exhaustively find such transform with random masks on the weekday bytes.

    monday:011011010110111101101110011001000110000101111001 tuesday:01110100011101010110010101110011011001000110000101111001 wednesday:011101110110010101100100011011100110010101110011011001000110 +000101111001 thursday:0111010001101000011101010111001001110011011001000110000101111 +001 friday:011001100111001001101001011001000110000101111001 saturday:0111001101100001011101000111010101110010011001000110000101111 +001 sunday:011100110111010101101110011001000110000101111001

    (with this:

    for (qw/monday tuesday wednesday thursday friday saturday sunday/){ print $_ .':'. join('', map { sprintf "%08b", ord($_) } split(//, lc +$_))."\n"; }
    )

      Honestly, I find those magical formulas░ always too voodoo.

      From a maintenance point of view they are horrible, because they don't scale well with new requirements.

      Even Doomsday fails if you go back before Gregorian calendar

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      ░) don't know the correct term here... │

      update

      there is only a limited number of orders 7!

      so you could try a brute force search composing formulas based on inputs like substrings or word-length.

      I'm still not sure if the solution will be shorter than just a clever list.▓

      update

      ▓) tybaldt's solution with "motuwethfrsasu" is just an example of such a clever list.

      │) kind of golfing, isn't it?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145634]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-12-09 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?