Calculator Community > Community Contests

[ENDED] Code Golf Contest #11

(1/5) > >>

JWinslow23:
#MakeEleventyAThing :P

NEXT: Here
PREVIOUS: Here

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:

* Take the number written out in English and count the number of letters (i.e. 21 is "twenty-one" is 9 letters, 102 is "one hundred two" is 13 letters, 9999 is "nine thousand nine hundred ninety-nine" is 33 letters).
* If the result is 4, output "4 is magic." and exit.
* Else, output "(original number) is (result)." and repeat step 1 with the result.Example:
--- Code: ---31 is 9.
9 is 4.
4 is magic.
--- End code ---

-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
RankUserSizeDateCode1Juju548-70%=164.49/23/2014 6:25:12 PMSpoiler 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
RankUserSizeDateCode1JWinslow23360-70%=10810/4/2014 9:37:27 AMSpoiler 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
RankUserSizeDateCode1Ivoah962-70%=288.69/26/2014 6:32:23 PMSpoiler 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
RankUserSizeDateCode13298611-70%=183.310/4/2014 2:54:53 PMSpoiler 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 PMSpoiler 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
RankUserSizeDateCode13298630-70%=18910/4/2014 2:54:53 PMSpoiler 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 AMSpoiler 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
RankUserSizeDateCode13298493.5-70%=148.0510/4/2014 2:54:53 PMSpoiler 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
      OVERINDEX@ SUB$1#
      BINT64 #> IT #1+
    LOOP
    UNROT "is " &$SWAP
    BINT4 #=case
    ::
      RDROP SWAPDROP ABND
      "magic" &$
    ;
    OVER #>$ &$SWAP
  AGAIN
;
Language Ranking
RankLangUserSizeDate1GolfscriptJWinslow2310810/4/2014 9:37:27 AM2SysRPL3298148.0510/4/2014 2:54:53 PM3RubyJuju164.49/23/2014 6:25:12 PM4Java3298183.310/4/2014 2:54:53 PM5C329818910/4/2014 2:54:53 PM6PythonIvoah288.69/26/2014 6:32:23 PM

Juju:
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)

JWinslow23:

--- Quote from: 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)

--- End quote ---
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.

Juju:
Thanks, that answers my questions, I'll send you my 558-byte solution now.

JWinslow23:
It works, and well at that! :thumbsup:

Navigation

[0] Message Index

[#] Next page

Go to full version