seul's blog

By seul, history, 12 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 HosseinYousefi.

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 ShortestPath)

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

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

is just so cringy.

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

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

»
12 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.

  • »
    »
    12 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.

»
12 months ago, # |
  Vote: I like it +25 Vote: I do not like it
Const
String array
  • »
    »
    12 months ago, # ^ |
    Rev. 5   Vote: I like it +2 Vote: I do not like it
    vector
    map
    set
»
12 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

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

Not C++ but wild enough

"YNEOS"[not CONDITION::2]
  • »
    »
    12 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;
    
»
12 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.

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

    Corrected, thank you for pointing out.

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

What about this?

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

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

In python:

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

»
12 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
»
12 months ago, # |
  Vote: I like it +47 Vote: I do not like it
cout << (char*)&(const int&)(23438671621410638LL >> (CONDITION << 5)) << endl;
»
12 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';  }
»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

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

Ideone

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

Things lockdown making people do.

»
12 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;
»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Great

»
6 weeks ago, # |
  Vote: I like it -62 Vote: I do not like it

seul Launda hun chutiye main, gaandu jaise message karna band karde mujhe ladki samajhkar. Saale saari chutiyon waali poems aur net se churaye hue tere gandu laundon waale one liners pde hote hain inbox mein jab bhi kholta hun ye account. Shakal tatti jaisi teri aur firbhi single rehne ki aadat nahi pdi tujhe 20 saal mein abtk. Mera alt account hai ye ispr problems dekhta hun main bas aur kuch apne bhaiyon ke code pdhta hun friend add krke. Mauj le rahe the teri uss din gaandu. Tabse gaandu jaise message kar raha hai humko ki teri gaand maarli humne, cancer ho jaane ka drama kar raha hai message karke. Saale apni tatti mein doobkr marja par tab bhi jhaant farak na pdta hum sabko. Bhenchod bohot chutiye dekhe zindagi mein par tere jaisa landoora na dekha aajtk. Pareshan karna band kar de chutiye!

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    ... Which language were you speaking...
    Plz use English thx

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Does using these methods improve anything? Is one faster than the other or is it just ways of writing "YES" and "NO"?

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    No, not really. Maybe using putchar() or puts() function can you give some speedup, but usually it is not required.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +7 Vote: I do not like it

    I once heard that using conditional operators (like if, for with condition, while or ?: construction) may slow down your program just a little bit, because your cpu can't predict its result and has to process it separately or something. But I'm not sure...

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +16 Vote: I do not like it

    Yes, they make your code much more readable. It's hard to understand 4 lines with an if block and all that stuff, but everyone understands something short like cout << (char*)&(const int&)(23438671621410638LL >> (CONDITION << 5)) << endl; immediately.

»
6 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In Perl:

Same as Level 4 by ShortestPath:

$condition and print q[YES] or print q[NO] xor print qq[\n]

Another way:

print "YES" x (1 - ! $condition) . "NO" x ! $condition
»
6 weeks ago, # |
  Vote: I like it -14 Vote: I do not like it

define yes cout<<"Yes"<<"\n"

define no cout<<"No"<<"\n"

I use these macros in my template

if(cond) yes; else no;