有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年3月29日 星期六

Python 學習小筆記 (五) - Conditionals & Control Flow (下)

Python 學習小筆記 (五) - Conditionals & Control Flow (下)

Python學習小筆記 for Codecademy Python
1.       Boolean運算子在判斷時,不是由左到右!如同數學也用先程除後加減。
在沒有特別括號的情形下,Boolean運算是如下:

Ø not  最先判斷
Ø and 第二判斷
Ø or   最後判斷

Ex:
False or not True and True à True

用括號來表示這樣: False or ((not True) and True)
雖有順序,但建議還是多多使用括號,避免閱讀困難。

2.       if…else… :就跟C很像,但條件式不用括號。
但是if要在條件式後面加上冒號,else後面也要冒號!
Ex:
if  9<19 :
        print "good"
else:
        print "bad"

3.      elif:就是C中的else if,可以在後面加上條件式。
Ex:
if  9<19 :
        print "good"
elif: 9>19
        print "bad"
else:
        print "ok"

Python 學習小筆記 (四) - Conditionals & Control Flow (上)

Python 學習小筆記 (四) - Conditionals & Control Flow (上)


1.       比較(關係)運算:都跟C, C++ 相同
Ø   ==   等於
Ø   !=    不等於
Ø   大於
Ø   >=   大於等於
Ø   <     小於
Ø   <=   小於等於

2.       當比較發生時,回傳的是boolean Type (True or False)
注意:True, False開頭是大寫。(C++是小寫的true, false)
Ex:
bool_test = 5 != 42
print bool_test à 輸出True

3.       邏輯運算 and, or:比較左右兩邊的boolean值,回傳boolean
Python跟C, C++ 很不同。C, C++ 裡面使用 &&, ||
Python直接使用英文and和or:and就是 &&, or 就是 ||
Ex:
bool_test1 = True or False  à True
bool_test2 = True and False à False
bool_test3 = 4 > 9 or False  à True
bool_test4 = 78 != 4 and 100 < 99à False

4.       邏輯運算 not:C, C++ 使用 "!" 代表not。
Python直接使用英文not
Ex:
not True à False
not not Falseà False
not 3**4 < 4**3 à True

Python 學習小筆記 (三) - Date and Time

Python 學習小筆記 (三) - Date and Time

Python 學習小筆記 for Codecademy Python



1.        要使用時間需要import datetime 才能用
用法: from datetime import datetime

2.        datetime.now() 表示現在時間
Ex: print datetime.now() à 2014-03-28 15:51:36.039938

也能用變數來存取這個"現在時間"
Ex: now = datetime.now()

3.        datetime.now() 也可以只取出年、月、日、時、分、秒等
Ex: print datetime.now().year   à 2014
Ex: print datetime.now().month  à 3
Ex: print datetime.now().day   à 28
Ex: print datetime.now().hour   à12
Ex: print datetime.now().minute à6
Ex: print datetime.now().second à32

2014年3月21日 星期五

NTHU OJ - 7665 PD - Cake wants to win

The following program is my ACcepted code for NTHU-7492 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃NTHU 7492 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

先把卡片的數字累加!
讓card[i] 代表 第一張卡片加到第i張卡片的值
之後只要決定 "開頭" 跟 "結尾" 的index
就可以用 card[結尾]-card[開頭] = 從 "開頭" 到 "結尾" 的合
所以只要用 O(N^2) 得時間複雜度

2014年3月19日 星期三

UVa OJ - 374 Big Mod

The following program is my ACcepted code for UVA-374 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UVA 374 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )


在乘法時可以先把數字分開 mod,在乘起來 mod,這樣結果會一樣
Ex: B^P mod M = ( (B^(P-1) mod M) * (B mod M) ) mod M

所以我用Divide and Conquer來解決他
先把 B^P 分成 B^(P/2) * B^(P/2)。
(如果P為奇數就再乘一個 P)
用遞迴的方式求出答案
大概是這樣的感覺  B^P mod M = (B^(P/2) mod M) * (B^(P/2) mod M) mod M

NTHU OJ - 7492 - 最後的紅線!(II)

The following program is my ACcepted code for NTHU-7492 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃NTHU 7492 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )


一看題目就知道找兩個數字的最小公倍數... 

我用先找到最大公因數(GCD),再藉此算出最小公倍數!

2014年3月16日 星期日

Python 學習小筆記 (二) - Strings & Console Output

Python 學習小筆記 for Codecademy Python

用於自我複習跟快速入門 (給會一種以上高階語言的各位)

1.          在 print 程式碼後面打 \ 可以往下接續下面一行的程式碼,
             避免一行的一行程式過長造成閱讀困難。
             Ex: print "abcd" \
             "efg"

2.          字串型態string,可以用兩個雙引號assign。Ex: start = "Hello world!"
3.          字串中的跳脫符號"\" 。Ex: 要輸出’,要在字串中打\’
4.          字串後面加上[index]會回傳字元。Ex: c = "cats" [1] (回傳a)

5.          有關字串的方法(methods)
   回傳長度len() 。Ex: len("fsdf") à 4
   變小寫 lower()。 Ex: "ABCdef".lower() à "abcdef"
   變大寫 upper()。 Ex: "ABCdef".upper() à "ABCDEF"
6.          轉換非字串為字串 str()。 Ex: 數字轉字串, str(3.14) à "3.14"

7.          基本IO: 輸出,直接打 print。Ex: print variavle_name
            輸出字串時可以用 + 連接。 Ex: print "A" + " and " + "B" à "A and B"
                                                               Ex: print "Pi is " + str(3.14) à "Pi is 3.14"

8.          print 一樣有標準格式輸出,像是可以用 %s 輸出字串變數。
            在print輸出的變數後面需要有 %,在接上(variable)。
             Ex:
             str1 = “home”
             str2 = “school”
             print "Let’s go %s, not %s." % (str1, str2)
à "Let’s go home, not school"

2014年3月15日 星期六

Python 學習小筆記 (一) - Python Syntax and Tip Calculator

Python 學習小筆記 for Codecademy Python

用於自我複習跟快速入門,因我對C, C++, JAVA更有經驗,所以這份筆記是以學過前述語言的角度所撰,如果以前學過這三種語言之一,可能會看得比較順,如果沒有請去找正規一點的教學吧XD

→前往 Python 學習小筆記 (二) - Strings & Console Output


1.          statement結尾不用加上。Ex: tmp_int = 5

2.          變數不用宣告,直接用
3.          變數型態有: int, float, bool (用True, False 而非true, false)
4.          變數命名。Ex: variable_bool = True

5.          用# 來加上一行的註解(不是用//開頭)
6.          用"""和"""來加上多行的註解 (不是使用 /* …. */,是使用三個雙引號)

7.          基本IO: 輸出,直接打 print。Ex: print variavle_name

8.          運算式,加(+) 減(-) 乘(*) 除(/) modulo求餘數(%)都一樣
   開方不同,使用兩個*。 Ex: 2 ** 3 à2的3次方 (不是 2^3)


9.          Function 不使用{},用縮排(tab or 空格)來表示
  Function EX:

  def spam():
      eggs = 12
      return eggs
  print spam()