Challenge

In this task you have to write a program that will take input an integer N (-1e9 <= N < 0 && 0 < N <= +1e9), then compute T = (abs(N) % M+1),if N is positive then output the T-th character from the beginning else output the T-th character from the end of your source.

M is the size of your source in bytes.

Example: If your source is: abcd efg/hi

Input:

 2

Output:

 c

Input:

-3

Output:

g 

Input:

-9249678

Output:

b 

Input:

-11

Output:

i 

Constraints

  • Don't use any FILE operation
  • You can use any language of your choice
  • Try to avoid or rather don't use 1 byte submissions, since it spoils all fun.
  • Shortest solution wins!

EDIT: Problem statement have been modified so that the solutions could be judged using a random test-data (and same data for all solutions) hence please update your solution accordingly,sorry for the inconvenience (if any).

  • In the give example test case if the input is 5 or -7 the output should be a single space : " " (without quotation). – Quixotic Mar 9 '11 at 19:11
  • What if N is 0? – aaaaaaaaaaaa Mar 9 '11 at 21:16
  • @eBusiness:Thanks for pointing that out, I have changed the problem statement,I don't think $0$ can occur now :-) – Quixotic Mar 9 '11 at 23:01
  • @Debanjan: your edit makes the case where N=M+1 a little weird. – J B Mar 9 '11 at 23:04
  • 2
    It sorta keeps on being a strange mapping, now a character is skipped at the jump from 0 to 1: -2 -> / -1 -> h 0 -> i 1 -> b 2 -> c. But at least the mapping is now unanimous. – aaaaaaaaaaaa Mar 10 '11 at 12:27
up vote 12 down vote accepted

x86 assembly (32-bit Linux, AT&T syntax): 548

No newline at end of file:

pushl 8(%esp)
call atoi
mov $274,%ebx
cmp $0,%eax
jg a
dec %eax
a:cdq
idiv %ebx
cmp $0,%edx
jge p
add %ebx,%edx
p:add $s,%edx
cmp $s+273,%edx
jl l
push $34
mov %esp,%edx
l:mov $4,%eax
mov $1,%ebx
mov %edx,%ecx
mov $1,%edx
int $128
mov $0,%ebx
mov $1,%eax
int $128
s:.ascii "pushl 8(%esp)
call atoi
mov $274,%ebx
cmp $0,%eax
jg a
dec %eax
a:cdq
idiv %ebx
cmp $0,%edx
jge p
add %ebx,%edx
p:add $s,%edx
cmp $s+273,%edx
jl l
push $34
mov %esp,%edx
l:mov $4,%eax
mov $1,%ebx
mov %edx,%ecx
mov $1,%edx
int $128
mov $0,%ebx
mov $1,%eax
int $128
s:.ascii "

I compiled it with gcc -nostartfiles -m32 qc1.S -o qc1

Verification, positive numbers:

$ for i in $(seq 548 1095); do ./qc1 $i; done | cmp - qc1.S && echo Good
Good

Verification, negative numbers:

$ for i in $(seq -1095 -548); do ./qc1 $i; done | cmp - qc1.S && echo Good
Good

Edit got it right about the weird numbering scheme. I think. It didn't change the length.

  • +1,That's brilliant work to do this in assembly,but one small thing I have modified the problem to make testing unanimous so please modify your solution like-wise, Thanks. – Quixotic Mar 10 '11 at 19:02
  • Heh, pretty cool. And a funny thing, despite being so different languages this does seem to resemble my 44 characters GolfScript solution. – aaaaaaaaaaaa Mar 10 '11 at 21:09
  • 1
    @Debanjan: I really can't seem to be able to wrap my head around the numbering scheme. Could you provide the proper verification lines? (the problem statement would be a fine place) – J B Mar 10 '11 at 22:50
  • +1, that's your second "invalid" asnwer here (code-golf wise) that worths upvoting :) – Eelvex Mar 11 '11 at 14:33
  • @Eelvex: for the record, the other one was valid at the time it was posted. – J B Mar 11 '11 at 14:56

Whaddaya know, HQ9+ makes its great return!

Q

No need to bother indexing when there's only one character to choose from!

  • Does it take any input? – Quixotic Mar 9 '11 at 23:05
  • @Debanjan: sure: echo '-1' | hq9+ qc1 – J B Mar 9 '11 at 23:07
  • Sorry,this is not making much sense to me,I have one similar solution in PHP but ain't this types of solution spoils all the fun? Thanks, – Quixotic Mar 9 '11 at 23:09
  • You don't have to accept it if you don't think it fits in, you know! You don't even have to upvote it. You may even downvote it, though I'd personally not appreciate ;-) – J B Mar 9 '11 at 23:18
  • No,It's not about acceptation or rejection,I was just stating a point also I would like up-vote it since I learn something new :-) – Quixotic Mar 9 '11 at 23:20

Ruby 1.9, 66 characters

z=gets.to_i;s="z=gets.to_i;s=%p;$><<(s%%s)[z%%66]";$><<(s%s)[z%66]

Not much difference to a normal quine, actually.

  • Edit: Follows the new specs now.

GolfScript 26 characters

{':f`f'+1/\~.1<- 26%=}:f`f

Quines were more fun before the invention of dynamic languages.

Edit: For the whiners, here is a "real" GolfScript quine, no ` and ~ only used for parsing the input.

Handicapped GolfScript 44 characters

'"\x27"\+1/\~.1<- 22%='"\x27"\+1/\~.1<- 22%=

Notice how nicely it is the same string repeated twice, so the string literal just need a ' hacked in front of it and it's ready for use.

  • 1
    I don't know GolfScript, but your comment pushes me to think your code leans towards the "FILE operation" category of constraints. Care to expand its innards? – J B Mar 9 '11 at 22:32
  • The magic happens by the use of the ` operator, basically I define a function, store it in a variable, convert the function to it's own string representation (` does that), and then I run the function, which can trivially finish the task as it has it's own innards in the string. – aaaaaaaaaaaa Mar 9 '11 at 22:53
  • +1: to Quines were more fun before the invention of dynamic languages. :-) – Quixotic Mar 9 '11 at 23:04
  • 3
    Well I sure as hell wouldn't be trying this one in assembly language... – J B Mar 9 '11 at 23:14
  • 6
    Scratch that. I did try this one in assembly language. – J B Mar 10 '11 at 16:46

Lenguage, 4 bytes

Source code consists of 4 null bytes. Regardless of the input, the output should thus be a null byes, which is accomplished by one . instruction.

Smalltalk, 94 90 59 52

for example, in the Object class, compile:

q:n|s|s:=thisContext method source.^s at:n\\s size+1

then send q:<n> to any object; here an integer:

verification:

1 q:0 -> q
1 q:1 -> :
1 q:2 -> n
...
1 q:-1 -> )
1 q:-2 -> )
1 q:-3 -> 1

or better:
(0 to:100) collect:[:n|1 q:n]as:String
-> ')q:n|s m|s:=thisContext method source.m:=s size.^s at:(n>=0ifTrue:n-1\\m+1ifFalse:m-(0-n\\m))q:n|s m|'

(-1 downTo:-500)collect:[:n|1 q:n]as:String
-> ')m\\n-0(-m:eslaFfi1+m\\1-n:eurTfi0=>n(:ta s^.ezis s=:m.ecruos dohtem txetnoCsiht=:s|m s|n:q))m\\n-0('

explanation for non-Smalltalkers:
thisContext is the current stack frame, which can be asked for its method, which can be asked for its source.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.