Posts

Unique prefixes

Image
This is a problem about finding the shortest unique prefixes for a list of words. To illustrate this, bellow is an example with the prefixes highlighted: playi ng playe r To solve this problem, we are going to implement a generic algorithm which will work for any number of words. A very fast way of doing this, is by using multidimensional hashes. The simplest way to create a multidimensional hash, is to create a reference to the original hash, iterate over the keys you want to add and create a new hash for each key which doesn't exists inside the current hash, and change the hash reference to this new hash. Let's define a function which takes a reference to a hash and a string. First, it splits the string into characters, then it traverses the hash, adding each character to the corresponding branch: sub add_entry { my ( $ ref , $ word ) = @ _ ; foreach my $ char ( split ( // , $ word ) ) { $ ref = $ ref -> { $ char } //...

Sidef Programming Language

Image
Sidef is a modern, open-source, object-oriented programming language, fully implemented in Perl. Book:  https://trizen.gitbooks.io/sidef-lang/content/ Tutorial:  https://github.com/trizen/sidef/wiki RosettaCode:  https://rosettacode.org/wiki/Sidef ** ** **** * ********* ********* * * ** * * **** ** ** ** ** ** ** ** ** ** **** *** ********* * * * ** ** ** **** * * ****** ****** * * * * * * * * * **** ** ** ** ** ** ** ** ** ** **** ****** ****** * * ** ** **** * * * ********* *** * * ** * * **** ** ** ** ** ** ** ** ** ** **** ********* ********* * Main features of the language include: variables, constants modules, classes metaprogramming loops, c...

LZ compression

Image
The LZ compression family include a large variety of lossless data compression algorithms, all derived from a very simple algorithm  created by  Abraham Lempel  and   Jacob Ziv  in the late '70s, known as LZ77 . The algorithm is elegant and very dynamic and works excellent on very large input streams, producing the encoded output right away, ready to be decoded at the other end. Many websites are compressed with this algorithm, which reduces their size considerably with almost no cost regarding the decompression time. In this post, we are going to take a look at a new algorithm, very similar with  LZSS , called LZT. Compression The compress() function takes two arguments: the input stream, which can have any length, and the output stream which is ready to  receive  the encoded data chucks. Input :   TOBEORNOTTOBEORTOBEORNOT# Output:   TOBEORNOT [0,6][0,9] # Steps of the algorithm: Read 256 bytes from the input stre...

fbrowse-tray

Image
fbrowse-tray: browse the filesystem through a Gtk2 status icon (related to obbrowser ). The filesystem is browsed recursively, but lazily, which means that a directory's content will be read only after hovering the mouse over a submenu entry. Github :  https://github.com/trizen/fbrowse-tray AUR :  https://aur.archlinux.org/packages/fbrowse-tray/

Smart Word Wrap

Image
Recursion? Oh, we all love it! This post will try to illustrate how useful the recursion is and how complicated it can get sometimes. We, programmers, use recursion frequently to create small and elegant programs which can do complicated things. Just think about how would you split the following sequence  "aaa bb cc ddddd"  into individual rows with a maximum width of 6 characters per row. Well, one solution is to go from the left side of the string and just take words, and when the width limit is reached, append a newline. This is called the 'greedy word wrap algorithm'. It is very fast, indeed, but the output is not always pretty. Here enters another algorithm into play. We call it the 'smart word wrap algorithm'. Don't be fooled by the name. It's not smart at all! It can be, but not this one. For now it just tries in its head all the possible combinations, does some math and returns the prettiest result possible. It is much slower than the ...

tzip - file compressor

Image
tzip  is (probably) the simplest text-file compressor. It compresses a file by counting the number of different bytes and map those bytes to shorter sequences of bits. Each byte is composed from 8 bits. A byte value can range from 0 to 255 (inclusive), which means that 8 bits can be mixed up to represent only 256 different bit-sequences. In a text file, however, not all this bytes are used, and for a lower number of patterns, we can use less bits per pattern. For example, if we take the word ' youtube ', it is composed from 7 bytes, but has only 6 different bytes: 'y' , 'o' , 'u' , 't' , 'b' , 'e' To represent this bytes, we will need sequences of only three bits. To calculate exactly how many bits are required in a sequence for a given number of different bytes ( n ) we have the following formulas:      ceil ( log ( n ) / log ( 2 ) ) ; or: p = next_power_of_two ( n ) ; log ...