# Code-Golf: Quine Challenge I

## 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 like this one ,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? – eBusiness 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
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. – eBusiness Mar 10 '11 at 12:27

## 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
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
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. – eBusiness Mar 10 '11 at 21:09
@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
show 1 more comment

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.

-
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. – eBusiness 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
Well I sure as hell wouldn't be trying this one in assembly language... – J B Mar 9 '11 at 23:14
Scratch that. I did try this one in assembly language. – J B Mar 10 '11 at 16:46