Thursday, July 21, 2016

Being an adult has some advantages (LEGO adventure + first MOCs)

Dear readers,

today I would like to share with you information about how I rediscovered the passion for LEGO pirates bricks and show you some of My Own Creations (MOCs).

Back in the days

When I was a child I got a lot of LEGO sets from my parents (I would like to thank them for that!). Most of them was rather small as salary in Poland was really low at that times. According to this source for an annual(sic!) average salary one was able to buy 108 USD. In other way around for a whole year work you were able to buy 1989 flagship - Black Seas Barracuda. With some LEGO sets the shops were giving the catalogues were I was able to admire the largest and most epic sets. I always glared the most at the pirates themed pages. To this day I remember some of them:



Coming out of dark ages

Few weeks ago while doing some work I was asked to position a marker on my desk so specialized
tracker can detect it and got an advice that LEGO bricks can be useful for that. This started everything. Later that day I pulled out of a closet some bricks that I bought on an auction portal a couple years ago. I don't even remember how much it was - one, two or maybe three kilograms. There were a lot of mixed sets, but few were almost complete. It even had some pirates and islanders! I play with thema little bit and got infected...

The madness starts

As I stated earlier - pirates were my favourite theme and I started looking for them. Now it's like Pokemon go frenzy - Gotta Catch 'Em All! And this is the best part - as an adult you do not have to wait for Christmas!

My own creations (MOCs)

From a couple of minifigures and sets I bought I have prepared some own creations.

The first one is entitled: "The vultures are coming"


The lone pirate has sipped all his beverage and there he doe not see any rescue coming. The see is everywhere around him and there is only on bullet in his gun. The sea vultures are gathering around as they feel the inevitable...

The second one depicts the soldier who has passed out during the interrogation. At the moment he is taken by medical services to be prepared for the second meeting with the interrogator.



The last scene presents two sides before incoming confrontation. How centuries of tradition and honor will fare against intruders that bring the technology and progress? Which one will prevail at the end? Who will rule the land? Do the natives has any chance?




BTW sorry for rather depressing theme for MOCs - some real world troubles made my thoughts grim.

TL; DR

LEGO bricks are awesome! Pirates are even more awesome! Get amused by my own creations!

Tuesday, April 12, 2016

Blog and challanges summary

I started this blog to write about things that interests me. At the beginning it was a little bit about motivation and running [here]. I set some goals for myself to achieve [here] and event create a page that stores all my successes and failures [here].

Today I would like to end this part of my webpage so I will do a quick summary of how I failed hard and succeed in some of the challenges.

Challenges summary - failure part

Lets start with the bad news - in both of the running challenges, namely:

  • run half marathon in 2015
  • run marathon in 2015

I failed. Last time, that I wrote about running [here] I mentioned that I got injured. Unfortunately the recovery period was long (I tried running after 2/3/4/5 weeks and my knee was still hurting). I took some pills but it did not help. I did some exercises instead - ride a bicycle, did over 100 km while cross country skiing. The break from running made it impossible to take part in half marathon in Poznan in April. I started running in April and gradually increased the intensity (I read a book about running during while being injured). Unfortunately in July my knee started hurting again and I had to pause running again. This shattered the dreams of marathon for me.

On the bright side of things - I bought a bike and I enjoyed riding it. I did almost 1500 kms last year, while in previous years I did not ride at all. I was even able to ride over 100 km in one day which I am proud of :) All in all despite not fulfilling my plans I enjoyed the year trying different sports.

Additional note for other people that are starting running or are planning to - do not force yourself to much. I know it is nice to exhaust yourself after a long day in front of the computer but for the sake of your health run slowly. Really. Or read a book or run with somebody at pace that you can talk comfortably.

Challenges summary - I don't know what it is part

I started the blog to express myself to the world. To hone my writing skills. To share my ideas. I challenged myself to keep the blog up with regular updates. It is still alive. Maybe not exactly alive and kicking but also not on the verge of death I guess. What do you think my dear reader? Was this challenge a success or was it a failure?

Challenges summary - I did something right part

At the beginning of the year somebody showed me a program for a mobile phone that allows learning the foreign languages. I am talking about Duolingo. It is really well executed application and what is most important it is free (and can be used on both mobile phone and the PC thanks to being a webpage). I started doing the exercises and today I achieved 100 days streak (which was my goal). That means I did the necessary exercises each and every day starting from 4th of January. I finished all the lessons a while ago and now I only did repetitions and it is less interesting that new lessons.
I would like to encourage everybody to try the Duoliong and the method of learning by small but frequent exercises. On the downside there is only one level of Polish-English course and is rather simple.

TL; DR

When I started this blog I set myself a few goals (challenges). Today I revised how I fare - I described my failures and successes. Now I retire this part of the webpage - no more challenges for me (at least for now). The archived ones can be found here.

Friday, January 15, 2016

Python is Over 9000!

In my last post I asked you, my dear reader, to try and flex your programming muscles solving some tasks in C++ language. Now is the time to compare the solutions of the aforementioned exercises in both Python (written by me, please take a note that I have learnt Python in the last year) and C++ (solutions from my friends who know how to program). Big shoutouts to them, as without their work this post will not be that long. You can check their profiles on github - przemkovv and MichalNowicki).

The code

1) lets assume we have two collections and would like to print values from them in the following format:
value_0_from_first_collection, value_0_from_second_collection
value_1_from_first_collection, value_1_from_second_collection
etc.

So for the warm-up we are doing something rather easy, that in Python looks really straightforward:

for n1, n2 in zip(numbers_1, numbers_2):
    print(n1, n2)

For those not familiar with Python - you can check zip function here. In this case it is returning elements from both collections which are assigned to names n1 and n2.

In C++ it looks a little bit more cumbersome (what are this magical * sign? :P):

for (auto it = kolekcjaA.begin(), it2 = kolekcjaB.begin(); 
     it != kolekcjaA.end() && it2 != kolekcjaB.end(); ++it, ++it2) {
    std::cout << *it << " " << *it2 << std::endl;
}

Or if you use some dark magic you can get something similar to what Python is offering:

vector<tuple<char="", int="">> out_zipped;

transform(vec.begin(), vec.end(), str.begin(), back_inserter(out_zipped), [](auto x, auto y){ return make_tuple(x, y);});

for_each(out_zipped.begin(), out_zipped.end(), [](auto t) {
    cout << get<0>(t) << ", " << get<1>(t) << endl;
});

It gets the job done but I would say that Python solution is much neater.

2) lets assume we have a text and we would like to print it without the first and the last sign. In addition we would like to print the text length.

Python:
print(text[1:-1], ", " + str(len(text)))

C++:
// solution 1
std::cout << text.substr(1, text.size() - 2) << " " << text.length() << std::endl;

// solution 2
string cut_from_both_sides(str.begin() + 1, str.end() - 1);
cout << cut_from_both_sides.length() << " " << cut_from_both_sides << endl;

All of them looks similar, but look at the next task to see the advantage of Python solution!

3) we have a collection of values and we would like to print it without the first and last values. In addition we would like to print the size of the collection.

Python:
print(numbers_1[1:-1], ", " + str(len(numbers_1)))

C++:
// solution 1
for (auto it = kolekcjaA.begin() + 1; it + 1 != kolekcjaA.end(); ++it) {
    std::cout << *it << std::endl;
}

// solution 2
vector<int> vec2(begin(vec) + 1, end(vec) - 1);
cout << vec2 << endl;

As you can see the Python is still using the same way, while C++ solution 1 evolved to using iterators, while the C++ solution 2 is the same but will not work for lists (and other collections that do not offer [] operator)!

4) we have a function that has to return:
a) two values,
b) three values.
How would you implement that?

Python:
def increment_all(a, b, c):
    return a+1, b+1, c+1

x, y, z = increment_all(x, y, z)
print(x, y, z)

C++:
auto triple_return() {
    return make_tuple(5, "good", 3.5);
}

auto gotcha3 = triple_return();
cout << get<0>(gotcha3) << " " << get<1>(gotcha3) << " " << get<2>(gotcha3) << endl;

To save space I left just triple return. At first glance all the solutions look similar, but take a closer look how you print the returned values and you will appreciate Python simplicity. Also it is possible to swap values without additional buffers:

a, b = b, a

5) for collection of your choice do:
a) print all values,

Python:
for v in values:
    print(v)

C++:
for (auto value : vec3) {
    std::cout << value << std::endl;
}

Nothing extraordinary. C++ has similar construction for getting elements from collections.

b) increase each value by 10,

Python:
values = [v+10 for v in values]

C++:
// solution 1
for (auto& x : vec3) {
    x += 10;
}

// solution 2
transform(begin(vec3), end(vec3), begin(vec3), [](auto x) { return x + 10; });

This is where things start to be interesting. In Python you use thing called list comprehension, which is general and elegant way to work on containers. In C++ you can use solution similar to the last one but you have to remember to use another special C++ operator - &. Solution 2 is a way to mimic Python behaviour.

c) remove last element,

Python:
values.pop()

C++:
// solution 1
kolekcjaA.resize(kolekcjaA.size() - 1);

// solution 2
vec3.pop_back();

Nothing special, but C++ guys offered two different solutions :)

d) remove n-th element,

Python:
n = 2
values.pop(n)

C++:
// solution 1
int n = 4;
kolekcjaA.erase(kolekcjaA.begin() + n);
}
// solution 2
const int nth = 10;
vec3.erase(begin(vec3) + nth);

Once again C++ solutions does not work for lists.

e) print value alongside its index,

Python:
for i, v in enumerate(values):
    print(i, v)

C++:
// solution 1
for (int i = 0; i < kolekcjaA.size(); i++) {
     std::cout << "Index = " << i << " " << kolekcjaA[i] << std::endl;
}

// solution 2
int i=0;
for (auto it = begin(vec3); it != end(vec3); ++i, ++it) {
    cout << i << " " << *it << endl;
}
// solution 3
i=0;
for (auto x : vec3){
    cout << i << " " << x << endl;
    ++i;
}
// solution 4
for (auto p = make_pair(0, begin(vec3)) ; p.second != end(vec3); ++p.first, ++p.second) {
    cout << p.first << " " << *(p.second)  << endl;
}

In Python this is straightforward - enumerate and done! C++ offers a lot of solutions, that have some pros and cons. I prefer to have one main solution - it helps when more than one person is working on with the code.

f) check if value x is in collection,

Python:
if 27 in values:
    print("27 is in the collection")

C++:
// solution 1
bool contains_25 = find(begin(vec3), end(vec3), 25) != end(vec3);
cout << contains_25 << endl;
// solution 2
auto contains_x = [](auto C, auto x) { return find(begin(C), end(C), x) != end(C); };
cout << contains_x(vec3, 25) << endl;

Once again you can witness how elegant Python is. It is the most straightforward way and even non-programers would understand what this code do.

g) check if all values are smaller than y.

Python:
if all(v < 30 for v in values):
    print("All values are smaller than 30!")

C++:
// solution 1
int twojaWartosc = 4;
auto wart = std::find_if(kolekcjaA.begin(), kolekcjaA.end(), [&twojaWartosc](auto const &x) { return x < twojaWartosc; });
std::cout << "Wartosc : " << *wart << std::endl;

// solution 2
int y = 50;
cout << all_of(begin(vec3), end(vec3), [=](auto x) { return x < y; }) << endl;
// solution 3
auto less_y = [](auto y) { return [=](auto x) { return x < y;  }; };
cout << all_of(begin(vec3), end(vec3), less_y(20)) << endl;

Python is clean and simple. In C++ you can use different ways, but even this simple task can cause problems - solution 1 throws exception when the value is not in the collection as you try to access (dereference) an iterator that point to the end of collection.

6) having a text count how many letters are lower-case and how many are upper-case

Python:
number_of_uppercase_case = sum(1 for c in text if c.isupper())
number_of_lowercase_case = sum(1 for c in text if c.islower())

C++:
// solution 1
int male = std::count_if(podanyTekst.begin(), podanyTekst.end(), [](unsigned char x) { return islower(x); });
int duze = std::count_if(podanyTekst.begin(), podanyTekst.end(), [](unsigned char x) { return isupper(x); });

// solution 2
cout << count_if(begin(s), end(s), islower) << endl;
cout << count_if(begin(s), end(s), isupper) << endl;

All the solutions are very similar - they use built-in functions. The thing with Python is that you can use almost the same code for other things like e.g. extraction of lower-case letters:

lower_case_letters = [c for c in text if c.islower()]

7) is there any situations that you do not use indentation?

We all have agreed that apart of code golfing everybody should use indentation. So if it is obligatory lets resign from { and } brackets. Their are unneeded and deprecated. Lets use Python!

8) are you using standard arrays (static or dynamic) at all (like: int a[5] or int* b = new int[5])?

Once again we had a consensus - dynamic tables should be avoided as they can be dangerous.
When I think about them two quotes comes to my mind:

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.

-- Bjarne Stroustrup

With great power comes great responsibility.

-- Voltaire

Other

On a side note - this is probably the longest most in my career.
If anybody is interested I used Syntax highlighter to post the code. It offers nice looking code but embedding small portions of code is cumbersome - you have to edit html code and doing it through the browser is a pain.

Summary

I hope you enjoyed this long, over 9000 signs comparison. By no mean I was trying to defame C++. I think it is an important programming language, that has its uses (C for microcontrollers, some high performance routines and libraries). Just in my opinion the new additions to C++ (C11, C14) that tries to convert it to modern programming are a failure. They change many things upside-down, make the code much different to understand for people that are not familiar with new standards while the code is still not as elegant and readable as Python. It is just my two cents, opinion that is probably heavy influenced by my experience - I did a lot of programming for embedded systems (microcontroller based).



If, by some accident, you are still not convinced about Python greatness you are not alone. Apparently some people think Python is overrated ;)

TL; DR

This post describes why I think Python is a great language, especially when you are starting learning how to program. Different simple exercises were done in both Python and C++ and the code was compared. Python rulez.