Saturday, August 12, 2017

A Trie


This is a trie that uses a sentinel node to denote the end of a word. This is more space efficient than having to flag each node as to whether it denotes an end of a word. To quickly find the number of prefix matches, it stores the prefix count in the node.

class Trie {
    char ch;
    int count = 0;
    Map<Character, Trie> list = new HashMap<Character, Trie>();
    
    public Trie(char ch) {
        this.ch = ch;
    }
    
    public Trie add(char ch) {
        Trie node = this.list.get(ch);
        if (node == null) {
            Trie newNode = new Trie(ch);
            this.list.put(ch, newNode);
            node = newNode;
        }

        //adding the count to the current node is preferable
        //to adding to the node that matches the character.
        //This way, we won't add to the sentinel node
        //and we add only in one place.
this.count++; return node; } public int size() { return this.count; } private Trie findChar(char ch) { return this.list.get(ch); } public boolean findWord(String word) { Trie node = this; for (char ch: word.toCharArray()) { node = node.findChar(ch); if (node == null) { return false; } }
        //we may have found a prefix, make sure it is a word
        //if it's a word, the list must have the sentinel.
        return node.list.get((char)0) != null;
    }
    
    public int findPartial(String prefix) {
        Trie node = this;
        for (char ch : prefix.toCharArray()) {
            node = node.list.get(ch);
            if (node == null) {
                return 0;
            }
        }
        return node.size(); 
    }
    
    public void add(String s) {
        Trie node = this;
        for (char ch : s.toCharArray()) {
            node = node.add(ch);
        }
        //add the sentinel to mark the end of the word.
        node.add((char)0);
    }
}

folding with python

Solving this problem the functional way =>

Find if a sorted list of positive numbers has duplicates.


>>> def has_dups(nums):
...     return reduce (lambda x,y: ( x[0] or (y == x[1]), y), nums, (False,0))[0]
... 
>>> has_dups([1])
False
>>> has_dups([1,1])
True
>>> has_dups([1,1,1])
True
>>> has_dups([1,2,4])
False
>>> has_dups([1,2,2])
True
>>> has_dups([1,2,2,3,4])
True

>>> 

From the definition of reduce:

reduce(functioniterable[initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:

To solve the problem, we need to check if two adjacent items have the same value. To do this the functional way, we walk through the list using the reduce operator. At each point in the list, the reduce operator applies the user supplied function to the previous output(x) and the current item(y) of the list.

We need to remember if we see two adjacent items, and pass it as the output. But we also have to pass the current value so that at the next step, reduce can evaluate the given function. So we have a tuple as output from the function :

(truth value whether we have seen two adjacent items, current item)

We need to pass an initial value for the tuple (initializer). The truth value would be False initially, and we pass a zero as all elements of the list are positive. (False, 0)

So within the lambda, we need to check if the current item is the same as the previous item (y == x[1]) but if we had already met this condition (x[0]), we need to pass this along.

One of the drawbacks of the fold is that there is no quick break from traversing the list once we find a duplicate. It is possible to raise an exception in lambda and force a termination that way, but I don't know of a clean way to terminate the walk of the complete list.

Monday, August 07, 2017

Python 2.7 scoping bug

Here is a piece of code that does not work on Python 2.7:

 #!/usr/bin/python  
 def img_type(s):  
   return str(s)  
 print (img_type(50))  
 a = [img_dir for (img_dir, img_type) in [("a",1),("b",2)]]  
 print (img_type(20))  

On the second print, it raises a TypeError:

TypeError: 'int' object is not callable

The interpreter is incorrectly identifying the scoped variables img_dir, img_type to be in global scope. Since the function is of the same name, the variable takes precedence. Actually it overwrites the function!

We can see what is happening by looking at the globals().items() and locals().items(). Each is a list of tuples where each tuple contains the variable name and its currently assigned value. Here is a modified program that lists the variables, before and after we define the list comprehension:

#!/usr/bin/python

def img_type(s):
    return str(s)

print (img_type(50))

print ("BEFORE")
print (globals().items())
print (locals().items())


a = [img_dir for (img_dir, img_type) in [("a",1),("b",2)]]

print ("AFTER")
print (globals().items())
print (locals().items())

print (img_type(20))

This outputs:

50
BEFORE
[('img_type', <function img_type at 0x7f740ad5eb18>), ('__builtins__', <module '__builtin__' (built-in)>), ('__file__', './proof1.py'), ('__package__', None), ('__name__', '__main__'), ('__doc__', None)]
[('img_type', <function img_type at 0x7f740ad5eb18>), ('__builtins__', <module '__builtin__' (built-in)>), ('__file__', './proof1.py'), ('__package__', None), ('__name__', '__main__'), ('__doc__', None)]
AFTER
[('img_type', 2), ('a', ['a', 'b']), ('__builtins__', <module '__builtin__' (built-in)>), ('img_dir', 'b'), ('__file__', './proof1.py'), ('__package__', None), ('__name__', '__main__'), ('__doc__', None)]
[('img_type', 2), ('a', ['a', 'b']), ('__builtins__', <module '__builtin__' (built-in)>), ('img_dir', 'b'), ('__file__', './proof1.py'), ('__package__', None), ('__name__', '__main__'), ('__doc__', None)]
Traceback (most recent call last):
  File "./proof1.py", line 19, in <module>
    print (img_type(20))
TypeError: 'int' object is not callable

Notice how after the list comprehension the img_type() function got clobbered by the locally scoped variable by the same name.

This is fixed as of Python 3.2. Here is the output running the second version of the program:

50
BEFORE
dict_items([('__name__', '__main__'), ('__doc__', None), ('__loader__', <_frozen_importlib_external.SourceFileLoader object at 0x7ff93f4b7780>), ('__file__', './proof1.py'), ('__builtins__', <module 'builtins' (built-in)>), ('__spec__', None), ('img_type', <function img_type at 0x7ff93f41c2f0>), ('__package__', None), ('__cached__', None)])
dict_items([('__name__', '__main__'), ('__doc__', None), ('__loader__', <_frozen_importlib_external.SourceFileLoader object at 0x7ff93f4b7780>), ('__file__', './proof1.py'), ('__builtins__', <module 'builtins' (built-in)>), ('__spec__', None), ('img_type', <function img_type at 0x7ff93f41c2f0>), ('__package__', None), ('__cached__', None)])
AFTER
dict_items([('__name__', '__main__'), ('__doc__', None), ('a', ['a', 'b']), ('__loader__', <_frozen_importlib_external.SourceFileLoader object at 0x7ff93f4b7780>), ('__file__', './proof1.py'), ('__builtins__', <module 'builtins' (built-in)>), ('__spec__', None), ('img_type', <function img_type at 0x7ff93f41c2f0>), ('__package__', None), ('__cached__', None)])
dict_items([('__name__', '__main__'), ('__doc__', None), ('a', ['a', 'b']), ('__loader__', <_frozen_importlib_external.SourceFileLoader object at 0x7ff93f4b7780>), ('__file__', './proof1.py'), ('__builtins__', <module 'builtins' (built-in)>), ('__spec__', None), ('img_type', <function img_type at 0x7ff93f41c2f0>), ('__package__', None), ('__cached__', None)])
20

Monday, March 13, 2017

Docker: delete all tags of image

Handy one liner to delete all tags of a particular docker image (on Linux):



docker images | grep rabbitmq | tr -s ' ' | cut -d ' ' -f 2 | xargs -I {} docker rmi docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq:{}  

Here are the image tags that this one-liner removes:

1
2
3
4
5
6
7
thusharaw@denali ois-rabbitmq (thushara/rabbitv1)*$ docker images | grep rabbitmq
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   2017031300          6fb1ed865ae6        3 hours ago         179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   2017031307          6fb1ed865ae6        3 hours ago         179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   2017031007          21b84ae0586e        2 days ago          179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   201703100           43d51d243631        2 days ago          179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   201703107           08927efd735e        2 days ago          179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   201703109           646cb7852d8e        2 days ago          179 MB

Let's break down the on-liner:


1) docker images => 

get the images

2) grep rabbitmq

find the tags you care about

3) tr -s ' '

convert all repeating contiguous spaces to a single space so we can easily index to a specific column, the tag in this case

4) xargs -I {} docker rmi docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq:{} 

pass the thusly recovered tag to the `docker rmi` command, but use xargs to change the stdout of the previous cmd to a cmd line arg (which is what `docker rmi` works with)

Saturday, August 13, 2016

Java : Don't compare Integer objects with ==

In Java, == operator, if used between objects, compares their references. However, if you compare an object and a primitive type, Java "unboxes" the object to the primitive type and compares the primitives. This auto-conversion is a very pleasant feature of the language, but unfortunately, there is no unboxing if both types being compared to are objects.

So comparing two Integer objects will not work.

Comparing the intValue() on the objects is the way to go.

Sometimes, we may forget that two integer objects are being compared. Think of using a count as the value of a hash map. This is an integer, but due to the way generics are implemented, the value needs to be an Integer object. Comparing counts will need to use the intValue of the Integer object, such as needed for sorting the map by count.

The other relational operators (>, <, >=, <=) work as one might more intuitively expect. They use the Comparable.compare(T obj) method of the Integer class and work on the integer value held in the Integer instances being compared.

It is not clear why the Java language designers chose to wire == operator very differently from the other relational operators.

So the lesson is that whenever you work with Integer instances, all relational operators except the equality operator (==) works in the intuitive sense of comparing the integers. For equality, you could use one of the following:


boolean Integer.equals(Object obj)  

int Integer.compareTo(Integer another)

integerObj1.intValue() == integerObj2.intValue()


Wednesday, July 27, 2016

Optimal Solutions

Here is a Hackerrank exercise that allows us to think of improving program speed in multiple ways.
This is my solution, after a few attempts trying to pass the last two test cases.



 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
    import java.util.*;
    public class Contiguous {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Deque<Integer> deque = new ArrayDeque<Integer>();
            int n = in.nextInt();
            int m = in.nextInt();

            int[] arr = new int[n];
            
            int maxUnqs = 0;
            int unqs = 0;
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                deque.add(num); //adds to tail
                //count occurrence of each number
                Integer c = map.get(num); 
                if (c == null || c == 0) {
                    map.put(num,1);
                    unqs++;
                } else {
                    map.put(num, c+1);
                }
                if (deque.size() == m) { //we have added m elements; go compute uniques
                    if (unqs == m) {
                        maxUnqs = m; //we can't do better
                        break;
                    }
                    if (unqs > maxUnqs) {
                        maxUnqs = unqs;
                    }
                    int first = deque.remove(); //we don't need the head any more; keep queue small
                    int firstCount = map.get(first);
                    if (firstCount == 1) { //the head had the only occurrence of the number, so uniques drop
                        unqs--;
                    }
                    map.put(first, firstCount-1); //one less occurrence of the first element now
                }
            }
            
            System.out.format("%d\n", maxUnqs);
        }
        
        
    }

What strikes me here is that a lot of different things are being done under a single loop. The code is therefore hard to read.

If the code is made to read well, we lose the speed.

The most readable code will do something like this:

1) Read the input into a collection (possibly an array)
2) Make sub arrays of size m, starting from position 0, ending at position n-m
3) For each sub array, count the uniques -> we could use a bitmap, a boolean array, or a hashmap for this
4) Keep track of the maximum unique count

However, this readable algorithm runs in O(nm) time, whereas the solution given above is O(n). The optimal solution keeps track of up to m integers read, and when the m+1 integer is read, it simply removes the effects of the first integer from the system. Thus we can go through each integer only once and solve the problem.

But this alone was not enough to pass the last two test cases. If we don't update <unqs> variable for each loop iteration, but compute it from the <map> each time we have  a queue of m items, the code is still too slow to pass, although the time complexity is the same. While the time complexity is the same, we roughly double the work here and times out.

This slower solution would, on each mth item go over the map, counting keys that mapped to non-zero values. This would take m steps, but since we do this only n/m times, the time complexity would still be O(nm), but it would mean that for each n/m time slot, we do m+m = 2m work, effectively taxing the CPU twice.

Another solution I saw uses the map, but removes an element when its count drops to zero. Now we can use map.size() to calculate uniques. Since the map size can be read in constant time, this solution works as well to pass the tests.

All this led me to think about this Code Katta. The goals we set out will determine the kind of program we write.

Having said that, I believe it is possible to make this optimal program slightly more readable. If we encapsulate the state of the window of integers we are dealing with into a SlidingWindow class, we could implement four functions :


1
2
3
4
5
6
class SlidingWindow {
  void addNumberToWindow(int num)
  boolean isFullWindow()
  void processFullWindow()
  int maxUnique()
}

This will at least break up the input processing logic from the state handling. Lines 16 - 24 would get folded into addNumberToWindow; Lines 26 - 40 would be inside processFullWindow.

Thursday, October 08, 2015

Cartesian product in Scala

val s  = Seq("1","2","3","4")
val t  = Seq("a","b","c","d","e","f")
val u  = Seq("x","y")
val v  = Seq("m","l","n","o","p")

val sq = Seq(s,t,u,v)

sq.foldLeft(Seq(""))((b,a) => b.flatMap{i=>a.map{j=>i+j}})


res9: Seq[String] = List(1axm, 1axl, 1axn, 1axo, 1axp, 1aym, 1ayl, 1ayn, 1ayo, 1ayp, 1bxm, 1bxl, 1bxn, 1bxo, 1bxp, 1bym, 1byl, 1byn, 1byo, 1byp, 1cxm, 1cxl, 1cxn, 1cxo, 1cxp, 1cym, 1cyl, 1cyn, 1cyo, 1cyp, 1dxm, 1dxl, 1dxn, 1dxo, 1dxp, 1dym, 1dyl, 1dyn, 1dyo, 1dyp, 1exm, 1exl, 1exn, 1exo, 1exp, 1eym, 1eyl, 1eyn, 1eyo, 1eyp, 1fxm, 1fxl, 1fxn, 1fxo, 1fxp, 1fym, 1fyl, 1fyn, 1fyo, 1fyp, 2axm, 2axl, 2axn, 2axo, 2axp, 2aym, 2ayl, 2ayn, 2ayo, 2ayp, 2bxm, 2bxl, 2bxn, 2bxo, 2bxp, 2bym, 2byl, 2byn, 2byo, 2byp, 2cxm, 2cxl, 2cxn, 2cxo, 2cxp, 2cym, 2cyl, 2cyn, 2cyo, 2cyp, 2dxm, 2dxl, 2dxn, 2dxo, 2dxp, 2dym, 2dyl, 2dyn, 2dyo, 2dyp, 2exm, 2exl, 2exn, 2exo, 2exp, 2eym, 2eyl, 2eyn, 2eyo, 2eyp, 2fxm, 2fxl, 2fxn, 2fxo, 2fxp, 2fym, 2fyl, 2fyn, 2fyo, 2fyp, 3axm, 3axl, 3axn, 3axo, 3axp, 3aym, 3ayl, 3ayn, 3ayo...