How is your Grammar?

Discussion of challenges you have already solved
moose
Posts: 67
Joined: Fri Jul 16, 2010 7:32 pm

How is your Grammar?

Post 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)
megabreit
Posts: 141
Joined: Sat Jan 03, 2009 3:33 pm

Post by megabreit »

<insert_your_favorite_editor_here> search&replace
User avatar
bsguedes
Posts: 103
Joined: Tue Feb 24, 2009 12:39 am
Location: Porto Alegre

Post by bsguedes »

did it by head :D
User avatar
laz0r
Posts: 290
Joined: Thu Feb 04, 2010 4:18 pm
Location: Within the depths of Unix

Post by laz0r »

The syntax was practically valid Mathematica anyway :)
There is no spoon.
lifthrasiir
Posts: 8
Joined: Tue Aug 16, 2011 12:35 pm

Post 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
AMindForeverVoyaging
Forum Admin
Posts: 496
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

Well, that one was easy.
jonik555
Posts: 43
Joined: Mon Aug 31, 2009 6:18 pm
Location: Prague

Post by jonik555 »

Fastest solution-> by head. just start from H and K (you see solution) and continue...
There are 10 types of people, those who understand ternary, those who think that this joke is about binary and the others.
Bliss
Posts: 10
Joined: Sat Oct 02, 2010 8:23 pm

Post by Bliss »

paper and pen very easy
Truly yours
muadiep
Posts: 1
Joined: Sun Apr 22, 2012 9:35 am

Post 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();
        }
    }
}


ChrFriedel
Posts: 3
Joined: Wed Aug 15, 2012 12:25 pm

Post 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

tiwe
Posts: 3
Joined: Thu Mar 05, 2009 2:09 pm

Post by tiwe »

using sed:
$ cat grammar.sed
:a
s/A/is/g
...
s/S/JKL/g
t a
$ echo S | sed -f grammar.sed
_matmat_
Posts: 2
Joined: Sat Aug 02, 2008 4:55 pm

Post 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"
kaio
Posts: 3
Joined: Mon Aug 23, 2010 1:59 pm

Post by kaio »

Should have been more steps to make it harder than simple pen&paper replacement.
AMindForeverVoyaging
Forum Admin
Posts: 496
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

You are free to submit a more complicated version of the same ;)
That is, once you reach the "create a challenge" challenge.
Limberneck
Posts: 3
Joined: Tue Jun 04, 2013 3:13 pm

Post by Limberneck »

Thisone reminded me of writing small compilers for expressions, very nice to do by hand.
Post Reply