39
\$\begingroup\$

(Title with thanks to @ChasBrown)

Sandbox

The Background

This challenge is inspired by a question that I recently posted on Puzzling Stack Exchange. Please feel free to follow the link if you are interested in the original question. If not then I won't bore you with the details here.

The Facts

Every printable standard ASCII character has a decimal value between 32 and 126 inclusive. These can be converted to their corresponding binary numbers in the range 100000 to 1111110 inclusive. When you sum the bits of these binary numbers you will always end up with an integer between 1 and 6 inclusive.

The Challenge

Given an integer between 1 and 6 inclusive as input, write a program or function which will output in any acceptable format all of the printable standard ASCII characters where the sum of the bits of their binary value is equal to the input integer.

The Examples/Test Cases

1 -> ' @'
2 -> '!"$(0ABDHP`'
3 -> '#%&)*,1248CEFIJLQRTXabdhp'
4 -> ''+-.3569:<GKMNSUVYZ\cefijlqrtx'
5 -> '/7;=>OW[]^gkmnsuvyz|'
6 -> '?_ow{}~'

An ungolfed Python reference implementation is available here (TIO).

The Rules

  1. Assume the input will always be an integer (or string representation of an integer) between 1 and 6 inclusive.
  2. You may write a program to display the results or a function to return them.
  3. Output may be in any reasonable format but must be consistent for all inputs. If you choose to output a quoted string then the same type of quotes must be used for all inputs.
  4. Standard loopholes prohibited as usual.
  5. This is code golf so shortest code in each language wins.
\$\endgroup\$
9
  • \$\begingroup\$ Are we allowed to return/print a list of the decimal ascii values or do we need to have them in the form of characters (eg. 63 vs ?)? \$\endgroup\$ Apr 23, 2019 at 19:27
  • 1
    \$\begingroup\$ Must be the actual characters. \$\endgroup\$
    – ElPedro
    Apr 23, 2019 at 19:29
  • 8
    \$\begingroup\$ "the same type of quotes must be used for all inputs" Python, for example, uses single quotes (') for the string representation of a string by default, but uses double quotes (") if the string contain a single quote and no double quotes. Not that this specific case will matter much, as you're probably better off returning the actual string instead of its representation, and you can still use single quotes in such a string for input anyway, but I feel it's worth mentioning here. \$\endgroup\$ Apr 23, 2019 at 19:59
  • \$\begingroup\$ @EriktheOutgolfer Agreed. That is why I thought it might be interesting just to throw that in as an extra rule :-) \$\endgroup\$
    – ElPedro
    Apr 23, 2019 at 23:03
  • 1
    \$\begingroup\$ @ElPedro I wasn't sure what to do as it is probably a good idea to have some quotes, as there is a space in the first example, but the usual quotes both appear in the output:) Edit: maybe use french guillemets (« »)? :D \$\endgroup\$
    – flawr
    Apr 24, 2019 at 13:11

62 Answers 62

1 2
3
1
\$\begingroup\$

Julia 1.0, 43 bytes

y->filter(x->count_ones(Int(x))==y,' ':'~')

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Arturo, 52 bytes

$->x[to[:char]select 32..127=>[x=∑digits.base:2&]]

Try it!

Outputs a list of characters, which seems to be allowed.

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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