Omnimaga

Calculator Community => Contests => Community Contests => Topic started by: JWinslow23 on August 25, 2014, 04:52:22 pm

Title: [ENDED] Code Golf Contest #7
Post by: JWinslow23 on August 25, 2014, 04:52:22 pm
This must be your lucky day!

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

Challenge 7

Problem
Make a program that, given an input as a string, outputs the (newline-separated) indexes of the string where the letters that make up the word "code" appear (one of each in order). For example, for input such as:

School is about to start! Be sure to do your homework!

the letters that make up the word "code" are marked by asterisks here:

School is about to start! Be sure to do your homework!
 ^ ^                                 ^          ^


(in that exact order) (search should be case-insensitive if possible)
And the output would be:

2
4
38
49


(the positions of the characters in the string).
Simply output ONLY 0 if such is impossible.

Deadline
September 1, 2014, 1:00 AM EST

If any further clarification is needed, contact me. I will try to search for order. (honestly, nobody comments about the puns anymore :P )

Java
RankUserSizeDateCode
132981898/29/2014 12:16:14 PM
Spoiler For Spoiler:
class G{public static void main(String[]s){String o="";int p=0;for(char c:"code".toCharArray()){p=s[0].toLowerCase().indexOf(c,p);if(p<0){o="0";break;}o=o+(p+1)+"\n";}System.out.print(o);}}
2pimathbrainiac2688/25/2014 6:57:57 PM
Spoiler For Spoiler:
import java.util.Arrays;public class C7{public static void main(String[] args){String s=Arrays.toString(args);int a,b,c,d;a=s.indexOf('c')-1;b=s.indexOf('o')-1;if(b>a){c=s.indexOf('d')-1;if(c>b){d=s.indexOf('e')-1;if(d>c){System.out.print(a+"\n"+b+"\n"+c+"\n"+d);}}}}}

Golfscript
RankUserSizeDateCode
1JWinslow23558/25/2014 7:22:08 PM
Spoiler For Spoiler:
0:a;{.96>32 0if-}/]"CODE"{1$?.a+:a\@>}/;]..$=*.{n*}0if

Nspire Lua
RankUserSizeDateCode
1Jens_K828/30/2014 6:52:41 AM
Spoiler For Spoiler:
function cgc7(s)
i=0
repeat
p=s:find(("c.*o.*d.*e"):sub(i*3+1),p)or 0
print(p)i=i+1
until p*(4-i)<1
end
2Adriweb1048/26/2014 9:43:18 AM
Spoiler For Spoiler:
function y(s)t={}for a=1,4 do t[#t+1]=s:find(("code"):sub(a,a),t[#t])end print(#t<4 and 0 or table.concat(t,"\n"))end end

TI-83+ BASIC
RankUserSizeDateCode
1JWinslow23508/26/2014 10:54:23 AM
Spoiler For Spoiler:
Input Str1
1
For(X,1,4
inString(Str1,sub("CODE",X,1),Ans
If not(Ans
ClrHome
Disp Ans
If not(Ans
Return
End

Axe
RankUserSizeDateCode
1JWinslow231528/28/2014 9:43:23 AM
Spoiler For Spoiler:
"CODE"->Str1
input->M
0->A
For(X,0,3)
If inData({X+Str1},A+M)
+A
End
!If ->A
4->X
ClrHome
End
Disp A>Dec,i
End

XTend
RankUserSizeDateCode
132981638/29/2014 12:16:14 PM
Spoiler For Spoiler:
class G{def static void main(String[]s){var o=""var p=0;for(c:"code".toCharArray){p=s.head.toLowerCase.indexOf(c,p)if(p<0){print(0)return}o=o+(p+1)+"\n"}print(o)}}

Haskell
RankUserSizeDateCode
132981748/29/2014 12:16:14 PM
Spoiler For Spoiler:
import Data.Char
g i=putStr o where(_,o,_)=foldl h(i,"",0)"code"
h(s,r,n)c=if t/=""then(tail t,r++(show m)++"\n",m)else(t,"0",m)where(d,t)=span((/=c).toLower)s;m=n+1+length d

SysRPL
RankUserSizeDateCode
132981048/29/2014 3:04:40 PM
Spoiler For Spoiler:
::
  NULL$SWAP ONESWAP "code"
  BEGIN
    2DUP CAR$
    2DUP 7PICK POSCHR UNROT
    CHR># BINT32 #- #>CHR 6ROLL POSCHR
    2DUP 2#0=OR ITE #MAX #MIN
    DUP#0=csedrp :: RDROP 3DROP tok0 ;
    DUP4UNROLL #>$ NEWLINE&$
    5ROLL SWAP&$ 4UNROLL CDR$
  DUPNULL$? UNTIL 3DROP
;

C
RankUserSizeDateCode
132981698/30/2014 9:50:11 AM
Spoiler For Spoiler:
#include<stdio.h>
main(int m,char**i){++i;int o[]={0,0,0,0},c=0,n=1;for(;**i&&c<4;++n)if("code"[c]==(*(*i)++|32))o[c++]=n;for(c=o[3]!=0?0:3;c<4;++c)printf("%i\n",o[c]);}
2alberthrocks1888/29/2014 4:09:31 PM
Spoiler For Spoiler:
#define f for(i=0;i<4;i++){
main(){char *s,*p,c[5]="code",r[4],i=0,x=0;scanf("%m[^\n]s",&s);f p=strchr(s+x,c);if(!p){printf("0\n");exit(0);}x=p-s;r=x+1;}i=0;f printf("%i\n",r);}}


Python3
RankUserSizeDateCode
1willrandship998/30/2014 3:07:52 AM
Spoiler For Spoiler:
b=0;e='';f=0
for c in input():
 b+=1;
 if'code}'[f]==c:e+='\n'+str(b);f+=1
if len(e)<8:e=0
print(e)
2alberthrocks1168/29/2014 4:09:31 PM
Spoiler For Spoiler:
import sys;s=input().lower();i=0;a=""
for l in "code":i=s.find(l,i);i<0 and sys.exit('0');a+=str(i+1)+"\n"
print(a)


Bash
RankUserSizeDateCode
1alberthrocks1818/29/2014 4:09:31 PM
Spoiler For Spoiler:
x(){ echo "0";exit 1; };s="code";r[0]=0;read i;for n in {0..3};do p=${r[-1]};r[$n]=`expr index "${i:${r[-1]}}" "${s:$n:1}"`||x;r[$n]=`expr ${r[-1]} + $p`;done;printf '%s\n' ${r
  • }[/l][/l][/l][/l]


Ruby2
RankUserSizeDateCode
1Juju778/29/2014 8:20:46 PM
Spoiler For Spoiler:
p 0if !gets.match(/.*?(c).*?(o).*?(d).*?(e).*/i){|a|4.times{|b|p a.end(b+1)}}


Perl
RankUserSizeDateCode
1willrandship968/30/2014 3:07:52 AM
Spoiler For Spoiler:
for(split//,<>){$b++;$d=substr"code",$f,1;if($d eq$_){$e.=$b."\n";$f++}}$_=length$e<8?0:$e;print


Language Ranking
RankLangUserSizeDate
1TI-83+ BASICJWinslow23508/26/2014 10:54:23 AM
2GolfscriptJWinslow23558/25/2014 7:22:08 PM
3Ruby2Juju778/29/2014 8:20:46 PM
4Nspire LuaJens_K828/30/2014 6:52:41 AM
5Perlwillrandship968/30/2014 3:07:52 AM
6Python3willrandship998/30/2014 3:07:52 AM
7SysRPL32981048/29/2014 3:04:40 PM
8AxeJWinslow231528/28/2014 9:43:23 AM
9XTend32981638/29/2014 12:16:14 PM
10C32981698/30/2014 9:50:11 AM
11Haskell32981748/29/2014 12:16:14 PM
12Bashalberthrocks1818/29/2014 4:09:31 PM
13Java32981898/29/2014 12:16:14 PM
[/list]
Title: Re: Code Golf Contest #7
Post by: pimathbrainiac on August 25, 2014, 07:56:25 pm
268 bytes of Java! YAYGIMMIEGIMMIE!
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 25, 2014, 08:26:02 pm
Nice for a first submission, pimath. :)
Title: Re: Code Golf Contest #7
Post by: LDStudios on August 26, 2014, 07:04:02 am
162 NSpire Lua, but still working on it
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 26, 2014, 08:19:54 am
Got a 101-bytes one in Lua.

Also, that's assuming you want to have displayed only 0 if it's not possible, and nothing else printed during the process.
(Because if other number can appear in the loop as well as 0, well, I got a shorter one.)
Title: Re: Code Golf Contest #7
Post by: Matrefeytontias on August 26, 2014, 08:37:11 am
44 bytes in z80 ASM.

EDIT : 42 bytes

EDIT 2 : 41 bytes

EDIT 3 : 38 bytes (unstoppable me)

EDIT 4 : 37 bytes

EDIT 5 : 36 bytes
Title: Re: Code Golf Contest #7
Post by: Juju on August 26, 2014, 10:24:24 am
47 43 42 bytes of Ruby here.
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 26, 2014, 10:28:55 am
Got a 101-bytes one in Lua.

Also, that's assuming you want to have displayed only 0 if it's not possible, and nothing else printed during the process.
(Because if other number can appear in the loop as well as 0, well, I got a shorter one.)
Only 0.
And WOW, some of these submissions are even smaller than my Golfscript solution! :P First time ever that a lang other than CJam has beaten Golfscript.
Title: Re: Code Golf Contest #7
Post by: Juju on August 26, 2014, 10:31:08 am
Oh wait, my solution throws an error instead of printing 0. Whatever, that just adds 8 bytes.
Title: Re: Code Golf Contest #7
Post by: Matrefeytontias on August 26, 2014, 10:38:05 am
What do you mean by printing 0 actually ? I didn't get that part.
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 26, 2014, 10:43:28 am
If "c" "o" "d" "e" can't be found (in that order) in the string, then print 0 and nothing else.

Anyway, for my Lua solution, wrapped in a function, it's 121 bytes, then.
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 26, 2014, 11:52:49 am
I got 50 bytes in TI-BASIC. I worked WAY too hard on it. O.O
Title: Re: Code Golf Contest #7
Post by: Matrefeytontias on August 26, 2014, 12:55:01 pm
Oh. Well I'll have to do it again then.
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 27, 2014, 08:44:03 pm
Lately, I've thought o fmakinga Lua "preprocessor" thingy/language that would feature much shorter things but in the end it would be run as Lua.
It might be called GolfLua, who knows (any idea ? :P)
Title: Re: Code Golf Contest #7
Post by: LDStudios on August 28, 2014, 07:58:43 am
Lately, I've thought o fmakinga Lua "preprocessor" thingy/language that would feature much shorter things but in the end it would be run as Lua.
It might be called GolfLua, who knows (any idea ? :P)

Set up as a library like BetterLuaAPI but designed for golf? That would be really cool, I might do something similar.
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 28, 2014, 08:05:52 am
Hum, no, I was rather thinking like being able to do :
Code: [Select]
if a>b c=3;a=b>3?4:5 etc. which would then translate into lua :
Code: [Select]
if a>b then c=3 end a=b>3 and 4 or 5But meh, I don't really have much time right now :p
Title: Re: Code Golf Contest #7
Post by: LDStudios on August 28, 2014, 08:36:08 am
Hum, no, I was rather thinking like being able to do :
Code: [Select]
if a>b c=3;a=b>3?4:5 etc. which would then translate into lua :
Code: [Select]
if a>b then c=3 end a=b>3 and 4 or 5But meh, I don't really have much time right now :p

Ah, that would definitely be useful. Let me know if you make any progress on it.
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 28, 2014, 10:20:45 am
And I will allow it as a language as soon as it is completed. I mean, I accept AND use Axe, don't I?

Speaking of which, matref, when are you going to submit your entry? You must submit it before the month of September!
Title: Re: Code Golf Contest #7
Post by: 3298 on August 29, 2014, 01:00:15 pm
Is it OK if I'm getting 2, 36, 38 and 49 on the example string from the first post instead of 2, 4, 38 and 49? Both 4 and 36 are indices of o's between the c and d. If yes, I have a valid 96.5-byte SysRPL solution (74 bytes if case-sensitive).
SysRPL has no case-insensitive string search stuff except for some commands dealing with the edit-line. So I need to search for the uppercase and lowercase character and take the one that's closer to the string end I started at. Due to how "not found" is reported (0 is returned in place of the 1-based index), it's easier to start from the end instead of the begin (max of the two numbers vs. if one of the two is zero, then max else min).
I also have a shorter Java program (189 bytes) and XTend and Haskell programs (163 and 174 bytes).
Title: Re: Code Golf Contest #7
Post by: alberthrocks on August 29, 2014, 01:15:44 pm
poof
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 29, 2014, 02:27:05 pm
Woo! Got a 4 byte C entry! :D
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 29, 2014, 09:34:03 pm
I could imagine some trolly rules bending, but in a few more chars than 4 ... so IDK yet :P
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 29, 2014, 09:37:12 pm
I could imagine some trolly rules bending, but in a few more chars than 4 ... so IDK yet :P
I have something as well...
Title: Re: Code Golf Contest #7
Post by: Juju on August 29, 2014, 10:39:07 pm
...how? I'm pretty sure it's something like "a();" and there's another files that does the job as well as preprocessor instructions.
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 29, 2014, 10:54:52 pm
...how? I'm pretty sure it's something like "a();" and there's another files that does the job as well as preprocessor instructions.
In batch. A different trick.
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 30, 2014, 09:34:16 am
something like naming the file with a weird name such that it's actually code and the few chars in the file just call it ? :D

If so, you should make all future challenges comply to the no standards loopholes rules (http://meta.codegolf.stackexchange.com/questions/1061/standard-loopholes-which-are-no-longer-funny) ;)
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 30, 2014, 10:53:46 am
something like naming the file with a weird name such that it's actually code and the few chars in the file just call it ? :D

If so, you should make all future challenges comply to the no standards loopholes rules (http://meta.codegolf.stackexchange.com/questions/1061/standard-loopholes-which-are-no-longer-funny) ;)
Dude, it's not really an entry. It isn't even counted. I will not accept anything like that ever.
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 30, 2014, 11:26:06 am
Oh I know, but that's for the future challenges. You may want to include the link in the rules, it can't hurt.
(but a lot of them can't apply for calc languages anyway)
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on August 30, 2014, 11:49:49 am
Oh I know, but that's for the future challenges. You may want to include the link in the rules, it can't hurt.
(but a lot of them can't apply for calc languages anyway)
People here have had enough common sense not to even submit those kinds of challenges.
Title: Re: Code Golf Contest #7
Post by: Adriweb on August 30, 2014, 09:13:48 pm
which is why we are all wondering what's the magic/crazy-thing/rule-bending behind the 3-4 bytes entry ;)
But I guess we can wait for another day until this challenge is over.
Title: Re: Code Golf Contest #7
Post by: LDStudios on August 30, 2014, 09:35:50 pm
Even if a command already existed to do this, you can't even write "code" in three characters.
Title: Re: Code Golf Contest #7
Post by: willrandship on August 30, 2014, 10:41:58 pm
Well, if you had a language that had sub-byte commands, like SysRPL, you could implement something that would, at the very least, loop a bit.
Title: Re: Code Golf Contest #7
Post by: Juju on August 30, 2014, 11:44:05 pm
Okay, I think I know how. Here's my guess.

Your file consists of "a();" and you compile it with gcc cheat.c -o cheat -D'a()=insert whole code here'.
Title: Re: Code Golf Contest #7
Post by: alberthrocks on September 01, 2014, 12:20:49 pm
Okay, I think I know how. Here's my guess.

Your file consists of "a();" and you compile it with gcc cheat.c -o cheat -D'a()=insert whole code here'.
Correct! Almost!

This is what it looks like:
Code: (test-noincl-crazy.c) [Select]
a
b

Code: (Makefile.crazy) [Select]
all:
        gcc test-noincl-crazy.c \
           -include "stdio.h" \
           -D'f=for(i=0;i<4;i++){' \
           -D'a=char s[255],*p,r[4],i=0,x=0,c[]="code";' \
           -D'b=void main(){fgets(s,255,stdin);f p=strchr(s+x,c[i]);if(!p){printf("0\n");return;}x=p-s;r[i]=x+1;}f printf("%i\n",r[i]);}}' \
           -o test

To build: Place the two files in the same directory, and run make -f Makefile.crazy. Requires GCC and make.

Again, this was NOT a valid entry, and was just submitted for fun and games! :)
But if there's any lesson from this, it's that C macros are very, very powerful! :D
Title: Re: Code Golf Contest #7
Post by: JWinslow23 on September 01, 2014, 11:40:37 pm
There it is! :D

Oh, and I actually was able to cut 3 bytes from my GS solution...yet TI-BASIC was stil victorious.
Title: Re: Code Golf Contest #7
Post by: DJ Omnimaga on September 01, 2014, 11:46:46 pm
Quote
[11:31:08 PM]   JWinslow23   Yes...1984.
[11:34:35 PM]   JWinslow23   Welp, got a TI-BASIC solution to 177 bytes.

O.O


EDIT: Wait nvm the first part was about another thing on IRC. I just saw your post now. I thought you went from a 1984 bytes solution to 177 lol XD
Title: Re: Code Golf Contest #7
Post by: lirtosiast on June 10, 2015, 11:04:23 am
Sorry for the necro.

Improving slightly (50->47) on JWinslow23's excellent TI-BASIC answer:

Input Str1
1
For(X,1,4
inString(Str1,sub("CODE",X,1),Ans
If not(Ans
ClrHome
Disp Ans
If Ans
End