Omnimaga

Calculator Community => Contests => Community Contests => Topic started by: JWinslow23 on September 22, 2014, 08:52:42 pm

Title: [ENDED] Code Golf Contest #11
Post by: JWinslow23 on September 22, 2014, 08:52:42 pm
#MakeEleventyAThing :P

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

Challenge 11

Problem

Create a program that, given an integer 0 through 999,999, does the "4 is magic" transformation. If you don't know what that is, this is it:
Example:
Code: [Select]
31 is 9.
9 is 4.
4 is magic.

-70% bonus if you actually display the number-words instead of the numbers.

Deadline
October 5, 2014, 1:00 AM EST

If any further clarification is needed, contact me. I'll try to make your troubles disappear. :P

Ruby
RankUserSizeDateCode
1Juju548-70%=164.49/23/2014 6:25:12 PM
Spoiler For Spoiler:
def w(n)
case n
when 0..19
["zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"][n]
when 20..99
["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"][n/10-2]+(n%10==0?"":" "+w(n%10))
when 100..999
w(n/100)+" hundred"+(n%100==0?"":" "+w(n%100))
when 1000..999999
w(n/1000)+" thousand"+(n%1000==0?"":" "+w(n%1000))
end
end
a=gets.to_i
while a!=4
p w(a)+" is "+w(a=w(a).delete(" ").length)+"."
end
p"four is magic."

Golfscript
RankUserSizeDateCode
1JWinslow23360-70%=10810/4/2014 9:37:27 AM
Spoiler For Spoiler:
~{.20<{.12>["zero""one""two""three""four""five""six""seven""eight""nine""ten""eleven""twelve""thir""four""fif""six""seven""eigh""nine"]@="teen"@*+}{.100<{.10/2-["twen""thir""for""fif""six""seven""eigh""nine"]{"ty"+}%\=\10%a" "\++}{.1000<{.100/a" hundred "+\100%a+}{.1000/a" thousand "+\1000%a+}if}if}if" zero"%""+}:a;{.4=}{a." "/""+," is "\.n\}until" is magic"

Python
RankUserSizeDateCode
1Ivoah962-70%=288.69/26/2014 6:32:23 PM
Spoiler For Spoiler:
import sys
a = {1:' one',2:' two',3:' three',4:' four',5:' five',6:' six',7:' seven',8:' eight',9:' nine',10:' ten',11:' eleven',12:' twelve',13:' thirteen',14:' fourteen',15:' fifteen',16:' sixteen',17:' seventeen',18:' eighteen',19:' nineteen',20:' twenty',30:' thirty',40:' forty',50:' fifty',60:' sixty',70:' seventy',80:' eighty',90:' ninety',100:' hundred',1000:' thousand'}
def b(n):
 if n<=20:
  if n==0:return 'zero'
  else:return a[n].strip()
 else:
  c=''
  for d,e in enumerate(str(n)):
   i=10**(len(str(n))-d-1)
   j=int(e)
   if i==1e5:c+=a[j]+a[100]
   elif i==1e4:c+=a[j*10]
   elif i==1e3:
    if c[-3:]=='ten'and j!=0:c=c[:-4]+a[j+10]
    else:c+=a[j]
    c+=a[1e3]
   elif i==100:c+=a[j]+a[100]
   elif i==10:c+=a[j*10]
   elif i==1:
    if c[-3:]=='ten'and j!=0:c=c[:-4]+a[j+10]
    else:c+=a[j]
  c=c.strip()
  return c
n=sys.argv[1]
while True:
 f=len(''.join(b(n).split()))
 print b(n),'is',b(f)
 n=f
 if f==4:print b(4),'is magic!';break

Java
RankUserSizeDateCode
13298611-70%=183.310/4/2014 2:54:53 PM
Spoiler For Spoiler:
class G{static String[]x={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"},y={"twen","thir","four","fif","six","seven","eigh","nine"};static String t(int n){String s="";if(n>99){s=x[n/100]+" hundred ";n%=100;if(n==0)return s;}return s+(n<13?x[n]+" ":n<20?y[n-12]+"teen ":y[n/10-2]+"ty "+(n%10==0?"":x[n%10]+" "));}public static void main(String[]i){for(int n=Integer.parseInt(i[0]);{String s=n>999?t(n/1000)+"thousand "+(n%1000==0?"":t(n%1000)):t(n);int m=0;for(char c:s.toCharArray())if(c>64)++m;System.out.println(s+"is "+(n==4?"magic":m));if(n==4)break;n=m;}}}
2ben_g676-70%=202.89/28/2014 2:52:53 PM
Spoiler For Spoiler:
class e{public static void main(String[] a){int i=Integer.parseInt(a[0]);while(i!=4){a[0]=b(i).trim();if(a[0].length()==0)a[0]="zero";System.out.println(a[0]+" is "+(i=a[0].replace(" ","").length()));}System.out.println("four is magic");}static String b(int i){String[]n={"","one","two","three","four","five","six","seven","eight","nine","then","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"},m={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety","hundred"};if(i<20)return n;if(i<100)return m[i/10]+" "+n[i%10];if(i<1000)return b(i/100)+" hundred "+b(i%100);return b(i/1000)+" thousand "+b(i%1000);}}

C
RankUserSizeDateCode
13298630-70%=18910/4/2014 2:54:53 PM
Spoiler For Spoiler:
#include<stdio.h>
char*x[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"},*y[]={"twen","thir","four","fif","six","seven","eigh","nine"};int l;p(char*s){printf("%s",s);l+=strlen(s);}t(int n){if(n>99){p(x[n/100]);p(" hundred");--l;if(!(n%=100))return;p(" ");--l;}if(n<13)p(x[n]);else if(n<20){p(y[n-12]);p("teen");}else{p(y[n/10-2]);p("ty");if(n%=10){p(" ");--l;p(x[n]);}}}main(int c,char**v){int n=0;while(*(v[1]))n=10*n+*(v[1]++)-48;r:l=0;if(n>999){t(n/1000);p(" thousand");--l;if(n%1000){p(" ");--l;t(n%1000);}}else t(n);if(n-4){n=l;printf(" is %i\n",n);goto r;}p(" is magic\n");}
2ben_g861-70%=258.310/3/2014 3:52:36 AM
Spoiler For Spoiler:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char *m[]={"","one","two","three","four","five","six","seven","eight","nine","then","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};static const char *n[]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety","hundred"};const char * w(int l, char* u){if(l==0){sprintf(u,"zero");return;}if(l<20){sprintf(u,"%s",m[l]);return;}if(l<100){sprintf(u,"%s %s",n[l/10],m[l%10]);return;}if(l<1000) {sprintf(u,"%s hundred ",m[l/100]);w(l%100,u+strlen(u));return;}w(l/1000,u);sprintf(u+strlen(u)," thousand ");w(l%1000,u+strlen(u));return;}int main(int argc,char *argv[]){int a=atoi(argv[1]);while(a!=4){char b[70]={0};int i=0;w(a,b);a=0;while(b!='\0'){if(b!=' ')a++;i++;}printf("%s is %i,\n",b,a);}printf("4 is magic.\n");}

SysRPL
RankUserSizeDateCode
13298493.5-70%=148.0510/4/2014 2:54:53 PM
Spoiler For Spoiler:
::
  { "twen" "thir" "four" "fif" "six" "seven" "eigh" "nine" }
  ' ::
    BINT100 #/ DUP#0=ITE DROPNULL$
    ::
      1GETLAMSWAP_ NTHCOMPDROP
      " hundred " &$
    ;
    SWAP DUP#0=csDROP
    BINT13 OVER#> case
    ::
      1GETLAMSWAP_ NTHCOMPDROP
      APPEND_SPACE &$
    ;
    BINT20 OVER#> case
    ::
      BINT11 #-
      3GETLAM SWAP NTHCOMPDROP
      "teen " &$ &$
    ;
    BINT10 #/ 3GETLAM SWAP#1-
    NTHCOMPDROP "ty " &$SWAP
    DUP#0=csedrp &$
    1GETLAMSWAP_ NTHCOMPDROP
    APPEND_SPACE &$ &$
  ;
  { "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" }
  3NULLLAM{}_ BIND
  COERCE BEGIN
    DUP#0=ITE "zero "
    ::
      DUP # 3E8 #/
      DUP#0=csedrp 2GETEVAL
      2GETEVAL "thousand " &$SWAP
      2GETEVAL &$
    ;
    BINT0 OVERLEN$ #1+_ONE_DO
      [email protected] SUB$1#
      BINT64 #> IT #1+
    LOOP
    UNROT "is " &$SWAP
    BINT4 #=case
    ::
      RDROP SWAPDROP ABND
      "magic" &$
    ;
    OVER #>$ &$SWAP
  AGAIN
;

Language Ranking
RankLangUserSizeDate
1GolfscriptJWinslow2310810/4/2014 9:37:27 AM
2SysRPL3298148.0510/4/2014 2:54:53 PM
3RubyJuju164.49/23/2014 6:25:12 PM
4Java3298183.310/4/2014 2:54:53 PM
5C329818910/4/2014 2:54:53 PM
6PythonIvoah288.69/26/2014 6:32:23 PM
Title: Re: Code Golf Contest #11
Post by: Juju on September 22, 2014, 10:14:50 pm
Yay, got it to 562-70%=168.6 characters in Ruby.

EDIT: 559-70%=167.7 now. Also I have a few questions:
- Do I have to print "4 is 4" with an input of 4? (No would save a few bytes)
- "Ninety-nine" or "ninety nine"? Does it matter? (No would save one byte)
- Capital letter at the beginning? (No would save a few bytes)
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on September 23, 2014, 05:24:53 pm
Yay, got it to 562-70%=168.6 characters in Ruby.

EDIT: 559-70%=167.7 now. Also I have a few questions:
- Do I have to print "4 is 4" with an input of 4? (No would save a few bytes)
- "Ninety-nine" or "ninety nine"? Does it matter? (No would save one byte)
- Capital letter at the beginning? (No would save a few bytes)
1. No, input of 4 means instant input of "4 is magic".
2. It shouldn't matter, but that's how I write numbers, usually. Do it if you want.
3. Whatever you want, but it's common courtesy to.
Title: Re: Code Golf Contest #11
Post by: Juju on September 23, 2014, 07:03:16 pm
Thanks, that answers my questions, I'll send you my 558-byte solution now.
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on September 23, 2014, 09:12:59 pm
It works, and well at that! :thumbsup:
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on September 25, 2014, 07:19:49 pm
Bump.

Woo, score of 111 in GS! :D
Title: Re: Code Golf Contest #11
Post by: Ivoah on September 26, 2014, 09:17:31 am
I made it in python, how do I enter it? p.s. this is my first time doing code golf
Title: Re: Code Golf Contest #11
Post by: Juju on September 26, 2014, 09:59:55 am
You PM your code to JWinslow23.
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on September 26, 2014, 05:16:43 pm
You PM your code to JWinslow23.
What he said ^^
Title: Re: Code Golf Contest #11
Post by: Ivoah on September 26, 2014, 07:49:32 pm
Message sent!
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on September 26, 2014, 09:50:23 pm
Message received!
Title: Re: Code Golf Contest #11
Post by: DJ Omnimaga on September 28, 2014, 04:33:09 pm
I would like to congratulate you for keeping a contest alive for this long (11 contests in such short period of time even though you are not a staff member). I know on IRC you said there weren't many entries for this one but the thing is that normally mini-contests die after 3 or 4 of them because the novelty wears off after a while (and in this case, school started). In fact, some recent contests like the revival of cage matches and the music contest never actually got any entry, if they even started at all. So good job. Some future suggestions to perhaps bring people back if they stopped participating:

-Put the contest theme inside the title (for example: Code Golf Contest #12: ThemeName)
-Perhaps do a small change of direction in terms of theme. Keep the same idea but explore themes that were not explored before
-Make the deadline 2 weeks for each contest if the regulars look busy
-Make sure that the rules are well planned for each contest (perhaps have somebody review them before the contest starts?) so that there are as few rule edits in the middle of the contest as possible and less loopholes. For example, for the olympic ring contest, the easiest way to win was to use HP PPL language.
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on September 28, 2014, 07:18:56 pm
I would like to congratulate you for keeping a contest alive for this long (11 contests in such short period of time even though you are not a staff member). I know on IRC you said there weren't many entries for this one but the thing is that normally mini-contests die after 3 or 4 of them because the novelty wears off after a while (and in this case, school started). In fact, some recent contests like the revival of cage matches and the music contest never actually got any entry, if they even started at all. So good job. Some future suggestions to perhaps bring people back if they stopped participating:

-Put the contest theme inside the title (for example: Code Golf Contest #12: ThemeName)
-Perhaps do a small change of direction in terms of theme. Keep the same idea but explore themes that were not explored before
-Make the deadline 2 weeks for each contest if the regulars look busy
-Make sure that the rules are well planned for each contest (perhaps have somebody review them before the contest starts?) so that there are as few rule edits in the middle of the contest as possible and less loopholes. For example, for the olympic ring contest, the easiest way to win was to use HP PPL language.
Thank you for your kind remarks. I will try to use your suggestions.

In related news, the deadline of this contest is moved to October 5, and new contests will now be on Saturdays instead of Mondays.

As for new contests, here are the ones that are most likely to happen up until #15 (if I can squeeze another month or two out of this thing) (these are only ideas, not giving away every detail yet):
#12: Befunge Numbers (make optimal expressions in RPN that evaluate to input)
#13: A Picture's Worth 140 Characters (give a way to compress and decompress images into a 140 character or less string)
#14: Getting Out What You Don't Put In (output every character your program DOESN'T have, and nothing else)
#15: History Repeats Itself (make a program that, when repeated, outputs two different specified strings)

If you have any questions about these questions, contact me. I can try to clarify most of the details for you.
Title: Re: Code Golf Contest #11
Post by: LDStudios on September 28, 2014, 08:19:21 pm
Upcoming challenges sound challenging, but really cool! Good luck, I hope this contest keeps up!
Title: Re: Code Golf Contest #11
Post by: Juju on September 28, 2014, 08:57:12 pm
I would like to congratulate you for keeping a contest alive for this long (11 contests in such short period of time even though you are not a staff member). I know on IRC you said there weren't many entries for this one but the thing is that normally mini-contests die after 3 or 4 of them because the novelty wears off after a while (and in this case, school started). In fact, some recent contests like the revival of cage matches and the music contest never actually got any entry, if they even started at all. So good job. Some future suggestions to perhaps bring people back if they stopped participating:

-Put the contest theme inside the title (for example: Code Golf Contest #12: ThemeName)
-Perhaps do a small change of direction in terms of theme. Keep the same idea but explore themes that were not explored before
-Make the deadline 2 weeks for each contest if the regulars look busy
-Make sure that the rules are well planned for each contest (perhaps have somebody review them before the contest starts?) so that there are as few rule edits in the middle of the contest as possible and less loopholes. For example, for the olympic ring contest, the easiest way to win was to use HP PPL language.
Thank you for your kind remarks. I will try to use your suggestions.

In related news, the deadline of this contest is moved to October 5, and new contests will now be on Saturdays instead of Mondays.

As for new contests, here are the ones that are most likely to happen up until #15 (if I can squeeze another month or two out of this thing) (these are only ideas, not giving away every detail yet):
#12: Befunge Numbers (make optimal expressions in RPN that evaluate to input)
#13: A Picture's Worth 140 Characters (give a way to compress and decompress images into a 140 character or less string)
#14: Getting Out What You Don't Put In (output every character your program DOESN'T have, and nothing else)
#15: History Repeats Itself (make a program that, when repeated, outputs two different specified strings)

If you have any questions about these questions, contact me. I can try to clarify most of the details for you.
Yep, what DJ said. Congrats for keeping it up for 11 iterations and getting many people to enter each time! I entered in every contest so far, and the ideas you gave for the next ones looks pretty interesting and challenging.
Title: Re: Code Golf Contest #11
Post by: ben_g on September 30, 2014, 04:38:24 pm
I decided to write a C entry mainly to learn C, but I've also submited it here.

I'd like to thank Eiyeron and KermM for helping me with this entry, because I wouldn't have gotten it to work witouth them.
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on October 03, 2014, 05:48:15 pm
Thank you. :)
Title: Re: Code Golf Contest #11
Post by: 3298 on October 04, 2014, 06:56:41 am
My output currently looks like this (for input 703450):
Code: [Select]
seven hundred three thousand four hundred fifty is 41
fourty-one is 9
nine is 4
four is magic
The aspects I'm not sure about are these:
- Do I need a period at the end? (2-6 additional bytes, depending on language, so not too bad)
- Is the correct number written out in letters, or should I mangle the other one instead, or both?
- Do I need an upper-case letter at the start of each line? (That would be a pain to do!)
- The spaces and dashes should not be counted, right?
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on October 04, 2014, 10:22:30 am
My output currently looks like this (for input 703450):
Code: [Select]
seven hundred three thousand four hundred fifty is 41
fourty-one is 9
nine is 4
four is magic
The aspects I'm not sure about are these:
- Do I need a period at the end? (2-6 additional bytes, depending on language, so not too bad)
- Is the correct number written out in letters, or should I mangle the other one instead, or both?
- Do I need an upper-case letter at the start of each line? (That would be a pain to do!)
- The spaces and dashes should not be counted, right?
1. No.
2. Do either, both, or none. I don't care, but to qualify for the -70% bonus, at least one of the numbers must be converted to a word.
3. No.
4. "forty-one", to me, is the same as "forty one". Dashes or spaces, it doesn't matter.

Be fast, though! You only have until the end of the day!
Title: Re: Code Golf Contest #11
Post by: 3298 on October 04, 2014, 03:28:39 pm
Okay then, my SysRPL, Java and C entries are done. My fourth question was actually meant in a different way ("thirty-one" is 10 chars, but all the examples look like we shall count the 9 letters only), but your answer reminded me that the dash can be replaced by a space, simplifying my programs a little. Thanks for that. :)
By the way: "forty" or "fourty"? I hope the latter is acceptable (despite my spell-checker complaining about it ... English is not my first language) because the former would break my concept of reusing the same lookup table for 13-19 and 2x-9x, and "forteen" looks just wrong.
Title: Re: Code Golf Contest #11
Post by: JWinslow23 on October 05, 2014, 01:41:30 pm
Okay then, my SysRPL, Java and C entries are done. My fourth question was actually meant in a different way ("thirty-one" is 10 chars, but all the examples look like we shall count the 9 letters only), but your answer reminded me that the dash can be replaced by a space, simplifying my programs a little. Thanks for that. :)
By the way: "forty" or "fourty"? I hope the latter is acceptable (despite my spell-checker complaining about it ... English is not my first language) because the former would break my concept of reusing the same lookup table for 13-19 and 2x-9x, and "forteen" looks just wrong.
Forty. Fourteen.

Also, WE'RE DONE! Befunge Numbers in a while.