N ^ 2 টি অক্ষর তৈরি করতে একটি স্নিপেটের N কপিগুলিতে যোগদান করুন


30

চ্যালেঞ্জ

কোড সম্ভব কম স্নিপেট লিখতে গেলেই এটি এন কপি একসঙ্গে ঘনিভূত হয়, অক্ষর আউটপুট সংখ্যা এন ধরনের যে, 2 । এন একটি ধনাত্মক পূর্ণসংখ্যা হবে।

উদাহরণস্বরূপ যদি স্নিপেটটি ছিল soln();, তবে চলমান soln();ঠিক 1 টি অক্ষর soln();soln();মুদ্রণ করবে এবং চলমান ঠিক 4 টি অক্ষর soln();soln();soln();মুদ্রণ করবে এবং দৌড়ানো ঠিক 9 টি অক্ষর প্রিন্ট করবে ইত্যাদি etc.

অক্ষরের মোট সংখ্যা সঠিক হওয়া পর্যন্ত কোনও অক্ষর আউটপুটে থাকতে পারে। ক্রস-ওএস বিভ্রান্তি এড়াতে, \r\nনিউলাইনগুলি একটি চরিত্র হিসাবে গণনা করা হয়।

প্রোগ্রামগুলি তাদের নিজস্ব উত্স না পড়তে পারে না বা তাদের ফাইলের আকারটি পড়তে পারে না বা এই জাতীয় অন্যান্য লফোলগুলি ব্যবহার করতে পারে। একটি কঠোর মত এই আচরণ চ্যালেঞ্জ।

আউটপুট stdout বা একটি ফাইল বা অনুরূপ বিকল্প যেতে পারে। কোনও ইনপুট নেই।

মিড-এক্সিকিউশন থেকে বেরিয়ে আসার সাথে সাথে কোডটিতে মন্তব্যগুলি ঠিক আছে।

কোনও অক্ষর প্রোগ্রামে থাকতে পারে। বাইটস মধ্যে সংক্ষিপ্ততম জমা জয়।


প্রোগ্রামটি কি শেষ করতে হবে?
মার্টিন ইন্ডার

@ মার্টিনব্যাটনার হ্যাঁ
ক্যালভিনের শখ

উত্তর:


24

টেকো, 4 বাইট

V1\V

Vপাঠ্য বাফারে বর্তমান লাইনের বিষয়বস্তু মুদ্রণ করে। 1\বর্তমান অবস্থানে 1 নম্বরটির স্ট্রিং প্রতিনিধিত্ব সন্নিবেশ করান।

সুতরাং এন প্রোগ্রামের তম পুনরাবৃত্তির, প্রথম Vইচ্ছার আউটপুট এন - 1 চরিত্র কপি 1, তারপর অন্য অ্যাড 1টেক্সট, তারপর আউটপুট এন 1 গুলি।


1
আপনি কি টেকোর সাথে একটি লিঙ্ক যুক্ত করতে পারেন?
এরিক দি আউটগল্ফার

22

ব্রেইনফাক, 17 16 বাইট

[>+>-..+<<-]-.>+

আপনি এটি এখানে পরীক্ষা করতে পারেন । শুধু যে সত্য ব্যবহার করুন ।n2+2n+1=(n+1)2


16
আমি বিশ্বাস করতে পারি না আমি প্রতিযোগিতামূলক স্তরে বাইটের বিএফ দেখছি!
আগস্ট

21

ব্রেইনফাক, 11

আমি প্রথম ব্রেইনফাকের উত্তর দেখেছি এবং ভেবেছিলাম এটি অনেক দীর্ঘ :)

[.<]>[.>]+.

যদি আপনি আরও অনেক প্লাস দিয়ে প্লাসটি প্রতিস্থাপন করেন তবে আউটপুটটি আরও সহজ হতে পারে।

নবম পুনরাবৃত্তিতে, প্রতিটি লুপ এনএস-এর 1 টি কপির সাথে ASCII মান 1 এবং তারপরে আরও একটি দিয়ে আউটপুট করে +.


আপনাকে N অক্ষর নয়, N ^ 2 টি অক্ষর মুদ্রণ করতে হবে। আমি বিএফ কোডটি পড়তে পারি না, সুতরাং আপনার কোডটি ভুল কিনা বা আপনার বিবরণটি ভুল কিনা তা আমি জানি না।
ব্রায়ান জে

@ ব্রায়ানজে এটি এন ^ 2 টি অক্ষর মুদ্রণ করে। আপনি এটি এখানে পরীক্ষা করতে পারেন: copy.sh/brainfuck আউটপুটটি দেখতে না পারলে একটি বিয়োগের সাথে প্লাসটি প্রতিস্থাপন করুন।
আলেফাল্ফ

@ ওলাল্ফা ওফস, আমি এখন দেখছি যে আমি মন্তব্যটি ভুলভাবে পড়েছি। কোডটি না করে (এন - 1) + 1 যেমনটি আমি মূলত ভেবেছিলাম।
ব্রায়ান জে

16

পাইথন 2, 22

a='';print a;a+='xx';a

খালি স্ট্রিং মুদ্রণ করে, তারপরে দুটি x, তারপরে x'চার' ইত্যাদি। প্রতিটি স্ট্রিংয়ের পরে নিউলাইন সহ, এটি n*nঅক্ষরের বাইরে আসে ।

একটি অনুলিপি: "\n"(1 চর)
দুটি অনুলিপি "\nxx\n"(4 টি চর)
তিনটি অনুলিপি "\nxx\nxxxx\n"(9 টি অক্ষর)

aপ্রতিটি চলমানটিকে পুনরায় পুনঃনির্মাণকরণ থেকে প্রাথমিক পরিবর্তনশীলটি থামানোর জন্য , আমি কোডটি একটি দিয়ে শেষ করি ;a, যা নিজেই সৌম্যযুক্ত, তবে পরবর্তী লুপের সাথে একত্রিত হয়ে এর aaপরিবর্তে নির্ধারিত বলির ছাগল তৈরি করে । এই কৌশলটি আমার নয়; আমি আগের উত্তরে দেখেছি। আমি কৃতজ্ঞ যদি কেউ আমাকে নির্দেশ করতে পারে যাতে আমি কৃতিত্ব দিতে পারি।


আসলে, চূড়ান্ত নিউলাইন কি ছাপা হয়?
xnor

না আমি চূড়ান্ত নিউলাইনটি মুদ্রিত বলে মনে করি না। কিন্তু কেবল অপসারণ, after print a should work. print a prints a newline after each print.
Justin

@Quincunx Oh, of course, thanks!
xnor

Are you talking about this post?
Sp3000

10

CJam, 6 bytes

LLS+:L

Uses the fact that n2 + n + (n+1) = (n+1)2.

L      "Push L. Initially this is an empty string, but its length increases by 1 with each copy
        of the snippet.";
 L     "Push another L.";
  S+   "Add a space to the second copy.";
    :L "Store the lengthened string in L for the next copy of the snippet.";

:L..1+গল্ফস্ক্রিপ্ট একই ধারণা।
পিটার টেলর

@ পিটার টেইলর আমি গল্ফস্ক্রিপ্টে ভাবছিলাম ..n+, কিন্তু সেই পেস্কি নতুন লাইনের পিছনে ... :(
মার্টিন এন্ডার

হ্যাঁ, আপনি ঠিক বলেছেন। :Lএটি ব্যবহার করা হয়নি কারণ প্রয়োজন নেই ।
পিটার টেলর

10

///, 21 bytes

I'm sure there is a really short and twisted way to solve this in /// but I couldn't find anything, beyond the "straightforward" way yet:

1/1\//112\///2\//1\//

এটি পরপর বিজোড় সংখ্যা মুদ্রণের পদ্ধতির উপর ভিত্তি করে। স্নিপেট একটি এ গঠিত1 শুরুতে একটি মুদ্রিত যা মুদ্রিত হয় এবং দুটি প্রতিস্থাপন যা 1স্নিপেটের প্রতিটি টানা অনুলিপিটির প্রথম অংশে আরও দুটি গুলি যুক্ত করে। এর জন্য এটি মাধ্যমে চলুন N = 3। নিম্নলিখিতগুলি 3 বা ততোধিক লাইনের গ্রুপে পড়তে হবে: 1. বর্তমান কোড, 2. প্রক্রিয়াজাত টোকেন (গুলি), 3. (এবং নিম্নলিখিত) উপরের টোকেনটি কী করে একটি মন্তব্য a

1/1\//112\///2\//1\//1/1\//112\///2\//1\//1/1\//112\///2\//1\//
1
is printed
/1\//112\///2\//1\//1/1\//112\///2\//1\//1/1\//112\///2\//1\//
/1\//112\//
replaces all occurrences of 1/ with 112/. This affects the starts of all further snippets
but not the substitution commands, because the slashes in those are always escaped.
It is necessary to put a 2 in there, because otherwise the interpreter goes into an infinite
loop replacing the resulting 1/ again and again.
/2\//1\//112/1\//112\///2\//1\//112/1\//112\///2\//1\//
/2\//1\//
Replace all occurrences of 2/ with 1/, so the the next snippets substitution works again.
111/1\//112\///2\//1\//111/1\//112\///2\//1\//
111
is printed
/1\//112\///2\//1\//111/1\//112\///2\//1\//
/1\//112\//
add two 1s again
/2\//1\//11112/1\//112\///2\//1\//
/2\//1\//
turn the 2 into a 1 again
11111/1\//112\///2\//1\//
11111
print 11111
/1\//112\///2\//1\//
the last two substitutions have nothing to substitute so they do nothing

মজার বিষয় হল, আমরা যদি 1শেষের দিকে চলে যাই তবে এটি ঠিক একইভাবে কাজ করে:

/1\//112\///2\//1\//1

7

> <> , 14 বাইট

1:na*a*';'10p!

Uses the "sum of consecutive odd integers starting from 1" idea. It starts off with 1 and multiplies it by 100 each time, increasing the length of the output progressively by increments of 2.

For example, appending 5 copies gives

1100100001000000100000000

I tested by piping the output to a file, and didn't see a trailing newline.

Breakdown

1                   Push 1, skipped by ! every time except the first
 :n                 Copy top of stack and output as num                  
   a*a*             Multiply by 10 twice
       ';'10p       Modify the source code so that the first : becomes a ; for termination
             !      Skip the next 1

5

CJam, 10 9 bytes

],)_S*a*~

This prints N2 spaces where N is the number of copies of the code.

Code eexpansion:

],            "Wrap everything on stack and take length";
  )_          "Increment and take copy";
    S*        "Get that length space string";
      a*      "Wrap that space string in an array and create that many copies";
        ~     "Unwrap so that next code can use to get length";

Try it online here



5

Java - 91 bytes

{String s=System.getProperty("a","");System.out.println(s);System.setProperty("a","xx"+s);}

This solution is equivalent to this other one in Python. It surely won't win, but it was fun :)


Don't you need a class to run anything?

No, since OP asked for snippets of code. We can assume this is running inside a main, for example.
cygnusv

Then I have a 59 or even 44 byte solution.

Cool :) I prefer one-liners, but yours is indeed shorter!
cygnusv

4

Perl, 14 bytes

print;s//__/;

This needs to be run with Perl's -l command switch, which causes print to append new lines.

It prints the default variable $_, then prepends two underscores via substitution.

Example:

$ perl -le 'print;s//__/;print;s//__/;print;s//__/;print;s//__/;'

__
____
______

flags are counted as 1 more byte per flag
Optimizer

What about say?
hmatt1

@chilemagic I tried that, but I couldn't get it working on my versions of Perl.
grc

@grc it's version 5.10 and higher and you need -E instead.
hmatt1

@chilemagic hmm, that didn't seem to work for me on 5.16.
grc

4

Brainfuck, 10 chars

Both previous Brainfuck solutions were waaay too long (16 and 11 chars) so here is a shorter one:

+[.->+<]>+

In the n-th block it prints out 2*n-1 characters (with codepoints from 2*n-1 to 1)


2
This wouldn't work in standard brainfuck, only if the cells are unlimited-size. Actually, it wouldn't totally make sense then either. How do you output character code 1 trillion?
feersum

3

Prelude, 18 12 bytes

^1+(9!1-)#2+

This prints N2 tabs. It assumes a standard-compliant interpreter which prints characters instead of numbers, so if you use the Python interpreter you'll need to set NUMERIC_OUTPUT to False.

The idea is simply to use the top of the stack (which is initially 0) as 2(N-1), and print 2N-1 tabs, then increment the top of the stack by 2. Hence each repetition prints the next odd number of tabs.


3

Java - 59 / 44 (depending on requirements)

static String n="1";
static{System.out.print(n);n+="11";}//

Apparently we're allowed to assume code runs in a class.

If it can go inside a main method:

String n="1";
System.out.print(n);n+="11";//

3

C, 87 bytes

#if!__COUNTER__
#include __FILE__
main(a){a=__COUNTER__-1;printf("%*d",a*a,0);}
#endif

This uses two magic macros. __COUNTER__ is a macro that expands to 0 the first time it is used, 1 the second, etc. It is a compiler extension, but is available in both gcc, clang, and Visual Studio at least. __FILE__ is the name of the source file. Including a file in C/C++ is literally the same as pasting it directly into your source code, so it was a little tricky to make use of.

It would still be possible to use this technique without __COUNTER__. In that case, the standard guard against using code twice could be used for the #if statement, and __LINE__ could be used to count the number of characters needed.


This solution is not written in C, but rather a C dialect. Please correct the language name.
FUZxxl

2
@FUZxxl Most code-golf answers are only designed to work in gcc, so I'm not sure why this would be an issue.
feersum

It isn't, but you should really declare that.
FUZxxl

I'm confused. Why declare a non-issue? O_o
corsiKa

@corsiKa It's only a non-issue if you declare it. The C gcc speaks is not standard C.
FUZxxl

2

Dyalog APL, 20 19 bytes

A matrix based solution.

{⍺≢⍵:⍵⍪⍵,⍺⋄∊⍺}⍨⍪'a'

Try it here. Returns a string of N2 repetitions of a. Explanation by explosion for N = 2:

{⍺≢⍵:⍵⍪⍵,⍺⋄∊⍺}⍨⍪'a'{⍺≢⍵:⍵⍪⍵,⍺⋄∊⍺}⍨⍪'a'
                                  ⍪'a'  Wrap 'a' into a 1x1 matrix.
                'a'{            }⍨      Binary function: bind 'a' to ⍵ and the matrix to ⍺.
                    ⍺≢⍵:                The arguments are not identical,
                        ⍵⍪⍵,⍺           so add to the matrix 1 column and 1 row of 'a's.
               ⍪                        Identity function for a matrix.
{            }⍨                         Unary function: bind the matrix to both ⍵ and ⍺.
 ⍺≢⍵:                                   The arguments are identical,
           ∊⍺                           so flatten the matrix into the string 'aaaa'.

2

STATA 20

di _n($a)
gl a=$a+2

There is a trailing new line to make sure that the display (di) statement works. First display the current number in $a newlines (and one additional from the default of display). Then add 2 to $a.

Uses the even numbers approach (i.e. odd numbers approach minus 1) with an extra newline every time.


2

T-SQL 117

IF OBJECT_ID('tempdb..#')IS NULL CREATE TABLE #(A INT)INSERT INTO # VALUES(1)SELECT REPLICATE('a',COUNT(*)*2-1)FROM #

Note the trailing space to ensure that the if condition is properly checked every time.

Uses the odd numbers approach. Not sure if there's a newline on select statements.

Not sure if there's a shorter way to create a table if it doesn't exist.


2
Kudos to you for an unusual language choice.
Xynariz

2

PostScript, 35 chars

count dup 2 mul 1 add string print

Each pass "leaks" one thing on the stack, so count goes up by 1 each time. Then it justs uses the sum of odd numbers trick.

The bytes output are all \000 because that's the initial value of strings.


2

Haskell, 72

putStr$let a="1";aputStr=(\n->take(n^2)$show n++cycle" ").(+1).read in a

Explanation

The apply operator $ acts as if you place surrounding parentheses around the rest of the line (there are exceptions to this, but it works in this case). aputStr is a function that takes a string with the format "abc ...", where "abc" is the square root of the length of the string, including abc. It will parse the string as an integer, and return a string starting with abc+1 and having that length squared. Because of the $ operator, this will get called recursively on "1" N times.


1

Pyth, 8 bytes

*d*2Z~Z1

This relies on the fact that N2 is equal to the sum of N odd numbers. Now Pyth auto prints an new line, so I have to just print Z * 2 characters in each code where Z goes from 0 to N - 1.

Code Expansion:

*d               "Print d whose value is a space character"
  *2Z            "2 * Z times where Z's initial value is 0"
     ~Z1         "Increment the value of Z";

Try it online here


1

Golflua, 23 bytes

X=2+(X|-2)w(S.t("&",X))

outputs a combination of & and \n characters.

Equivalent Lua code

X = 2 + (X or -2)          -- initialize X to 0 the first time, add 2 ever other time

print(string.rep("&", X))

Each time the code snippet runs it produces 2 more characters of output than the last time, starting with 1 character. The print function appends a newline, so I initialize X to 0 instead of 1.


0

ActionScript - 27 / 26 bytes

var n=""
trace(n);n+="11"//

or

var n=1
trace(n);n+="11"//

How it works:

var n=""
trace(n);n+="11"//var n=""
trace(n);n+="11"//

It simply comments out the first line. Note: trace adds a newline. Or maybe all the IDE's I use do that automatically.


আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.