Numbers that are actually letters

Given a non-negative integer input, write a program that converts the number to hexadecimal and returns a truthy value if the hexadecimal form of the number contains only the characters A through F and a falsey value otherwise.

Test cases

10
==> True (A in hexadecimal)

100
==> False (64 in hexadecimal)

161
==> False (A1 in hexadecimal)

11259375
==> True (ABCDEF in hexadecimal)

0
==> False (0 in hexadecimal)


Bonus: -40 bytes if your program prints Only letters for the challenge described above, Only numbers if the hexadecimal version of the number only contains the digits 0-9 and Mix if the hexadecimal number contains at least one number and at least one letter.

This is code golf. Standard rules apply. Shortest code in bytes wins. Either functions or full programs are allowed.

• Currently drafting an answer in Golfical. Dec 12, 2015 at 20:42
• My current idea: covert to base 16 string, then See if trying to parse that string as a base 10 number returns NaN Dec 12, 2015 at 20:54
• @Cyoce That may work, depending on your choice of language Dec 12, 2015 at 20:56
• Unrealistic bonus (once again): just the string MixOnlynumbersletters is 21 chars Dec 12, 2015 at 21:51
• You say "positive integer input", but 0 is a test case.
– xnor
Dec 12, 2015 at 22:30

Pyth, 43 - 40 = 3 bytes

?&K@J.HQG-JG"Mix"%"Only %sers"?K"lett""numb


Test suite

This achieves the bonus. Only numbers and Only letters fortunately only differ by 4 letters. printf-style formatting is used with %.

The selection system is done by both taking the intersection of the hex with G, the alphabet, and subtracting out G. If neither ends up falsy, it's a mix, while if the intersection is falsy, it's numbers, and if the subtraction is falsy, it's letters.

• Damn this Pyth is weird. Would be very cool if you could get a negative score though. Dec 14, 2015 at 20:55

Pyth, 6 bytes

!-.HQG

.HQ   # Converts the input to hexadecimal
-   G  # Deletes all letters
!       # If empty, output True, else False


Test it here

Jelly, 6 bytes

b16>9P


Try it online!

How it works

b16>9P  Input: z

b16     Convert the input to base 16.
>9   Compare each resulting digit with 9; return 1 iff greater.
P  Take the product of the resulting Booleans.

• Isn't there a built in for 16 or is that not a thing? Apr 20, 2016 at 4:36
• There is now. This was one of the first Jelly answers... Apr 20, 2016 at 4:41

TeaScript, 11 bytes 13 15 16

xT(16)O(Sz)


Pretty simple. This uses TeaScript 2.0. You can get this version from the Github

Explanation

        // Implicit: x = input, Sz = alphabet
xT(16)  // input -> hex
O(Sz)   // Only letters?


Try it online (slightly modified version that works on web)

• Is the down-vote because this is long? Is there something else wrong with this answer that I'm not aware of? Or does someone just not personally like this answer? Dec 12, 2015 at 19:32
• I wouldn't say 16 or 13 bytes is long! Dec 12, 2015 at 19:37
• You have to admit that down-goating your posts is pretty tempting. Dec 12, 2015 at 19:57

Python, 24 bytes

lambda n:min('%x'%n)>'9'


Converts the input a hex string (without 0x prefix) with '%x'%n. Sees if all its chars are greater than '9' (which letters are) by seeing if the min is above '9'.

• I don't know how Python handles characters / numbers, but the ASCII code of '9' is 54. So if you can write ...>54 you can save a byte. Dec 15, 2015 at 10:06
• @CompuChip Python doesn't treat chars or strings as numbers. In fact, Python 2 considers them to be greater than all numbers.
– xnor
Dec 15, 2015 at 10:27

MATL, 10

i16YA1Y2mA


Examples

>> matl i16YA1Y2mA
> 240
0
>> matl i16YA1Y2mA
> 255
1


Explanation

i       % input
16YA    % convert to string representation in base 16
1Y2     % predefined literal: 'A':'Z'
m       % true for set member
A       % all


Bonus challenge: 53−40 = 13

i16YA1Y2mXKA?'Only letters'}Ka?'Mix'}'Only numbers']]


Examples

>> matl
> i16YA1Y2mXKA?'Only letters'}Ka?'Mix'}'Only numbers']]
>
> 255
Only letters

>> matl
> i16YA1Y2mXKA?'Only letters'}Ka?'Mix'}'Only numbers']]
>
> 100
Only numbers

>> matl
> i16YA1Y2mXKA?'Only letters'}Ka?'Mix'}'Only numbers']]
>
> 240
Mix


Explanation

i                       % input
16YA                    % convert integer to string representation in base 16
1Y2                     % predefined literal: 'A':'Z'
m                       % true for set member
XK                      % copy to clipboard K
A                       % all
?                       % if (top of the stack)
'Only letters'        % string literal
}                       % else
K                     % paste from clipboard K
a                     % any
?                     % if (top of the stack)
'Mix'               % string literal
}                     % else
'Only numbers'      % string literal
]                     % end
]                       % end

• hmmm what is that ? seems like matlab got some nuc power now ! Dec 12, 2015 at 20:12
• @Agawa001 Hahaha. Sort of Dec 13, 2015 at 4:14

LabVIEW, 52-40 = 12 LabVIEW Primitives

Praise the built-ins!

C, 4643 37 bytes

Now with more recursion! (Thanks Dennis):

F(x){return(x%16>9)*(x<16?:F(x/16));}


Bonus: even shorter (33 bytes), but fails for x = 0:

F(x){return!x?:(x%16>9)*F(x/16);}


b;F(x){for(b=x;x;x/=16)b*=x%16>9;return b;}


F() takes an int and returns either 0 (false) or non-zero (true).

I didn't even try to achieve the bonus, "MixOnly lettersnumbers" takes 23 bytes alone, tracking the new condition would have required 9 additional bytes, printf() is 8 bytes, which adds up to 40, nullifying the effort.

Test main:

#include <stdio.h>

int main() {
int testdata[] = {10, 100, 161, 11259375, 0};
for (int i = 0; i < 5; ++i) {
int d = testdata[i];
printf("%d (0x%x) -> %s\n", d, d, F(d)?"yep":"nope");
}
}

• What's ?:? I had to replace it with || to get it to compile. Also, can you save a byte by replacing the * with an & thus avoiding the ()s on the left (although you then have to add a space)?
– Neil
Dec 16, 2015 at 0:21
• @Neil a?:b is a GNU extension that evaluates to a if a is a truthy value, otherwise to b. Comes handy to handle null pointers like send(message ?: "(no message)");. I know it's not portable, but code portability is never a concern in code golf :) Dec 17, 2015 at 0:37
• @StefanoSanfilippo you can get this down to 33 bytes by doing this: F(x){x=(x%16>9)*(x<16?:F(x/16));} this abuses a (GCC) bug where if there is no return variable in a function and the main argument was set, it will auto-return the main-argument in some cases (based on what logic was done), and this happens to be one of those cases! Try it online: bit.ly/2pR52UH Apr 22, 2017 at 19:58

Python 3, 30 29 bytes

1 byte stripped thanks to sysreq and Python 3.

lambda n:hex(n)[2:].isalpha()


Simple lambda and slicing.

Perl 6, 18 bytes

{.base(16)!~~/\d/} # 18 bytes


usage:

# give it a name
my &code = {.base(16)!~~/\d/}

for 10, 100, 161, 11259375, 0 {

Ruby, 19 bytes

->n{!('%x'%n)[/\d/]}


Ungolfed:

-> n {
!('%x'%n)[/\d/]
}


Usage:

f=->n{!('%x'%n)[/\d/]} # Assigning it to a variable
f[0]
=> false
f[10]
=> true
f[100]
=> false
f[161]
=> false
f[11259375]
=> true


With bonus, 70 - 40 = 30 bytes

->n{'%x'%n=~/^(\d+)|(\D+)$/;$1?'Only numbers':$2?'Only letters':'Mix'}  Usage: f=->n{'%x'%n=~/^(\d+)|(\D+)$/;$1?'Only numbers':$2?'Only letters':'Mix'}
f[10]
=> Only letters
f[100]
=> Only numbers
f[161]
=> Mix

• Late, but '%x'%n!~/\d/ is a shorter check for solution one, and solution two has a raw byte count of 70, not 75. Jan 26, 2017 at 19:39

Perl, 69 - 40 = 29 bytes

$_=sprintf"%X",<>;print s/\d//?$_?"Mix":"Only numbers":"Only letters"


Ceylon, 55 bytes

Boolean l(Integer n)=>!any(formatInteger(n,16)*.digit);


Straightforward ... we format n as a hexadecimal number (which produces a string), call on each character of that string the .digit number (which returns true if it is a digit), then check whether any of them are true, then negate this.

The version with bonus has a lot higher score of 119 - 25 = 94:

String c(Integer n)=>let(s=formatInteger(n),d=s*.digit)(every(d)then"Only numbers"else(any(d)then"Mix"else"Only letters"));


I'm not sure how anyone could make a bonus version short enough to be better than the no-bonus version, even those strings alone have length 28 together. Maybe a language which makes it really hard to produce a truthy/falsey value.

Here is a formatted version:

String c(Integer n) =>
let (d = formatInteger(n,16)*.digit)
(every(d) then "Only numbers"
else (any(d) then "Mix"
else "Only letters"));


Rust, 70 bytes

fn f(n:i32)->bool{format!("{:x}",n).chars().all(|c|c.is_alphabetic())}


Because, ya know, Java Rust.

It's actually quite elegant, though:

format!("{:x}", n)         // format n as hex (:x)
.chars()                 // get an Iter over the characters
.all(                    // do all Iter elements satisfy the closure?
|c| c.is_alphabetic()  // self-explanatory
)


But it's a shame the function definition boilerplate is so long.... :P

• I have an idea. More closures! Apr 22, 2016 at 15:18

CJam, 44 bytes - 40 bonus = 4 bytes

2,riGbAf/&:-"Only numbersOnly lettersMix"C/=


Try it here~

Seriously, 12 bytes

4ª,¡OkúOkd-Y


Hex Dump:

34a62cad4f6b
a34f6b642d59


Try It Online

It's the same as the other stack language answers. It would be only 7 bytes if Seriously supported string subtraction yet.

EDIT: Seriously now supports string subtraction and the following 7 byte solution now works:

ú4╙,¡-Y


Hex Dump:

a334d32cad2d59


Try It Online

• 11: 4╙,¡#S;ú∩S= (or 4ª or 8τ, lots of ways to spell :16: in two bytes :P)
– user45941
Dec 14, 2015 at 1:06
• I didn't know S I think. Dec 14, 2015 at 18:31

Python 3, 28 bytes

lambda x:min(hex(x)[1:])>'@'


Common Lisp, 40 bytes

(every'alpha-char-p(format()"~x"(read)))


Try it online!

SmileBASIC 3.2.1, 78 bytes

INPUT V FOR I=0 TO 9
IF INSTR(HEX$(V),STR$(I))>-1 THEN ?"FALSE"END
NEXT?"TRUE"

• I don't see any non-ASCII characters here (unless there are some); this counter is the definitive decider and it says 81.
– cat
Dec 12, 2015 at 21:08
• It doesn't matter that the implementation uses UTF-16 internally; to the rest of the world they may as well be UTF-8 chars in the range 0-127, thus your byte count is 81.
– cat
Dec 12, 2015 at 21:10
• Thanks for the feedback, wasn't sure if "bytes" meant I should count 2 per char. 81 it is. Dec 13, 2015 at 18:17
• Actually, the current +10/-0 consensus (which, by the way, I now disagree with) is that we must always use the encoding that an interpreter uses. If you disagree, please post a dissenting opinion on that question. Dec 13, 2015 at 18:25
• @ThomasKwa pfft, okay, oops, now I know better. I didn't realise there was a consensus about that, and I spend a fair bit of time on meta.
– cat
Dec 13, 2015 at 20:57

05AB1E, 2 bytes

Code:

ha


Ha! That is two bytes! Sadly non-competing because this language postdates the challenge :(

Explanation:

h   # Convert input to hexadecimal
a  # is_alpha, checks if the value only contains letters

• hdÔ©1åi•4?ŸâÏ„Î›•}®0åi•4?ŸàpÕTà•}®g2Qi•²•}36B for the bonus resulting in... 6 more! Wooooo bonuses! Jan 24, 2017 at 18:40

Japt, 12 bytes

!UsG r"[a-f]


Try it online!

How it works

!UsG r"[a-f] // Implicit: U = input integer, G = 16
UsG         // Convert U to a base-16 string.
r"[a-f] // Replace all lowercase letters with an empty string.
!            // Take the logical NOT of the result.
// This returns true for an empty string; false for anything else.

• I think !!UsG r"\\d might work and save a byte Dec 13, 2015 at 0:53
• @Downgoat Good observation, but that returns true for any number that contains a letter. Dec 13, 2015 at 1:09

Gema, 41 characters

*=@c{@radix{10;16;*}}
c:<D>=f@end;?=;\Z=t


There is no boolean in Gema, so it simply outputs “t” or “f”.

Sample run:

bash-4.3\$ echo -n '11259375' | gema '*=@c{@radix{10;16;*}};c:<D>=f@end;?=;\Z=t'
t
`