I'm going to begin my adventure into polyphasic sleep. In particular, I'm going to use the three-nap Everyman cycle. This blog will be a log of this adventure.
The three hour sleep is from 22:00 to 01:00. The three naps are at 06:30, 11:30, 16:30.
Hopefully I can adhere to this schedule. On school days, 06:30 is bus time, 11:30 is around lunch time, and 16:30 is after school and sometimes car time.
Entries for the USACO contests require a template header and some boilerplate code. To make things easier, I put boilerplate generation into a little script.
#!/bin/sh FILENAME=$1 TASKNAME=$(echo "$1" | sed -e 's/\..*$//') TEMPLATENAME=$(echo "$1" | sed -e 's/^.*\.//') sed -e 's/#TASKNAME#/'"$TASKNAME/" < template."$TEMPLATENAME" > "$FILENAME"
Template files (e.g. template.c look like this:
/* ID: your_id_here LANG: C TASK: #TASKNAME# */ #define PROG "#TASKNAME#" #include <stdio.h> int main() { FILE *fin = fopen(PROG ".in", "r"); FILE *fout = fopen(PROG ".out", "w"); return 0; }
Just give the Bash script the name of the file you need and it will make the file with the appropriate language (based off the extension given) and task name.
Yesterday, our math teacher announced there was to be held a county-sponsored math competition the following day. Long story short, I had a day to prepare for the competition. Sadly, I didn't know what to prepare for until the following day (that is, the day of the test). I was given a sample test and worked those math problems.
One of the problems was of the following sort:
Give the first 4 digits of 20092009.
This is a crazy problem, and I was sure there was some stupid math trick which wasn't really math to accomplish this.
I found a mathy way, though. I'm so proud.
Begin by writing the equation:
x = 20092009
Take the common logarithm (log10) of both sides:
log x = log 20092009
Turn the power into multiplication:
log x = 2009 * log 2009
Evaluate the right-hand expression:
log x = 6635.68669 (approx.)
The key to the problem is the next step. We split the right-hand expression into two, along the decimal point:
log x = 6635 + 0.68669 (approx.)
Then raise both sides as exponents with the base 10:
x = 106635 * 100.68669 (approx.)
Okay, we're almost done. Let's revisit the original problem:
Give the first 4 digits of 20092009.
How do we find the first four digits? Simple: because multiplying by 10n for any integer n simply moves the decimal place, not affecting any of the digits, we can disregard it. This reduces the problem to simply:
x = 100.68669 (approx.)
Then use a calculator to finish:
x = 4.86060131 (approx.)
We now have our four digits: 4, 8, 6, 0. (Not we aren't rounding.) Checking using Wolfram|Alpha, this answer is correct.
Simply change the numbers to suit any problem. You can even solve for the first digits using a different base by changing the base of the logarithm (using the change-of-base forumula if your calculator isn't godly).
A while ago, I wondered why I had a Twitter account. I didn't use it, and everything interesting I was doing I put in my away message in irssi (with, in addition to IRC, my IM accounts, thanks to BitlBee).
So I thought, why not Twitter my away messages? I made a Perl script for irssi to do just that:
use strict; use warnings; use vars qw($VERSION %IRSSI); use Irssi; use WWW::Curl::Easy; $VERSION = '1.00'; %IRSSI = ( authors => 'Strager Neds', contact => 'strager.nds@gmail.com', name => '/away twitter', description => 'Fuck descriptions', license => 'WTFPL', ); sub setTwitterStatus { # $_ is the message ... dunno how to do a substitute without $_. Perl's gay. $_ = shift; # Taken from http://support.internetconnection.net/CODE_LIBRARY/Perl_URL_Encode_and_Decode.shtml s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; my $newStatus = $_; $SIG{CHLD} = 'IGNORE'; # We fork so we don't freeze irssi. if(fork() == 0) { close(STDOUT); close(STDIN); close(STDERR); my $curl = new WWW::Curl::Easy; open(my $curlOutput, '>', '/dev/null'); open(my $curlInput, '<', '/dev/null'); open(my $curlStderr, '>', '/dev/null'); $curl->setopt(CURLOPT_WRITEDATA, $curlOutput); $curl->setopt(CURLOPT_READDATA, $curlInput); $curl->setopt(CURLOPT_STDERR, $curlStderr); $curl->setopt(CURLOPT_NOPROGRESS, 1); $curl->setopt(CURLOPT_URL, 'http://twitter.com/statuses/update.xml'); $curl->setopt(CURLOPT_USERPWD, Irssi::settings_get_str('twitter_username') . ':' . Irssi::settings_get_str('twitter_password')); $curl->setopt(CURLOPT_POST, 1); $curl->setopt(CURLOPT_POSTFIELDS, 'status=' . $newStatus); my $curlStatus = $curl->perform; close($curlOutput); close($curlInput); close($curlStderr); if($curlStatus != 0) { # Do something here. } exit(0); } } Irssi::command_bind 'away' => sub { my($data, $server, $windowItem) = @_; if(!($data =~ /./)) { $data = Irssi::settings_get_str('twitter_notawaytext'); } if(!($data =~ /./)) { return; } setTwitterStatus($data); }; Irssi::settings_add_str('twitter', 'twitter_username', ''); Irssi::settings_add_str('twitter', 'twitter_password', ''); Irssi::settings_add_str('twitter', 'twitter_notawaytext', ''); Irssi::theme_register([ 'away_hilight_notice_loaded', '%R>>%n %_Scriptinfo:%_ Loaded $0 version $1 by $2.' ]);
Dump the code into awaytwit.pl in ~/irssi/plugins/. In irssi, type /script load awaytwit. Configure using /set twitter.
I've found that when beginning to write a tokenizer from scratch, it's best to lay out the definitions of your token types. It doesn't have to be in some magic format; just something you can understand will work.
For each token type, create a function to read that token and no more, and return that token as an object. (Be sure to add error handling and such.) Different types of tokens should use the same base class. (You can use an enum or subclasses. I chose an enum for its simplicity.) As for the name, Read<em>Type</em> is what I chose, as it does exactly that: reads a number, string, etc. from the input.
Then have a function to figure out what token follows, and have it return the token using the function you created earlier. ReadToken is a descriptive name.
It's a simple while loop to read in multiple tokens. Be sure to skip whitespace between tokens if it's unimportant. Naturally, this goes in a function called Tokenize or similar.
And now you have your tokenizer. Simple, wasn't it?
Yes, a blag.
Why? To put shit people may want to read.
Yes. A blag.