Quine Challenge I

12

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

Quixotic

Posted 2011-03-09T18:54:55.977

Reputation: 2 199

1I guess the && in the first sentence is meant to be a ||? – Paŭlo Ebermann – 2015-10-25T16:17:31.163

In the give example test case if the input is 5 or -7 the output should be a single space : " " (without quotation). – Quixotic – 2011-03-09T19:11:39.127

What if N is 0? – aaaaaaaaaaaa – 2011-03-09T21:16:55.090

@eBusiness:Thanks for pointing that out, I have changed the problem statement,I don't think $0$ can occur now :-) – Quixotic – 2011-03-09T23:01:57.450

@Debanjan: your edit makes the case where N=M+1 a little weird. – J B – 2011-03-09T23:04:47.100

Opertor precedence %M will be executed first before addition ;-) – Quixotic – 2011-03-09T23:11:15.407

@Debanjan: oh I see. The spacing got me. – J B – 2011-03-09T23:15:15.580

Your examples don't match your challenge description, if I'm not completely mistaken. First example: N=2, so T=3. The 3rd character is c, not b. – Ventero – 2011-03-09T23:42:08.080

@Ventero:Yes,thanks for pointing,I fixed it now. – Quixotic – 2011-03-10T06:30:56.897

3It 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 – 2011-03-10T12:27:14.033

Answers

12

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.

J B

Posted 2011-03-09T18:54:55.977

Reputation: 9 638

+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 – 2011-03-10T19:02:33.830

Heh, pretty cool. And a funny thing, despite being so different languages this does seem to resemble my 44 characters GolfScript solution. – aaaaaaaaaaaa – 2011-03-10T21:09:46.357

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 – 2011-03-10T22:50:05.400

+1, that's your second "invalid" asnwer here (code-golf wise) that worths upvoting :) – Eelvex – 2011-03-11T14:33:47.363

@Eelvex: for the record, the other one was valid at the time it was posted. – J B – 2011-03-11T14:56:01.977

@Eelvex: there you go, this one's now totally valid. Perhaps a bit long, though O:-) – J B – 2011-03-11T16:27:25.707

10

Whaddaya know, HQ9+ makes its great return!

Q

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

J B

Posted 2011-03-09T18:54:55.977

Reputation: 9 638

Does it take any input? – Quixotic – 2011-03-09T23:05:08.167

@Debanjan: sure: echo '-1' | hq9+ qc1 – J B – 2011-03-09T23:07:46.723

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 – 2011-03-09T23:09:51.693

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 – 2011-03-09T23:18:01.770

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 – 2011-03-09T23:20:15.710

5

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.

Ventero

Posted 2011-03-09T18:54:55.977

Reputation: 9 842

5

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.

aaaaaaaaaaaa

Posted 2011-03-09T18:54:55.977

Reputation: 4 365

1I 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 – 2011-03-09T22:32:26.083

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 – 2011-03-09T22:53:07.533

+1: to Quines were more fun before the invention of dynamic languages. :-) – Quixotic – 2011-03-09T23:04:22.550

3Well I sure as hell wouldn't be trying this one in assembly language... – J B – 2011-03-09T23:14:28.187

@J B:This is great fun in C,I did something like this recently :-) – Quixotic – 2011-03-09T23:27:46.453

Ok, maybe I'll give it a shot. Just not this week. – J B – 2011-03-09T23:51:02.713

I think I'm 100 years behind you people :( – Clyde Lobo – 2011-03-10T04:27:26.707

6Scratch that. I did try this one in assembly language. – J B – 2011-03-10T16:46:01.593

2

Lenguage, 4 bytes

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

pppery

Posted 2011-03-09T18:54:55.977

Reputation: 3 987

0

Runic Enchantments, 17 bytes

"3X4+kSq:l͍iS%%@

Try it online!

Just uses standard quine to generate the program string, then uses string-based modulo operator1 to get the character at a given index (which natively support negative and positive indicies, behavior influenced by Python).

  1. The , "divide" command cuts the string up into chunks (logical enough inverse of * duplicating the string x times), + concatenates, and - removes x characters from the end (a logical enough inverse of +). So the % command takes the role of charAt(x): the only basic string operation remaining (after stack-like operations being handled by the same commands that deal with the stack; eg. l is the size of the stack so is the length of a string).

Draco18s no longer trusts SE

Posted 2011-03-09T18:54:55.977

Reputation: 3 053

0

JavaScript (ES6), 28 bytes

f=n=>("f="+f).substr(n%28,1)

A modified quine.

A value larger than 28 is handled by %28, a value less than 0 is handled by .substr().

Naruyoko

Posted 2011-03-09T18:54:55.977

Reputation: 459

0

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.

blabla999

Posted 2011-03-09T18:54:55.977

Reputation: 1 869