Single letter HTML
Indicate whether a letter is an HTML element.
Input
- A single lowercase letter.
Output
- An indication of whether the input is a standard HTML element.
- This could be one of 2 distinct values to indicate true or false, or any truthy or falsy value if your programming language supports that.
- You may choose whether truthy or falsy indicates an HTML element.
Test cases
Test cases are in the format input : output.
These are correct at time of posting, and will remain the measure of validity for answers to this challenge even if the HTML specification changes in future.
a : true
b : true
c : false
d : false
e : false
f : false
g : false
h : false
i : true
j : false
k : false
l : false
m : false
n : false
o : false
p : true
q : true
r : false
s : false
t : false
u : false
v : false
w : false
x : false
y : false
z : false
Scoring
This is a code golf challenge. Your score is the number of bytes in your code.
Explanations are optional, but I'm more likely to upvote answers that have one.
Python, 20 bytes ``` lambd …
5mo ago
[C (gcc)], 31 30 bytes …
5mo ago
Haskell + hgl, 9 bytes ```t …
4mo ago
awk, 12 bytes Print nothing …
4mo ago
Perl, 9 bytes ``` /[abipq] …
2mo ago
sed, 12 bytes Print nothing …
4mo ago
Japt, 7 bytes Returns the i …
4mo ago
sed, 16 bytes /[abipq]/ …
5mo ago
Vyxal 3, 6 bytes ``` " |. …
5mo ago
Retina, 9 bytes ``` a`[abi …
5mo ago
10 answers
Python, 20 bytes
lambda _:_ in'abipq'
Same approach as the others, standard Python golf techniques.
0 comment threads
C (gcc), 31 30 bytes
#define f(a) strchr("abipq",a)
f(a){return strchr("abipq",a);}
The most obvious (and non-creative) solution. This under default rules that a truthy value in the language is anything non-zero. To display explicitly 1 or 0 would require two !! before the strchr call.
(Passing a as pointer and returning value through it gives me worse golf results.)
EDIT: function-like macros are generally less ideal than functions for golf but in this case it shaves one character.
0 comment threads
Haskell + hgl, 9 bytes
fe"abipq"
This is the obvious solution and it's pretty much unbeatable. I also tried this solution avoiding the hardcoded string:
27 bytes
('x'>)+fe[7,8,15]<dgs 16<Or
-
Or: convert to character point. -
dgs 16: take the digital sum base 16. -
fe[7,8,15]: check if it is either 7, 8, or 15. -
('x'>): check the input is also less than'x'.
Obviously this approach is nowhere near as short as the obvious approach but it's a little fun. (Anyone want to suggest adding <x> to the HTML standard?)
Reflection
- There is the enduring issue that
feis way more useful thane. There names should be swapped. This would save a byte in both answers. -
(+)as a lifted and exists, and there is a lifted or, but it's way longer.(*)should act as a lifted or. Meaning at the very leastPredicate ashould have aRiginstance. If this were the case we could do:
Which saves 3 bytes in the long answer.eq 'i'*fe[7,8]<dgs 16<Or - There could be hard-coded variants for common digital sums. It's pretty much random chance that the number used here (16) is a power of two, but numbers from 2-11 plus 16, 32, and 64 are probably wise to have.
0 comment threads
awk, 12 bytes
Print nothing if false, 1 if true
$0=/[abipq]/
- replace input with result of match
- no match: assigns false (empty string) ⇒ default print action is skipped
- match: assigns true (stringifies to
1) ⇒ default print action is run
0 comment threads
sed, 12 bytes
Print nothing if true; newline if false.
/[abipq]/d;x
- if match, delete and go to next line of input
- else swap input with empty hold space and print (gives newline)
0 comment threads
Perl, 9 bytes
/[abipq]/
Perl's got an m// operator (which you also get if you use a bare regex, like here) that by default matches on $_. Returns true if it matches, false if it doesn't.
The regex itself isn't anything special.
If you wanted to actually use it, you could do something like say "is HTML" if /[abipq]/;.
0 comment threads
Vyxal 3, 6 bytes
"
|.„∩
Returns an empty string if the input is not a single letter element. Returns the input otherwise.
Simply perform intersection with the string "abipq". "<newline>|.„" is a base-252 compressed string.
0 comment threads
sed, 16 bytes
/[abipq]/{c
q};d
Prints a newline for true and an empty string for false.
Annotated
/[abipq]/{ # run grouped commands for lines matching [abipq]
c # change current buffer with remainder of this line (i.e. \n)
q # quit
}; # start new command
d # delete current buffer

0 comment threads