Calculator Community > Community Contests

[ENDED] Code Golf Contest #7

(1/8) > >>

JWinslow23:
This must be your lucky day!

NEXT: Here
PREVIOUS: Here

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
RankUserSizeDateCode132981898/29/2014 12:16:14 PMSpoiler 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 PMSpoiler 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
RankUserSizeDateCode1JWinslow23558/25/2014 7:22:08 PMSpoiler For Spoiler: 0:a;{.96>32 0if-}/]"CODE"{1$?.a+:a\@>}/;]..$=*.{n*}0if
Nspire Lua
RankUserSizeDateCode1Jens_K828/30/2014 6:52:41 AMSpoiler 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
end2Adriweb1048/26/2014 9:43:18 AMSpoiler 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
RankUserSizeDateCode1JWinslow23508/26/2014 10:54:23 AMSpoiler 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
RankUserSizeDateCode1JWinslow231528/28/2014 9:43:23 AMSpoiler 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
RankUserSizeDateCode132981638/29/2014 12:16:14 PMSpoiler 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
RankUserSizeDateCode132981748/29/2014 12:16:14 PMSpoiler 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
RankUserSizeDateCode132981048/29/2014 3:04:40 PMSpoiler 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
RankUserSizeDateCode132981698/30/2014 9:50:11 AMSpoiler 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 PMSpoiler 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
RankUserSizeDateCode1willrandship998/30/2014 3:07:52 AMSpoiler 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 PMSpoiler 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
RankUserSizeDateCode1alberthrocks1818/29/2014 4:09:31 PMSpoiler 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
RankUserSizeDateCode1Juju778/29/2014 8:20:46 PMSpoiler For Spoiler: p 0if !gets.match(/.*?(c).*?(o).*?(d).*?(e).*/i){|a|4.times{|b|p a.end(b+1)}}

Perl
RankUserSizeDateCode1willrandship968/30/2014 3:07:52 AMSpoiler 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
RankLangUserSizeDate1TI-83+ BASICJWinslow23508/26/2014 10:54:23 AM2GolfscriptJWinslow23558/25/2014 7:22:08 PM3Ruby2Juju778/29/2014 8:20:46 PM4Nspire LuaJens_K828/30/2014 6:52:41 AM5Perlwillrandship968/30/2014 3:07:52 AM6Python3willrandship998/30/2014 3:07:52 AM7SysRPL32981048/29/2014 3:04:40 PM8AxeJWinslow231528/28/2014 9:43:23 AM9XTend32981638/29/2014 12:16:14 PM10C32981698/30/2014 9:50:11 AM11Haskell32981748/29/2014 12:16:14 PM12Bashalberthrocks1818/29/2014 4:09:31 PM13Java32981898/29/2014 12:16:14 PM[/list]

pimathbrainiac:
268 bytes of Java! YAYGIMMIEGIMMIE!

JWinslow23:
Nice for a first submission, pimath. :)

LDStudios:
162 NSpire Lua, but still working on it

Adriweb:
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.)

Navigation

[0] Message Index

[#] Next page

Go to full version