seul's blog

By seul, history, 16 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

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

is just so cringy.

»
16 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).

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

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

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

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

Not C++ but wild enough

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

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

    Corrected, thank you for pointing out.

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

What about this?

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

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

In python:

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

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

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

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

Ideone

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

Things lockdown making people do.

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

Great

»
5 months 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!

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

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

»
5 months 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"?

  • »
    »
    5 months 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.

  • »
    »
    5 months 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...

  • »
    »
    5 months 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.

»
5 months 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
»
5 months 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;

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

Stop messaging me again and again!! Saale gaandu ab aur msg mt kar mujhe tere chutiye msgs dekh dekhkr pakk gaya hun pareshan karna chod de Main ladki nahi hun chutiye jo ladki smjhkr peeche pda hua hai tu mere!! Ladka hun main aur tu nahi jaanta mujhe main kaun hun tere batch ka bhi nahi hun! Jab tereko maalum bhi hai main kaun hun chutiye toh kyun mujhe apni bakchodi sunaye ja raha hai itne mahine se?? Tere baap ka account ka hai kya jo baar baar msg kr raha hai mujhe ki main use na karun? Apna kaam kar na gaandu seul last warning hai ab band karde warna bohot maarunga

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

One I made, similar to monsoon's:

cout<<(char*)&(const int&)(28494^7539223*CONDITION)<<'\n';
»
2 months ago, # |
  Vote: I like it -31 Vote: I do not like it

seul Are you a school kid? You wrote in my DM that "message me if you have guts", sent me this filmy dialogue " The greatest gladiator fight in the history of mankind or I gave you a chance to walk away in the previous message. Let's see, what you want, peace or war. And this time, I am not playing fair. " And then blocked me from sending messages? Kis delusional duniya mein reh raha hai tu? Also please stop fake messaging me that you've got cancer. Asli logon ko bhi cancer hota hai Don't make fun of people suffering from cancer I've told you so many times I'm not a girl, I'm a guy. I don't know why you're after me like this desperate.

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

    I will take this as a "yes". You threatened to kill me on DM after all. Why would I not chase you?

    MikeMirzayanov please take a look into this.

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

      So it was all a sham? You let him walk away? No forgiveness without an apology. I will take it from here.

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

can someone explain to me how level 3 code works?

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

    It basically works as something like this:

    const char *message = "NO\0YES";
    const char *no_message = message + 0;
    const char *yes_message = message + 3;
    if (CONDITION)
      cout << yes_message << "\n";
    else
      cout << no_message << "\n";
    

    The message string contains two asciiz strings glued together (the "no" part starts at the offset 0 and the "yes" part starts at the offset 3). Then we can see that it's possible to remove the "if" branch and just print message + 3 * CONDITION, which will evaluate to either message + 0 or message + 3 depending on the CONDITION variable. And finally it's possible to get rid of the "message" variable altogether and just use the string literal directly.

    Another similar single-liner trick is indexing an array of strings with two elements:

    cout << (const char *[]){"NO\n", "YES\n"}[CONDITION];
    
»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Amazing