WeixiangYan/CodeTransOcean huggingface dataset:
Task: Old lady swallowed a fly

C: #include <stdio.h>
static char const *animals[] = {
    "fly", 
    "spider", 
    "bird", 
    "cat", 
    "dog", 
    "goat", 
    "cow", 
    "horse"
};
static char const *verses[]  = {
    "I don't know why she swallowed that fly.\nPerhaps she'll die\n",
    "That wiggled and jiggled and tickled inside her",
    "How absurd, to swallow a bird",
    "Imagine that. She swallowed a cat",
    "What a hog to swallow a dog",
    "She just opened her throat and swallowed that goat",
    "I don't know how she swallowed that cow",
    "She's dead of course"
};

#define LEN(ARR) (sizeof ARR / sizeof *ARR)

int main(void)
{
    for (size_t i = 0; i < LEN(animals); i++) {
        printf("There was an old lady who swallowed a %s\n%s\n", animals[i], verses[i]);
        for (size_t j = i; j > 0 && i < LEN(animals) - 1; j--) {
            printf("She swallowed the %s to catch the %s\n", animals[j], animals[j-1]);
            if (j == 1) {
                printf("%s\n", verses[0]);
            }
        }
    }
}










Fortran: program fly
  
  implicit none

  character(len=52), dimension(0:8,2) :: line
  integer :: i,j
  
  line(1,1) = 'fly'
  line(2,1) = 'spider'
  line(3,1) = 'bird'
  line(4,1) = 'cat'
  line(5,1) = 'dog'
  line(6,1) = 'goat'
  line(7,1) = 'cow'
  line(8,1) = 'horse'
  line(0,2) = "Perhaps she'll die."
  line(1,2) = "I don't know why she swallowed that fly."
  line(2,2) = "That wiggled and jiggled and tickled inside her." 
  line(3,2) = "How absurd to swallow a bird."                      
  line(4,2) = "Imagine that. She swallowed a cat."                  
  line(5,2) = "What a hog to swallow a dog."                        
  line(6,2) = "She just opened her throat and swallowed that goat." 
  line(7,2) = "I don't know how she swallowed that cow."            
  line(8,2) = "She's dead of course."                                  

  
  verses:do i = 1,8
     write(*,*) 'There was an old lady who swallowed a '//trim(line(i,1))//"."
     write(*,*) trim(line(i,2))
     
     swallowed:do j = i,2,-1
        write(*,*) "She swallowed the "//trim(line(j,1))//" to catch the "//trim(line(j-1,1))//","
     end do swallowed
     write(*,*)  trim(line(0,2))
     write(*,*)
  end do verses
     
end program fly
Task: Old lady swallowed a fly





Java: public class OldLadySwallowedAFly {

    final static String[] data = {
        "_ha _c _e _p,/Quite absurd_f_p;_`cat,/Fancy that_fcat;_j`dog,/What a hog"
        + "_fdog;_l`pig,/Her mouth_qso big_fpig;_d_r,/She just opened her throat_f_"
        + "r;_icow,/_mhow she_ga cow;_k_o,/It_qrather wonky_f_o;_a_o_bcow,_khorse.."
        + "./She's dead, of course!/", "_a_p_b_e ", "/S_t ", " to catch the ", "fly,/Bu"
        + "t _mwhy s_t fly,/Perhaps she'll die!
        + "t wr_nj_ntickled inside her;_aspider_b_c", ", to_s a ", "_sed ", "There_qan"
        + " old lady who_g", "_a_r_bpig,_d", "_acat_b_p,_", "_acow_b_r,_i", "_adog_bcat"
        + ",_j", "I don't know ", "iggled and ", "donkey", "bird", " was ", "goat", " swal"
        + "low", "he_gthe"};

    static boolean oldLady(String part, boolean s) {
        for (char c : part.toCharArray()) {
            if (s)
                s = oldLady(data[c - '_'], false);
            else if (c == '_')
                s = true;
            else
                System.out.print(c == '/' ? '\n' : c);
        }
        return s;
    }

    public static void main(String[] args) {
        oldLady(data[0], false);
    }
}






Fortran: program fly
  
  implicit none

  character(len=52), dimension(0:8,2) :: line
  integer :: i,j
  
  line(1,1) = 'fly'
  line(2,1) = 'spider'
  line(3,1) = 'bird'
  line(4,1) = 'cat'
  line(5,1) = 'dog'
  line(6,1) = 'goat'
  line(7,1) = 'cow'
  line(8,1) = 'horse'
  line(0,2) = "Perhaps she'll die."
  line(1,2) = "I don't know why she swallowed that fly."
  line(2,2) = "That wiggled and jiggled and tickled inside her." 
  line(3,2) = "How absurd to swallow a bird."                      
  line(4,2) = "Imagine that. She swallowed a cat."                  
  line(5,2) = "What a hog to swallow a dog."                        
  line(6,2) = "She just opened her throat and swallowed that goat." 
  line(7,2) = "I don't know how she swallowed that cow."            
  line(8,2) = "She's dead of course."                                  

  
  verses:do i = 1,8
     write(*,*) 'There was an old lady who swallowed a '//trim(line(i,1))//"."
     write(*,*) trim(line(i,2))
     
     swallowed:do j = i,2,-1
        write(*,*) "She swallowed the "//trim(line(j,1))//" to catch the "//trim(line(j-1,1))//","
     end do swallowed
     write(*,*)  trim(line(0,2))
     write(*,*)
  end do verses
     
end program fly
Task: Old lady swallowed a fly







Python: import zlib, base64

b64 = b
print(zlib.decompress(base64.b64decode(b64)).decode("utf-8", "strict"))




Fortran: program fly
  
  implicit none

  character(len=52), dimension(0:8,2) :: line
  integer :: i,j
  
  line(1,1) = 'fly'
  line(2,1) = 'spider'
  line(3,1) = 'bird'
  line(4,1) = 'cat'
  line(5,1) = 'dog'
  line(6,1) = 'goat'
  line(7,1) = 'cow'
  line(8,1) = 'horse'
  line(0,2) = "Perhaps she'll die."
  line(1,2) = "I don't know why she swallowed that fly."
  line(2,2) = "That wiggled and jiggled and tickled inside her." 
  line(3,2) = "How absurd to swallow a bird."                      
  line(4,2) = "Imagine that. She swallowed a cat."                  
  line(5,2) = "What a hog to swallow a dog."                        
  line(6,2) = "She just opened her throat and swallowed that goat." 
  line(7,2) = "I don't know how she swallowed that cow."            
  line(8,2) = "She's dead of course."                                  

  
  verses:do i = 1,8
     write(*,*) 'There was an old lady who swallowed a '//trim(line(i,1))//"."
     write(*,*) trim(line(i,2))
     
     swallowed:do j = i,2,-1
        write(*,*) "She swallowed the "//trim(line(j,1))//" to catch the "//trim(line(j-1,1))//","
     end do swallowed
     write(*,*)  trim(line(0,2))
     write(*,*)
  end do verses
     
end program fly
Task: Old lady swallowed a fly








VB: a=array( _
array("fly",""),_ 
array("spider","That wiggled and jiggled and tickled inside her"), _
array("bird","How absurd, to swallow a bird"), _
array("cat","Imagine that. She swallowed a cat"), _
array("dog","What a hog to swallow a dog"), _
array("goat","She just opened her throat and swallowed that goat"),_ 
array("cow","I don
array("horse","She
)

b1="I don
b2="There was an old lady who swallowed a @ani@."
b3="She swallowed the @ani@ to catch the @pani@."

sub print(s): wscript.stdout.write(s) & vbcrlf : end sub 

for i=0 to ubound(a)
     ani=a(i)(0)
     print replace(b2,"@ani@",ani)
     print a(i)(1)
     if i=ubound(a) then exit for  
   for j= i-1 to 0 step -1
      oldani=ani
      ani=a(j)(0)
      print replace(replace(b3,"@ani@",oldani),"@pani@",ani)
      if j=1 then print a(1)(1) 
   next 
   print b1
next



Fortran: program fly
  
  implicit none

  character(len=52), dimension(0:8,2) :: line
  integer :: i,j
  
  line(1,1) = 'fly'
  line(2,1) = 'spider'
  line(3,1) = 'bird'
  line(4,1) = 'cat'
  line(5,1) = 'dog'
  line(6,1) = 'goat'
  line(7,1) = 'cow'
  line(8,1) = 'horse'
  line(0,2) = "Perhaps she'll die."
  line(1,2) = "I don't know why she swallowed that fly."
  line(2,2) = "That wiggled and jiggled and tickled inside her." 
  line(3,2) = "How absurd to swallow a bird."                      
  line(4,2) = "Imagine that. She swallowed a cat."                  
  line(5,2) = "What a hog to swallow a dog."                        
  line(6,2) = "She just opened her throat and swallowed that goat." 
  line(7,2) = "I don't know how she swallowed that cow."            
  line(8,2) = "She's dead of course."                                  

  
  verses:do i = 1,8
     write(*,*) 'There was an old lady who swallowed a '//trim(line(i,1))//"."
     write(*,*) trim(line(i,2))
     
     swallowed:do j = i,2,-1
        write(*,*) "She swallowed the "//trim(line(j,1))//" to catch the "//trim(line(j-1,1))//","
     end do swallowed
     write(*,*)  trim(line(0,2))
     write(*,*)
  end do verses
     
end program fly
Task: Old lady swallowed a fly






PHP: <?php

$swallowed = array(
  array('swallowed' => 'fly.',
        'reason' => "I don't know why she swallowed the fly."),
  array('swallowed' => 'spider,',
        'aside' => "which wiggled and jiggled and tickled inside her.",
        'reason' => "She swallowed the spider to catch the fly"),
  array('swallowed' => 'bird.',
        'aside' => "How absurd! To swallow a bird!",
        'reason' => "She swallowed the bird to catch the spider,"),
  array('swallowed' => 'cat.',
        'aside' => "Imagine that! To swallow a cat!",
        'reason' => "She swallowed the cat to catch the bird."),
  array('swallowed' => 'dog.',
        'aside' => "What a hog! To swallow a dog!",
        'reason' => "She swallowed the dog to catch the cat."),
  array('swallowed' => 'horse',
        'aside' => "She's dead, of course. She swallowed a horse!",
        'reason' => "She swallowed the horse to catch the dog."));

foreach($swallowed as $creature)
{
  print "I knew an old lady who swallowed a " . $creature['swallowed'] . "\n";
  if(array_key_exists('aside', $creature))
    print $creature['aside'] . "\n";

  $reversed = array_reverse($swallowed);
  $history = array_slice($reversed, array_search($creature, $reversed));

  foreach($history as $note)
  {
    print $note['reason'] . "\n";
  }

  if($swallowed[count($swallowed) - 1] == $creature)
    print "But she sure died!\n";
  else
    print "Perhaps she'll die." . "\n\n";
}





Fortran: program fly
  
  implicit none

  character(len=52), dimension(0:8,2) :: line
  integer :: i,j
  
  line(1,1) = 'fly'
  line(2,1) = 'spider'
  line(3,1) = 'bird'
  line(4,1) = 'cat'
  line(5,1) = 'dog'
  line(6,1) = 'goat'
  line(7,1) = 'cow'
  line(8,1) = 'horse'
  line(0,2) = "Perhaps she'll die."
  line(1,2) = "I don't know why she swallowed that fly."
  line(2,2) = "That wiggled and jiggled and tickled inside her." 
  line(3,2) = "How absurd to swallow a bird."                      
  line(4,2) = "Imagine that. She swallowed a cat."                  
  line(5,2) = "What a hog to swallow a dog."                        
  line(6,2) = "She just opened her throat and swallowed that goat." 
  line(7,2) = "I don't know how she swallowed that cow."            
  line(8,2) = "She's dead of course."                                  

  
  verses:do i = 1,8
     write(*,*) 'There was an old lady who swallowed a '//trim(line(i,1))//"."
     write(*,*) trim(line(i,2))
     
     swallowed:do j = i,2,-1
        write(*,*) "She swallowed the "//trim(line(j,1))//" to catch the "//trim(line(j-1,1))//","
     end do swallowed
     write(*,*)  trim(line(0,2))
     write(*,*)
  end do verses
     
end program fly
Task: Old lady swallowed a fly






PHP: <?php

$swallowed = array(
  array('swallowed' => 'fly.',
        'reason' => "I don't know why she swallowed the fly."),
  array('swallowed' => 'spider,',
        'aside' => "which wiggled and jiggled and tickled inside her.",
        'reason' => "She swallowed the spider to catch the fly"),
  array('swallowed' => 'bird.',
        'aside' => "How absurd! To swallow a bird!",
        'reason' => "She swallowed the bird to catch the spider,"),
  array('swallowed' => 'cat.',
        'aside' => "Imagine that! To swallow a cat!",
        'reason' => "She swallowed the cat to catch the bird."),
  array('swallowed' => 'dog.',
        'aside' => "What a hog! To swallow a dog!",
        'reason' => "She swallowed the dog to catch the cat."),
  array('swallowed' => 'horse',
        'aside' => "She's dead, of course. She swallowed a horse!",
        'reason' => "She swallowed the horse to catch the dog."));

foreach($swallowed as $creature)
{
  print "I knew an old lady who swallowed a " . $creature['swallowed'] . "\n";
  if(array_key_exists('aside', $creature))
    print $creature['aside'] . "\n";

  $reversed = array_reverse($swallowed);
  $history = array_slice($reversed, array_search($creature, $reversed));

  foreach($history as $note)
  {
    print $note['reason'] . "\n";
  }

  if($swallowed[count($swallowed) - 1] == $creature)
    print "But she sure died!\n";
  else
    print "Perhaps she'll die." . "\n\n";
}
Task: Move-to-front algorithm




Go: package main

import (
	"bytes"
	"fmt"
)

type symbolTable string

func (symbols symbolTable) encode(s string) []byte {
	seq := make([]byte, len(s))
	pad := []byte(symbols)
	for i, c := range []byte(s) {
		x := bytes.IndexByte(pad, c)
		seq[i] = byte(x)
		copy(pad[1:], pad[:x])
		pad[0] = c
	}
	return seq
}

func (symbols symbolTable) decode(seq []byte) string {
	chars := make([]byte, len(seq))
	pad := []byte(symbols)
	for i, x := range seq {
		c := pad[x]
		chars[i] = c
		copy(pad[1:], pad[:x])
		pad[0] = c
	}
	return string(chars)
}

func main() {
	m := symbolTable("abcdefghijklmnopqrstuvwxyz")
	for _, s := range []string{"broood", "bananaaa", "hiphophiphop"} {
		enc := m.encode(s)
		dec := m.decode(enc)
		fmt.Println(s, enc, dec)
		if dec != s {
			panic("Whoops!")
		}
	}
}








Ada: with Ada.Text_IO;

procedure Move_To_Front is 
   
   subtype Lower_Case is Character range 'a' .. 'z';
   subtype Index is Integer range 0 .. 25;
   type Table is array (Index) of Lower_Case;
   Alphabet: constant Table := "abcdefghijklmnopqrstuvwxyz";
   type Number_String is array(Positive range <>) of Natural;
   
   function Encode(S: String) return Number_String is
      Key: Table := Alphabet;
   
      function Encode(S: String; Tab: in out Table) return Number_String is
	 
	 procedure Look_Up(A: in out Table; Ch: Lower_Case; Pos: out Index) is  
	 begin
	    for I in A'Range loop
	       if A(I) = Ch then 
		  Pos := I;
		  A := A(Pos) & A(A'First .. Pos-1) & A(Pos+1 .. A'Last);
		  return;
	       end if;
	    end loop;
	    raise Program_Error with "unknown character";
	 end Look_Up;
	 
	 Empty: Number_String(1 .. 0);
	 Result: Natural;   
      begin
	 if S'Length = 0 then 
	    return Empty;
	 else
	    Look_Up(Tab, S(S'First), Result);
	    return Result & Encode(S(S'First+1 .. S'Last), Tab);
	 end if;
      end Encode;
   
   begin
      return Encode(S, Key);
   end Encode;
      
   function Decode(N: Number_String) return String is
      Key: Table := Alphabet;
   
      function Decode(N: Number_String; Tab: in out Table) return String is
	 
	 procedure Look_Up(A: in out Table; Pos: Index; Ch: out Lower_Case) is  
	 begin
	    Ch := A(Pos);
	    A := A(Pos) & A(A'First .. Pos-1) & A(Pos+1 .. A'Last);
	 end Look_Up;
	 
	 Result: String(N'Range);
      begin
	 for I in N'Range loop
	    Look_Up(Tab, N(I), Result(I));
	 end loop;
	 return Result;
      end Decode;
      
   begin
      return Decode(N, Key);
   end Decode;
      
   procedure Encode_Write_Check(S: String) is
      N: Number_String := Encode(S);
      T: String := Decode(N);
      Check: String := (if S=T then "Correct!" else "*WRONG*!");
   begin
      Ada.Text_IO.Put("'" & S & "' encodes to");
      for Num of N loop
	 Ada.Text_IO.Put(Integer'Image(Num));
      end loop;
      Ada.Text_IO.Put_Line(". This decodes to '" & T & "'. " & Check);
   end Encode_Write_Check;
   
begin
   Encode_Write_Check("broood");
   Encode_Write_Check("bananaaa");
   Encode_Write_Check("hiphophiphop");
end Move_To_Front;
Task: Move-to-front algorithm







Python: from __future__ import print_function
from string import ascii_lowercase

SYMBOLTABLE = list(ascii_lowercase)

def move2front_encode(strng, symboltable):
    sequence, pad = [], symboltable[::]
    for char in strng:
        indx = pad.index(char)
        sequence.append(indx)
        pad = [pad.pop(indx)] + pad
    return sequence

def move2front_decode(sequence, symboltable):
    chars, pad = [], symboltable[::]
    for indx in sequence:
        char = pad[indx]
        chars.append(char)
        pad = [pad.pop(indx)] + pad
    return ''.join(chars)

if __name__ == '__main__':
    for s in ['broood', 'bananaaa', 'hiphophiphop']:
        encode = move2front_encode(s, SYMBOLTABLE)
        print('%14r encodes to %r' % (s, encode), end=', ')
        decode = move2front_decode(encode, SYMBOLTABLE)
        print('which decodes back to %r' % decode)
        assert s == decode, 'Whoops!'





Ada: with Ada.Text_IO;

procedure Move_To_Front is 
   
   subtype Lower_Case is Character range 'a' .. 'z';
   subtype Index is Integer range 0 .. 25;
   type Table is array (Index) of Lower_Case;
   Alphabet: constant Table := "abcdefghijklmnopqrstuvwxyz";
   type Number_String is array(Positive range <>) of Natural;
   
   function Encode(S: String) return Number_String is
      Key: Table := Alphabet;
   
      function Encode(S: String; Tab: in out Table) return Number_String is
	 
	 procedure Look_Up(A: in out Table; Ch: Lower_Case; Pos: out Index) is  
	 begin
	    for I in A'Range loop
	       if A(I) = Ch then 
		  Pos := I;
		  A := A(Pos) & A(A'First .. Pos-1) & A(Pos+1 .. A'Last);
		  return;
	       end if;
	    end loop;
	    raise Program_Error with "unknown character";
	 end Look_Up;
	 
	 Empty: Number_String(1 .. 0);
	 Result: Natural;   
      begin
	 if S'Length = 0 then 
	    return Empty;
	 else
	    Look_Up(Tab, S(S'First), Result);
	    return Result & Encode(S(S'First+1 .. S'Last), Tab);
	 end if;
      end Encode;
   
   begin
      return Encode(S, Key);
   end Encode;
      
   function Decode(N: Number_String) return String is
      Key: Table := Alphabet;
   
      function Decode(N: Number_String; Tab: in out Table) return String is
	 
	 procedure Look_Up(A: in out Table; Pos: Index; Ch: out Lower_Case) is  
	 begin
	    Ch := A(Pos);
	    A := A(Pos) & A(A'First .. Pos-1) & A(Pos+1 .. A'Last);
	 end Look_Up;
	 
	 Result: String(N'Range);
      begin
	 for I in N'Range loop
	    Look_Up(Tab, N(I), Result(I));
	 end loop;
	 return Result;
      end Decode;
      
   begin
      return Decode(N, Key);
   end Decode;
      
   procedure Encode_Write_Check(S: String) is
      N: Number_String := Encode(S);
      T: String := Decode(N);
      Check: String := (if S=T then "Correct!" else "*WRONG*!");
   begin
      Ada.Text_IO.Put("'" & S & "' encodes to");
      for Num of N loop
	 Ada.Text_IO.Put(Integer'Image(Num));
      end loop;
      Ada.Text_IO.Put_Line(". This decodes to '" & T & "'. " & Check);
   end Encode_Write_Check;
   
begin
   Encode_Write_Check("broood");
   Encode_Write_Check("bananaaa");
   Encode_Write_Check("hiphophiphop");
end Move_To_Front;
Task: Move-to-front algorithm

C: #include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_SIZE 100

int move_to_front(char *str,char c)
{
    char *q,*p;
    int shift=0;
    p=(char *)malloc(strlen(str)+1);
    strcpy(p,str);
    q=strchr(p,c); 
    shift=q-p;      
    strncpy(str+1,p,shift);
    str[0]=c;
    free(p);
  
    return shift;
}

void decode(int* pass,int size,char *sym)
{
    int i,index;
    char c;
    char table[]="abcdefghijklmnopqrstuvwxyz";
    for(i=0;i<size;i++)
    {
        c=table[pass[i]];
        index=move_to_front(table,c);
        if(pass[i]!=index) printf("there is an error");
        sym[i]=c;
    }
    sym[size]='\0';
}

void encode(char *sym,int size,int *pass)
{
    int i=0;
    char c;
    char table[]="abcdefghijklmnopqrstuvwxyz";
    for(i=0;i<size;i++)
    {
        c=sym[i];
        pass[i]=move_to_front(table,c);
    }
}

int check(char *sym,int size,int *pass)
{
    int *pass2=malloc(sizeof(int)*size);
    char *sym2=malloc(sizeof(char)*size);
    int i,val=1;

    encode(sym,size,pass2);
    i=0;
    while(i<size && pass[i]==pass2[i])i++;
    if(i!=size)val=0;

    decode(pass,size,sym2);
    if(strcmp(sym,sym2)!=0)val=0;

    free(sym2);
    free(pass2);

    return val;
}

int main()
{
    char sym[3][MAX_SIZE]={"broood","bananaaa","hiphophiphop"};
    int pass[MAX_SIZE]={0};
    int i,len,j;
    for(i=0;i<3;i++)
    {
        len=strlen(sym[i]);
        encode(sym[i],len,pass);
        printf("%s : [",sym[i]);
        for(j=0;j<len;j++)
            printf("%d ",pass[j]);
        printf("]\n");
        if(check(sym[i],len,pass))
            printf("Correct :)\n");
        else
            printf("Incorrect :(\n");
    }
    return 0;
}
Task: Move-to-front algorithm



C#: using System;
using System.Collections.Generic;
using System.Text;

namespace MoveToFront
{
    class Program
    {
        private static char[] symbolTable;
        private static void setSymbolTable()
        {
            symbolTable = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
        }

        private static void moveToFront(int charIndex)
        {
            char toFront = symbolTable[charIndex];
            for (int j = charIndex; j > 0; j--)
            {
                symbolTable[j] = symbolTable[j - 1];
            }
            symbolTable[0] = toFront;
        }

        public static int[] Encode(string input)
        {
            setSymbolTable();
            var output = new List<int>();
            foreach (char c in input)
            {
                for (int i = 0; i < 26; i++)
                {
                    if (symbolTable[i] == c)
                    {
                        output.Add(i);
                        moveToFront(i);
                        break;
                    }
                }
            }         
            return output.ToArray();
        }

        public static string Decode(int[] input)
        {
            setSymbolTable();
            var output = new StringBuilder(input.Length);
            foreach (int n in input)
            {
                output.Append(symbolTable[n]);
                moveToFront(n);
            }
            return output.ToString();
        }

        static void Main(string[] args)
        {
            string[] testInputs = new string[] { "broood", "bananaaa", "hiphophiphop" };
            int[] encoding;
            foreach (string s in testInputs) 
            {
                Console.WriteLine($"Encoding for '{s}':");
                encoding = Encode(s);
                foreach (int i in encoding)
                {
                    Console.Write($"{i} ");
                }
                Console.WriteLine($"\nDecoding for '{s}':");
                Console.WriteLine($"{Decode(encoding)}\n");
            }
        }
    }
}
Task: Move-to-front algorithm


C++: #include <iostream>
#include <iterator>
#include <sstream>
#include <vector>

using namespace std;

class MTF
{
public:
    string encode( string str )
    {
	fillSymbolTable();
	vector<int> output;
	for( string::iterator it = str.begin(); it != str.end(); it++ )
	{
	    for( int i = 0; i < 26; i++ )
	    {
		if( *it == symbolTable[i] )
		{
		    output.push_back( i );
		    moveToFront( i );
		    break;
		}
	    }
	}
	string r;
	for( vector<int>::iterator it = output.begin(); it != output.end(); it++ )
	{
	    ostringstream ss;
	    ss << *it;
	    r += ss.str() + " ";
	}
	return r;
    }

    string decode( string str )
    {
	fillSymbolTable();
	istringstream iss( str ); vector<int> output;
	copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter<vector<int> >( output ) );
	string r;
	for( vector<int>::iterator it = output.begin(); it != output.end(); it++ )
	{
	    r.append( 1, symbolTable[*it] );
	    moveToFront( *it );
	}
	return r;
    }

private:
    void moveToFront( int i )
    {
	char t = symbolTable[i];
	for( int z = i - 1; z >= 0; z-- )
	    symbolTable[z + 1] = symbolTable[z];

        symbolTable[0] = t;
    }

    void fillSymbolTable()
    {
        for( int x = 0; x < 26; x++ )
	    symbolTable[x] = x + 'a';
    }

    char symbolTable[26];
};

int main()
{
    MTF mtf;
    string a, str[] = { "broood", "bananaaa", "hiphophiphop" };
    for( int x = 0; x < 3; x++ )
    {
        a = str[x];
        cout << a << " -> encoded = ";
        a = mtf.encode( a );
        cout << a << "; decoded = " << mtf.decode( a ) << endl;
    }
    return 0;
}