文章目錄
- Input & Output
- Variables & Data types
- Python字符串重復(字符串乘法)
- 字符串和數字連接在一起print時,要強制類型轉換int為str
- 用input()得到的用戶輸入,是str類型,如果要以int形式計算的話,需要強制類型轉換為int
- 我們可以只使用一個變量user_input來節省內存
- convert string type to date type
- convert date to string
- Multi-line code statement 換行符
- 在括號內,行的延續的自動的
- Escape sequence 轉義字符
- String format
- string format 中限制輸入占位大小的同時小數點后位數
- Arithmetic operators
- Fundamentals of the Analysis of Algorithm Efficiency
- Algorithm analysis framework 算法分析框架
- 1. Measuring Input Sizes
- 2. Units for Measuring Running Time
- 3. Order of growth
- 4. Worst-Case, Best-Case, and Average-Case Efficiency
- Summary
- 漸進式符號
- no faster
- at least as fast as
- at same rate
- Summary
- Some Properties
- Using Limits for Comparing Orders of Growth
- Analysis of non-recursive algorithms 非遞歸算法的分析
- Analysis of recursive algorithms 遞歸算法的分析
- Examples
- 求n!
- 重要的遞歸類型
- For-loop
- 字符串操作
- 字符串內容大寫/小寫
- 找字串位置,找不到返回-1
- 字符串長度
- 根據下標返回字符串中對應字符
- 切割字符串
- Data Structure
- Abstract data type (ADT)
- Function 函數
- 四舍五入保留小數點后多少位函數
- min,max函數是python內置的
- random函數
Input & Output
Variables & Data types
str: a string represents a sequence of characters.
int: an integer, a whole number
float: a decimal number
bool: a boolean value is either True or False.
Date data type: including year, month, day, (not the time)
Date-time data type: including year, month, day, hour, minute, second, …
Python字符串重復(字符串乘法)
字符串和數字連接在一起print時,要強制類型轉換int為str
用input()得到的用戶輸入,是str類型,如果要以int形式計算的話,需要強制類型轉換為int
我們可以只使用一個變量user_input來節省內存
convert string type to date type
strptime
convert date to string
strftime
Multi-line code statement 換行符
在括號內,行的延續的自動的
Line continuation is automatic when the split comes while a
statement is inside parenthesis ( , brackets [ or braces {
Escape sequence 轉義字符
String format
格式 | 意義 |
---|---|
<15 | left alignment, using 15 spaces |
^25 | center alignment, using 25 spaces |
>15 | right alignment, using 15 spaces |
string format 中限制輸入占位大小的同時小數點后位數
還可以通過這種方式實現四舍五入取整
.0f
Arithmetic operators
Floor division = 地板除 = 向下取整除
floor division地板除是什么意思
向下取整除,就是地板除 floor division
向上取整除,就是天花板除,ceil division
來自 https://zhuanlan.zhihu.com/p/221901326
Fundamentals of the Analysis of Algorithm Efficiency
Algorithm analysis framework 算法分析框架
Analysis of algorithms means to investigate an algorithm’s efficiency with respect to resources: running time and memory space
算法分析是指研究一個算法在資源方面的效率:運行時間和內存空間。
1. Measuring Input Sizes
Efficiency is defined as a function of input size.
F(n)
2. Units for Measuring Running Time
Count the number of times an algorithm’s basic operation is executed
計算一個算法的基本操作被執行的次數
Basic operation: the operation that contributes the most to the total
running time.
例如,基本操作通常是算法最內部循環中最耗時的操作。
3. Order of growth
4. Worst-Case, Best-Case, and Average-Case Efficiency
==Efficiency (# of times the basic operation will be executed) ==
Average case:
Efficiency (#of times the basic operation will be executed) for a typical/random
input of size n. NOT the average of worst and best case. How to find the
average case efficiency?
平均情況。對于大小為n的典型/隨機輸入的效率(基本操作將被執行的次數),而不是最壞和最好情況的平均值。如何找到平均案例的效率?
Summary
算法的運行時間(空間)隨著其輸入大小的增加而增長的階數為無窮大。
對于相同大小的輸入,一些算法的效率可能有很大的不同
漸進式符號
no faster
at least as fast as
at same rate
Summary
Some Properties
意義:算法的整體效率將由增長順序較大的部分決定。
Using Limits for Comparing Orders of Growth
所有的對數函數loga n都屬于同一個類別
所有相同度數k的多項式都屬于同一類別
指數函數對于不同的a有不同的增長順序
Analysis of non-recursive algorithms 非遞歸算法的分析
Analysis of recursive algorithms 遞歸算法的分析
- 計算遞歸調用的次數
- 解決遞歸問題,或通過后向替代或其他方法估計解決方案的數量級
Examples
求n!
重要的遞歸類型
For-loop
range(0,10) 范圍是左閉右開
字符串操作
字符串內容大寫/小寫
.upper()
.lower()
找字串位置,找不到返回-1
字符串長度
根據下標返回字符串中對應字符
切割字符串
[i:j] 范圍左開右閉,從下標為i的字符到下標為j-1的字符,獲得的子串長度為j-i
Data Structure
data, relationship , operation
Abstract data type (ADT)
Function 函數
四舍五入保留小數點后多少位函數
min,max函數是python內置的
random函數
左閉右閉