We all know true and false, but what do speakers around the globe say?
+----------------------+------------+------------+
| Language | True | False |
+----------------------+------------+------------+
| Arabic | sahih | zaif |
| Armenian | irakan | kelc |
| Assamese | asol | misa |
| Breton | gwir | gaou |
| Bulgarian | veren | neveren |
| Catalan | veritable | fals |
| Cornish | gwir | gaw |
| Czech | pravdivy | nepravdivy |
| Danish | sand | falsk |
| Dutch | waar | onwaar |
| English | true | false |
| Esperanto | vera | malvera |
| Finnish | tosi | epatosi |
| French | vrai | faux |
| Galician, Portuguese | verdadeiro | falso |
| Georgian | namdvili | cru |
| German | wahr | falsch |
| Greek | alithis | psevdis |
| Hebrew | hiyuvi | shikri |
| Hindi, Urdu | thik | jhutha |
| Hungarian | igaz | hamis |
| Icelandic | sannur | rangur |
| Indonesian, Malay | benar | salah |
| Irish | fior | breagach |
| Italian | vero | falso |
| Japanese | shin | nise |
| Korean | cham | geojit |
| Latin | verus | falsus |
| Latvian | patiess | nepareizs |
| Mandarin Chinese | zhen | jia |
| Maori | pono | pate |
| Persian | dorost | galat |
| Polish | prawdziwy | falszywy |
| Romanian | adevarat | fals |
| Russian | vernyj | falsivyj |
| Sardinian | beru | falsu |
| Scottish Gaelic | fior | breugach |
| Spanish | verdadero | falso |
| Swedish | sann | falskt |
| Sylheti | hasa | misa |
| Turkish | dogru | yanlis |
| Volapuk | veratik | dobik |
| Welsh | gwir | anwir |
+----------------------+------------+------------+
All words ASCIIfied from Wiktionary: true, false. Preference given to first entry under 'A state in Boolean logic that indicates an affirmative or positive result'/'state in Boolean logic that indicates a negative result', then first entry under 'concurring with a given set of facts'/'untrue, not factual, wrong'. I apologise if your favourite language is not included or the word choice for your language is not optimal - go edit Wiktionary!
Write a program or function that takes one word from the table above as input and outputs a consistent truthy value if the word means 'true' and a consistent falsy value otherwise. Your code must produce the correct output for all 79 possible inputs. Shortest code (in bytes) in each language wins.
Sorted list of unique words meaning 'true':
adevarat,alithis,asol,benar,beru,cham,dogru,dorost,fior,gwir,hasa,hiyuvi,igaz,irakan,namdvili,patiess,pono,pravdivy,prawdziwy,sahih,sand,sann,sannur,shin,thik,tosi,true,vera,veratik,verdadeiro,verdadero,veren,veritable,vernyj,vero,verus,vrai,waar,wahr,zhen
Sorted list of unique words meaning 'false':
anwir,breagach,breugach,cru,dobik,epatosi,fals,falsch,false,falsivyj,falsk,falskt,falso,falsu,falsus,falszywy,faux,galat,gaou,gaw,geojit,hamis,jhutha,jia,kelc,malvera,misa,nepareizs,nepravdivy,neveren,nise,onwaar,pate,psevdis,rangur,salah,shikri,yanlis,zaif
12 Answers 12
Python 2, (削除) 56 (削除ここまで) 48 bytes
thanks to dingledooper for -8 bytes!
lambda s:0x420AF14A5F8266>>hash(s)%3317%890%57&1
All these answers do the same thing:
Convert the string into an unique integer.
Makes these integers smaller by repeated modulo operations. These operations are bruteforced to make the numbers as small as possible while not mixing up the two classes.
Index into a binary lookup table.
Python 3, (削除) 59 (削除ここまで) 58 bytes
lambda s:0x48A2D06199310566F06>>int(s[:4],36)%542%400%78&1
lambda s:0x453CCA1066840810431C1>>int(s,36)%2387%1770%86&1
lambda s:0x42744262AEA01A914800A12C>>int(s,36)%155687%95&1
05AB1E, (削除) 30 (削除ここまで) (削除) 29 (削除ここまで) 28 bytes
4öŽ31%ƵTM%84%o•1±87÷Јù3Í:•&Ā
6öŽ9{%521%76%o•B&¦¿3ʒв F6•&Ā
-
1\$\begingroup\$ How did you find the equation? \$\endgroup\$PkmnQ– PkmnQ2020年05月31日 07:22:29 +00:00Commented May 31, 2020 at 7:22
-
5\$\begingroup\$ @PkmnQ bruteforcing, a lot of it. And I need to try a lot of different thing, like multiplying the integer by some constant, different ranges for the modulo, different numbers of modulo operations, ... \$\endgroup\$ovs– ovs2020年05月31日 07:30:52 +00:00Commented May 31, 2020 at 7:30
-
\$\begingroup\$ Can't you remove the [:4]? (and change the modulus values) \$\endgroup\$user202729– user2027292020年05月31日 08:30:02 +00:00Commented May 31, 2020 at 8:30
-
5\$\begingroup\$ I managed to get 48 bytes. \$\endgroup\$dingledooper– dingledooper2020年05月31日 18:07:52 +00:00Commented May 31, 2020 at 18:07
-
2\$\begingroup\$ @ovs Feel free to update your current Python answer with the 48-byte one :). \$\endgroup\$dingledooper– dingledooper2020年06月01日 22:08:33 +00:00Commented Jun 1, 2020 at 22:08
Ruby -n, (削除) 54 (削除ここまで) (削除) 49 (削除ここまで) 47 bytes
p !/^[fgmryz]a|[ncks][erw]|[bjm][hir]|ep|la|te/
Thanks to Dingus for a byte saved and Value Ink for inspiring another -2.
-
3\$\begingroup\$ @Dingus
$_!~/^reads more like Perl than Ruby to me :) \$\endgroup\$the default.– the default.2020年05月31日 13:09:21 +00:00Commented May 31, 2020 at 13:09 -
\$\begingroup\$ Currently the shortest regex. Nice! \$\endgroup\$Dingus– Dingus2020年05月31日 13:59:32 +00:00Commented May 31, 2020 at 13:59
-
1\$\begingroup\$ You should list the language as "Ruby
-n" or something like that. codegolf.meta.stackexchange.com/a/14339 Also, I'm personally a fan ofp ! ~/...even though the bytecount doesn't change. \$\endgroup\$Value Ink– Value Ink2020年05月31日 23:33:49 +00:00Commented May 31, 2020 at 23:33 -
\$\begingroup\$ @ValueInk, thanks for reminding, I wish TIO template would include flags automatically, as I keep forgetting them. \$\endgroup\$Kirill L.– Kirill L.2020年06月01日 06:40:58 +00:00Commented Jun 1, 2020 at 6:40
-
\$\begingroup\$ BTW, this juggling with output methods actually yielded another -2 bytes \$\endgroup\$Kirill L.– Kirill L.2020年06月01日 07:00:54 +00:00Commented Jun 1, 2020 at 7:00
Retina, (削除) 68 (削除ここまで) (削除) 67 (削除ここまで) (削除) 60 (削除ここまで) 58 bytes
^(n?a[^n]|be|ch|gw|p[or]|sa[hn]|h?[itvw]|zh)|as|ss|in|og?r
Regex that matches all truthy values and none of the falsey ones.
-
3\$\begingroup\$ Would it be shorter to match falsy inputs instead? (they seem to have a very common substring
fa) (I'm not sure whether you can output inverted, probably not) \$\endgroup\$the default.– the default.2020年05月31日 06:38:33 +00:00Commented May 31, 2020 at 6:38 -
-
\$\begingroup\$ If so, this works for 54 bytes (I won't be surprised if there's a shorter way to invert the regex, though) (assuming Arnauld is okay with their regex being stolen) \$\endgroup\$the default.– the default.2020年05月31日 09:26:08 +00:00Commented May 31, 2020 at 9:26
-
-
\$\begingroup\$ @Arnauld Thanks, but I feel that updating my answer now would make it too similar to the other answers that use regex (currently yours and the Ruby answer) \$\endgroup\$math junkie– math junkie2020年05月31日 14:32:44 +00:00Commented May 31, 2020 at 14:32
C (gcc), (削除) 55 (削除ここまで) 52 bytes
-3 bytes thanks to @G. Sliepen
f(s){s=0x4240165C085F34>>a64l(s)%19537U%11702%56&1;}
The strategy used is the same as in @ovs's answer. We brute-force values corresponding to each string, making sure that no two truthy and falsey words share the same value. The answer is then extracted from a binary lookup table.
Here, the a64l() function converts a given string into a 32-bit signed integer.
-
\$\begingroup\$ "32-bit unsigned integer" should that be "signed"? (so you have to use a byte (
U) to convert it to unsigned? \$\endgroup\$user202729– user2027292020年06月01日 10:49:37 +00:00Commented Jun 1, 2020 at 10:49 -
\$\begingroup\$ @user202729 I double-checked, and you are correct. Fixed now. \$\endgroup\$dingledooper– dingledooper2020年06月01日 16:55:53 +00:00Commented Jun 1, 2020 at 16:55
-
\$\begingroup\$ 52 bytes by converting it to a function:
f(s){s=0x4240165C085F34>>a64l(s)%19537U%11702%56&1;}\$\endgroup\$G. Sliepen– G. Sliepen2020年06月01日 19:09:00 +00:00Commented Jun 1, 2020 at 19:09 -
\$\begingroup\$ @G.Sliepen That is interesting. Can you explain why the
sparameter doesn't need to be declared withchar*? \$\endgroup\$dingledooper– dingledooper2020年06月01日 19:32:46 +00:00Commented Jun 1, 2020 at 19:32 -
4\$\begingroup\$ If you don't declare it, it becomes an
int. Since you didn't#include <stdlib.h>, the compiler doesn't know thata64l()expects a string, so it just passes on the "integer"s.a64l()in turn interprets it as a pointer again. The trick with assigning the result toscauses the result to also become the return value due to the calling conventions. Of course these techniques should only ever used for Code Golf :) \$\endgroup\$G. Sliepen– G. Sliepen2020年06月01日 21:39:25 +00:00Commented Jun 1, 2020 at 21:39
x86-64 machine code, 27 bytes
Hexdump:
6b 01 35 c1 e8 06 6b c8 d3 d1 c1 48 ba 4e 88 00
02 c3 45 88 8b 48 d3 e2 1a c0 c3
A function which receives a pointer to the string in rcx, and returns the result in al.
−1 means true, and 0 means false.
Assembly source code, using ml64 (MASM) syntax:
.CODE
my PROC
imul eax, dword ptr[rcx], 53
shr eax, 6
imul ecx, eax, -45
rol ecx, 1;
mov rdx, 8b8845c30200884eh;
shl rdx, cl;
sbb al, al;
ret;
my ENDP
end
Disassembly, while stopped on a breakpoint at the start of the function:
00007FF73978F4A0 6B 01 35 imul eax,dword ptr [rcx],35h
00007FF73978F4A3 C1 E8 06 shr eax,6
00007FF73978F4A6 6B C8 D3 imul ecx,eax,0FFFFFFD3h
00007FF73978F4A9 D1 C1 rol ecx,1
00007FF73978F4AB 48 BA 4E 88 00 02 C3 45 88 8B mov rdx,8B8845C30200884Eh
00007FF73978F4B5 48 D3 E2 shl rdx,cl
00007FF73978F4B8 1A C0 sbb al,al
00007FF73978F4BA C3 ret
It uses hashing, like many other answers. The hash function uses the first 4 bytes of the string - by luck, all strings are at least 4 bytes long (including terminating zero byte). It does the following:
- Multiply by 53, ignoring overflow
- Shift right by 6 bits
- Multiply by -45, ignoring overflow
- Rotate left by 1 bit
- Access the 64-bit hash table, using 6 LSB of the result
Found by brute-force search. The search space was 8 +たす 5 +たす 8 +たす 5 =わ 26 bits. The "rotate left" bit count is 1 by luck, which reduces code size by 1 byte, compared to the general "rotate left" case.
-
-
1
-
\$\begingroup\$ I'm impressed that x86 is able to beat all of the golfing languages. Clearly our encodings are not good enough yet! \$\endgroup\$Mario Carneiro– Mario Carneiro2020年06月06日 01:25:40 +00:00Commented Jun 6, 2020 at 1:25
-
\$\begingroup\$ I suspect all the golfers just didn't try hard enough. user202729 suggests that there is a short answer in Jelly. \$\endgroup\$anatolyg– anatolyg2020年06月06日 05:41:39 +00:00Commented Jun 6, 2020 at 5:41
Brachylog, 70 bytes
¬{~ṇ"pate
shik
dob
sal
ham"∧"nezabrcrpsangagenifa"ġ2;?,"yeojrkm"∋∋~a0}
Takes input through the output variable and outputs through success or failure.
Charcoal, 41 bytes
¬∨=θgaw⊙⪪"&⌈→⊖L↓&s⦃R9CV÷⊕O⸿↔Vf′′′λ⌕9↶7"2Noθι
Try it online! Link is to verbose version of code. Output is a Charcoal boolean, i.e. - for true, nothing for false. Explanation:
θ Input string
= Equals
gaw Literal string `gaw`
∨ Boolean Or
"..." Compressed string `bibrcrepfagujhjikekrlamaminenwouseteyaza`
⪪ 2 Split into substrings of length 2
⊙ Where any is nonzero
No Count of
ι Current substring in
θ Input string
¬ Boolean Not
Implicitly print
JavaScript (ES6), (削除) 63 62 (削除ここまで) 60 bytes
Saved 2 bytes thanks to @Neil
A regular expression that matches all falsy words and none of the truthy ones.
s=>!/ao|mi|ob|w$|[gnst]e|[bck]r|[flz]a|^[ejkmry]|nw/.test(s)
05AB1E, (削除) 41 (削除ここまで) 39 bytes
.•6ðó_ ï2£Ëý3⁄4Sð7§Ê3®6 ́¡Žmã•2ôåàI...gawQ~≠
-2 bytes by using a shorter compressed string from @Neil's Charcoal answer, who apparently uses the exact same approach.
Try it online or verify all test cases.
Explanation:
.•6ðó_ ï2£Ëý3⁄4Sð7§Ê3®6 ́¡Žmã•
# Push compressed string "bibrcrepfagujhjikekrlamaminenwouseteyaza"
2ô # Split it into parts of size 2
å # Check for each whether it's a substring of the (implicit) input-string
à # And check if any is truthy
I # Push the input again
...gawQ # Check that it's equal to string "gaw"
~ # Check if either of the two is truthy by using a bitwise-OR
≠ # And invert the boolean (!= 1)
# (after which the result is output implicitly)
See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•6ðó_ ï2£Ëý3⁄4Sð7§Ê3®6 ́¡Žmã• is "bibrcrepfagujhjikekrlamaminenwouseteyaza".
-
\$\begingroup\$ What was your previous compressed string? The edit history is unenlightening... \$\endgroup\$Neil– Neil2020年06月02日 10:26:55 +00:00Commented Jun 2, 2020 at 10:26
-
\$\begingroup\$ @Neil Oh, I edited a few seconds too soon apparently before the grace period of 5 minutes was over.. -_-' It was "aobrcreleoepfahuiaifkrlamaminengninlnwobpste" (
.•bÙвfƒôSÐ4Ólα$šη€mªδP‚Á¥PnÀ•) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年06月02日 10:33:41 +00:00Commented Jun 2, 2020 at 10:33 -
\$\begingroup\$ So the uncompressed string was the same, but you just found a better way of compressing it? Or have I missed something? \$\endgroup\$Neil– Neil2020年06月02日 11:02:29 +00:00Commented Jun 2, 2020 at 11:02
-
\$\begingroup\$ @Neil Now it probably makes more sense.. Not sure how I ended up copying the wrong strings to my answer (the TIOs were correct..) It went from
.•bÙвfƒôSÐ4Ólα$šη€mªδP‚Á¥PnÀ•/"aobrcreleoepfahuiaifkrlamaminengninlnwobpste" to.•6ðó_ ï²£Ëý¾Sð7§Ê³®6´¡Žmã•/"bibrcrepfagujhjikekrlamaminenwouseteyaza" \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年06月02日 11:29:40 +00:00Commented Jun 2, 2020 at 11:29 -
\$\begingroup\$ Ah, thanks, it's good to know that my list of falsy letter pairs is pretty short! \$\endgroup\$Neil– Neil2020年06月02日 11:57:45 +00:00Commented Jun 2, 2020 at 11:57
Io, 109 bytes
-10 bytes thanks to Neil.
method(x,"bi br cr ep fa gu jh ji ke kr la ma mi ne nw ou se te ya za gaw"split select(i,x findSeq(i))size<1)
Io, 119 bytes
Searches for prefixes of existing values.
method(x,"dob fa ga ham an br cr e pate ge j k ma mi ne ni on ps sal shik ra ya za"split select(i,x findSeq(i)==0)size)
-
\$\begingroup\$
method(x,"bi br cr ep fa gu jh ji ke kr la ma mi ne nw ou se te ya za gaw"split select(i,x findSeq(i))size<1)is 109 bytes, and also gives the correct truthiness. \$\endgroup\$Neil– Neil2020年05月31日 10:36:39 +00:00Commented May 31, 2020 at 10:36
C (gcc), (削除) 125 (削除ここまで) \$\cdots\$ (削除) 101 (削除ここまで) 95 bytes
Saved a byte thanks to ceilingcat!!!
#define f(s)!index(" %&(-.049;ADHJQRSYZis",*s**s*s[l=strlen(s)-1]*s[l-1]%3519%163%108%92+32)
l;
Inputs a string and returns \1ドル\$ for words meaning 'true' and \0ドル\$ for words meaning 'false'.
How?
The first, second to last, and last characters of all the word strings form a unique triplet of characters across all words. Multiplying the ASCII values of first character squared and the other two together yields unique 32-bit integers across all words. These numbers modulus values found by a Python script yield a distinct set of integers for all 'false' words in the range \$(0,96)\$. These numbers can then be transformed back to printable ASCII characters by adding \32ドル\$ to them. Then it's simply a test if a string put through these calculations yields a character that can be found in a given string (also generated by the Python script).
truthyorfalsy. \$\endgroup\$truthyandfalsy. You're not required to output the literal strings 'truthy' and 'falsy'. I'll clarify in the question. \$\endgroup\$