36
\$\begingroup\$

Your task is simple: write a program that receives an integer as input and outputs it if it was odd, and does nothing otherwise (not halting is allowed). The challenge is, that you can only use odd bytes.

You can optionally append a trailing newline to the output.

This is code-golf, shortest answer in (odd) bytes wins.

Standard loopholes are forbidden. Furthermore, you cannot use versions of languages, which are newer than the posting of this challenge.

Example input > output:

13 > 13

42 >

\$\endgroup\$
  • 10
    \$\begingroup\$ I downvoted this challenge because it is a "Do X without Y" challenge. \$\endgroup\$ – Leaky Nun Jul 11 '17 at 9:32
  • 4
    \$\begingroup\$ What exactly does does nothing otherwise mean? In Haskell a function with type Int -> Int either returns an integer or does not halt or throws some error. If the input is even it should obviously not be returned, so would not halting or throwing an error in this case be acceptable? \$\endgroup\$ – Laikoni Jul 11 '17 at 13:29
  • 2
    \$\begingroup\$ If you wanna know if you can use your source code, try these scripts in your browser console. Create valid charset: alphabet=new Set(Array(256).fill(0).map((v,i)=>i).filter(v=>v%2).map(String.fromCharCode));. Check if source is valid: [..."SOURCE CODE"].every(v=>alphabet.has(v)). See invalid characters in source: [...new Set("SOURCE CODE")].filter(v=>!alphabet.has(v)). Remember to escape your quotes, " => \" ;) \$\endgroup\$ – kamoroso94 Jul 11 '17 at 15:19
  • 14
    \$\begingroup\$ @LeakyNun "do X without Y" doesn't refer to general restricted-source challenges, but challenges that forbid you to use certain features of a language. This challenge isn't that and neither does it have any of the issues that make actual "do X without Y" challenges problematic. \$\endgroup\$ – Martin Ender Jul 12 '17 at 6:29
  • 1
    \$\begingroup\$ @Laikoni As there are some answers already that go into an infinite loop, I would say that it is okay to show such a behaviour. Not returning can be interpreted as doing nothing. \$\endgroup\$ – M.Herzkamp Jul 12 '17 at 14:55

22 Answers 22

1
\$\begingroup\$

Japt -f, 1 byte

u

u has a char-code value of 117.

Try it

\$\endgroup\$
  • 1
    \$\begingroup\$ Late to the party, but my... what an answer! \$\endgroup\$ – M.Herzkamp Aug 22 at 8:25
1
\$\begingroup\$

MathGolf, 3 bytes

¥{k

Try it online!

The used bytes are 157, 123, 107.

Explanation

¥     Input modulo 2
 {    Loop that many times
  k   Push input
\$\endgroup\$
67
\$\begingroup\$

Lenguage, 645529908926937253684695788965635909332404360034079 9394157991500940492270727190763049448735 1174269748937617561533841898064735499551 2293382937520069758100171520285996319 bytes

That's roughly equal to 2 duodecillion bytes.

The file translates to the following brainfuck program:

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

Takes input as an ASCII code, with a maximum value of 256. Uses wrapping.

\$\endgroup\$
  • \$\begingroup\$ @Okx My mistake. But why don't you just use ,[<<+>+>-]<[>>+<<-]+>>[-[->]<]<[<[<]>.<]? \$\endgroup\$ – user202729 Jul 13 '17 at 13:32
  • 1
    \$\begingroup\$ How about ,[[>]++[-<]<+>>]>[<<<.<] for only 3452857532394791089951 bytes? \$\endgroup\$ – Jo King Aug 21 at 3:52
  • \$\begingroup\$ Actually, since infinite loops are allowed, ,-[-->++<]>+. works as well \$\endgroup\$ – Jo King Aug 22 at 2:02
2
\$\begingroup\$

TI-BASIC, 14 bytes

Input Q
sinֿ¹(gcd(Q²,int(e
Q

This throws a domain error (printing nothing to the homescreen) on an even number.

Input Q             Q is a variable with an odd code. We cannot use "Ans" as it's even.

            int(e   floor(e) = 2; "2" is even.
         Q²         Q may be negative, so we square it which preserves parity.
     gcd(Q²,int(e   1 if Q is odd; 2 if Q is even.
sinֿ¹(gcd(Q²,int(e   Error iff Q is even.

Q                   If there was no error, print Q.
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 18 bytes

Saved 2 bytes thanks to lirtosiast

:Prompt Q
:While 5Q/ᴇ1-int(5Q/ᴇ1
:Q

in bytes (+2 newlines = 3F)

:Prompt Q
 DD     51
:While 5Q/ᴇ1-int(5Q/ᴇ1
 D1    35 3B B1  35 3B  
        51 31     51 31
         83 71     83
:Q
 51

See http://tibasicdev.wikidot.com/one-byte-tokens

\$\endgroup\$
  • 1
    \$\begingroup\$ Why not use fPart(? \$\endgroup\$ – lirtosiast Jul 13 '17 at 23:11
  • \$\begingroup\$ @lirtosiast seems to be an even byte (BA = 186) according to tibasicdev.wikidot.com/one-byte-tokens \$\endgroup\$ – Oki Jul 14 '17 at 22:32
  • \$\begingroup\$ Oh, I misread which entries were rows and which are columns. \$\endgroup\$ – lirtosiast Jul 14 '17 at 22:46
  • 1
    \$\begingroup\$ I found Input Q[newline]sinֿ¹(gcd(Q²,int(e[newline]Q (14 bytes). May I post as separate answer? \$\endgroup\$ – lirtosiast Jul 14 '17 at 23:47
  • \$\begingroup\$ @lirtosiast Wow! It's definitely different enough for a seperate answer. Love it \$\endgroup\$ – Oki Jul 15 '17 at 0:11
4
\$\begingroup\$

Cubix, 23 19 17 bytes

;;u!I1)%/;O=!;;/;

Try it!

@, which terminates a Cubix program, is ascii 64, so unfortunately this actually just enters an infinite loop after testing for oddness. No TIO link since it'll time out.

= (ascii 61) is a no-op in Cubix.

This is a slight modification of the prior algorithm (same # of bytes) that actually works for negative integers.

Cube version:

    ; ;
    u !
I 1 ) % / ; O =
! ; ; / ; . . .
    . .
    . .

Algorithm:

  • I (73) : read in input as number
  • 1 (49) : push 1
  • ) (41) : increment
  • % (37) : take the mod
  • / (47) : turn left
  • ! (33) : skip next instruction if odd
    • 1;;/; ; is (59) : prepares the stack for output
    • O (79) : Output as a number.
    • Then it re-enters the algorithm but I reads a 0 for end of input, so we are guaranteed to enter the even branch
  • u (117) : right-hand u-turn
  • ;;;!I : loop, effectively doing nothing.
\$\endgroup\$
  • \$\begingroup\$ @MickyT that fails for negative input, which is why I avoided using ? \$\endgroup\$ – Giuseppe Jul 14 '17 at 12:27
  • \$\begingroup\$ Apologies, I didn't check it properly \$\endgroup\$ – MickyT Jul 14 '17 at 15:14
3
\$\begingroup\$

Perl, 54 bytes

Requires -E.

I really really enjoyed this challenge, I think I'd like to try and improve this answer, but I think that's might be the shortest I can make for now. I've been playing with these answers on and off for a few days now, but feel I'm happy with the 54 byte solution!

s//A_=Y[;A_=A_%O?A_W''/;y/#A-_/#-A/;s/[#-}]+/uc/ee;say

Try it online!

Explanation

By default, most of Perl's string functions work on $_, which is empty to start.

First, s//A_=Y[;A_=A_%O?A_W''/ replaces the empty string in $_ with A_=Y[;A_=A_%O?A_W'', then y/#A-_/#-A/ replaces characters based on the following list (char above becomes char below):

#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
#$%&'()*+,-./0123456789:;<=>?@A

which updates $_ to contain $_=<>;$_=$_%2?$_:''. Next s/[#-}]+/uc/ee replaces all chars [#-}]+ with uc. Without the /ee this would just be the string uc, but /ee evaluates the contents of the string twice. The first evaluation returns the result of uc, which is an uppercase version of $_ but since $_ contains no alphabetic characters, this just returns the whole string, then the second /e evaluates the string again, which sets $_ to either $_ (the input number) or '' depending on whether or not the number is odd or even.

Finally, since $_ now contains what we want, we call say (which is what requires -E instead of -e) which prints $_ followed by a newline.


Alternative Perl, 93 bytes

92 bytes code + 1 for -p, which I feel would make it non-competing.

s//_/g;s//uc/e;y/\x09-\x0b/_/;s/____/}[/;s/___/%C]/;s/_/{''H/;y/'{}1CDGH/'-)1-3+--/;s/[#-}]+/uc/ee

Contains a tab and a vertical tab in the y///, indicated as \x09 and \x0b.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

LOGO, 390 46 52 50 bytes

[ask    1   se[make]iseq    1-3 cos ?/ask   1[1/sum 1   179]?]

That is a template-list that return the input if the input is odd, and cause an error if the input is even.

Usage:

show invoke [ask ... (paste code here) ... ] 5

output

5

since 5 is odd, and

invoke [ask ... (paste code here) ... ] 6

will cause an error because 6 is even.


\$\endgroup\$
0
\$\begingroup\$

Bash, 31 bytes

read a
[ $[a%2] = 0 ] ||echo $a

Try it online!

Explanation:

read a                   reading the input and define it on the variable a
[ $[a%2] = 0 ]      outputs exit status 0 when a/2 is 0 and exit status 1 if not.
||                           evaluates only when the exit status left of it is 1
echo $a                displays the variable a
\$\endgroup\$
  • \$\begingroup\$ You seem to have missed that this is a restricted source challenge - you are using forbidden bytes. (rd $20|h and newline.) \$\endgroup\$ – Ørjan Johansen Jul 13 '17 at 22:58
  • \$\begingroup\$ @ØrjanJohansen fixed I guess \$\endgroup\$ – ADDB Jul 13 '17 at 23:00
  • 2
    \$\begingroup\$ Um no. There is no restriction on the length of the program, but every character in it must have an odd byte value. \$\endgroup\$ – Ørjan Johansen Jul 13 '17 at 23:04
5
\$\begingroup\$

Python REPL, 38 bytes

Takes input as the value of the previous expression using _. Output will be a string (the string representation of the integer for odd, or the empty string for even).

'%s'%[['']+[_]][-1][[_%[1+1][-1]][-1]]

Try it online

To run it in an actual shell, you can try it here. Type the input, hit enter. Paste the code, hit enter.

Explanation:

This took a while to figure out. There's no multiplication, no branching, no slicing, no commas, no periods, no imports, no parentheses, no exec, no eval, no print, and no functions. I got one solution working that output using stderr, but then I realized we had to output the actual integer, not just a truthy/falsey value.

I use brackets in place of parentheses with [expression][-1]. Simplifying that turns the above code into '%s'%(['']+[_])[_%(1+1)].

Since there can be no commas, I used list addition to create ['',_]. Using string formatting, the desired result is obtained.

\$\endgroup\$
  • \$\begingroup\$ Kudos. As a bonus, it also works for Ruby IRB :) Nitpick: is it okay to return a string for odd numbers, is it okay to return an empty string for even numbers? \$\endgroup\$ – Eric Duminil Jul 13 '17 at 20:14
  • \$\begingroup\$ Doing nothing and printing or returning nothing are the same, I'd think. In a way, empty string is nothing. \$\endgroup\$ – mbomb007 Jul 13 '17 at 20:29
  • \$\begingroup\$ Yes, I was really nitpicking. It still returns a string instead of nothing. In Python REPL, I'd say that None would be an even better match than ''. Anyway, that's still 10000 times better than anything I could come up with. \$\endgroup\$ – Eric Duminil Jul 13 '17 at 20:31
  • \$\begingroup\$ @EricDuminil I could return False. Idk how you could get None, though. \$\endgroup\$ – mbomb007 Jul 13 '17 at 20:59
3
\$\begingroup\$

Mathematica, 20 bytes

Appear to be the first solution in non-golfing language.

a=1+1;g[i_/;!a\[Divides]i]=i

In MacintoshChineseTraditional character encoding. \[Divides] is {161, 253} (2 bytes)

Alternative version (23 bytes)

\[CapitalAlpha]=1+1;\[CapitalGamma][\[CapitalChi]_/;\[CapitalAlpha]\[LeftFloor]\[CapitalChi]/\[CapitalAlpha]\[RightFloor]\[NotEqual]\[CapitalChi]]=\[CapitalChi]

or (shown in Unicode)

Α=1+1;Γ[Χ_/;Α⌊Χ/Α⌋≠Χ]=Χ

in Symbol character encoding. (use only 1-byte characters)

The solution defines a function g (or Γ) that, evaluate to input when the input is odd, and literally "do nothing" (does not evaluate) when the input is even.

\$\endgroup\$
  • \$\begingroup\$ +1 byte (; at the end) if the number of byte must be even; and \[Divides] also have odd Unicode codepoint. \$\endgroup\$ – user202729 Jul 12 '17 at 15:30
  • \$\begingroup\$ You don't need I[1+1], you can just use {1+1}. Also, have you checked whether the bytes of the flooring brackets are valid? If so, you can obtain 2 from \[LeftFloor]E\[RightFloor]. \$\endgroup\$ – Martin Ender Jul 12 '17 at 16:09
  • \$\begingroup\$ @MartinEnder Only Symbol make floor valid, but it has neither E nor \[ExponentialE]. \$\endgroup\$ – user202729 Jul 13 '17 at 11:21
  • \$\begingroup\$ I'm afraid you aren't allowed to use the characters D; v; nor d, since they have an even byte-value.. :( \$\endgroup\$ – Kevin Cruijssen Jul 14 '17 at 8:32
  • 1
    \$\begingroup\$ @KevinCruijssen \[Divides] is one single character in Mathematica, which is represented by 2 bytes {161, 253}. \$\endgroup\$ – user202729 Jul 14 '17 at 9:01
5
\$\begingroup\$

Haskell, 36 33 bytes

c[1]=1;c[m]=1+1+c[m-1-1];o	m=c[m]

Try it online!

Usage: o 7 yiedls 7, o 8 enters an infinite loop.

The actual algorithm is

o 1 = 1
o m = 2 + o(m-2)

The first problem I faced was the lack of space and (, because a function o which takes an argument m is usually defined as o m=... or o(m)=.... However I found out that an inline comment {- some comment -} works as token delimiter too, so a definition o{--}m=... is possible under the given rules. Edit: Ørjan Johansen pointed out that one can use a tab character instead of a space, saving three bytes: o m=...

The second problem was the recursive call o(m-2). -2 is just -1-1, but here the comment trick does not work because the parentheses are required. I fixed this by letting the function work on a singleton list containing a number: o[m-2], however, as this is not a standard way of providing input, I outsourced the computation to a helper function c which works on lists and call c from o which has the correct format.

\$\endgroup\$
  • \$\begingroup\$ You can use a tab instead of a space. \$\endgroup\$ – Ørjan Johansen Jul 12 '17 at 16:36
  • \$\begingroup\$ @ØrjanJohansen I can verify that it works with \t instead of {--}. \$\endgroup\$ – Esolanging Fruit Jul 12 '17 at 22:48
  • \$\begingroup\$ @ØrjanJohansen Thanks, that's good to know. \$\endgroup\$ – Laikoni Jul 13 '17 at 7:01
4
\$\begingroup\$

CJam, 6 bytes

q_iY%%

113 95 105 89 37 37

This program takes the mod 2 of the input (call it r) and prints every r th character in the input string. If the input number is odd, it prints the whole string, but if asked to print every 0th character the program throws an error.

Try it here

\$\endgroup\$
  • \$\begingroup\$ Nice! I was going to post qi_Y%]W%{}/M? and then I saw this. \$\endgroup\$ – Esolanging Fruit Jul 12 '17 at 22:40
20
\$\begingroup\$

x86-64 Machine Code, 8 bytes

Inspired by Bruce Forte's solution, but slightly under par. :-)

8D 07          lea      eax, [rdi]    ; put copy of input parameter in EAX
D1 EF          shr      edi, 1        ; shift LSB into CF
             InfiniteLoop:
F3 73 FD       rep jnc  InfiniteLoop  ; test CF; infinite loop back here if input was even
C3             ret                    ; return with original input in EAX if it was odd

A single integer parameter is taken in the EDI register, following the System V AMD64 calling convention.

A copy of this value is initially made, and put into EAX so it can be returned if appropriate. (LEA is used instead of the normal MOV because we need an instruction with odd bytes.)

Then, the value in EDI is shifted right by 1, which places the shifted-off bit into the carry flag (CF). This bit will be 0 if the number was even, or 1 if it was odd.

We then test CF using the JNC instruction, which will branch only if CF is 0 (i.e., the number was even). This means that we will go into an infinite loop for even values. For odd values, we fall through and the original value (in EAX) is returned.

There is a bit of a trick with the JNC instruction, though—it has a REP prefix! Normally, REP prefixes are only used with string instructions, but because the Intel and AMD manuals both agree that irrelevant/superfluous/redundant REP prefixes are ignored, we throw one on the branch instruction here to make it 3 bytes long. That way, the relative offset that gets encoded in the jump instruction is also odd. (And, of course, REP is itself an odd-byte prefix.)

Thank goodness RET is encoded using an odd byte!

Try it online!


In case you don't think returning the value if it's odd or going into an infinite loop if it's even (so that you never return) satisfies the "output" requirements of the challenge, or you just want something more interesting, here's a function that outputs the value to a serial port (but only if it's odd, of course).

x86-64 Machine Code (output to serial port), 17 bytes

8D 07         lea   eax, [rdi]   ; put copy of input parameter (EDI) in EAX

B1 F7         mov   cl, 0xf7     ; put 0xF7 into low-order bits of CX
B5 03         mov   ch, 0x03     ; put 0x03 into high-order bits of CX
FE C1         inc   cl           ; increment low-order bits of CX to 0xF8 (so all together it's now 0x3F8)
0F B7 D1      movzx edx, cx      ; move CX to DX ("MOV DX, CX" would have a 16-bit prefix of 0x66)

D1 EF         shr   edi, 1       ; shift LSB of input parameter into CF
73 01         jnc   IsEven       ; test CF: branch if 0 (even), fall through if 1 (odd)
EF            out   dx, eax      ; output EAX (original input) to I/O port 0x3F8 (in DX)
            IsEven:
C3            ret                ; return

What makes this a bit more interesting is that the code does more, which means it was more challenging to do it all using instructions that are encoded using only odd bytes. Of course, this also means that it fails at code golf, so it's kind of a tradeoff—do you want interesting and challenging, or do you want short?

Anyway, this uses the x86 OUT instruction to write to I/O port 0x3F8, which is the standard COM1 serial port on a PC. The fun part, of course, is that all standard I/O ports (serial and parallel) have even addresses, so they can't simply be encoded as immediates for the OUT instruction or moved directly into a register. You have to initialize with one less than the actual value, and then increment the value in the register. You're also limited to using certain registers for the manipulation because you need registers that are encoded using odd bytes in the instruction when used as operands.

Also, I had to initialize the DX register (via the CX register) at the top of the loop, even though this is only needed if the value is odd, to ensure that the JNC instruction would have an odd offset. However, since what we're skipping over is the OUT instruction, all this code does is waste cycles and clobber scratch registers; it doesn't actually output anything, so it doesn't break the rules.

Finally, this function will return (after having either done or not done the output to the serial port) with the input value left in EAX. But that doesn't actually break any rules; all functions in assembly language will return with a value in EAX—the question is just is it a significant value or a garbage value. That's determined by the function's documentation (essentially, does it return a value or does it return void), and in this case, I'm documenting it as not returning a value. :-)

No TIO link for this one, as it doesn't implement output to serial ports. You'll need real iron, or an imagination.

\$\endgroup\$
  • \$\begingroup\$ +1 for the explanation! Why can you use mov in the second line but not in the first? \$\endgroup\$ – M.Herzkamp Jul 12 '17 at 16:17
  • \$\begingroup\$ In addition to encoding that it's a MOV instruction, its operands also have to be encoded in the bytes. In this case, that would be the source and destination registers (although operands can also be immediate values, which are like constants). Different registers map to different bytes, and some of those bytes are even. So, for example, what I would want to use would be mov eax, edi, but that is 89 F8 in bytes. See way more than you ever wanted to know on encodings here, under the "code" section. @M.Herzkamp \$\endgroup\$ – Cody Gray Jul 12 '17 at 18:34
3
\$\begingroup\$

x86_64 machine code (Linux), 12 11 bytes

Unfortunately 0x80 is even, but it still worked out (assuming "does nothing" means not to return):

0000000000000000 <odd>:
   0:   8d 07                   lea  (%rdi), %eax
   2:   83 e7 01                and    $0x1, %edi
   5:   83 ff 01                cmp    $0x1, %edi
   8:   75 fb                   jne       5  <odd+0x5>
   a:   c3                      retq

-1 byte, thanks @CodyGray!

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Since this is x86-64, lea (%edi),%eax requires an address size override prefix (0x67) when the source is a 32-bit register. You can eliminate that by doing lea (%rdi),%eax. That saves a byte, and actually would make the code slightly more efficient (prefixes slow down decoding and fill up i-cache). There are some other things you can do to shorten this even more, but that essentially constitutes a complete rewrite, so I'll post my own answer. :-) I've upvoted this, too, of course! (Oh, just realized you may have used the prefix to make the jump offset odd. Well, you'll need more magic.) \$\endgroup\$ – Cody Gray Jul 12 '17 at 11:10
  • \$\begingroup\$ Also, really clever solution for TIO! I've been skipping it in most of my asm answers because I didn't want to write all of the boilerplate in the "footer" to print output in asm. I finally broke down and did it the other day, but this is a much better idea, allowing you to use C for everything else. I'll be stealing this from now on! :-) \$\endgroup\$ – Cody Gray Jul 12 '17 at 11:20
  • \$\begingroup\$ Cheers for your inputs, I'm a complete noob at assembly but this seemed easy enough! Curious for your solution. Oh, credit goes to @ceilingcat - I stole it myself ;) \$\endgroup\$ – ბიმო Jul 12 '17 at 11:51
  • \$\begingroup\$ I didn't actually save you a byte… What you have now has an even byte (F6) for the JNE instruction because it has an even offset. That's what I meant by the last parenthetical I edited into my first comment. You actually need that prefix to create an odd alignment. Or you have to rewrite the code in some other way. I played with a bunch of different variations. I actually looked at this last night and was trying to figure out a good way to use OUT to output odd values, but no good solutions really occurred to me. The insight here for me is to go into an infinite loop for even values. \$\endgroup\$ – Cody Gray Jul 12 '17 at 14:08
  • \$\begingroup\$ This is a pretty silly challenge, actually, because none of the reasonable solutions work. It really just tests how well you know the obscure corners of the x86 instruction set. I even tried the BT family of instructions, which nobody uses ever (unfortunately, all register encodings are even bytes). I thought it might be more interesting to write code that output to a serial port, rather than just returning a value or whatever, but that does jack the byte count up there (especially since serial port addresses in hex are even!), so I gave up on it. \$\endgroup\$ – Cody Gray Jul 12 '17 at 14:12
1
\$\begingroup\$

dc, 21 bytes

[c]sa?kKKCI-1;1k%1!=a

Decimal: 91 99 93 115 97 63 107 75 75 67 73 45 49 59 49 107 37 49 33 61 97

As per this IO default, this program leaves the input on the main stack if it is odd and empties the stack otherwise. This can be confirmed by adding the f debug command to the end of the program as is the case on the TIO page.

Try it online!

Explanation

[    # Begin macro definition.
 c   # Clear the main stack.
]sa  # End macro definition and push the macro into register "a".
?k   # Prompt for input and store it into precision parameter "k".
KK   # Push two copies of the input to the main stack.
CI-  # Push C-I = 12-10 = 2 to the main stack  
1;1  # Push the value stored in the first index of array "1" (0 by default) 
     # to the main stack. 
k    # Push 0 into parameter "k" to reset the precision (otherwise the modulus 
     # function will not work correctly).
%    # Push the input (mod 2) to the main stack.
1!=a # If the input (mod 2) does not equal 1, execute the macro in register "a".
f    # OPTIONAL: Output the contents of the main stack.
\$\endgroup\$
19
\$\begingroup\$

Jelly, 2 bytes

ẋḂ

These characters correspond to bytes 0xF7 and 0xBF in Jelly's code page.

Try it online!

\$\endgroup\$
  • 5
    \$\begingroup\$ Would be a shame if this solution with even byte count won the challenge... a 1 byte solution anyone? \$\endgroup\$ – M.Herzkamp Jul 14 '17 at 9:50
1
\$\begingroup\$

Japt, 4 bytes

u)çU

Japt uses ISO/IEC 8859-1, so this corresponds to (in decimal) 117 41 231 85.

Test it online!

Explanation

Uu)çU   Implicit: U = input integer
Uu)     Return Python-style U % (missing argument = 2). This gives 1 if U is odd, 0 if even.
   çU   Implicitly convert U to a string and repeat it that many times.
        Implicit: output result of last expression

I first tried solutions using p, which is basically ç with reversed arguments. However, p performs exponentiation if its left argument is a number, so we'd need to explicitly convert it to a string. This solution turns out to actually be a byte shorter, in addition to not containing any odd bytes.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 9 bytes

¿﹪Iθ÷χ⁵→θ

Try it online!

Basically it prints rightwards the input if it is not a multiple of 10/5 (the ² char is even in the Charcoal code page). The characters used are:

  • ¿: code BF.
  • : code A5.
  • : code C9.
  • θ: code F1.
  • ÷: code AF.
  • χ: code E3.
  • : code B5.
  • : code 13.

In fact the near the end is redundant but then I saw that the answer had to have an odd length...

Explanation:

¿      if (
 ﹪      Modulo (
  Iθ      the input as number,
  ÷χ⁵      χ (predefined value: 10) divided by 5 = 2 ) [is 1])
 →θ      then print rightwards the input as string
\$\endgroup\$
  • \$\begingroup\$ You're printing and then moving right; print rightwards would actually be →θ. \$\endgroup\$ – Neil Jul 11 '17 at 12:11
  • \$\begingroup\$ @Neil oops, fixed. At least that didn't invalidate my answer... :-D \$\endgroup\$ – Charlie Jul 11 '17 at 12:15
9
\$\begingroup\$

05AB1E, 3 bytes

Éi=

The code corresponds to the byte values C9,69,3D or 201,105,61 which are all odd.

Try it online!

Explanation

É     # input % 2 == 1
 i    # if true
  =   # print without popping
\$\endgroup\$
  • \$\begingroup\$ = # print without newline that's actually print with newline without popping \$\endgroup\$ – Erik the Outgolfer Jul 11 '17 at 10:43
  • \$\begingroup\$ @EriktheOutgolfer: True. I'll fix the explanation. \$\endgroup\$ – Emigna Jul 11 '17 at 10:45
  • \$\begingroup\$ Unfortunately, É× doesn't seem to work ): \$\endgroup\$ – kalsowerus Jul 11 '17 at 10:55
  • \$\begingroup\$ @kalsowerus: Yeah, I noticed that as well. Works correctly with a D in front, but that's even and not any shorter. The reason it doesn't work is that the repeated implicit input is taken as 2nd argument. \$\endgroup\$ – Emigna Jul 11 '17 at 11:03
  • \$\begingroup\$ @Emigna but the weird thing is, swapping the top two values on the stack twice, makes it work as well... \$\endgroup\$ – kalsowerus Jul 12 '17 at 11:21
5
\$\begingroup\$

MATL, 3 bytes

o?G

MATL uses ASCII characters, so o?G corresponds to bytes (in decimal) 111, 63, 71.

Try it online!

Explanation

o     % Push (implicit) input modulo 2
?     % If nonzero
  G   %   Push input
      % End (implicit)
      % Display stack contents (implicit)
\$\endgroup\$
1
\$\begingroup\$

Pyth, 14 11 10 9 bytes

I+Q_+K/Q+3_1KQ
I!sIcQ+3_1Q
I!u+1_GQ1Q
?u+1_GQ1k

Test suite.

\$\endgroup\$
  • \$\begingroup\$ That's a clever way, but shouldn't something like I%Qy1Q also work? \$\endgroup\$ – Jakube Jul 12 '17 at 6:42
  • \$\begingroup\$ @Jakube I clearly didn't figure out y1... However, % is not allowed, sadly. \$\endgroup\$ – Leaky Nun Jul 12 '17 at 7:07
  • \$\begingroup\$ Why not? % has ascii value 37 and is odd. \$\endgroup\$ – Jakube Jul 12 '17 at 9:17
  • 3
    \$\begingroup\$ @Jakube because apparently I trusted in this list the OP provided (which is wrong) \$\endgroup\$ – Leaky Nun Jul 12 '17 at 9:30

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.