Friday, September 19, 2014

Find supported image formats by matplotlib package in python

While you call matplotlib.figure.savefig, you may be wondering what type of figure I can save. You can find the answer by entering some commands in python.

By checking backend engines of pyplot
import matplotlib
['GTK', 'GTKAgg', 'GTKCairo', 'FltkAgg', 'MacOSX', 'QtAgg', 'Qt4Agg', 'TkAgg', 'WX', 'WXAgg', 'CocoaAgg', 'agg', 'cairo', 'emf', 'gdk', 'pdf', 'ps', 'svg', 'template']

By checking the supported file types by canvas object
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> print fig.canvas.get_supported_filetypes()
{'svgz': 'Scalable Vector Graphics', 'tiff': 'Tagged Image File Format', 'jpg': 'Joint Photographic Experts Group', 'raw': 'Raw RGBA bitmap', 'jpeg': 'Joint Photographic Experts Group', 'png': 'Portable Network Graphics', 'ps': 'Postscript', 'emf': 'Enhanced Metafile', 'svg': 'Scalable Vector Graphics', 'eps': 'Encapsulated Postscript', 'rgba': 'Raw RGBA bitmap', 'pdf': 'Portable Document Format', 'tif': 'Tagged Image File Format'}

Thursday, August 14, 2014

Android Glossary


obfuscate: Obfuscators replace these names with short, machine generated alternatives.This makes it more difficult to intuit the purpose of these functions without access to the original source code.
http://android-developers.blogspot.com/2010/09/securing-android-lvl-applications.html
http://android-developers.blogspot.com/2010/09/proguard-android-and-licensing-server.html

Java reflection: Enable language to inspect and dynamically classes, methods and attributes at run-time.

dynamic class-loading: Load and reload class dynamically at runtime in Java. The program doesn't know the name of the class before being executed. In Java, loading classes at runtime must be done by subclass of java.lang.ClassLoader.

root exploit:Attain privileged control (root access) of the system.  While the concept of Apple community's jailbreak is different by two additional factors of unlocking the bootloader, enable sideloading.

zero day: Malwares/Virus whose detection signature has not been obtained.

honeypot: a trap set to detect, deflect, or, in some manner, counteract attempts at unauthorized use of information systems

Wednesday, August 13, 2014

Repeating number problem - 3


Given a (potentially large) array of integers, all but one repeating an even number of times, how would you find the one repeating an odd number of times in an efficient way? eg [1 2 3 3 2 2 1 4 2] should return 4

Method 1: Use map data structure to track the occur times of the number in the list. Then loop through the map to find the number occurs even times.
Output:
Number occur even times: 6


Method 2: Find the unique set of the integer array and append the unique set to the original array. Then XOR the new array leaving only the number repeat even times. The trick is XOR with odd times itself remains zeros, XOR with even times itself remains itself.
Output:
Number that occurs even times: 6

Repeating number problem - 2

Given an array in which all numbers except two are repeated once. (i.e. we have 2n+2 numbers and n numbers are occurring twice and remaining two have occurred once). Find those two numbers in the most efficient way.

Method 1: Use xor operators only to find the two non duplicate numbers. Xor all (2n+2) numbers first, then the n duplicate numbers are cancelled out, leaving the two unique number being XORed. The set bits in the result indicate bits at which the two numbers differ. The bit in a specific position can be either set or clear. Correspondingly, we can divide the numbers into set group numbers and clear group numbers w.r.t the set/diff bits in the result. Since all the other numbers repeat once, it doesn't change the XORed result either in the set group or the clear group. Thus, the XOR of set group and clear group are the two numbers occurred only once.
To divide numbers into set and clear group, we need to extract the set/diff bit of all 2n+2 numbers, here is the way to extract the right most diff/set bit of the XOR result of all 2n+2 numbers.
int diffBitMask = xor & ~(xor-1);
xor-1 zero the right most set bit and keep the other set bits. The complement operator ~ mask off the other set bits and set the right most one.

The code is as follows:
public class Main {
    public static void main(String args[]) {
        int arr[] = {1, 2, 6, 1, 6, 8, 7, 8};
        findNonDuplicate(arr);
    }
    public static void findNonDuplicate(int arr[]) {
        int xor = 0, xor1 = 0, xor2 = 0;
        // Xors of the all numbers, obtaining the difference mask of two numbers
        for(int i = 0; i < arr.length; i++) {
            xor ^= arr[i];
        }
        int diffBitMask = xor & ~(xor-1);   // Right most set/diff bit
        // Grouping based on the diff bit
        for(int i = 0; i < arr.length; i++) {
            if((arr[i] & diffBitMask) > 0) {
                xor1 ^= arr[i];
            } else {
                xor2 ^= arr[i];
            }
        }
        System.out.println("The two numbers that occur once are: " + xor1 + ", " 
                + xor2);
    }
}   
Output is:
The two numbers that occur once are: 7, 2


Repeating number problem - 1

Here is the collection of problems that related to finding  the duplicate numbers in a array.

1. Given an array of numbers ranging from 1 to N, in which numbers could repeat itself any number of times, find the duplicate numbers in O(n) with constant memory space.

Method 1: Use an extra array keep track of the elements counting.

public class Duplicate {
    public static void main(String args[]) {
        int arr[] = {1, 3, 5, 8, 5, 7, 4, 2, 4};
        findDuplicate(arr);

    }
    public static void findDuplicate(int arr[]) {
        // Using the current number as the key/index to the value
        for(int i = 0; i < arr.length; i++) {
            // Positvie - never seen before
            if( arr[Math.abs(arr[i]) - 1] > 0 )  {
                arr[Math.abs(arr[i]) - 1] =  -arr[Math.abs(arr[i]) - 1];
            }
            // Negative - already seen, duplicate 
            else if(arr[Math.abs(arr[i]) - 1] < 0) {
                System.out.print(Math.abs(arr[i]) + " ");
                arr[Math.abs(arr[i]) - 1] = 0;
            }
            else {// 0 - already detected
            }
        }
        System.out.println();
    }
}
output:
5 4

Wednesday, June 25, 2014

Short travel to Baltimore

趁着QQ去Baltimore开会的机会去感受一下这个马里兰州最大的城市。巴尔的摩是大西洋沿岸重要的海港城市,有着得天独厚的海运条件,紧邻的切萨皮克湾很宽广,而且航道很深,万吨级的远洋轮可直接驶入巴尔的摩港区,它是美国五大湖去,中央盆地与大西洋上联系的一个重要的出海口。在这个工业港口城市里,黑人约占55%,这里既有1812年间美国独立战争时的遗迹,也有内战期间亲南民众与政府军之间的弹火冲突。有意思的是这里是美国国歌的诞生地,1904年的巴尔的摩大火催生了国家标准与技术院(NIST)。
周六上午在费城30街Amtrak火车站搭乘驶向Baltimore的火车。车厢内有免费的Wifi,到Baltimore大概一个小时的路程。刚下火车感觉破破烂烂的,有点费城的味道。出了火车站,我们坐taxi来到之前预订的QualityInn,进门之后有股阴暗发霉的味道,房间里也很阴暗。不过酒店离QQ开会的地方还是很近的,遂我们决定先去那边转转,顺道搞些吃的。我们沿着接到走了10分钟左右就到了Baltimore的商业观光区Inner Harbor。这里是由之前的工业区和居民区转变成的观光旅游去。内港上有各式各样的油轮,皮挺和脚踏船,还有各式各样的餐厅。来到港口城市当然不能少了海鲜,正赶上这里的crab & beer festival,一家历史悠久的海鲜餐厅Phillips就坐落在这里。正好遇到QQ的lab mates,我们4个人就坐下开始享受海鲜大烩了。我们点了著名的blue crab和其他贝壳累的海鲜。抱参之后我们就去看之前查过的出租自行车的店了,这个时候我的肚子又不争气了,回来的途中拉了一路。。
由于受不了QualityInn阴暗的环境,我们第二天又搬进了附近的一家叫作brookshire suits的酒店,这次环境好多了,而且离开会的地方又尽了些。我拿出跑鞋开始了在Baltimore的第一次慢跑,一路上随手拍了一些Inner Harbor的景色。













What you 'can' and 'cannot' do with Tizen SDK for Wearable

I was trying to using Samsung galaxy gear 2 for Bluetooth device discovering and audio recording. However, the SDK wouldn't let me create a native project. So I figured out I could using the Web App API to control the Bluetooth and microphone components of the watch. I was wrong, these features are not supported yet according to the release notes of the SDK.

The Bluetooth

They mentioned in the SDK version 1.0.0b1:
Device APIs to access to a device’s platform capabilities support
  • Alarm, Application, Bluetooth, Filesystem, System Information, Power, Motion(currently pedometer supported), SAP(communication between host and wearable device) API
But in the SDK version 1.0.0b2, they stated:
The Bluethooth API , which was incorrectly listed in the supported API lists in the 1.0.0b1 release note, is now removed in the list

The Microphone/Audio Input

It's not clear whether audio recording is supported or not now. In their release note 1.0.0b1 they stated that the SDK support webkit framework and HTML5 audio/video element. And in the release note 1.0.0b2, they also stated in the fixed bugs section:
  • Audio recording support with Camera API.
    • “audio:true” of MediaStreamOptions is now supported in getUserMedia() API
    • Audio recording is supported with Camera API by passing audio-only MediaStream object to createCameraControl() API
    • Supported audio recording format : AMR and 3GP
In a word, for Tizen SDK for wearable, control of Bluetooth is not supported yet and audio may be supported by using HTML 5 element (here is the link of how to capture audio/video in HTML 5: http://www.html5rocks.com/en/tutorials/getusermedia/intro/).