Calculator Community > Community Contests
[ENDED] Code Golf Contest #1 (thanks for the many submissions!)
JWinslow23:
This is the first of what I hope to be many contests I will hold here on Omnimaga: Code Golf.
Code golf is a competition where you have to solve a coding challenge in the fewest bytes possible. For example, a TI-BASIC entry for a prime tester could be:
--- Code: ---Input N:0:If N and not(fPart(N:2=sum(seq(not(fPart(abs(N)/I)),I,1,abs(N:Ans
--- End code ---
(note that this is not the speediest it could be, but speed is not factored in your score, only size)
The score would be 34 bytes (for TI-BASIC programs, score=size - 9 - length of name). The lowest score out of the entries will be the winner.
How this tournament will work:
First off, you need to code an actual program that will solve the given problem (or at least give the right result for all the test cases :P ). All languages are allowed, including calc languages and computer languages. When you have an entry, PM it to me, and I will test it if possible (but just in case I don't have an Nspire or I can't download the latest version of Perl or some such problem, try if you can to give back the results of any and all given test cases). I will then save your entry and update the scores accordingly.
After one week, a winner shall be determined in each language category, as well as the smallest overall. In each language category, the winners shall all suggest possible problems for the next competition. I shall pick the next challenge out of these, and present test cases for any possible input or output. Also, you will get to see everyone else's solutions for the previous challenge.
Please, ask any and all questions that you may have about the contest!
NEXT: Here
PREVIOUS: Here
Challenge 1
Problem
Determine if an inputted number is happy. Happy numbers are defined like this: Take any positive integer, replace it with the sum of the squares of its digits, and repeat the process until it equals 1 or it loops indefinitely in a loop that does not include 1. If it ends up with 1, the number is happy, otherwise it's sad.
Deadline
July 21, 2014, 1:00 AM EST
Sample input 1:
1
Sample output 1:
--- Code: ---HAPPY
--- End code ---
Sample input 2:
1337
Sample output 2:
--- Code: ---HAPPY
--- End code ---
Sample input 3:
385
Sample output 3:
Either one of
--- Code: ---SAD
--- End code ---
or
--- Code: ---UNHAPPY
--- End code ---
Ranking
Python
RankUserSizeDateCode1willrandship947/19/2014 11:16:56 PMSpoiler For Spoiler: x=input();b="UNHAPPY"
for a in b:
z=0
for y in str(x):z+=eval(y)**2;x=z
print(b[(z==1)*2:7])2Juju1487/15/2014 4:22:50 PMSpoiler For Spoiler: def h(n):
while n>1 and n!=4:
n=sum(dict([(c,int(c)**2)for c in"0123456789"])[d] for d in str(n))
return n==1
print(("SAD","HAPPY")[h(input())])
Golfscript
RankUserSizeDateCode1Runer112327/15/2014 5:17:50 PMSpoiler For Spoiler: ~{`0\{48-.*+}/}9*("SAD""HAPPY"if
CJam
RankUserSizeDateCode1Runer112307/15/2014 5:17:50 PMSpoiler For Spoiler: q~{Ab0\{_*+}/}9*("SAD""HAPPY"?
TI-83+ BASIC
RankUserSizeDateCode1calc84maniac467/16/2014 5:03:49 PMSpoiler For Spoiler: Repeat Ans<5
sum(.5×√int(10fPart(Ans/10^(cumSum(binomcdf(98,0→A
End
"HAPPY
If log(A
"SAD
Ans2Runer112467/16/2014 5:09:28 PMSpoiler For Spoiler: Repeat A≤4
iPart(10fPart(Ans10^(~cumSum(binomcdf(14,0
sum(Ans²→A
End
"HAPPY
If log(A
"SAD
Ans3Hayleia717/16/2014 2:18:36 AMSpoiler For Spoiler: Prompt N
Repeat N=1 or N=4
sum(seq((10fPart(iPart(N10^(~I))/10))²,I,0,14→N
End
"HAPPY
If N=4
"SAD
Disp Ans
TI-83+ z80
RankUserSizeDateCode1Runer112587/20/2014 9:32:08 PMSpoiler For Spoiler: ;#SECTION "MAIN", CODE
org userMem - 2
db 0BBh, 6Dh
Start:
B_CALL _RclAns
StepLoop:
push bc
sbc hl, hl
ld b, h
DigitPairLoop:
dec e
DigitLoop:
ex de, hl
xor a
rrd
ex de, hl
ld c, a
SquareLoop:
add hl, bc
dec a
jq nz, SquareLoop
ld a, (de)
or a
jq nz, DigitLoop
ld a, e
cp (OP1 + 2) & 0FFh
jq nz, DigitPairLoop
push hl
B_CALL _SetXXXXOP2
rst 30h
pop hl
pop bc
djnz StepLoop
dec l
ld hl, UnhappyStr
jq nz, Unhappy
inc hl
inc hl
Unhappy:
B_CALL _PutS
ret
;#SECTION "StrData", DATA
UnhappyStr:
db "UNHAPPY", 02calc84maniac607/16/2014 12:12:44 PMSpoiler For Spoiler: #define bcall(xxxx) rst 28h \\ .dw xxxx
#define _RclAns $4AD7
#define _PutS $450A
#define OP1 $8478
.org $9D93
.db $BB,$6D
bcall(_RclAns)
ex de,hl
HappyCalcLoop:
xor a
ld c,a
ld d,a
ld e,a
HappyByteLoop:
;Carry is reset, upper nibble of A is 0
dec l
HappyNibbleLoop:
rrd
ld b,a
HappyMulLoop:
push af
add a,e
daa
ld e,a
ld a,c
adc a,d
daa
ld d,a
pop af
djnz HappyMulLoop
ccf
jr c,HappyNibbleLoop
ld a,l
sub (OP1+2)&$FF
jr nz,HappyByteLoop
ld (hl),d
inc l
ld (hl),e
inc l
inc (hl)
jr nz,HappyCalcLoop
ld hl,HappyString
dec e
jr z,$+4
dec hl
dec hl
bcall(_PutS)
ret
UnhappyString:
.db "UN"
HappyString:
.db "HAPPY",0
Batch
RankUserSizeDateCode1JWinslow231827/19/2014 7:26:37 PMSpoiler For Spoiler: @set/p#=
:@
@set $=0&@for /f "delims=" %%a in ('cmd /U /C echo %#%^|find /V ""')do @set/a$+=%%a*%%a
@set #=%$%&@if %$% neq 1 if %$% neq 4 goto @
@if %$%==1 (echo happy) else echo sad
Language Ranking
RankLangUserSizeDate1CJamRuner112307/15/2014 5:17:50 PM2GolfscriptRuner112327/15/2014 5:17:50 PM3TI-83+ BASICcalc84maniac467/16/2014 5:03:49 PM4TI-83+ z80Runer112587/20/2014 9:32:08 PM5Pythonwillrandship947/19/2014 11:16:56 PM6BatchJWinslow231827/19/2014 7:26:37 PM
Deep Toaster:
Can I assume that since the problem states that the input is a positive integer, that the program does not have to check for these properties?
Runer112:
Do you/others think it's better for entries to be messaged to you privately, rather than just posted here in a spoiler?
calc84maniac:
For z80 assembly, will it be judged on the size of the executable? I guess that's the only thing that makes sense...
Also, I guess we can probably take floating-point input from Ans, and work with the precision available? That is, 14 digits of precision in TI-OS floats.
One last thing, can we make it print HAPPY/UNHAPPY rather than HAPPY/SAD? This would actually be slightly more size-optimized :P
Deep Toaster:
--- Quote from: Runer112 on July 14, 2014, 09:13:06 pm ---Do you/others think it's better for entries to be messaged to you privately, rather than just posted here in a spoiler?
--- End quote ---
OP does say to PM entries to him, so I'd assume so. I think it makes sense to PM them anyway. We could share the code in the topic after the deadline, maybe
Navigation
[0] Message Index
[#] Next page
Go to full version