Most Wanted Letter

Again some nice puzzles that I solved and where the solution was much simpler than I expected. The standard libraries of python are very powerfull. We look at a first puzzle:

The description of the problem is as follows:

You are given a text, which contains different english
letters and punctuation symbols. You should find the most
frequent letter in the text. The letter returned must
be in lower case. While checking for the most wanted letter,
casing does not matter, so for the purpose of your search,
“A” == “a”. Make sure you do not count punctuation symbols,
digits and whitespaces, only letters.

If you have two or more letters with the same frequency,
then return the letter which comes first in the latin alphabet.
For example — “one” contains “o”, “n”, “e” only once
for each, thus we choose “e”.

I did try it and came with the following solution:

1
2
3
4
5
6
7
8
9
10
11
def keyf(a):
    return a[1]
    
def checkio(text):
    a = [x.lower() for x in text if x.isalpha()]
    a.sort()
    c=[(l,a.count(l)) for l in a]
    result = max(c,key=keyf)
    
    #replace this for solution
    return result[0]

But there was a better and smaller solution. Just take all the letters of the alfabeth in lowercase
and take the same max key, but take the count of each letter in the original text and that it is.

1
2
3
4
5
import string
 
def checkio(text):
    text = text.lower()
    return max(string.ascii_lowercase, key=text.count)

This are fewer lines of code and make use of the string library.

Keeping non unique items in a list

I tried a new game at (http://checkio.org)[http://checkio.org]. This is a kind of game for programmers. Learn you programming skills by playing a game. Cool, not? The game contains some kind of problems you need to solve. An interactive programming environment is at your service in this game.
The first problem however was not so simple. I never needed that problem at this point, but it revealed some new things for me.

The problem is simple: I have an array of items. Just delete all items that are not unique in the list and return the other values as a list, with all duplicates in it and in the same order. For example:

1
2
3
[1, 2, 3, 2, 1] must return [1,2,2,1]
[1, 3, 1, 3, 2, 3] must return [1, 3, 1, 3, 3]
[5, 5, 5, 5, 5, 5] must return [5, 5, 5, 5, 5, 5]

Read More

globals in python

Today I am going to have some coding fun as I will call it. The following code was written by me. For the simplicity I just create messages with two characters. A message id character and an optional character

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Message(object):
    def __init__(self):
        self.__data = ""
        self.__valid = False

    @property
    def data(self):
        return self.message_id + self.__data

    @data.setter
    def data(self, input):
        if input[0] == self.message_id:
            self.__data = input[1:]
            self.__valid = True
        else:
            self.__valid = False


    @property 
    def valid(self):
        return self.__valid


class MsgRead(Message):
    def __init__(self):
        super(MsgRead, self).__init__()
        self.message_id = '1'


class MsgWrite(Message):
    def __init__(self):
        super(MsgWrite, self).__init__()
        self.message_id = '2'


class GenerateMessage(object):
    @staticmethod
    def create_write(data):
        msg = MsgWrite()
        msg.data = data
        return msg

    @staticmethod
    def create_read(data):
        msg = MsgRead()
        msg.data = data
        return msg

    @staticmethod
    def gen_message(data):
        msg = GenerateMessage.create_read(data)
        if msg.valid:
            return msg
        msg = GenerateMessage.create_write(data)
        if msg.valid:
            return msg

    @staticmethod
    def generate(data):
        msg = GenerateMessage.gen_message(data)
        return msg

g = GenerateMessage()
a = g.generate("1a")
print type(a)
b = g.generate("2b")
print type(b)

Read More

First usage of elixir


I bought a little book of a new programming language named Elixir. The book is called Programming Elixir. I read the first two or three chapters and I must say that it is indeed different from other languages I know. It has some elements of Erlang, but not all.

Then I tried to install elixir on my mac. I followed the guidelines on the official webpage:

1
2
brew update
brew install elixir

This went fine. I could start the interactive shell with the command iex, like it was in the book.

1
2
3
4
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (0.14.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

I could do for example 3 + 4 and I saw :

1
2
3
iex(1)> 3 + 4
7
iex(2)>

I tried other examples and then I decided to close the shell. I did not read about a command to close the shell, so because I saw Erlang on the shell, I tried the command q. In Erlang it is q(). Therefore I also tried it as a function, but no luck.

Aparently there is no command implemented yet to close the interactive shell correctly. What we should do is the following like it is possible in Erlang too:

1
2
CTRL-G 
press now q

My first try with elixir was already ended with a call to google. Nice I would say. Let the explorer in me begin to explore the language.

Variable keywords in robot framework

Suppose we have a library for the robotframework with some functions that are more or less the same. For example, there are three keywords set_parameter_a_in_module_b_to_value, set_parameter_a_in_module_c_to_value, set_parameter_a_in_module_d_to_value. Suppose now that we must set all these functions to a specific value for example the value 3.

Then in robot framework we define a new keyword Set Parameter A

1
2
3
4
5
*** keywords ***
Set Parameter A
    set_parameter_a_in_module_b_to_value  3
    set_parameter_a_in_module_c_to_value  3
    set_parameter_a_in_module_d_to_value  3

We can now make the value to be set in a variable and input it as a parameter in the keyword

Read More

Several git tips

Retrieve a deleted stash

If you delete the stash per accident, and did not want it to be deleted, is is possible to retrieve if it was done recently and the garbage collector was not active yet.
First list all unreachable commits.

1
2
3
4
5
6
7
8
$ git stash drop 
Dropped refs/stash@{0} (1392dfcd6418f5b7cb009c24416630597add918f)

$ git fsck --unreachable
Checking object directories: 100% (256/256), done.
unreachable blob 2762d40c58c9ab3601529eb9bbf35f873be605c2
unreachable commit 11d01fc547c3438e24c06ae9a18828e5eaa0b02e
unreachable commit 1392dfcd6418f5b7cb009c24416630597add918f

This will show you all the commits which are not reachable by a branch or tag and which haven’t been garbage collected yet. Look for the ones that say unreachable commit, ignore the blobs. The chances are it will be the one closest the top, unless you’ve performed some other actions which created more unreachables.

You can examine the commits by doing ‘git show ‘, and this should identify the stash, it’ll probably be called ‘On master: ‘ or similar. Once you’ve found it, copy the SHA.

To recover this you then do:

1
2
3
4
5
$ git show e9fed4e0950aae7ae2943ad0121d2668bd151dbf

This shows the diff we needed

$ git stash apply e9fed4e0950aae7ae2943ad0121d2668bd151dbf

Which will bring it back into your working copy.


Switching branches

In bash, the command cd - switches to the last directory you were in. You can do the same thing with git to switch to the last branch you were on with git checkout -.


1
2
3
4
5
6
7
8
$ git checkout -b b1
Switched to a new branch 'b1'

$ git checkout -
Switched to branch 'master'

$ git checkout -
Switched to branch 'b1'

first robot framework patch

Today I saw a little issue in the Robot Framework. The Robot Framework is a test automation framework for acceptance testing and acceptance test-driven development. The issue that I saw, was when running the unittests. One of them failed on my windows operating system in some cases.

The test was splitting args from letters with a colon (:) In the unittest, a test with “L:” exists, but on my windows machine that drive letter exists, so this means that the test per accident failed on my machine. So I replaced the drive letter by a number in the test and it succeeds. Here is my patch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
diff --git a/utest/conf/test_settings.py b/utest/conf/test_settings.py
index c10972f..ac440ce 100644
--- a/utest/conf/test_settings.py
+++ b/utest/conf/test_settings.py
@@ -32,7 +32,7 @@ class TestSplitArgsFromNameOrPath(unittest.TestCase):
         assert not os.path.exists('foo'), 'does not work if you have foo folder!' 
         assert_equals(self.method('foo:'), ('foo', [''])) 
         assert_equals(self.method('bar:arg1::arg3'), ('bar', ['arg1', '', 'arg3'])) 
-        assert_equals(self.method('L:'), ('L', [''])) 
+        assert_equals(self.method('3:'), ('3', ['']))

     def test_with_windows_path_without_args(self):
         assert_equals(self.method('C:\\name.py'), ('C:\\name.py', []))
--

I created on github a pull request, and I hope it get’s somehow accepted. Maybe the test was correct and I need to change some real code, but I have to wait now what the developers deciding to do with my patch.