Page 1 of 2

How is your Grammar?

Posted: Wed Aug 24, 2011 8:01 am
by moose
Here is my Python-solution:

Code: Select all

import re

#thanks to http://stackoverflow.com/questions/937697/can-you-pass-a-dictionary-when-replacing-strings-in-python
def dict_sub(d, text): 
  """ Replace in 'text' non-overlapping occurences of REs whose patterns are keys
  in dictionary 'd' by corresponding values (which must be constant strings: may
  have named backreferences but not numeric ones). The keys must not contain
  anonymous matching-groups.
  Returns the new string.""" 

  # Create a regular expression  from the dictionary keys
  regex = re.compile("|".join("(%s)" % k for k in d))
  # Facilitate lookup from group number to value
  lookup = dict((i+1, v) for i, v in enumerate(d.itervalues()))

  # For each match, find which group matched and expand its value
  return regex.sub(lambda mo: mo.expand(lookup[mo.lastindex]), text)


def replace(string):
    d={'A': 'is', 'B': 'mm', 'C': 'oo', 'D': 'rgr', 'E':'ryg', 'F': 'dth', 'G': 'you', 'H': 'esol', 'I': 'ionA', 'J':'GDaBarA', 'K':'veECFHutI','L':'PQ', 'M':'n','N':'m', 'O':'oaNcho', 'P':'MO', 'Q':'NR', 'R':'sky','S':'JKL'}
    return dict_sub(d, string)

string = 'S'
for i in xrange(0,20):
  print("%i: %s" %(i, string))
  string = replace(string)

Posted: Wed Aug 24, 2011 6:59 pm
by megabreit
<insert_your_favorite_editor_here> search&replace

Posted: Wed Aug 24, 2011 8:44 pm
by bsguedes
did it by head :D

Posted: Wed Aug 24, 2011 9:39 pm
by laz0r
The syntax was practically valid Mathematica anyway :)

Posted: Sat Aug 27, 2011 10:06 am
by lifthrasiir
Simple solution:

Code: Select all

g = {
  'A': 'is',
  'B': 'mm',
  'C': 'oo',
  'D': 'rgr',
  'E': 'ryg',
  'F': 'dth',
  'G': 'you',
  'H': 'esol',
  'I': 'ionA',
  'J': 'GDaBarA',
  'K': 'veECFHutI',
  'L': 'PQ',
  'M': 'n',
  'N': 'm',
  'O': 'oaNcho',
  'P': 'MO',
  'Q': 'NR',
  'R': 'sky',
  'S': 'JKL',
}
a = 'S'; b = ''
while a != b:
  a, b = ''.join(g.get(c,c) for c in a), a
print a

Posted: Sat Aug 27, 2011 7:48 pm
by AMindForeverVoyaging
Well, that one was easy.

Posted: Sun Sep 04, 2011 10:40 am
by jonik555
Fastest solution-> by head. just start from H and K (you see solution) and continue...

Posted: Thu Oct 06, 2011 5:13 pm
by Bliss
paper and pen very easy

Posted: Thu Jun 21, 2012 11:13 am
by muadiep
Made a simple C# Programm for running this Grammar ;)

Code: Select all

namespace How_is_your_grammer
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string,string> dic = new Dictionary<string,string>();
            dic.Add("A","is");
            dic.Add("B","mm");
            dic.Add("C","oo");
            dic.Add("D","rgr");
            dic.Add("E","ryg");
            dic.Add("F","dth");
            dic.Add("G","you");
            dic.Add("H","esol");
            dic.Add("I","ionA");
            dic.Add("J","GDaBarA");
            dic.Add("K","veECFHutI");
            dic.Add("L","PQ");
            dic.Add("M","n");
            dic.Add("N","m");
            dic.Add("O","oaNcho");
            dic.Add("P","MO");
            dic.Add("Q","NR");
            dic.Add("R","sky");
            dic.Add("S", "JKL");
            
            int i = 0;
            string start = "JKL";
            string temp;
            while (i < 100)
            {
                temp = start;
                foreach (char s in start)
                {
                    string buchstabe = s.ToString();
                    string r = "";

                    if (dic.ContainsKey(buchstabe))
                    {
                        temp = temp.Replace(buchstabe, dic[buchstabe]);
                    }
                }
                start = temp;
                i++;
            }

            Console.WriteLine(start);
            Console.Read();
        }
    }
}



Posted: Thu Aug 16, 2012 5:19 pm
by ChrFriedel
Did it without thinking ;)

Code: Select all


Public Class Challenge

    Private msg As String = ""

    Public Sub Start()
        S()
        Debug.Print(msg)
    End Sub

    Public Sub AddLetters(letters As String)
        msg &= letters
    End Sub

    Public Sub A()
        AddLetters("is")
    End Sub

    Public Sub B()
        AddLetters("mm")
    End Sub

    Public Sub C()
        AddLetters("oo")
    End Sub

    Public Sub D()
        AddLetters("rgr")
    End Sub

    Public Sub E()
        AddLetters("ryg")
    End Sub

    Public Sub F()
        AddLetters("dth")
    End Sub

    Public Sub G()
        AddLetters("you")
    End Sub

    Public Sub H()
        AddLetters("esol")
    End Sub

    Public Sub I()
        AddLetters("ion")
        A()
    End Sub

    Public Sub J()
        G()
        D()
        AddLetters("a")
        B()
        AddLetters("ar")
        A()
    End Sub

    Public Sub K()
        AddLetters("ve")
        E()
        C()
        F()
        H()
        AddLetters("ut")
        I()
    End Sub

    Public Sub L()
        P()
        Q()
    End Sub

    Public Sub M()
        AddLetters("n")
    End Sub

    Public Sub N()
        AddLetters("m")
    End Sub

    Public Sub O()
        AddLetters("oa")
        N()
        AddLetters("cho")
    End Sub

    Public Sub P()
        M()
        O()
    End Sub

    Public Sub Q()
        N()
        R()
    End Sub

    Public Sub R()
        AddLetters("sky")
    End Sub

    Public Sub S()
        J()
        K()
        L()
    End Sub

End Class


Posted: Sun Aug 19, 2012 10:50 pm
by tiwe
using sed:
$ cat grammar.sed
:a
s/A/is/g
...
s/S/JKL/g
t a
$ echo S | sed -f grammar.sed

Posted: Sun Apr 14, 2013 5:54 am
by _matmat_

Code: Select all

$value = "JKL";

while ($value =~ m/[A-Z]/) {
	$value =~ s/A/is/g;
	$value =~ s/B/mm/g;
	$value =~ s/C/oo/g;
	$value =~ s/D/rgr/g;
	$value =~ s/E/ryg/g;
	$value =~ s/F/dth/g;
	$value =~ s/G/you/g;
	$value =~ s/H/esol/g;		
	$value =~ s/I/ionA/g;	
	$value =~ s/J/GDaBarA/g;
	$value =~ s/K/veECFHutI/g;
	$value =~ s/L/PQ/g;
	$value =~ s/M/n/g;
	$value =~ s/N/m/g;				
	$value =~ s/O/oaNcho/g;				
	$value =~ s/P/MO/g;				
	$value =~ s/Q/NR/g;				
	$value =~ s/R/sky/g;								
	$value =~ s/S/JKL/g;				
}

print "val " . $value . "\n"

Posted: Fri May 24, 2013 5:26 pm
by kaio
Should have been more steps to make it harder than simple pen&paper replacement.

Posted: Fri May 24, 2013 7:33 pm
by AMindForeverVoyaging
You are free to submit a more complicated version of the same ;)
That is, once you reach the "create a challenge" challenge.

Posted: Wed Jun 05, 2013 10:01 pm
by Limberneck
Thisone reminded me of writing small compilers for expressions, very nice to do by hand.