2014年3月16日 星期日

[Python] key function for sorting

Example
 def checkio(numbers_array):  
   list_num = list(numbers_array)  
   list_num = sorted(list_num, key=abs)  
   return list_num  


[Note]

1. The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes

2. A common pattern is to sort complex objects using some of the object's indices as a key
Example 
>>> student_tuples = [  
     ('john', 'A', 15),  
     ('jane', 'B', 12),  
     ('dave', 'B', 10),  
 ]  
 >>> sorted(student_tuples, key=lambda student: student[2])  # sort by age  
 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]  

[Ref]

https://wiki.python.org/moin/HowTo/Sorting/#Key_Functions

2014年3月8日 星期六

[Python] Roman Numerals Converter

Speedy solution:

1:  def checkio(input):  
2:    ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)  
3:    nums = ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')  
4:     
5:    result = ""  
6:    
7:    for i in range(len(ints)):  
8:       count = int(input / ints[i])  
9:       result += nums[i] * count  
10:      input -= ints[i] * count  
11:     
12:    return result  


ref . http://www.rapidtables.com/math/symbols/roman_numerals.htm

--

人家寫12行我寫108行,真 他媽的

2013年3月9日 星期六

Android layout

Review Android Layout

 <?xml version="1.0" encoding="utf-8"?>  
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
           android:id="@+id/relativeLayout1"  
           android:layout_width="fill_parent"   
           android:layout_height="fill_parent"  
           >  
           <LinearLayout   
                android:id="@+id/LinearLayout02"   
                android:layout_height="wrap_content"  
                 android:layout_width="fill_parent"  
                 android:layout_alignParentBottom= "true">  
                 <Button   
                      android:layout_width="fill_parent"  
                      android:layout_weight="1"  
                     android:layout_height="wrap_content"   
                     android:text="exit"   
                     android:id="@+id/btExit"  
                     android:layout_alignParentBottom= "true"/>  
                <Button   
                     android:layout_width="fill_parent"  
                     android:layout_weight="1"  
                     android:layout_height="wrap_content"   
                     android:text="add"   
                     android:id="@+id/btAdd"  
                     />  
                <Button   
                     android:layout_width="fill_parent"   
                     android:layout_weight="1"  
                     android:layout_height="wrap_content"   
                     android:text="test"   
                     android:id="@+id/btTest"  
                     />  
            </LinearLayout>       
      <FrameLayout   
           android:id="@+id/frameLayout1"  
            android:layout_width="fill_parent"   
            android:layout_height="fill_parent"  
            android:layout_above= "@+id/LinearLayout02">  
            <Button  
                android:id="@+id/btframeLayout"  
                android:layout_width="fill_parent"  
                android:layout_height="fill_parent"  
                android:text="framelayout"/>  
      </FrameLayout>  
 </RelativeLayout>  


效果如下:



Ref.
http://corn0521.blogspot.tw/2011/04/android-relativelayout.html

2013年2月1日 星期五

Concrete Class vs. Abstract Class

Abstract Class

An abstract class is a class for which one or more methods are declared but not defined, meaning that the compiler knows these methods are part of the class, but not what code to execute for that method.

Eg.
 class shape  
 {  
 public:  
  virtual void draw() = 0;  
 };  

You cannot instantiate this class because it is abstract, after all, the compiler wouldn't know what code to execute if you called member draw.


Concrete Class

A class that has any abstract methods is abstract, any class that doesn't is concrete.

Eg.
 class circle : public shape {  
 public:  
  circle(int x, int y, int radius) {  
   /* set up the circle */  
  }  
  void draw() {  
   /* do stuff to draw the circle */  
  }  
 };  
 class rectangle : public shape {  
 public:  
  rectangle(int min_x, int min_y, int max_x, int max_y) {  
   /* set up rectangle */  
  }  
  void draw() {  
   /* do stuff to draw the rectangle */  
  }  
 };  

Now you can instantiate the concrete objects circle and rectangle and use their draw methods:

Eg.
 circle my_circle(40, 30, 10);  
 rectangle my_rectangle(20, 10, 50, 15);  
 my_circle.draw();  
 my_rectangle.draw();  


Question 1: Why would you want to do this?

Ans: To take advantage of the inheritance

Eg.
 std::vector<shape*> my_scene;  
 my_scene.push_back(new circle(40, 30, 10));  
 my_scene.push_back(new rectangle(20, 10, 50, 15));  


Question 2: Why the draw function of shape is abstract, and not just an empty function?

Ans: We don't want objects of type shape, they wouldn't be real things anyway. So it doesn't make sense to define an implementation for the draw method. Making the shape class abstract prevents us from mistakenly instantiating the shape class, or mistakenly calling the empty draw function of the base class instead of the draw function of the derived classes.

--

ref. http://stackoverflow.com/questions/2149207/what-is-the-difference-between-a-concrete-class-and-an-abstract-class

Design patterns - overview


2013年1月27日 星期日

英文俚語一則

 dafaq  
 【義】Shortened form of "The Fuck". Also used instead of "What The Fuck".   
 【例】Dafaq is this?  


2013年1月23日 星期三

英文俚語三則


 whipped  
 【義】Being completely controlled by your girlfriend or boyfriend  
 【例】He is whipped  

Whipped 通常用在男生身上,意思類似妻(女友)管嚴。



 tramp  
 【義】Any woman who will open her legs for anyone, except for me.  
 【例】She is such a tramp  

中文意思有點像把不到的花蝴蝶(貶意)

























 fucked in the head  
 【義】Idiotic, stupid, not too clever.  
 【例】He is fucked in the head  

就是stupid的意思,簡單稱為腦殘。 下面圖裡的學生就是fucked in the head的經典。