Omnimaga

Calculator Community => Contests => Community Contests => Topic started by: JWinslow23 on July 22, 2014, 11:32:51 am

Title: [ENDED] Code Golf Contest #2
Post by: JWinslow23 on July 22, 2014, 11:32:51 am
Here is the second code golf contest. Rules here (http://www.omnimaga.org/other-calculator-discussion-and-news/code-golf-contest-1/msg389458/#msg389458).

NEXT: Here (http://www.omnimaga.org/other-calculator-discussion-and-news/code-golf-contest-3)
PREVIOUS: Here (http://www.omnimaga.org/other-calculator-discussion-and-news/code-golf-contest-1)

Challenge 2

Problem
Make a program with the following input and output:

Input: A string of any length, made up of clusters of characters and numbers separated by spaces.

Output: All non-number "words", concatenated together in reverse order, with the sum of all numeric "words" following, all separated by spaces.

Deadline
July 28, 2014, 1:00 AM EST

Sample input 1
"1 asdf 15 1fg Iamamazing 14"
Sample output 1
"Iamamazing 1fg asdf 30"
Sample input 2
"Hello W0rld 63 How 4r3 you 6"
Sample output 2
"you 4r3 How W0rld Hello 69"

If any further clarification is necessary, please contact me or willrandship. We will try to explain.

Ranking

Ruby 2
RankUserSizeDateCode
1Juju807/23/2014 12:39:40 PM
Spoiler For Spoiler:
a=0;print gets.split.reverse.reject{|b|a!=a+=Integer(b)rescue 0}.join(" ")," ",a

Golfscript
RankUserSizeDateCode
1Runer112327/27/2014 12:27:24 PM
Spoiler For Spoiler:
" "%-1%0\{.{|}*64<{~+}{" "@}if}/
2JWinslow23607/22/2014 1:02:09 PM
Spoiler For Spoiler:
" "%-1%0:a;{..{.47>\59<and},={~a+:a;}{}if}/]{.!{}{" "+}if}/a

Nspire Lua
RankUserSizeDateCode
1Jens_K113 (copy input to clipboard)7/23/2014 9:15:32 AM
Spoiler For Spoiler:
s,n="",0
for w in clipboard.getText():gmatch"%S+"do
if tonumber(w)then n=n+w else s=w.." "..s end
end
print(s..n)
2LDStudios1627/23/2014 3:30:25 PM
Spoiler For Spoiler:
i="" p={} function on.charIn(h) s="" i=i..h p=i:split(s) n=0 for i,v in ipairs(p) do if v:find("%a") then s=v.." "..s elseif v:find("%d") then n=n+v end end print(i) print(s..n) end

Python3
RankUserSizeDateCode
1willrandship837/22/2014 3:08:29 PM
Spoiler For Spoiler:
s=0;o=''
for w in input().split():
 try:s+=int(w)
 except:o=o+w+" "
print(o+str(s))

Java
RankUserSizeDateCode
1Runer1121747/27/2014 12:27:24 PM
Spoiler For Spoiler:
class B{public static void main(String[]a){int x=0,i=a.length;while(i>0)try{x+=Integer.parseInt(a[--i]);}catch(Exception e){System.out.print(a+' ');}System.out.print(x);}}
232981787/27/2014 1:58:00 PM
Spoiler For Spoiler:
class G{public static void main(String[]c){int n=0;String s="";for(String i:c[0].split(" ")){try{n+=Integer.parseInt(i);}catch(Exception e){s=i+" "+s;}}System.out.println(s+n);}}
3ben_g1987/22/2014 4:01:06 PM
Spoiler For Spoiler:
public class Main{public static void main(String[] args){String s="";int i=0;for(String t:args[0].split(" ")){try{i+=Integer.parseInt(t);}catch(Exception e){s=t+" "+s;}}s+=i;System.out.println(s);}}

CJam
RankUserSizeDateCode
1Runer112277/27/2014 12:27:24 PM
Spoiler For Spoiler:
qS%W%0\{_:i:|'A<{~+}{S@}?}/

XTend
RankUserSizeDateCode
132981597/27/2014 1:58:00 PM
Spoiler For Spoiler:
class G{static var n=0;def static void main(String[]c){println(c.get(0).split(" ").fold("")[s,i|try{n=n+Integer.parseInt(i);s}catch(Exception _){i+" "+s}]+n)}}

Haskell
RankUserSizeDateCode
132981387/27/2014 1:58:00 PM
Spoiler For Spoiler:
import Text.Read
g c=(fst f)++show(snd f)where f=foldr(\i(s,n)->case readMaybe i of Nothing->(s++i++" ",n);Just m->(s,n+m))("",0)(words c)

SysRPL
RankUserSizeDateCode
13298797/27/2014 1:58:00 PM
Spoiler For Spoiler:
::
  BINT0 FPTR2 ^StrCutNchr_
  Z0_ NULL$ ROT BEGIN
    SEP$NL FPTR2 ^S>Z? ITE
    :: 4ROLL FPTR2 ^QAdd UNROT ;
    :: APPEND_SPACE ROT &$SWAP ;
  DUPNULL$? UNTIL
  DROPSWAP FPTR2 ^Z>S &$
;

Language Ranking

RankLangUserSizeDate
1CJamRuner112277/27/2014 12:27:24 PM
2GolfscriptRuner112327/27/2014 12:27:24 PM
3SysRPL3298797/27/2014 1:58:00 PM
4Ruby 2Juju807/23/2014 12:39:40 PM
5Python3willrandship837/22/2014 3:08:29 PM
6Nspire LuaJens_K113 (copy input to clipboard)7/23/2014 9:15:32 AM
7Haskell32981387/27/2014 1:58:00 PM
8XTend32981597/27/2014 1:58:00 PM
9JavaRuner1121747/27/2014 1:58:00 PM
Title: Re: Code Golf Contest #2
Post by: Juju on July 22, 2014, 12:42:06 pm
Woo, got 99 bytes in Ruby.
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 22, 2014, 03:07:16 pm
Yup. And I finally figured out a Golfscript solution.
Title: Re: Code Golf Contest #2
Post by: willrandship on July 22, 2014, 04:56:51 pm
Here's a handy tool to get your code size.

http://mothereff.in/byte-counter
Title: Re: Code Golf Contest #2
Post by: Juju on July 23, 2014, 02:08:51 am
Aha, down to 80 bytes! Try to beat me :P
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 23, 2014, 03:28:19 am
Aha, down to 80 bytes! Try to beat me :P
60 in Golfscript. :P

Processing the new solution tomorrow.
Title: Re: Code Golf Contest #2
Post by: Runer112 on July 23, 2014, 09:38:08 am
Can we get some official clarification on input format, JWinslow23/willrandship? The main questions that come to mind are:


A good way to solidify all of these rules at once could be to specify a regular expression for valid input.
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 23, 2014, 12:48:02 pm
Can we get some official clarification on input format, JWinslow23/willrandship? The main questions that come to mind are:

  • Are words/clusters composed of alphanumeric characters, any printable characters, or something in between?
  • Are leading, trailing, or duplicated (successive) spaces valid?

A good way to solidify all of these rules at once could be to specify a regular expression for valid input.
Well, if you use regular expressions, now you got TWO problems. :P

Words are made up of any printable characters except for the space. Trailing, leading, and repeating spaces are valid, but shall be deleted so there is only one space between words upon output.
Title: Re: Code Golf Contest #2
Post by: LDStudios on July 23, 2014, 01:51:12 pm
(http://img.ourl.ca/07-23-2014%20Image001.png)
Title: Re: Code Golf Contest #2
Post by: Runer112 on July 23, 2014, 03:29:52 pm
Words are made up of any printable characters except for the space. Trailing, leading, and repeating spaces are valid, but shall be deleted so there is only one space between words upon output.

Are we sure this is the official ruling? Because when I asked willrandship his thoughts in IRC yesterday, he gave basically the opposite answers; words made up of alphanumeric characters only, and no trailing, leading, or repeating spaces. I personally prefer willrandship's view, because it gives a better chance to languages that don't have string splitting/regex functions and generally allows for more golfed/hacky solutions.
Title: Re: Code Golf Contest #2
Post by: Juju on July 23, 2014, 05:30:03 pm
Words are made up of any printable characters except for the space. Trailing, leading, and repeating spaces are valid, but shall be deleted so there is only one space between words upon output.

Are we sure this is the official ruling? Because when I asked willrandship his thoughts in IRC yesterday, he gave basically the opposite answers; words made up of alphanumeric characters only, and no trailing, leading, or repeating spaces. I personally prefer willrandship's view, because it gives a better chance to languages that don't have string splitting/regex functions and generally allows for more golfed/hacky solutions.
I kind of prefer the one words include those beginning with numbers, as stated in the examples in the OP. Either way, if numbers include alphanumeric characters after them as Runer said, my entry would be even more golfed and I would save about 12 characters. But I don't care.
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 23, 2014, 05:33:23 pm
You know what? Let's just assume no trailing, leading, or repeating spaces, and all alphanumeric characters. That would bring my solution down from 465 bytes to about 280 bytes. :P
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 26, 2014, 06:36:40 pm
2 more days left for submissions! Oh, and all winners in categories, think of possible next challenges.
Title: Re: Code Golf Contest #2
Post by: 3298 on July 27, 2014, 09:40:43 am
Hello there, I have been reading some sections of Omnimaga for several months now (mostly the Casio and HP sections), and I finally decided to register so I can participate in this contest. After registering I found out that I need 5 posts to be able to send PMs, and I'm not the type of user who then posts in 5 random topics, so ... shall I just put my programs in spoilers, or what should I do?
(Edited to add: That's at least what some thread in the news section said. Now I see a PM icon below your user text which wasn't there before, but trying to use that will probably fail.)
I have written solutions in Java (190 bytes including a single-letter class name), XTend (a language implemented on top of Java; 160 bytes, with the same single-letter class name), Haskell (156 bytes, again including a single-letter function name) and SysRPL (79 bytes without name in compiled form, but due to the on-calc decompiler that's a feasible format for storage, you only lose pretty formatting and comments).
Even though not required, I tried throwing strings with non-alphanumeric characters and duplicate / leading / trailing spaces at my programs to see what happens. Java and XTend handle other characters just fine, the spaces are retained (can be fixed with 17 more bytes for Java and 9 for XTend). Haskell removes those spaces, but it might treat newlines and tabs as spaces. SysRPL keeps the spaces, but with 5 more bytes that's fixed. It also treats at least newlines as spaces, maybe tabs as well.
Title: Re: Code Golf Contest #2
Post by: DJ Omnimaga on July 27, 2014, 11:28:47 am
Hi and welcome to the forums. To check if PMs work for you right now you could try to send a PM to yourself. Just go to the Inbox then click send message and put your nickname there. If it works then you probably can send PMs to everyone.

The limit used to be 5 posts because automatically registered bots would just advertise non-stop via PM, sometimes with very bad links. If you can't send PMs I guess you could also e-mail JWinslow23, if he gives it to you.
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 27, 2014, 01:16:33 pm
Welcome to the forums! You somehow remind me of the guy who registered and made a new topic just so he could get a solution to a level in Portal Prelude. XD

Anyways, so yeah, unless you post 4 more times AND PM me your solutions by the end of the day, I'll PM you with my email.
Title: Re: Code Golf Contest #2
Post by: 3298 on July 27, 2014, 01:19:53 pm
Thanks for the welcome messages! I have written a few posts on the UCF and the recently closed DCF in the last few years, and I saw some of DJ's old posts on the UCF. And before the CasioScene drama happened I was a registered user at CasioKingdom. The restore to an old backup over there removed all evidence of that, but I won't go back there because it's become a toyland for spambots.
The permissions seem a bit screwed up, because I tried the test message you suggested, and it actually worked. I understand why this limit is in place (look at CasioKingdom to see what happens without such protection), and I think someone should enforce what that post in the news section says. I can only assume this no-PM rule has been softened, because I had to solve a captcha like at my registration. (Not a problem for a human, though.)
So with the PM way apparently working, I'll use it.
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 27, 2014, 04:42:44 pm
That was the LONGEST update I've ever had to do. O.O But I did it! :thumbsup:
Title: Re: Code Golf Contest #2
Post by: DJ Omnimaga on July 27, 2014, 04:45:28 pm
Thanks for the welcome messages! I have written a few posts on the UCF and the recently closed DCF in the last few years, and I saw some of DJ's old posts on the UCF. And before the CasioScene drama happened I was a registered user at CasioKingdom. The restore to an old backup over there removed all evidence of that, but I won't go back there because it's become a toyland for spambots.
The permissions seem a bit screwed up, because I tried the test message you suggested, and it actually worked. I understand why this limit is in place (look at CasioKingdom to see what happens without such protection), and I think someone should enforce what that post in the news section says. I can only assume this no-PM rule has been softened, because I had to solve a captcha like at my registration. (Not a problem for a human, though.)
So with the PM way apparently working, I'll use it.
Yeah I wish that CK forums owners added more anti-spams on the site after the restoral. It was old but it had lots of useful info. I guess for Omni the permissions were messed up in the upgrade we also had a few months ago. Admins will probably re-instate the post limit if more bots come in here, but let's hope the bots won't do so.

Also I didn't know that DCF closed, that sucks D:
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 27, 2014, 10:12:24 pm
Quickly quickly quickly! Submit your final entries by the end of the night! Also, any winners, think of challenges for our next contest!
Title: Re: Code Golf Contest #2
Post by: willrandship on July 28, 2014, 01:19:04 am
Here's my suggestion for the next challenge:

Input: a positive integer of reasonable size
Output: The highest prime factor of that integer, in binary, but with all 0s replaced with underscores (_) and all 1s replaced with minus signs (-)

Example:
Input: 15
Output: -_-

Example:
Input: 7
Output: ---

Example:
Input: 115
Output: -_---
Title: Re: Code Golf Contest #2
Post by: Princetonlion.tibd on July 28, 2014, 08:29:53 am
I don't think there are underscores in TI BASIC, but I'll try if you win
Title: Re: Code Golf Contest #2
Post by: Runer112 on July 28, 2014, 12:04:31 pm
Here's my suggestion for the next challenge:

Input: a positive integer of reasonable size
Output: The highest prime factor of that integer, in binary, but with all 0s replaced with underscores (_) and all 1s replaced with minus signs (-)

Example:
Input: 15
Output: -_-

Example:
Input: 7
Output: ---

Example:
Input: 115
Output: -_---

This is the kind of creatively new yet simple challenge that I wish I could think of. :P

I don't think there are underscores in TI BASIC, but I'll try if you win

There is an underscore token, although it's not typeable on a calculator through normal means. It's two-byte code is BBD9. It could be entered either in a computer tokenizer or perhaps produced by an on-calc tool made for producing untypeable characters, like this (http://www.ticalc.org/archives/files/fileinfo/384/38404.html).
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 28, 2014, 01:07:22 pm
Interesting. I like it! :thumbsup:

Now here's my TI-BASIC solution for contest #2 in 201 bytes:
Code: [Select]
:Input Str1
:DelVar CDelVar X" →Str2
:Repeat not(B
:X+1→X
:0→M
:For(N,1,X
:Ans+1→A
:inString(Str1," ",Ans→B
:End
:If not(Ans
:1+length(Str1
:sub(Str1,A,Ans-A
:For(L,1,length(Ans
:If inString("0123456789",sub(Ans,L,1
:IS>(M,M
:
:End
:If M=length(Ans
:Then
:C+expr(Ans→C
:Else
:Ans+" "+Str2→Str2
:End
:End
:ClrHome
:Output(1,1,Str2
:length(Str2)/16
:Output(int(Ans)+not(int(Ans)),16fPart(Ans)+16not(fPart(Ans)),C
Title: Re: Code Golf Contest #2
Post by: 3298 on July 28, 2014, 02:51:23 pm
Looking over the solutions, I noticed that the shortest Java version does not process one string, but instead assumes someone has split it already and put the parts into the command-line parameters array before the program starts. On the other hand, it shows how my solution could be shortened further (println ->print, and I could have omitted curly braces around the body of the for loop).
Edit: And that version apparently has a bug, it prints the entire array when it failed to parse one element to an integer. There are three characters missing (a+' ' should be a+' '). Due to the size being 171 instead of 174, they apparently got lost somewhere between size measurement and code posting.

I assume there are some languages with an advantage for the new challenge above. If you have a CAS-equipped calculator, the factorization is as simple as a function call, on most other languages you'll have to implement it yourself. Though such a discrimination is hard to avoid.
Luckily the 50G has a CAS, so I already have a 57-byte SysRPL solution. ;D The other languages I used will be a bit harder.
Title: Re: Code Golf Contest #2
Post by: ben_g on July 28, 2014, 03:05:13 pm
Looking over the solutions, I noticed that the shortest Java version does not process one string, but instead assumes someone has split it already
That's because java automatically splits the input, as long as the input string is not surrounded by quotes.

By the way, I had a smaller version, but I have been quite busy with other projects and I forgot to submit that one. Anyway, here was that code (169 bytes):
Code: [Select]
class C{public static void main(String[] a){String s="";int i=0;for(String t:a){try{i+=Integer.parseInt(t);}catch(Exception e){s=t+" "+s;}}s+=i;System.out.println(s);}}
Title: Re: Code Golf Contest #2
Post by: 3298 on July 28, 2014, 03:21:23 pm
Okay, challenge accepted, I'm going to cut that one down.
Code: [Select]
String[] aDrop the space, 1 byte saved.
Code: [Select]
for(String t:a){...}Drop the curly braces, 2 more bytes down (I just saw that in my submission as well). Your other solution already has that.
Code: [Select]
s+=i;System.out.println(s);Two things here: Drop the ln (like in your submission), 2 bytes saved. The concatenation of string and number can be done inside the parentheses of the print, saving 3 more bytes.
Total improvement: 8 bytes, so the size is 161 now. :thumbsup:
Title: Re: Code Golf Contest #2
Post by: JWinslow23 on July 28, 2014, 03:31:27 pm
New contest is underway!

Please move any and all discussion on code golfing here (http://www.omnimaga.org/other-calculator-discussion-and-news/code-golf-contest-3/).

I already have a solution in TI-BASIC that is 122 bytes large. Who can do better?
Title: Re: Code Golf Contest #2
Post by: Juju on July 29, 2014, 02:13:06 am
Welp, I just learned I could cut out 4 bytes in my Ruby entry by shortening "print" with "p" o.o

Welp, now I know.