Android

A couple of tricks with Android’s CollapsingToolbarLayout


No Comments

Recently I had some fun with CollapsingToolbarLayout, and I learned a few tricks that I would love to share. When using this piece of magic it is very easy to implement material behaviour, your App looks amazing with very little effort. However you might find that you want something slightly different sometimes, and find yourself stuck googling for a solution, or digging into the code.

This happened to me because I wanted to be able to lock the CollapsingToolbarLayout, meaning that I wanted to prevent the user from expanding and collapsing the toolbar by dragging his finger. However I still needed the collapsing behaviour to work programmatically. Took me longer than expected to figure this out, however the solution turned out to be pretty easy, this is the result (more…)

Algorithms

HackerRank “Almost Sorted” challenge


No Comments

I received an email from HackerRank a few days ago, “Can you solve the ‘Almost sorted’ challenge?”. Well, I thought, probably.. let me see.
The problem is about figuring out how to complete the sorting of an array of integers, we receive an input like the following:

1, 2, 8, 4, 5, 6, 7, 3, 9

The goal is to figure out how the input array can be transformed into a sorted array, using only one of two allowed operations:

  1. Swap two elements
  2. Reverse a continuous subsequence of the array

The challenge requires to print out the operation that can complete the sorting. If two elements can be swapped then we have to print swap l r where l and r are the positions of the two items to be swapped. If, on the other hand, a subsequence can be reversed to complete the sorting, then we print reverse s e where s an e are the initial and final positions of the subsequence. When printing the positions we have to use 1 based numbers.
Using the array above we should therefore be able to print swap 3 8
(more…)

Git

Git. A pre-push hook to protect remote branches, done right.


No Comments

I was looking for a way to accomplish remote branches protection using a pre-push git hook. What I needed was basically a way to prevent people, and myself, from accidentally pushing to master. I took the lazy approach went to Google for a quick solution which I could just copy and paste.

And I found a lot. However I noticed that most of them share one issue, which makes them unreliable in some scenarios. The most common way to implement such a hook is to use git rev-parse or git symbolic-ref to figure out the current local branch, the next step is to assume that the current branch will be pushed to a branch with the same name on the remote and prevent it if that’s the case. This, as mentioned, is not always the best approach. (more…)

Algorithms, java

Algorithm challenge. Find the running median of an array of integers.


No Comments

This is a famous algorithm challenge, it’s also a very tough one. I’ve seen this problem so many times around and never took the time to actually solve it. Practicing this stuff is good to keep the mind sharp so this time I invested some time to solve it, actually more time than expected. I’m writing this to help future seekers of knowledge that will come by.
Here’s the challenge:

You are given a set of integers coming from user input. The first number you get is the amount of integer values in the set. The next numbers you get are the integers in the set. You have to find the running median of the set of integers. The running median is the midpoint value of the sorted set, or the average of the two mid points if the number of elements in the set is even.

(more…)

Android, java

7 copy & paste snippets for Android


No Comments

I listed seven common tasks that Android developers often face, it’s the kind of stuff that interrupts your train of tought and makes you think “Oh God.. this is boring and I have other things to do, let’s google for some code”.
And here it is. In no particular order.
Just copy and paste, no questions asked. (more…)

Android

Using BitmapShader in Android


1 Comment

Recently I had to implement some custom UI controls for one of our customers, and some of these controls involved drawing Bitmaps with rounded corners.
Android provides a handy approach to this problem, all the magic can be done with a little help from a class named BitmapShader.

With BitmapShader you can draw bitmaps in any shape and even more, here’s an example:

bitmap-shader-01

(more…)

Algorithms

Codility and the “K complementary pairs in array” challenge


5 Comments

Recently I had to confront myself with Codility, for those who don’t know it Codility is an online tool that provides coding challenges and it is often used by companies in the process of hiring new people when they want to test people’s coding skills.

But that’s not all, Codility also provides a great training section where you can confront yourself with a ton of coding challenges and improve your understanding of algorithms and your problem solving skills.

Yes, someone was testing my coding skills.

(more…)

Algorithms, java

Merge Sort algorithm in Java


1 Comment

This is a very simple Java implementation of the popular merge sort algorithm.
I decided to put this here because I needed to refresh my knowledge of sorting algorithms, problem is I could not find an easy-to-understand implementation online, so I decided to implement my own version and share it in case someone is struggling on understanding how to implement this algorithm. I think this is the shortest implementation ever.

Introduction

Merge sort works by splitting the original array into halves several times until many subarrays of size 1 are obtained.
The algorithm then merges back every subarray in such a way that the result of merging two subarrays is a single, sorted array twice as big as the subarrays.
The process is repeated until an array of the same size of the input array is obtained.

Here is an example:

             
              [24, 3, 17, 109, 6, 72, 11, 0]                  Unsorted input

      [24, 3, 17, 109]              [6, 72, 11, 0]            Split #1

   [24, 3]       [17, 109]      [6, 72]        [11, 0]        Split #2

  [24]  [3]     [17]  [109]    [6]  [72]      [11]  [0]       Split #3

   [3, 24]       [17, 109]      [6, 72]        [0, 11]        Merge #2
   
       [3, 17, 24, 109]             [0, 6, 11, 72]            Merge #3
           
              [0, 3, 6, 11, 17, 24, 72, 109]                  Merge #4 : Sorted output

This is a very simple introduction to the algorithm, more details on Wikipedia.

Give me the code!

Here’s the code I wrote that implements this merge sort, it can be improved a lot especially on the space it requires to run, you’ll see that at every iteration two new subarrays are created, this is not optimal if we need to save memory.

Android

How to get the SHA1 hash sum of a String in Android


3 Comments

Here’s one piece of code that comes very handy in some situations, just copy and paste this to get the SHA1 hash sum from a text String. The Hash is returned as an hex encoded human readable String, see below for an example of use:

Here’s how to use this:

The code above will print this output:

String: Hello World
SHA1 HASH: 0a4d55a8d778e5022fab701977c5d840bbc486d0

Enjoy 😉