kingmanas's blog

By kingmanas, history, 2 months ago, In English,

Level 1:

if(CONDITION) cout << "YES"; else cout << "NO"; cout << '\n';

Level 2:

cout << (CONDITION ? "YES" : "NO") << '\n';

Level 3:

This one just came to me while reading this amazing blog by Swift.

cout << "NO\0YES" + 3 * CONDITION << '\n';

Maybe be also written as (thanks to Xellos for correction)

cout << &("NO\0YES"[3 * CONDITION]) << '\n';

With a bit of learning curve:

yeputons's one-liner:

puts(reinterpret_cast<const char*>(&(const int&)(218124110 - CONDITION * 01453204765)));

monsoon's one-liner:

cout << (char*)&(const int&)(23438671621410638LL >> (CONDITION << 5)) << '\n';

Level 4:

(by Alperen_T)

CONDITION && cout << "YES" || cout << "NO"; cout << '\n';

Please share other "YES-NO" one-liners you know. Will add it to this list.

 
 
 
 
  • Vote: I like it
  • +61
  • Vote: I do not like it

»
2 months ago, # |
  Vote: I like it +17 Vote: I do not like it
flag==1

is just so cringy.

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by kingmanas (previous revision, new revision, compare).

»
2 months ago, # |
Rev. 2   Vote: I like it +17 Vote: I do not like it

I think you meant in level 3 like this:

cout << "NO\0YES" + 3 * ([CONDITION]) << endl;

Otherwise if condition is true, it prints NO.

  • »
    »
    2 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I updated the post, thank you for pointing it out. I forgot to make CONDITION variable common across all the levels.

»
2 months ago, # |
  Vote: I like it +25 Vote: I do not like it
Const
String array
  • »
    »
    2 months ago, # ^ |
    Rev. 5   Vote: I like it +2 Vote: I do not like it
    vector
    map
    set
»
2 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

theres also

if (CONDITION)
{
cout << "NO";
return 0;
}

if youre checking for universal quantification in a loop

»
2 months ago, # |
  Vote: I like it +73 Vote: I do not like it

Not C++ but wild enough

"YNEOS"[not CONDITION::2]
  • »
    »
    2 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Assuming CONDITION is a variable:

    for (auto c : "YNEOS\n\n") (CONDITION ^= 1) || cout << c;
    
»
2 months ago, # |
  Vote: I like it +5 Vote: I do not like it

cout << "NO\0YES\0"[3 * CONDITION] << endl; doesn't work because [] on const char * only gives you one char, i.e. 'N' or 'Y'. On the other hand, it can be replaced by cout << (3 * CONDITION)["NO\0YES\0"] << endl;.

Don't use endl though unless you need to flush.

»
2 months ago, # |
  Vote: I like it +24 Vote: I do not like it

What about this?

(CONDITION && cout << "Yes") || cout << "No";

»
2 months ago, # |
  Vote: I like it +15 Vote: I do not like it

In python:

print("YNEOS"[1-CONDITION::2])
»
2 months ago, # |
  Vote: I like it +59 Vote: I do not like it

»
2 months ago, # |
Rev. 9   Vote: I like it +3 Vote: I do not like it
Const
String array
Deque
Define
Inline function
switch
Struct
class
define-rangebased-putchar
Another putchar
map
unordered_map
prinf with logical
goto bitwise
»
2 months ago, # |
  Vote: I like it +47 Vote: I do not like it
cout << (char*)&(const int&)(23438671621410638LL >> (CONDITION << 5)) << endl;
»
2 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

A 2-item constant string array declared using a C++ #define macro.

#include <iostream>
using namespace std;

#define answer(ARRAY,NO,YES) const string ARRAY[] = {#NO,#YES}

int main() {
	answer(ans,NO,YES); int n; 
	cin >> n;
	for (int condition, i = 0; i < n; ++i)
		cin >> condition, cout << ans[condition] << '\n';  }
»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

puts(cond ? "YES" : "NO"); // adds new line at end

»
2 months ago, # |
  Vote: I like it +29 Vote: I do not like it
puts(reinterpret_cast<const char*>(&(const int&)(218124110 - CONDITION * 01453204765)));

Ideone

»
2 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Things lockdown making people do.

»
2 months ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it

Well, I think this also works and most people will be messaging me mean things for writing this.

string yes = "YES", no = "NO";
if(condition == true && !condition == false){
   for(char &i: yes)printf("%c",i);
}else if(condition == false && !condition == true){
   for(char &i: no)printf("%c",i);
}
cout << endl;
»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Great