Monday, January 18, 2016

Maintaining a sorted array of tuples ~ Python

Hello Pythonistas!

After some Christmas despair and endless reference searching  I thought I could do a good thing and put everything together here ;)

Objective : Maintain a sorted array of tuples in Python

For our recipe you will need : 1 array of tuples, 1 array of comparable values , this post

        The main array (from now on "tupArray") is an array of tuples (say tuples of three values) so we have tupArray = [(0,0,0)]  keep in mind the value we want to use to keep the array sorted HAS to be comparable. The second array (from now on "indexArray") is going to act as an index for the first so it will be sorted too and it will contain the element from each tuple that is our sorting index. Confused? Let me clear it out a bit with an example. Say we want to sort our array of tuples based on the second value of each tuple. This would be tupArray[i][1].  Then given an already populated and sorted tupArray, the indexArray would be created like this:

for elem in tupArray:
     indexArray.append (elem[1])

        That's it. We now have both tupArray and indexArray populated and sorted. Now to maintaining. This was probably the hardest part, not to understand but to find information about. First make sure you're familiar with slicing, you can tag along in any case but having a basic understanding of slicing would help you understand much faster.
        We will be utilizing a function called bisect. What bisect does is it returns the position in which you would insert an element in a sorted array in order for it to remain sorted. Bisect has a few variants, that allow you to define whether you want the new element to be placed before or after any existing elements with the same value (bisect_left is for before and bisect_right is for after we will be using the later). First import bisect:

import bisect

Then we call bisect giving indexArray and the element to be inserted as values:

insertIndex =  bisect.bisect_right (indexArray, elementToInsert)

Then we place it in indexArray using slicing:

indexArray[insertIndex:insertIndex] = elementToInsert

This will create a NEW element at indexArray[insertIndex] and not replace the one already there (like indexArray[insertIndex] = elementToInsert would). Finally, we have to insert our tuple to tupArray. For that we'll be using slicing again:
tupArray[insertIndex:insertIndex] = tupleToInsert

And that's really all there is to it! We've made an index list out of our array of tuples and managed to insert a new element without having to resort the array. A little heads up, if you are to pop or in any way remove elements from tupArray (and you most probably are) REMEMBER to do the exact same operation to indexArray too!

Until next time, have fun and happy coding!


Sunday, November 29, 2015

What was and what is JustAddCoffee

A question I've come to ask my self a lot of times as the brand progressed. Initially, in April 2014, we created JustAddCoffee with the vision of making a gaming blog, not to get filthy rich, but as a way to express our views and share the joy we got from the games we played, and the occasional frustration.

Along the way, seeing that not many people would choose to read in their free time (not from the gaming community we were addressing at least), we decided to give YouTubeing a shot. Although we put considerable amount of time in it, it was never enough. YouTube, especially when it comes to gaming channels, has a pretty strongly established base. The main challenges was the time it required to produce a decent gaming video and the equipment it required to produce a hardware review. So having limited available time and a vary small budget we eventually let go of YouTubing.

So this WAS JustAddCoffee, the above followed a period of inactivity, at least in our public pages. During this period, we were redefining what JustAddCoffee was and so we get to what JustAddCoffee is now.

 The fact that we were both programmers and long time gamers gave us a head-start in game development. So yes, that's what we're into right now, Indie game development. We've tried many development platforms, from dead simple to exhaustively  complicated, and experimented with many genres. Finally we've settled to our choice and are now developing our first game hopping to give something new to the gaming community.

So that's who we were, that's who we are and if you want more ... JustAddCoffee!

Monday, July 13, 2015

Elephone P3000s First Impressions


So this isn't going to run as a review but more as first impressions post (as the title suggests). So I've had the Elephone P3000s for about a week know and thought about sharing the feel that I get from using it.
Right off the bat I'll say that initially I chose the brand and the model for the ration of specs/price. At the moment I purchased it, it was around 150 euros and it came with a tempered blueglass and the official case (an it does matter).
Things I loved about it and things that let me down. Well as I said before the first thing is the specs/price ration there's just no way you can't love it. Second comes the design of the phone, elegant simple and a bit blocky (not totally roundish). The colors are also pretty amazing. Then, watering down a bit, we move to the battery life. The battery life is good, just that nothing special. But being a guy that charges all his electronics every night, well it doesn't really affect me. To close with, and the only outright let down, is the fingerprint scanner. It's just not usable. It works, but it's so damn hard to get it to recognise your fingerprint, as it's angle sensitive, that nearly every time I exceeded the maximum tries and had to use the alternative password, eventually rendering useless the whole system. If you're absolutely looking for a fingerprint unlockable phone I'd recommend getting a later model with a touch ID system (where the fp-scaner is located in the homescreen button and is a bit more agile).

All in all, it's a great device with a great specs/price ration and will cover almost any user's need. I've been using it for video streaming, for games, and for e-mail and document viewing and can't complain about anything. Also used it as a hotspot for a while, but not enough yet to have a complete view.

That's all for now!

(This was to be uploaded to www.justaddcoffee.org but due to some changes we're implementing to the server the blog will be down for a while)