1. Code Golf
- Posted by mattlewis (admin) Dec 17, 2008
- 1436 views
Over on StackOverflow, someone posted a Code Golf question (i.e., solve the problem in the shortest way possible). Here is the problem statement:
The goal is to create the program in a language with the least amount of characters. New lines and indenting should be left in for clarity, but not counted. Try to just have 1 submission per language. Edit the existing one if you can make it shorter.
Write a program which takes 2 command line arguments as defined below:
Arg1: a string to be "encoded"
Arg2: a pipe separated list of comma separated characters representing an encoding map to apply to the first argument. Example:
e,f|M,N|c,d|n,m|E,F|o,p|d,e
The program shall apply the character mapping specified in arg 2 to the characters of arg1, and display the output (map e to f, M to N, etc). The program above would output:
FmdpefNf (not FodpefNf as originally posted)
Additional Info:
- If a character in arg 1 is missing from the mapping in arg 2, that particular character should merely be skipped.
- Assume that no invalid input will ever be given (lack of separator, too many separators, etc), so no additional error checking or handling is needed
And here was my answer for euphoria. It has 94 non-whitespace characters. Can anyone shorten it further?
object e = command_line(), s = e[3] e = 0 & 0 & e[4] for i = 1 to length(s) do puts( 1, e[ match( s[i] & 44, e ) + 2] ) end for
Matt
2. Re: Code Golf
- Posted by SDPringle Dec 21, 2008
- 1353 views
Okay you can bump that down by 7 characters. I read the stackoverflow site and I wasn't surprised that java took two and a half times more characters to write than C. Seeing as you have to write CharacterAt() just to do what you could do with [] in EUPHORIA or C.
If you supply a character to the question mark operator it doesn't put a new line:
object e = command_line(), s = e[3] e = 0 & 0 & e[4] for i = 1 to length(s) do ? e[ match( s[i] & 44, e ) + 2] end for
Shawn Pringle
3. Re: Code Golf
- Posted by mattlewis (admin) Dec 21, 2008
- 1319 views
If you supply a character to the question mark operator it doesn't put a new line:
I don't think it works like you say. Here's what I get when I run your version:
$ exu encode.ex EncodeMe "e,f|M,N|c,d|n,m|E,F|o,p|d,e" 70 109 100 112 101 102 78 102
Here's what I get from the original version:
$ exu encode.ex EncodeMe "e,f|M,N|c,d|n,m|E,F|o,p|d,e" FmdpefNfMatt
4. Re: Code Golf
- Posted by ne1uno Dec 21, 2008
- 1363 views
If you supply a character to the question mark operator it doesn't put a new line:
I don't think it works like you say.
it's throwing nulls in there too. (little bit evil) since whitespace doesn't count, could be ok.
5. Re: Code Golf
- Posted by mattlewis (admin) Dec 21, 2008
- 1338 views
it's throwing nulls in there too. (little bit evil) since whitespace doesn't count, could be ok.
Yeah, you could change the second & 0 to & {""}, which would give the proper behavior if you're redirecting to a file, but since the nulls don't make it to the console, I figured the 3 character savings was worth the chance, since the output wasn't totally clear.
Matt
6. Calling call_back addresses
- Posted by SDPringle Dec 23, 2008
- 1319 views
I would like to call a call back address from EUPHORIA to simulate a call from C.
So, suppose I have a function like:
function five() return 5 end function atom five_cb = call_back("five")
How do I call five using five_cb?
Shawn
7. Re: Calling call_back addresses
- Posted by jimcbrown (admin) Dec 23, 2008
- 1341 views
Why is this in the Code Golf thread?
I would like to call a call back address from EUPHORIA to simulate a call from C.
So, suppose I have a function like:
function five() return 5 end function atom five_cb = call_back("five")
How do I call five using five_cb?
Shawn
include std/machine.e include std/dll.e function five() return 5 end function atom five_cb = call_back(routine_id("five")) integer cb = define_c_func("", {'+', five_cb}, {}, C_INT) ? c_func(cb, {})
8. Re: Calling call_back addresses
- Posted by ghaberek (admin) Dec 30, 2008
- 1279 views
Why is this in the Code Golf thread?
Did somebody make a fubar, or is this bug in the forum?
9. Re: Calling call_back addresses
- Posted by jeremy (admin) Dec 30, 2008
- 1276 views
I think a mistake by hitting reply. Each reply can have it's own subject title, if you want to break off into a sub-thread. Seems Reply was hit here on accident for a totally different thread.
Jeremy
10. Re: Calling call_back addresses
- Posted by SDPringle Dec 31, 2008
- 1267 views
That's right. I clicked reply and changed the subject line. It seems the changing subject line doesn't put you on a new thread.
To me golf should be to minimize the interpreter itself (the c code) while getting all of the features of 4.0. That is many new builtins could have just been left as EUPHORIA code written with existing no no builtins. Regular expressions could have been implemented using c_func/c_proc calling into a dll or the binary itself.
Golf would seem to favor recursive algorithms to ones that simulate the runtime stack and also encourage more and smaller routines. It also favors code that uses routines that both load from and into some default variable and return on the stack rather than only returning on the stack.
Golf rules should also include:
Count all user identifers as some constant size no matter how long each would be and no recycling Variables allowed.
Shawn Pringle