2014年3月26日 星期三

[Python][CheckIO] Solve Ghosts age

莫名其妙一個簡單的題目寫了兩天於是紀念一下


 def is_fibonacci(num):  
   cur = 0  
   prev1 = 1  
   prev2 = 0  
   while cur < num:  
     cur = prev1 + prev2  
     prev2 = prev1  
     prev1 = cur  
   if num == cur:  
     return True  
   else:  
     return False  
 def checkio(opacity):  
   if opacity == 10000:  
     return 0  
   cur = 10000  
   last_fibonacci = 1  
   for yrs in range(1, 5000):  
     if is_fibonacci(yrs) == True:  
       if cur - yrs == opacity:  
         return yrs  
       else:  
         cur -= yrs  
         last_fibonacci = yrs  
     else:  
       if cur + 1 == opacity:  
         return yrs  
       else:  
         cur += 1  

2014年3月22日 星期六

[Python] Formatted Binary

>>> '{0:08b}'.format(6)
00000110
Just to explain the parts of the formatting string:
  • {} places a variable into a string
  • 0 takes the variable at argument position 0
  • : adds formatting options for this variable (otherwise it would represent decimal 6)
  • 08 formats the number to eight digits zero-padded on the left
  • b converts the number to its binary representation
ref. http://stackoverflow.com/questions/10411085/converting-integer-to-binary-in-python 

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