16
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Could you please clarify on the instructions? \$\endgroup\$ – LegionMammal978 Nov 3 '15 at 11:12
  • 1
    \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Nov 3 '15 at 11:20
  • \$\begingroup\$ @LegionMammal978 I'm actually bad at expressing thing, but I hope it will make it clearer. \$\endgroup\$ – Akangka Nov 3 '15 at 11:39
  • 2
    \$\begingroup\$ @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.) \$\endgroup\$ – Martin Ender Nov 3 '15 at 15:27
  • 1
    \$\begingroup\$ It would have felt more logical to give a 10% bonus to programs which are palindromes \$\endgroup\$ – Bassdrop Cumberwubwubwub Nov 3 '15 at 17:01
8
\$\begingroup\$

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

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

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 $_
'
\$\endgroup\$
0
\$\begingroup\$

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

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

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

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.

\$\endgroup\$
  • \$\begingroup\$ Right now this is the longest solution, but try find a shorter one, and reply if you can. \$\endgroup\$ – Erik the Outgolfer Dec 9 '16 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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