37 points · by eatnumber1 · 12y ago · 46 comments
#!/bin/sh
grep -e "g\(()\)*('al')" $0 | sed 's/()/o/g' | sed "s/('al')/al/"
exit
g()('al')
On its own, g()('al') is valid but g()()('al') is a syntax error. Also, bash and sh don't have non-numeric return values, so regardless of the solution you're stuck with printing. case class Goal(count: Int) {
def apply() = Goal(count + 1)
def apply(s: String) = s"g${"o" * count}$s"
}
object Goal {
def g() = Goal(1)
}
Non-idiomatic shorter answer: case class g(p: String = "go") {
def apply() = g(p + "o")
def apply(s: String = "o") = p + s
}g(s: String) = "gal"
to handle the case of no 'o's.
using System;
using System.Text;
namespace Goal
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(G("al"));
Console.WriteLine(G()("al"));
Console.WriteLine(G()()()()()("al"));
}
static StringBuilder accumulator = new StringBuilder();
delegate dynamic GoalDelegate(string s = "o");
static dynamic G(string s = "o")
{
switch (s)
{
case "o":
accumulator.Append("o");
return (GoalDelegate)G;
case "al":
string result = "G" + accumulator.ToString() + "al";
accumulator.Clear();
return result;
default:
return null;
}
}
}
} using System;
using System.Linq;
class Program {
static void Main() {
Console.WriteLine(g("al"));
Console.WriteLine(g()("al"));
Console.WriteLine(g()()("al"));
Console.WriteLine(g()()()("al"));
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
delegate dynamic Goal(params string[] args);
static dynamic g(string suffix = null, string prefix = "g") {
if (suffix == "al") return prefix + suffix;
return (Goal)(a => g(a.FirstOrDefault(), prefix + "o"));
}
}
It was a fun exercise. I found that creating a github pull request was not such a fun exercise, and took about 10x longer than the actual task at hand. Perhaps I could learn to do it more smoothly with practice. print g('al')
for solution1 or: print m('rton')
for solution2, you'll find they don't print gal/ mrton.Here's what I came up with before looking at those solutions that seems to work:
def g(arg=None):
if arg == 'al':
return 'gal'
def inner(arg=None):
if arg == 'al':
return 'g' + (inner.counter * 'o') + 'al'
inner.counter += 1
return inner
inner.counter = 1
return innerI'd make a solution which resets the state linked by the table though, so please submit one!
g('al') must return "gal".
If you add assert g('al') == 'gal'
as the last line of solution 1, the assertion will not pass. I'm not saying there is a rule that mentions "resetting state". I'm saying that their failure to reset state is the bug leading to them not following the rules. # goal.rb
module G
def [](o=nil)
o ? "g#{values[0].to_s}al" : ({ :n => values[0].to_s + "o" }.extend G)
end
end
g = {}.extend G
eval DATA.read.gsub("(", "[").gsub(")", "]")
__END__ gal = g('al')
p gal
goal = g()('al')
p goal
goooooooal = g()()()()()()()('al')
p goooooooal
# ruby goal.rb
# "gal"
# "goal"
# "goooooooal"
# https://gist.github.com/morganhankins/de13ee378907143a7789More generally, what properties must a programming language satisfy for this problem to be solvable in it?
My current intuition is that if a language does not denote function calls with () enclosing arguments, AND if a language does not support metaprogramming (eg. LISP macros), then the problem is not solvable in it. Still thinking about it.
def g(a='', n=0):
return 'g '+ 'o'*n + a if a else lambda a='': g(a, n+1)https://github.com/kylecronin/goal/commit/70c9277ec99f80dca2...
https://github.com/eatnumber1/goal/blob/master/non-solutions...
is actually doable if you parse the error.
> perl -e "g()()"
syntax error at -e line 1, near ")("
Execution of -e aborted due to compilation errors.Can you submit a solution then?
function g(o)
if o then return "gal" end
o = "o"
local function go(al)
if al then
return "g"..o..al
else
o = o .. "o"
return go
end
end
return go
end
print(g()('al'))
More amusing and hackish solution: setmetatable(_G, -- or _ENV, but maintain 5.1 compatibility
{ __index = function(_, g)
local o = ""
local function go(al)
if al then
return g..o..al
end
o = o.."o"
return go
end
return go
end})
print(g()('al'))
print(G()'al')
print(Wh()()'pdedo') function goal(n)
return function(al)
if al then
return "G" .. string.rep("o", n) .. al
else
return goal(n+1)
end
end
end
G = goal(0)
print( G()()('al') )
Also note that G()'al' is just syntactic sugar for G()('al') in Lua