2011-02-27

How to Highlight Code in Blogger



僕はBloggerでコードに色付けするときは、Syntax Highlighterを使っているんですが、古いversionのSyntax Highlighterだと、どうやらshellなどのコードは色付け出来ないようです。色付けするためには、最新のversionに更新する必要があります。僕の場合は、こんな感じで更新出来ました。

1. DashboardからDesign -> Edit HTMLに行き、"/head"を検索。

2. 見つかった</head>タグの直前に、次のコードを挿入する。

<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css"></link>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
赤字は、ハイライトしたい言語のソースです。今回はbashのスクリプトをハイライトしたいので、shBrushBash.jsになります。他の利用可能な言語とそのhostはココを参照します。

3. 投稿する記事のハイライトしたいコードを、次のタグで囲います。

<pre class="brush: bash">
echo "hoge"
</pre>

赤字は、ハイライトしたい言語のaliasです。他の利用可能な言語のaliasはココを参照します。

僕の場合、こんな感じで記事を投稿したら、先の投稿のように、bashをハイライトする事が出来ました。何かの参考になれば幸いです。

Package Search Shell Script for Mac



お久しぶりです。twitterでも少しつぶやきましたが、最近、CSに興味を持った学生に対して、プログラミングやUnix、Macについて少し教えています。先日は、macのfinkやportについて教えていたんですが、そのときに例として使ったpackage-searchという自作シェルスクリプトが結構受けたので、一応公開しておきます (といっても、たかだか10行程度のスクリプトなんですが)。

#!/bin/sh
if [ $# -eq 1 ]
then
    echo "----- fink package -----" > '/tmp/package-search.tmp'
    fink list $1 >> '/tmp/package-search.tmp'
    echo "" >> '/tmp/package-search.tmp'
    echo "----- port package -----" >> '/tmp/package-search.tmp'
    port search $1 >> '/tmp/package-search.tmp'
    lv '/tmp/package-search.tmp'
else
    echo "Usage: package-search PACKAGE_NAME"
fi


.bashrc等に上記スクリプトを登録して、terminal上で実行するとこんな感じに動きます。

     $ package-search wget


----- fink package -----
        gwget   1.0.4-2 Download manager for GNOME
        wget    1.12-1  Automatic web site retriever (SSL)
        wget-ssl        1.10.2-16       Placeholder package to update to unified wget. (OBSOLETE)

----- port package -----
gwget @1.0.4 (gnome, net)
Gwget is a Download Manager for Gnome 2. It uses wget as a backend.

wget @1.12 (net, www)
internet file retriever

wgetpro @0.1.3 (net, www)
advanced internet file retriever

wput @0.6.2 (net)
wput is like wget but is for uploading files to ftp-servers

Found 4 ports.



ところで、教えるのは結構難しいですね。如何に良い質問を相手に与えて考えさせるか、そして、返ってきた回答(特に、予想もしてなかった回答)に対して如何に本筋から離れること無く反応するか、ここら辺が難しい気がします。

2011-02-02

A* Search Algorithm

どうも、お久しぶりです。アメリカの中西部は今日と明日が最悪のブリザード日和で、もう外はカオス状態です。どのぐらいカオスかというと、ドアを解錠したはずのに何故か開かないと思ったら、実はドアが凍ってたとか、そのぐらいカオスです。

それはさておき、先日から散々tweetしていたように、最近AIの授業の課題で、A*探索を実装しました。あまり需要は無いかもしれませんが、何かの参考になれば幸いです。ちなみに、pythonで実装してます。「A* Searchって何ぞや?」って人はココら辺を参照してください。





以下、プログラムの具体的な説明(READMEから引用)です。
ソースコード?それをみせるなんて、とんでもない!

Description
-----------
This program implements the idea of A* algorithm
with following three heuristic functions:

- 1. Zero heuristic
- 2. Manhattan heuristic
- 3. Euclidean heuristic

In Zero heuristic, each space in a maze is equally cost,
so the next-exploring space totally depends on how spaces
are put into the queue. In this program, the spaces are
put into the queue in the way from top left to bottom right.
For example, if the program seeks the 3x3 map, it should put
as follows:

1 2 3
4 5 6
7 8 9


Manhattan heuristic calculates costs with Manhattan distance.
For example, in the following state, where the '*' is goal,
'x' is current location, and 'o' is the start, the cost
should be 4.

    * . .
    . x .
    . . o
  
In addition, if some elements in the queue has
same cost, the queue sorts them according to the sort of
Queue.PriorityQueue in Python library.

In particular, the priority queue sorts elements in the queue
by the ascend order of number x if given element's costs are
all same. Also, it sorts them by the ascend order of number y
if given element's costs and number of x are all same.


Euclidean heuristic calculates costs with Euclidean distance.
For example, in the following state, the cost should be 10.

f(x) = h(x) + g(x)
     = sqrt(3^2+4^2) + sqrt(4^2+3^2)
     = 5 + 5
     = 10

    * . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . o . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . x

So, using these heuristics, this program shows how A* algorithm
works step by step.



How To Run
----------
1. Get python 2.6 or higher

2. Type following command in your terminal.

  $ python main.py [options]

Options are not needed to run the program. But,
if you want to specify input files, heuristic
function, or make program display much information,
the following options will be useful.

  -h, --help          
show this help message and exit

  -f FILE, --file=FILE
choose a formatted text file for creating a maze. This program uses 'mazes/sample' by default.

  -H HEURISTICS      
choose a heuristic function for A* algorithm from 'zero', 'manhattan', 'euclidean' (or it's abbreviations: 'z', 'm', and 'e'). This option sets'zero' by default.

  -v, --verbose      
display information verbosely.

  -d, --diagonal      
switch to the program that allows diagonal travel for a cost of 3, and horizontal/vertical motion costs 2.



Get Latest Code
---------------
If you would like to see/run the latest code,
type following command to clone it with Git.

  $ git clone git://github.com/yasulab/a-star-algorithm.git

Or, visit the following repository.


To clone the code, you need to install Git.

   Git - Fast Version Control System