2020年1月10日 星期五

目錄 檔案2

先找出現在home 的路徑


import os
home=os.path.expanduser("~")
print(home)

==============Q1
開始一個檔案 在home的路徑 建一個檔案 dumu.txt 文字為
 遠上寒山石徑斜,
白雲生處有人家。
停車坐愛楓林晚,
霜葉紅於二月花。

 

import os
home=os.path.expanduser("~")
print(home)
file1=open("/home/up/dumu.txt","a")
file1.write("杜枚 山行")
file1.write("\n遠上寒山石徑斜,")
file1.write("\n白雲生處有人家。")
file1.write("\n停車坐愛楓林晚,")
file1.write("\n霜葉紅於二月花。")
file1.close()

=====================
 Q2:開啟檔案 並讀取檔案

import os
home=os.path.expanduser("~")
print(home)
file2=open("/home/up/dumu.txt","r")
data=file2.readlines()
print(data)
file2.close()


==========
Q3: 列印標題及指標在文件的位址

import os
home=os.path.expanduser("~")
print(home)
file3=open("/home/up/dumu.txt","r")
file3.seek(0) #移動到指定位址
title=file3.read(5) #到指定位址 讀五個字
title_address=file3.tell()
print("標題是{}".format(title))
print("指標在文件的位址是{}".format(title_address))
file3.close()

2020年1月9日 星期四

big o of n

Big o of n ==>演算法的時間複雜度
                 ==>最常用表示理論的上限



時間複雜度的等級  (最下方時間 愈久)


O(1)              常數時間  (constant time)

O(logn)      次線性時間 (sub-linear)

O(n)             線性時間  (linear)

O(n logn)   線性乘對數時間
O(n2)           平方時間  (quadratic)
O(n3)           立方時間  (cubic)
O(2n)           指數時間   (exponential)
O(n!)           階乘時間   (factorial)


實例  from   
from  https://ithelp.ithome.com.tw/articles/10221991



1.O(1)              常數時間  (constant time)
   陣列讀取
   
   fruits=["apple","banana","cherry","dragonfruit","elderberry","fig"]
   print(fruits[5])


2.O(logn)      次線性時間 (sub-linear)
   二分搜尋法



3.O(n)             線性時間  (linear)
    簡易搜尋
   循序搜尋 (Sequential Search)

fruits=["apple","banana","cherry","dragonfruit","elderberry","fig"]
for i in range(len(fruits)):
    if fruits[i]=="fig":
        print("找到了在{}個裡".format(i+1))
        break
    else:

        print("第{}個裡找不到".format(i+1))


4.O(n logn)   線性乘對數時間
   高級排序算法(合併排序 (Merge sort)、快速排序 (Quick sort)


5.O(n2)           平方時間  (quadratic)

    簡單排序算法(選擇排序 (Selection sort)、插入排序(Insertion sort)、氣泡排序 (Bubble sort)


6   O(n3)           立方時間  (cubic)
    Floyd 算法(動態規畫) / 矩陣乘法運算

7.   O(2n)           指數時間   (exponential)
      河內塔(動態規畫)  簡單背包問題(動態規畫)

8. O(n!)           階乘時間   (factorial)

     旅行推銷員問題 - NP