I am in desperate need of some ear defenders, so I can program in peace. Unfortunately, I don't have ear defenders. But I do have a pair of headphones, a microphone and a microcontroller, so I thought I'd make some noise-cancelling headphones.

However, there is one tiny problem. I can't program in noisy environments! So you'll need to write the code for me.


Given a constant noisy input, output the input's "complement"; a value that will completely cancel out the input value (0x00 cancels 0xff, 0x7f cancels 0x80). Because the headphones block some noise, also take a value as input that you will divide the "complement" by, using the following algorithm: if (c < 0x80) ceil ( 0x7f - ( (0x7f - c) / n ) ) else floor ( ( (c - 0x80) / n ) + 0x80 ) where c is the "complement" and n is the value to divide by. (If the value exceeds the byte range, it should be clipped to 0 or 255.)

The input will always be a sequence of bytes, via stdin or function parameter (see bonuses) but the modifier can be given via any input method. The output sequence should be output via stdout or as a return value (see bonuses). The output should be a series of bytes (e.g. $.d<±3Ç/ó¢), or a set of numbers representing those bytes (e.g. 217 135 21 43 126 219).

Oh, and your code will have to be short, because there isn't much flash memory on the microcontroller (4096 bytes; and I need to fit on drivers too!).


In these examples, I have used hexadecimal codes to represent bytes, because DEL and NULL are somewhat hard to read. The actual input and output should be done as a string of bytes or as a byte-stream.

Modifier: 2
Input: 0x00 0x10 0xe0 ...
Output: 0xbf 0xb7 0x4f ...

Modifier: 128 Input: 0x00 0x10 0xe0 ...
Output: 0x80 0x80 0x7f ...

Modifier: 0.5
Input: 0x00 0x10 0xe0 ...
Output: 0xff 0xff 0x00 ...

Your code should cope with much longer inputs than this; these are just examples.


To add a bonus to a score, use the following formula: s * (100% - b) where s is the current score and b is the bonus as a percentage.

  • 1% If you output as raw bytes.
  • 5% If there's no stdin pipe (or byte input), get the bytes straight from the microphone, such as with parec.
  • 5% If you output to the speakers as well as returning / outputting to stdout.
  • 5
    \$\begingroup\$ Why require STDIN/STDOUT? I don't see a reason why we shouldn't be allowed to use function arguments/returns values instead. \$\endgroup\$
    – Denker
    Mar 15, 2016 at 18:30
  • 2
    \$\begingroup\$ Although, there is a good reason you shouldn't be allowed to; it would be a bit of a rubbish noise-canceller if there wasn't any cancelling until all the bytes had been received through the microphone! \$\endgroup\$
    – wizzwizz4
    Mar 15, 2016 at 18:35
  • 2
    \$\begingroup\$ What's a compliment? Is it like a complement? \$\endgroup\$ Mar 15, 2016 at 18:37
  • 1
    \$\begingroup\$ I'm assuming we can use an algorithm equivalent to the provided one if it's shorter? \$\endgroup\$ Mar 15, 2016 at 19:13
  • 4
    \$\begingroup\$ Bonuses are generally discouraged in code-golf. \$\endgroup\$
    – lirtosiast
    Mar 15, 2016 at 21:10

1 Answer 1


Pyth, 54 51 bytes


Try it here!


J128m.MZ,0h.mb,255?<dJ.E-tJc-tJdQs+c-dJQJm-255id16E   # Q = modifier, E = input list

J128                                                  # set J to 128
                                         m        E   # map each input d
                                              id16    # convert to decimal
                                          -255        # get the complement
    m                                                 # map each complement d
                  ?<dj                                # if d < 128
                      .E-tJc-tJdQ                     # use the ceil-formula
                                 s+c-dJQJ             # otherwise use the floor-formular
          .mb,255                                     # minimum of the result and 255
    .MZ,0h                                            # maximum of the result and 0

Your Answer

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

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