4
\$\begingroup\$

So, this is a task from one very popular educational site in Russia: https://acmp.ru/index.asp?main=task&id_task=786&ins=1

Mathematically, you need to find the power of two not exceeding the given number, read from the standard input stream, and output their difference in the standard output stream. My python3 solution takes 41 characters, but the leader’s solution is only 40 characters. Who can shorten the code?

Newlines, spaces and tabs are not included while counting code length.

x = int(input())
print(x - 2 ** (len(bin(x)) - 3))
\$\endgroup\$
  • \$\begingroup\$ Your code (with spaces removed) is 42 characters. Did you forget to count the newline, perhaps? \$\endgroup\$ – Dingus Jul 5 '20 at 12:31
  • 2
    \$\begingroup\$ You should state that in the question. Not counting spaces/tabs/newlines is unusual on this site. \$\endgroup\$ – Dingus Jul 5 '20 at 12:35
  • 3
    \$\begingroup\$ It is almost certainly a bad idea not to count newlines, spaces and tabs because somebody can encode a binary string using spaces and tabs, decode it to ASCII and evaluate it. \$\endgroup\$ – the default. Jul 5 '20 at 12:50
  • 1
    \$\begingroup\$ Not counting whitespace in the score is not a good idea. That said, the task here is so simple that the naive approach appears to be shorter than any of the answers to that question, so I won't close as duplicate. \$\endgroup\$ – pppery Jul 5 '20 at 17:02
  • 1
    \$\begingroup\$ @cairdcoinheringaahing The issue is that if you don't count whitespace there is a simple wrapper that solves any task in python in ~45 bytes of non-whitespace. Thus it doesn't matter what the task is the the score is always the same assuming that you can solve the problem. I agree with pppery here though that because this task is so simple that the wrapper method is not viable and thus this is not a duplicate. \$\endgroup\$ – Wheat Wizard Jul 5 '20 at 18:57
6
\$\begingroup\$

Python 3, 40 39 bytes (not counting the newline)

First I simplified \$2^{a-3}\$ to \$\frac{2^a}8\$, then I used the lower-precedence operators << and ^ so that // can be replaced by - (which saves a byte).

x=int(input())
print(1<<len(bin(x))-3^x)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ If Python 2 is allowed, this can be 37 bytes (not counting the space or newline). \$\endgroup\$ – Dingus Jul 5 '20 at 13:44
  • \$\begingroup\$ @Dingus I think the int(...) is also not necessary in Python 2. \$\endgroup\$ – the default. Jul 5 '20 at 13:46
  • \$\begingroup\$ Indeed, it looks like you're right. (I don't know Python well enough to say for sure.) \$\endgroup\$ – Dingus Jul 5 '20 at 13:50
  • \$\begingroup\$ @mypronounismonicareinstate any idea to get 38?))) \$\endgroup\$ – Evgeny Jul 5 '20 at 18:10
10
\$\begingroup\$

Python 3, 39 bytes

print(int('0'+bin(int(input()))[3:],2))

Try it online!

Subtracting the largest power of two less than a number is the same as removing the first 1 from its binary representation.

Unfortunately python rather mysteriously errors on trying to convert the empty binary string to an int so we need '0'+ if we want it to work on zero or one. If we relax the requirements to only require two or more then we can remove 4 bytes.

Python 3, 35 bytes

print(int(bin(int(input()))[3:],2))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ The question doesn't specify the version of Python to be used so you could switch to 2 and drop the inner int(). \$\endgroup\$ – Shaggy Jul 5 '20 at 16:59
  • 3
    \$\begingroup\$ @Shaggy ACMP only supports Python 3 (as far as I know). \$\endgroup\$ – the default. Jul 5 '20 at 17:22
  • \$\begingroup\$ @Ad Hoc Garf Hunter, Great! \$\endgroup\$ – Evgeny Jul 5 '20 at 18:07
  • 1
    \$\begingroup\$ "we need '0'+ if we want it to work on zero or one" - 0 is an invalid input anyway, since it's smaller than all powers of 2. \$\endgroup\$ – user2357112 supports Monica Jul 6 '20 at 11:34

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.