From: superpollo on 27 Apr 2010 18:40 hi. i had to test an hypothesis about the relative frequencies of weekdays on the new-year-day (january 1). i came up with this quickie: $ echo -ne '\t' ; cal -m | head -2 | tail -1 ; for YEAR in $(seq 1585 1 3000) ; do cal -m 01 $YEAR | head -3 | tail -1 ; done | sort -r | uniq -c Mo Tu We Th Fr Sa Su 199 1 2 3 4 5 6 7 206 1 2 3 4 5 6 202 1 2 3 4 5 202 1 2 3 4 204 1 2 3 198 1 2 205 1 $ any suggestion for improvement? bye
From: pk on 27 Apr 2010 18:48 superpollo wrote: > hi. > > i had to test an hypothesis about the relative frequencies of weekdays > on the new-year-day (january 1). i came up with this quickie: > > $ echo -ne '\t' ; cal -m | head -2 | tail -1 ; for YEAR in $(seq 1585 1 > 3000) ; do cal -m 01 $YEAR | head -3 | tail -1 ; done | sort > -r | uniq -c > Mo Tu We Th Fr Sa Su > 199 1 2 3 4 5 6 7 > 206 1 2 3 4 5 6 > 202 1 2 3 4 5 > 202 1 2 3 4 > 204 1 2 3 > 198 1 2 > 205 1 > $ > > any suggestion for improvement? Well, an obvious way (which needs GNU date and bash) is something like day=("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun") for y in {1585..3000}; do d=$(date +%w -d "1/1/$y") freq[$d]=$((${freq[$d]}+1)) done i=0 while [ $i -lt 7 ]; do echo "${day[$i]} ${freq[$i]}" i=$((i+1)) done Sun 205 Mon 198 Tue 206 Wed 202 Thu 202 Fri 205 Sat 198 (not sure and too tired now to worry about the difference in the number of Fridays)
From: Seebs on 27 Apr 2010 20:15 On 2010-04-27, pk <pk(a)pk.invalid> wrote: > for y in {1585..3000}; do > d=$(date +%w -d "1/1/$y") > freq[$d]=$((${freq[$d]}+1)) > done I'd point out that this is a poor range to use. I'd suggest 1801-2200. You want a 400 year cycle. (Cool trivia point: In that 400 year cycle, which is an exact multiple of 7 days, it turns out that the 13th is more likely to be a Friday than any other day of the week.) -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: pk on 28 Apr 2010 04:22 pk wrote: > Well, an obvious way (which needs GNU date and bash) is something like > > day=("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun") > > for y in {1585..3000}; do > d=$(date +%w -d "1/1/$y") > freq[$d]=$((${freq[$d]}+1)) > done > > i=0 > while [ $i -lt 7 ]; do > echo "${day[$i]} ${freq[$i]}" > i=$((i+1)) > done > > Sun 205 > Mon 198 > Tue 206 > Wed 202 > Thu 202 > Fri 205 > Sat 198 Also, this can be implemented with GNU awk, dramatically faster and more efficient as there's only one proecess involved: awk 'BEGIN{split("Sun Mon Tue Wed Thu Fri Sat Sun",day) for(y=1585;y<=3000;y++)freq[strftime("%w", mktime(y " 01 01 00 00 00"))]++ for(i=0;i<7;i++)print day[i+1],freq[i]}' Sun 205 Mon 198 Tue 206 Wed 202 Thu 202 Fri 205 Sat 198 (Seeb's remarks about year ranges still apply)
From: Dr J R Stockton on 28 Apr 2010 12:34 In comp.unix.shell message <4bd767c7$0$1135$4fafbaef(a)reader1.news.tin.it >, Wed, 28 Apr 2010 00:40:04, superpollo <utente(a)esempio.net> posted: >hi. > >i had to test an hypothesis about the relative frequencies of weekdays >on the new-year-day (january 1). i came up with this quickie: > >$ echo -ne '\t' ; cal -m | head -2 | tail -1 ; for YEAR in $(seq 1585 1 >3000) ; do cal -m 01 $YEAR | head -3 | tail -1 ; done | sort >-r | uniq -c > Mo Tu We Th Fr Sa Su > 199 1 2 3 4 5 6 7 > 206 1 2 3 4 5 6 > 202 1 2 3 4 5 > 202 1 2 3 4 > 204 1 2 3 > 198 1 2 > 205 1 >$ > >any suggestion for improvement? The Gregorian Calendar repeats every 400 years, apart from Easter, so you should test 400 years exactly. For Easter, test 5,700,000 years. The question is strongly related to that of the frequency if Friday 13th, which might be a good thing to search for. JavaScript : A = [0,0,0,0,0,0,0] for (J=2000 ; J<2400 ; J++) A[new Date(J, 0, 1).getDay()]++ A // result : 58,56,58,57,57,58,56 -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05. Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
|
Next
|
Last
Pages: 1 2 Prev: parsing XML file with sed Next: Dealing with different number of fields in a file |