Make a program that prints its own size

-3

Challenge: Create a program that prints out the size of its own source code file.

For example, a 35 bytes size of program must print out 35. Pre-calculation is not valid. What I mean by pre-calculation is:

 // test.js -> its size is 15 byte
 console.log(15)

It must dynamically calculate the size of own source code file. The winner is who prints the minimum number , same as who has the minimum size of code.

vvvnn

Posted 2018-07-12T12:05:42.483

Reputation: 119

Question was closed 2018-07-12T13:36:56.560

Em, console.log(15) is valid? – Luis felipe De jesus Munoz – 2018-07-12T12:07:16.233

Well it is not a good solution, if you calculate first and print :), pre-calculation is not valid. – vvvnn – 2018-07-12T12:08:59.280

1You should set some rules for cases like that so – Luis felipe De jesus Munoz – 2018-07-12T12:09:29.490

2

The program should dynamically calculate its own length then? You should mention such rules as part of the question (And have you seen the "Sandbox for Proposed Challenges"? You can post your questions there first and get help making them clearer and more likely to yield useful, interesting answers.)

– sundar - Reinstate Monica – 2018-07-12T12:11:18.967

Yes it must be dynamically. thanks for advice, I will get help there to make it clear. – vvvnn – 2018-07-12T12:13:15.487

1Do you mean by reading the size of its source file or were you thinking of some other means? – Neil – 2018-07-12T12:14:43.607

@Neil , I mean the size of the source code. – vvvnn – 2018-07-12T12:16:02.177

2Possible duplicate. – Erik the Outgolfer – 2018-07-12T12:28:40.800

@Mr.Xcoder It must dynamically calculate the size of own source code file. – Adám – 2018-07-12T12:30:35.990

Is not "reading the source code file and count the bytes" the only form of "dynamically calculate" possible? I feel that "calculate" gives the impression we can somehow work out how big the source code is from some other values. – gastropner – 2018-07-12T12:37:22.403

calculate can also mean enumerate, i.e. to count. – Adám – 2018-07-12T12:40:59.717

1Can the program contain the name of its own source file? – ngm – 2018-07-12T13:23:07.620

1So...? Anything that would count as "reading your own source code" and invalid for normal quine challenge is valid here? – user202729 – 2018-07-12T15:52:56.667

Welcome to PPCG! Your challenge has been put on hold. It means it can be reopen once clarification questions have been addressed. I do have one such question: usually programs and functions are allowed, do you allow functions? – JayCe – 2018-07-13T01:26:35.917

So, is print(2*5) considered dynamic calculate? – tsh – 2018-07-13T09:50:13.233

Answers

2

APL (Dyalog Unicode), 10 bytesSBCS

≢⊃⌽⎕NR⊃⎕SI

Try it online! Prints 11 because TIO's APL is set up to auto-format the code, but on a system where this isn't the case, it prints 10:

Session

⎕SIState Indicator (list of all functions on the stack)

 pick the first one (i.e. the currently running function)

⎕NRNested Representation (one character list per line)

 reverse (to bring the actual code line to the front)

 pick the first (i.e. the code line)

 tally the characters of that

Adám

Posted 2018-07-12T12:05:42.483

Reputation: 37 779

0

Python 3, 32 bytes

Not much to say really, opens the file and checks the length of its contents as a string.

print(len([*open(__file__)][0]))

There might be something shorter but I couldn't think of it.

Edit: Try it Online! (stolen from the comments - thanks for the reminder!)

ACraftyMarmoset

Posted 2018-07-12T12:05:42.483

Reputation: 59

Was just typing these exact characters lol, you can remove one byte from print by switching to python2, provided it doesn't break the rest – sagiksp – 2018-07-12T12:30:19.123

I'm not 100% sure if [*x] works in Python 2 - I don't have an install to test it, but if it does work then yes, you could save one :) – ACraftyMarmoset – 2018-07-12T12:37:09.380

[*...] does not work in Python 2, but print len(open(__file__).read()) does for 32 bytes as well. – Mr. Xcoder – 2018-07-12T12:38:55.030

Try it online! – Adám – 2018-07-12T12:41:39.033

Why only the first line ([0])? – Jonathan Allan – 2018-07-12T19:47:15.790

...i.e.TIO

– Jonathan Allan – 2018-07-12T19:53:32.283

1

Making it work for all lines takes a fair few more bytes since you have to do something like print(sum(len(x)for x in[*open(__file__)])) instead, but the source is only one line so that isn't necessary (the question didn't say it had to adapt to additional source being added). Example

– ACraftyMarmoset – 2018-07-13T08:17:55.947

0

JavaScript (Node.js), 40 20 bytes

-20 thanks to @Shaggy

f=_=>("f="+f).length

f=_=>("f="+f).length

alert(f())

Luis felipe De jesus Munoz

Posted 2018-07-12T12:05:42.483

Reputation: 9 639

1f=_=>("f="+f).length – Shaggy – 2018-07-12T14:35:16.380

... Because this is valid for normal quine challenge, it's invalid here? ... Also why not just 2+? – user202729 – 2018-07-13T03:52:41.520

@user202729 but 2+(''+f).length do not save a byte – tsh – 2018-07-13T09:51:41.523