What are the difficult tasks done or problems solved in the simplest and shortest line(s) of code?

Let’s start with this super ultra useful program every one has ever written.

  1. #include<stdio.h>
  2. int main()
  3. {
  4. printf (“Hi Quora\n”);
  5. return 0;
  6. }

We can then go to the classic C string copy routine

  1. while (*d++ = *s++);

A really interesting code is the Duff’s device. This is the so called Loop unrolling

  1. void send(short *to, short *from, int count)
  2. {
  3. int n = (count +7 ) / 8;
  4. switch (count % 8)
  5. {
  6. case 0: do { *to = *from++;
  7. case 7: *to = *from++;
  8. case 6: *to = *from++;
  9. case 5: *to = *from++;
  10. case 4: *to = *from++;
  11. case 3: *to = *from++;
  12. case 2: *to = *from++;
  13. case 1: *to = *from++;
  14. } while( — n > 0);
  15. }
  16. }

*to is memory-mapped output register. This is why it isn’t incremented. Formemory-to-memory copy *to++ should be used.

And something easy, but really clean that I really like. Swapping 2 vars

  1. int a,b;
  2. b=a-b;
  3. a=a-b;
  4. b=a+b;

It’s time for this ultra mega super cool code by John Carmack. Fast inverse square root

  1. float Q_rsqrt( float number )
  2. {
  3. long i;
  4. float x2, y;
  5. const float threehalfs = 1.5F;
  6. x2 = number * 0.5F;
  7. y = number; i = * ( long * ) &y; // evil floating point bit level
  8. hacking i = 0x5f3759df — ( i >> 1 ); // what the ****?
  9. y = * ( float * ) &i;
  10. y = y * ( threehalfs — ( x2 * y * y ) ); // 1st iteration
  11. // y = y * ( threehalfs — ( x2 * y * y ) ); // 2nd iteration, this can be removed
  12. return y;
  13. }

Enough C++. What about a little Python

Decode a base64 encoded file

  1. import base64, sys;
  2. base64.decode(open(sys.argv[1], “rb”), open(sys.argv[2], “wb”))

CSV file to json

  1. import csv,json;
  2. print json.dumps(list(csv.reader(open(‘csv_file.csv’))))

Compress CSS file

  1. import re,sys;
  2. print re.sub(“\s*([{};,:])\s*”, “\\1”, re.sub(“/\*.*?\*/”, “”, re.sub(“\s+”, “ “, sys.stdin.read())))

Well everything is one line with Python. Let’s get back to C++.

Design patters? What about Singleton

  1. class Singleton
  2. {
  3. public:
  4. static Singleton& getInstance()
  5. {
  6. static Singleton instance;
  7. return instance;
  8. }
  9. private:
  10. Singleton() {}
  11. };

You wanna compose music with code. Try this:

Herb Sutter favorite C++ 10-liner as “a thread-safe reference-counted object cache

  1. shared_ptr<widget> get_widget(int id)
  2. {
  3. static map<int, weak_ptr<widget>> cache;
  4. static mutex m;
  5. lock_guard<mutex> hold(m);
  6. auto sp = cache[id].lock();
  7. if (!sp) cache[id] = sp = load_widget(id);
  8. return sp;
  9. }

And again Swapping 2 vars, but with XOR :D

  1. a = a xor b;
  2. b = a xor b;
  3. a = a xor b;

And The Brainfuck Programming Language interpreter

  1. #include <iostream>
  2. #include <string>
  3. int main()
  4. {
  5. int mp = 0, memory[1024] = {0}, ip, layer; std::string code; while( ip = -1, std::getline( std::cin, code ) && code != “quit” ){ while(++ip < code.size() && (memory[ mp += code[ip] == ‘>’ ? +1 : code[ip] == ‘<’ ? -1 : 0 ] += code[ip] == ‘+’ ? +1 : code[ip] == ‘-’ ? -1 : 0, 1)) if( layer = code[ip] == ‘[‘ ? +1 : code[ip] == ‘]’ ? -1 : 0, code[ip] == ‘[‘ && memory[mp] == 0 || code[ip] == ‘]’ && memory[mp] != 0 ) while(ip += memory[mp] == 0 ? +1 : -1, !( (layer += code[ip] == ‘[‘ ? +1 : code[ip] == ‘]’ ? -1 : 0) == 0 && ( code[ip] == ‘]’ && memory[mp] == 0 || code[ip] == ‘[‘ && memory[mp] != 0 ) ) && ip < code.size()); std::cout << mp << “ : “ << memory[mp] << std::endl; }
  6. return 0;
  7. }

For beginners let’s reduce the if-else with ternary operator

  1. x = (y < 0) ? 10 : 20;

Happy holidays :)

P.S.
I wrote this originally as answer in Quora