Quine multiple times

16

Your task is make program that do following:

  1. You should take number. (Positive, negative, fraction is possible input)
  2. If it is negative, you reverse the quine. and negate that number (Become positive)
  3. Then you repeat <integer part of the input number> times and print first <floor(fraction part of the input number*length)> from your source program. If it is integer, then the fraction part is zero.

-10% bonus if your program isn't palindrome.

Example

If your program is "ABCDEFG", then

1.

5
ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFG

Explanation

ABCDEFG five times

2.

-2
GFEDCBAGFEDCBA

Explanation

GFEDCBA (reversed ABCDEFG) 2 times

3.

7.5
ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABC

Explanation

ABCDEFG 7 times followed by ABC (first 3(floor(0.5*7)=floor(3.5)=3) letter on ABCDEFG)

4.

-0.3
GF

Explanation

GFEDCBA (reversed ABCDEFG) 0 times followed by GF (first 2(floor(0.3*7)=floor(2.1)=2) letter on GFEDCBA(reversed ABCDEFG))

5.

0
<empty>

Explanation:

<empty> here means that your program doesn't output. It is ABCDEFG zero times that is defined as empty string.

Akangka

Posted 2015-11-03T11:02:00.373

Reputation: 1 859

Could you please clarify on the instructions? – LegionMammal978 – 2015-11-03T11:12:41.900

1Related. – Martin Ender – 2015-11-03T11:20:45.283

@LegionMammal978 I'm actually bad at expressing thing, but I hope it will make it clearer. – Akangka – 2015-11-03T11:39:47.900

What if a programming language has neither fractions nor negative numbers? – mbomb007 – 2015-11-03T15:20:11.730

2@mbomb007 I guess you can parse the string yourself and handle - and . manually (representing the fraction as positive integers). Or you can turn your attention to the next challenge. ;) (Not every language can participate in every challenge, but as long as the challenge doesn't deliberately rule out arbitrary individual languages, that's completely fine. Just think of all of the audio/image processing or file system challenges.) – Martin Ender – 2015-11-03T15:27:46.557

@MartinBüttner Yeah, I'm not doing one of those in BF, lol. – mbomb007 – 2015-11-03T15:34:15.587

1It would have felt more logical to give a 10% bonus to programs which are palindromes – Bassdrop Cumberwubwubwub – 2015-11-03T17:01:23.010

I'm guessing the answer is no, but is a trailing newline allowed? – ThisSuitIsBlackNot – 2015-11-03T22:42:09.373

Is the quine allowed to read its own source code? – Mama Fun Roll – 2015-11-04T04:16:43.190

@ןnɟuɐɯɹɐןoɯ That's called a cheating quine, which is by definition not actually a quine.

– ThisSuitIsBlackNot – 2015-11-04T05:02:42.310

@ThisSuitIsBlackNot Nevertheless, it is a quine (albeit a cheating one). My question was if this kind of quine was allowed, because the specs don't address this. – Mama Fun Roll – 2015-11-04T05:04:26.140

@ןnɟuɐɯɹɐןoɯ No, it is not a quine. The term "cheating quine" is a misnomer. A quine is a program that takes no input (including reading from files). A program that reads its own source code is not a quine. – ThisSuitIsBlackNot – 2015-11-04T05:06:05.977

@ןnɟuɐɯɹɐןoɯ Although now that I think about it, a program that prints its own source code in reverse probably isn't a quine, either, technically speaking. :P – ThisSuitIsBlackNot – 2015-11-04T05:09:05.217

@Bas You're right, and I regret it, however, the milk has been spilt. – Akangka – 2015-11-04T09:41:44.227

Answers

8

CJam, 28 26 bytes * 0.9 = 23.4

Thanks to Sp3000 for saving 2 bytes.

{`"_~"+rd_z26*,\g1|@%f=}_~

Test it here.

Explanation

{`"_~"+ e# Generalised quine framework. Leaves the source code on the stack.
  rd    e# Read input and parse as double.
  _z    e# Copy and get absolute value.
  26*   e# Multiply by 26, the length of the quine to get length N of the output.
  ,     e# Get a range [0 1 ... floor(N-1)].
  \g    e# Swap with other copy and computer signum to determine direction of string.
  1|    e# This might be zero though, so take bitwise OR with 1 to avoid an error.
  @%    e# Pull up source string. Reverse it if the signum was -1 (no-op otherwise).
  f=    e# The range we pushed earlier corresponds to the (cyclic) indices of the source
        e# which make up the desired result, so we map each index to the corresponding
        e# character to get the final result.
}_~

Martin Ender

Posted 2015-11-03T11:02:00.373

Reputation: 184 808

5

Vitsy, 34 * 0.9 = 30.6 Bytes

Thanks to @Sp3000 for pointing out a flaw in my code!

Woo. My Physics teacher reminded me I had power functions to help me with this. Go figure.

'r(;Vd3*V2^12/^DvV/1+(rvl1-*\[DO{]
'                                   Start recording as a string - this grabs everything and pushes it to the stack as a string.
 r                                  Reverse the stack order.
  (;                                If the top item is zero, exit the program.
    V                               Grab the input as a final global variable.
     d3*                            Push the character ' to the stack.
        V2^12/^                     Get the absolute value of the input value.
               Dv                   Duplicate and save in a temp variable.
                 V                  Push the global variable to the stack.
                  /                 Divide the top two items - this gets -1 or 1 depending on the polarity of the input.
                   1+(              If it's -1, do the next instruction. Otherwise, don't.
                      r             Reverse the stack
                       v            Push the temporary variable to the stack.
                        l1-*        Multiply by the length of the stack minus 1.
                            \[   ]  Repeat everything in brackets top item of the stack times.
                              DO{   Duplicate an item, pop it off the stack and output it, then move one item over in the stack.

Addison Crump

Posted 2015-11-03T11:02:00.373

Reputation: 10 763

2

Perl, 104 bytes - 10% = 93.6

perl -i-0.3 -e '$_=q{$_="\$_=q{$_};eval";$_=reverse if$^I<0;$n=abs$^I;print+($_ x$n).substr$_,0,y///c*($n-int$n)};eval'

102 bytes + 2 bytes for -i - 10% for not being a palindrome. Input is passed as the argument to -i (e.g. -0.3 above).

How it works

This solution is based on the following quine:

$_=q{print"\$_=q{$_};eval"};eval

This works as follows. First, set $_ to the string:

print"\$_=q{$_};eval"

Next, call eval, which works on $_ by default. This calls print with one argument, a string literal:

"\$_=q{$_};eval"

Since this string is double-quoted, variables are interpolated. After interpolating $_, the value of the string is:

\$_=q{print"\$_=q{$_};eval"};eval

When printed, this outputs:

$_=q{print"\$_=q{$_};eval"};eval

which is the source code of the program itself.

The nice thing about this quine is that you can embed arbitrary code inside the string to be eval'd.


Here's a breakdown of the full solution:

perl -i-0.3 -e'
    $_=q{                     # string to be eval'd
        $_="\$_=q{$_};eval";  # append beginning and end of quine so the
                              #  entire thing can be reversed if necessary
        $_=reverse if $^I<0;  # reverse if input < 0
        $n=abs $^I;           # set $n to absolute value of input
        print                 # print
            +($_ x $n)        # $_ repeated $n times
            .                 # concatenated with
            substr $_,        # substring of $_
                   0,         # starting at the beginning
                   y///c      # having length x, where x is length of $_
                   *          # multiplied by
                   ($n-int$n) # fractional part of $n
    };
    eval                      # eval $_
'

ThisSuitIsBlackNot

Posted 2015-11-03T11:02:00.373

Reputation: 1 050

0

Python 2, 193 bytes - 10% = 173.7

x=input();y=int(x);_='x=input();y=int(x);_=%r;_=(_%%_)[::y/abs(y)];x,y=abs(x),abs(y);_=_*y+_[:int(y*(x-y)*193)];print _';_=(_%_)[::y/abs(y)];x,y=abs(x),abs(y);_=_*y+_[:int(y*(x-y)*193)];print _

Errors out on 0, but, ignoring STDERR, you still get empty output.

Erik the Outgolfer

Posted 2015-11-03T11:02:00.373

Reputation: 38 134

Right now this is the longest solution, but try find a shorter one, and reply if you can. – Erik the Outgolfer – 2016-12-09T18:12:08.343

0

Mathematica, 139 - 10% = 125.1 bytes

StringJoin[Table[s = If[#1 > 0, #1 & , StringReverse][ToString[#0, InputForm]], {Abs[Floor[#1]]}], StringTake[s, Floor[Mod[#1, 1]*139]]] & 

Note the trailing space. The whitespace, standard notation, etc. are the result of the ToString[#0, InputForm].

LegionMammal978

Posted 2015-11-03T11:02:00.373

Reputation: 15 731

0

Haskell, 158 * 0.9 = 142.2 bytes

c i|i<0=reverse|1<2=id;f i=putStr$take(floor$abs i*158)$cycle$c i$s++show s;s="c i|i<0=reverse|1<2=id;f i=putStr$take(floor$abs i*158)$cycle$c i$s++show s;s="

A quine function.

*Main> f (-0.3)
"=s;s wohs++s$i c$elcyc$)851*i sba$roolf(ekat$r

*Main> f 1.1
c i|i<0=reverse|1<2=id;f i=putStr$take(floor$abs i*158)$cycle$c i$s++show s;s="c i|i<0=reverse|1<2=id;f i=putStr$take(floor$abs i*158)$cycle$c i$s++show s;s="c i|i<0=reverse

*Main> f 0
              <-- empty

nimi

Posted 2015-11-03T11:02:00.373

Reputation: 34 639