12
\$\begingroup\$

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

\$\endgroup\$
10
  • \$\begingroup\$ In the give example test case if the input is 5 or -7 the output should be a single space : " " (without quotation). \$\endgroup\$
    – Quixotic
    Commented Mar 9, 2011 at 19:11
  • \$\begingroup\$ What if N is 0? \$\endgroup\$ Commented Mar 9, 2011 at 21:16
  • \$\begingroup\$ @eBusiness:Thanks for pointing that out, I have changed the problem statement,I don't think $0$ can occur now :-) \$\endgroup\$
    – Quixotic
    Commented Mar 9, 2011 at 23:01
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 10, 2011 at 12:27
  • 1
    \$\begingroup\$ I guess the && in the first sentence is meant to be a ||? \$\endgroup\$ Commented Oct 25, 2015 at 16:17

8 Answers 8

12
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ +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. \$\endgroup\$
    – Quixotic
    Commented Mar 10, 2011 at 19:02
  • \$\begingroup\$ Heh, pretty cool. And a funny thing, despite being so different languages this does seem to resemble my 44 characters GolfScript solution. \$\endgroup\$ Commented Mar 10, 2011 at 21:09
  • 1
    \$\begingroup\$ @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) \$\endgroup\$
    – J B
    Commented Mar 10, 2011 at 22:50
  • \$\begingroup\$ +1, that's your second "invalid" asnwer here (code-golf wise) that worths upvoting :) \$\endgroup\$
    – Eelvex
    Commented Mar 11, 2011 at 14:33
  • \$\begingroup\$ @Eelvex: for the record, the other one was valid at the time it was posted. \$\endgroup\$
    – J B
    Commented Mar 11, 2011 at 14:56
10
\$\begingroup\$

Whaddaya know, HQ9+ makes its great return!

Q

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

\$\endgroup\$
5
  • \$\begingroup\$ Does it take any input? \$\endgroup\$
    – Quixotic
    Commented Mar 9, 2011 at 23:05
  • \$\begingroup\$ @Debanjan: sure: echo '-1' | hq9+ qc1 \$\endgroup\$
    – J B
    Commented Mar 9, 2011 at 23:07
  • \$\begingroup\$ 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, \$\endgroup\$
    – Quixotic
    Commented Mar 9, 2011 at 23:09
  • \$\begingroup\$ 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 ;-) \$\endgroup\$
    – J B
    Commented Mar 9, 2011 at 23:18
  • \$\begingroup\$ 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 :-) \$\endgroup\$
    – Quixotic
    Commented Mar 9, 2011 at 23:20
5
\$\begingroup\$

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.
\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – J B
    Commented Mar 9, 2011 at 22:32
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 9, 2011 at 22:53
  • \$\begingroup\$ +1: to Quines were more fun before the invention of dynamic languages. :-) \$\endgroup\$
    – Quixotic
    Commented Mar 9, 2011 at 23:04
  • 3
    \$\begingroup\$ Well I sure as hell wouldn't be trying this one in assembly language... \$\endgroup\$
    – J B
    Commented Mar 9, 2011 at 23:14
  • 6
    \$\begingroup\$ Scratch that. I did try this one in assembly language. \$\endgroup\$
    – J B
    Commented Mar 10, 2011 at 16:46
2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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).
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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