…meie igapäevast IT’d anna meile igapäev…

2015-05-13

Some fun with FizzBuzz in C#

Filed under: Isiklikud,Programmeerimine — Sander @ 13:34:03
Tags: ,

image As a very first thing, I must say that I don’t like whiteboard coding exercises during the job interviews.

These exercises have almost never any relation to real-life programming problems (“Reverse a linked list?” If your reply is anything besides “linkedList.Reverse();”, your answer is wrong.). There are only a few dozen common whiteboard coding questions – and every candidate with a common sense will review those before the interview.

Finally, success with whiteboard coding seems to have next to no connection with the actual qualities of the candidate. And yes, this includes “solving a problem under pressure”.

I much prefer to give the candidates a small task, to do at home in 2..3h. This will give me much more information – what is his coding style? Can he write high-quality and well-commented code? Did he actually manage to finish all parts of the task – and if not, then why? Does he even want the position enough to do a proper job?

That all said, FizzBuzz does have one good use – it can be used to quickly weed out the candidates who cannot program at all. If they cannot do a simple logical exercise, and write a loop with a couple of if-clauses inside it, they really shouldn’t be hired as developers.

That is why ex-coworker Erti-Chris and I were rather surprised after reading “Tales of a Non-Unicorn: A Story About The Trouble with Job Titles and Descriptions” (reddit thread). A person claiming to be “designer/developer if there ever was one” goes “OMG MATH” and cannot solve FizzBuzz (which has almost nothing to do with math)?!

Out of boredom we started to play around with FizzBuzz – albeit in C#, not JS (but all our ideas work with Javascript as well, with a little prodding) – first the “standard solution” and then limiting ourselves further and further.

The “standard solution” using modulus:

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {
  3.     var d3 = x % 3;
  4.     var d5 = x % 5;
  5.  
  6.     Print(d3 == 0, d5 == 0, x);
  7. });

Method Print is used repeatedly, it is the same everywhere:

  1. private static void Print(bool three, bool five, int i)
  2. {
  3.     if (three && !five)
  4.         Console.WriteLine("Fizz");
  5.     else if (five && !three)
  6.         Console.WriteLine("Buzz");
  7.     else if (five)
  8.         Console.WriteLine("FizzBuzz");
  9.     else
  10.         Console.WriteLine(i);
  11. }

But what if we don’t allow modulus to be used?

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {
  3.     var d3 = x / 3f == Math.Round(x / 3f);
  4.     var d5 = x / 5f == Math.Round(x / 5f);
  5.  
  6.     Print(d3, d5, x);
  7. });

But what about not using neither round nor modulus?

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {
  3.     var d3 = x / 3 * 3 == x;
  4.     var d5 = x / 5 * 5 == x;
  5.  
  6.     Print(d3, d5, x);
  7. });

(and also the same limitations as above, nutcase version. Proudly mine).

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {            
  3.     var d3 = !(x / 3f).ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  4.     var d5 = !(x / 5f).ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  5.  
  6.     Print(d3, d5, x);
  7. });

Still too easy. What about not allowing modulus, rounding, multiplication and division?

  1. for (int i = 0, three = 0, five = 0; i < 100; i++, three++, five++)
  2. {
  3.     if (three == 3 && five == 5)
  4.     {
  5.         Console.WriteLine("FizzBuzz");
  6.         three = 0;
  7.         five = 0;
  8.     }
  9.     else if (three == 3)
  10.     {
  11.         Console.WriteLine("Fizz");
  12.         three = 0;
  13.     }
  14.     else if (five == 5)
  15.     {
  16.         Console.WriteLine("Buzz");
  17.         five = 0;
  18.     }
  19.     else
  20.         Console.WriteLine(i);
  21. }

Amusingly, the last version is fastest of them all.

And by now we were out of time and bored of this. It was fun, though – a sort of minor coding kata.

All the code above can be seen and executed in dotNetFiddle, https://dotnetfiddle.net/84PodI

2014-04-24

Eesti-inglise sõnastiku uusversioon

Sain valmis uue versiooni ome Eesti-inglise sõnastikust – idee poolest siis juba v4.0 (v2, v3). Muuhulgas uuenes ka aadress – dukelupus.com/dict asemel on dukelupus.net/dict. Vana versioon jääb esialgu samuti alles, kuivõrd uus versioon ei pruugi vanade brauseritega koostööd teha.

image

Eesmärkideks uue versiooni loomisel olid mobiilseadmete-sõbralikkus ja kiirus. Viimasel juhul eelkõige lehe laadimise aeg – ehk mahu ja päringute arvu vähendamine, ehkki ka otsing läks veerandi võrra kiiremaks. Tulemus – Google PageSpeed Insights annab nii mobiili- kui ka desktopi-lehele sada punkti sajast.

Võrdluseks väike tabel (GA on Google Analytics’ skript; suurus on reaalne võrgus liigutatavate andmete maht, mis tänu GZIP’imisele on väiksem kui suurus brauseris).

  dukelupus.com/dict dukelupus.net/dict dukelupus.net/dict/m
Lehe laadimine (suurus) 101.1KB (15.4KB GA) 43.2KB (10KB GA) 13.1KB (10KB GA)
Lehe laadimine 1.94s 1.68s 964ms
Lehe taaslaadimine (suurus) 15KB 4.6KB 2.1KB
Lehe taaslaadimine (aeg) 839ms 196ms 190ms
Otsingu tulemus (suurus) 1.4KB 777B 565B
Otsingu aeg 305ms 240ms 224ms

Mis on muutunud?

  • Mobiiliversioonist loe allpoolt
  • HTML5, CSS3.
  • Sõnastik ise pole enam tõlgitav
  • Veelgi minimalistlikum kasutajaliides. Suuna vahetuse raadionuppude asemel on nüüd kaks otsingunuppu.  Suunda saab endiselt TAB-klahvi abil muuta.
  • Serveripoolse otsingu aja asemel näidatakse koguaega, st. lisanduvad võrgu latentsus jm.
  • JSONP’i pole enam (loe selle lisamisest eelmisele versioonile). Seda ei kasutatud – kui aga keegi peaks soovima, siis saab lihtsalt nii CORS kui ka JSONP toe lisada.
  • jQuery UI autocomplete asemel on HTML5 <datalist> element. Kahjuks käitub see aga eri brauseritel täiesti erinevalt, ning võimalik et olen sunnitud jQuery UI peale tagasi minema.
  • Sõnapaare on veidi puhastatud, samuti natuke termineid lisatud.

Mobiiliversioon

image

Mobiili või pihuarvutit kasutavad sõnastiku kasutajad suunatakse automaatselt väiksema resolutsiooniga seadmete lehele, kuid võimalik on kasutada ka otseteed – http://dukelupus.net/dict/m.

Play Store’s on nüüdseks vist pea kümneid eesti-inglise sõnastikke – miks ma siis spetsiaalse mobiiliversiooni peale aega raiskasin?

Esiteks, need kõik võtavad su seadmes ruumi – pahatihti 10+ MB. Online-versioon aga ei võta ruumi, ei eelda paigaldamist ega uuendamist, ei näita reklaame, ei paigalda pahavara jne.

Teiseks, pea kõik neist kasutavad Eesti Keele Instituudi avalikku sõnakogu, mis on aga väga piiratud. Minu sõnastikus on aga hetkel 185605 eesti-inglise ja 198319 inglise-eesti vastet.

Küll aga eeldab online-sõnastik iseenesest mõistetavalt võrgu olemasolu. Kuivõrd tegin sõnastiku mobiiliversiooni tehniliselt äärmiselt minimalistliku (ei ühtegi skripti, välja arvatud GA – lehe esmalaadimine on 3 HTTP requesti, taaslaadimine vaid 1) – ehk teisisõnu, isegi Edge ühenduse korral on sõnastik kasutatav.

Erinevused tavalehest on järgmised:

  • Mobiilseadmete suurusele/resolutsioonile sobiv kujundus, tumedamate värvidega ekraani akukulu vähendamiseks. Touchscreen-sõbralikkus.
  • Autocomplete puudub, et veelgi vähendada liigutavate andmete mahtu
  • Maksimaalselt 10 vastet, mitte 25
  • Puudub otsingu ajakulu.
  • Otsingut ei panda URL’i/ei alustata URL’ist (so, võrdluseks http://dukelupus.net/dict#&term=s%C3%B5nastik&direction=1).
  • Otsingu suunda ei jäeta meelde cookie abil. Ühe sessiooni piires jäetakse otsingu suund meelde (enteriga otsingute tegemiseks).

Nagu ikka, kui sul on ideid, soovitusi, kommentaare või leiad bugisid, anna nendest palun teada postituse kommentaarides.

2014-02-19

Bright sayings of bright people: programming quotes

Filed under: Programmeerimine — Sander @ 13:56:40
Tags: ,

While the last post had my own sayings, this one has some of my favorite programming quotes from others. Even the first part of the title is from the late Frederik Pohl.

  1. "Don’t just echo the code with comments – make every comment count."
  2. "Make sure comments and code agree."
  3. "Don’t comment bad code – rewrite it."
    "The Elements of Programming Style" by Kernighan and Plauger, 1974
  • "It’s important to communicate what the code should be doing.” – Joe Kunk
  • “It’s hard enough to find an error in your code when you’re looking for it; it’s even harder when you’ve assumed your code is error-free.” – Steve McConnell
  • “If debugging is the process of removing software bugs, then programming must be the process of putting them in. ” – Edsger Dijkstra
  • “Walking on water and developing software from a specification are easy if both are frozen.” – Edward V. Berard
  • “First, solve the problem. Then, write the code.” – John Johnson
  • “Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.” – Stan Kelly-Bootle
  • “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” – unknown (attributed to John F. Woods, Rick Osborne, Martin Golding)
  • “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler
  • “Programs must be written for people to read, and only incidentally for machines to execute.” – Abelson/Sussman
  • “Computers are good at following instructions, but not at reading your mind. ” – Donald Knuth
  • “Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” – Rick Cook, The Wizardry Compiled
  • „Do unto the next developer as you would have them do unto you“ – Joe Kunk
Järgmine lehekülg »

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 83 other followers