Get the k-th digit out of a number

2008/12/29

One really common problem is to get the k-th digit out of a number.
For example given the number

10210

we want to know what it the second digit.

We’ll see how we can solve elegantly this problem in python.

Read the rest of this entry »


Implementation of algorithms for inference of recursive types

2008/12/17

I uploaded my thesis thesis and a shorter abstract , the last one in italian and english.


Multilanguage and multiparadigm navigator 1/n Introduction

2008/12/08

In this series of post (where n > 1 and probably < 10) we’ll see how to implement a simple “navigator” in different languages.
This was a project I did for my university a long time ago that appeared to me interesting enough to improve it and publish it.
For navigator we mean a program that, given:

  • a table with distances between cities
  • a path
  • It returns the shortest path possible (if there’s one) with the total distance calculated.

    The algorithm used is mainly Floyd Warshall which calculates iteratively the nth-distance between couple of cities and creates a table.

    After that the only “problem” is building up the path of shortest paths.

    The problem has been coded in very different languages (using the specified compiler):

  • Prolog: swi prolog
  • lisp: sbcl
  • Python: python
  • In plus there is an input generator (still in python), a performance analyzer (in bash + gnuplot) and a nice grapher (in python + pydot) to get a graphical view of the problem’s instances.
    In this first chapter we’re just going to briefly analyze the how the input/output are formatted and the general structure of the program.

    The full source code (and much more) that I’m going to show can be found (maybe more updated) on my github page navigator .

    You can download everything by simply cloning

      git clone git://github.com/AndreaCrotti/navigator.git
    

    Read the rest of this entry »


    Copy paste from the command line

    2008/12/07

    If you are at least lazy as me and you use macosx and you hate the mouse I just found this little “shortcut”.

    Just go to your favorite terminal and do something like that:

    sh-3.2# pbcopy < file
    

    Now in your copy buffer you have your text file (pbcopy defaults to ASCII, see man pbcopy for further informations) and you can paste it everywhere you want or using pbpaste from terminal.

    Of course you can also put them in pipes, as for example this deletes comments from a shells script and puts the result in the copy buffer:

    sh-3.2# grep -v '^#' file | pbpaste
    

    Otherwise you should:

  • open the file
  • select all
  • copy
  • close the file
  • This is much faster, isn’t it?


    Random fortune cookie

    2008/12/04

    One of the nicest thing of linux I found at the very beginning of my *nix file was the automatically generated fortune at every login (with slackware).

    Well I know it’s not the most important thing for an operating system but it puts me in a good mood to start working in my favorite shell.
    Another nice program is cowsay , a funny cows (but not only) that tells you something.

    Putting them together you can have a random ascii-animal which tells you a random famous sentence!

    Just set your correct COWFILES directory and put it somewhere ;)

    COWFILES="/opt/local/share/cowsay/cows"
    NFILE=$(ls -l $COWFILES | wc -l)
    NUM=$(jot -r 1 1 $NFILE)
    COW=$(find $COWFILES -iname '*.cow' -print | sed -n "$NUM p")
    
    fortune | cowsay -f "$COW"
    

    If on your system you don’t have jot then you can obtain the random number line also in this way:

    NUM=$(($RANDOM % $NFILE))
    

    Read the rest of this entry »


    Downloading the whole rfc database

    2008/12/04

    This is my first post and I created it with textmate , which has a wonderful bundle to blog directly inside it.
    I created a little function which allows you to download the whole database of the request for comments:

    function update_rfc() {
    	RFC_DIR="$HOME/howto_guide/rfc/"
    	INDEX_FILE="1rfc_index.txt"
    	INDEX="http://www.ietf.org/iesg/$INDEX_FILE"
    	SITE="http://www.ietf.org/rfc"
    	[ -d "$RFC_DIR" ] ||  mkdir "$RFC_DIR" || return
    	cd "$RFC_DIR"
    	[ -f $INDEX_FILE ] || wget $INDEX || (echo "error can't get the index" 1>&2 && exit 2)
    
    	grep -E '^[0-9]{4}' $INDEX_FILE | awk -F ' ' '{print $1}' | while read NUM
    	do
    		FNAME="rfc$NUM.txt"
    		[ -f $FNAME ] && continue
    		# otherwise just try download it
    		echo "downloading rfc number $FNAME"
    		wget "$SITE/$FNAME" > /dev/null || echo "rfc $NUM not downloaded" 1>&2
    	done	
    }

    Just set where you want to download them in the variable “RFC_DIR”, put the function somewher (“.bashrc, .bash_profile” or whatever) and call it from the shell.
    You need to have wget installed, the other commands should be present on every *nix machine.

    Pay attention because it’s pretty big:

    du -sh $HOME/howto_guide/rfc
    266M	/Users/andrea/howto_guide/rfc
    


    Follow

    Get every new post delivered to your Inbox.