by Rain
my_list = [0, 1, 2, 3, 4]
print(sum(my_list))
10
data = list(range(0, 10))
print("data: \t\t", data)
print("sum(data): \t", sum(data))
print("sum(data, 2): \t", sum(data, 2))
data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] sum(data): 45 sum(data, 2): 47
def my_sum(data):
value = 0 # 剛開始總何為零
for i in data: # 逐個增加
value += i
return value # 回傳總和
my_credits = [12, 15, 14, 14, 15, 12, 18, 14]
print("sum(my_credits): \t", sum(my_credits))
print("my_sum(my_credits): \t", my_sum(my_credits))
sum(my_credits): 114 my_sum(my_credits): 114
用於取最大值,最小值
>>> eight_queens([[0, 0], [1, 4], [2, 7], [3, 5], [4, 2], [5, 6], [6, 1], [7, 3]])
True
實現這個 double_side_list 的功能
第一個參數是中心,第二個參數是邊界,第三個參數是間隔
輸出有回文效果的 list
各位會打橋牌嗎~?
目前手上有一副牌,已經分好 [黑桃、紅心、鑽石、梅花],像這樣:
[["2", "K", "6", "A", "10", "Q"], ["8", "K"], ["9", "Q", "8", "A"], ["7"]]
import timeit
my_code = "a = 2+ 2"
print (timeit.timeit(stmt = my_code, number = 10000000))
0.1237273857756027
import timeit
my_code = "a = 2+ 2"
print (timeit.timeit(stmt = my_code, number = 10000000))
0.1268828928702971
print([x for x in range(10)]) # 類似 for 迴圈,不過可以直接回傳 list
print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
import timeit
my_code_0 = "my_array_0 = [x for x in range(1000)]"
my_code_1 = "my_array_1 = list(range(1000))"
print (timeit.timeit(stmt = my_code_0, number=100000), "\n")
print (timeit.timeit(stmt = my_code_1, number=100000), "\n")
3.3298487705082436 1.2840124134359763
import timeit
# 要計時的模組載入
my_setup = "import random"
my_code = "my_value = random.randint(0, 10)"
print (timeit.timeit(setup= my_setup, stmt= my_code, number= 100000))
0.11075855429419335
import timeit
my_code_0 = "my_array_0 = [x for x in range(1000)]"
my_code_1 = "my_array_1 = list(range(1000))"
print (timeit.repeat(stmt = my_code_0, repeat= 5, number = 100000), "\n")
print (timeit.repeat(stmt = my_code_1, repeat= 5, number = 100000), "\n")
[3.1711770519183347, 3.1522771945051318, 3.123463363224232, 3.1682753565580697, 3.0885824833436004] [1.3151003395096836, 1.222520321349009, 1.2315335552970055, 1.2361298815910118, 1.2993275447726091]
import time
print("休息 10 分鐘~ \n")
# time.sleep(600)
print("上課啦!上課啦!上課啦!上課啦!上課啦!上課啦!")
休息 10 分鐘~
import itertools
for i in itertools.combinations("1234", 2):
print(i)
('1', '2') ('1', '3') ('1', '4') ('2', '3') ('2', '4') ('3', '4')
from itertools import combinations_with_replacement
for i in combinations_with_replacement("1234", 2):
print(i)
('1', '1') ('1', '2') ('1', '3') ('1', '4') ('2', '2') ('2', '3') ('2', '4') ('3', '3') ('3', '4') ('4', '4')
from itertools import permutations
for i in permutations("1234", 2):
print(i)
('1', '2') ('1', '3') ('1', '4') ('2', '1') ('2', '3') ('2', '4') ('3', '1') ('3', '2') ('3', '4') ('4', '1') ('4', '2') ('4', '3')
import random
# 午餐列表
lunch = ["好煮義", "大肥鴨", "三商巧福", "鄉村", "一心二葉", "不要吃"]
# 隨機選擇午餐
selectLunch = random.randint(0, len(lunch)-1)
print(lunch[selectLunch])
一心二葉
from random import randint
def project_excuse():
pool_0 = ["負責談合約的", "坐我旁邊那個", "(閉上眼睛)", "到上架前一天"]
pool_1 = ["設計師", "工程師", "行銷", "PM"]
pool_2 = ["還不知道有這專案", "根本是在整我", "需求沒說清楚", "只會出一張嘴"]
return pool_0[randint(0, len(pool_0)-1)]+ pool_1[randint(0, len(pool_1)-1)]+ pool_2[randint(0, len(pool_2)-1)]
print(project_excuse())
坐我旁邊那個設計師根本是在整我
import random
print("random.random()= \t", random.random())
print("random.uniform(0, 10)= \t", random.uniform(0, 10))
random.random()= 0.39129216850749593 random.uniform(0, 10)= 7.413711633930923
凱薩加密法是其中一種替換加密法
其中有個關鍵數值是偏移量,明文會依據偏移量替換為密文
假設明文為 xvillage,偏移量為 3
那麼明文中每個字母會依照 A→Z 的方向位移3個字母
因此密文就是 ayloodjh
目標:
寫一個函數實現凱薩加密法功能
第一個參數為明文字串,須為全小寫英文字母
第二個參數為偏移量,為整數
最後會回傳凱薩加密後的密文
有一副新買的撲克牌,已將鬼牌抽掉
剩下的排組為 deck, 4 種花色各 13 張
現在需要將牌發成四人份,一人 13 張
洗牌過後依照固定順序輪流發牌
最後以二維陣列呈現四人手牌:
[第一人手牌, 第二人手牌, 第三人手牌, 第四人手牌]
deck = [x+y for x in ["黑桃", "紅心", "鑽石", "梅花"]
for y in ["A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"]]
print(deck)
['黑桃A', '黑桃K', '黑桃Q', '黑桃J', '黑桃10', '黑桃9', '黑桃8', '黑桃7', '黑桃6', '黑桃5', '黑桃4', '黑桃3', '黑桃2', '紅心A', '紅心K', '紅心Q', '紅心J', '紅心10', '紅心9', '紅心8', '紅心7', '紅心6', '紅心5', '紅心4', '紅心3', '紅心2', '鑽石A', '鑽石K', '鑽石Q', '鑽石J', '鑽石10', '鑽石9', '鑽石8', '鑽石7', '鑽石6', '鑽石5', '鑽石4', '鑽石3', '鑽石2', '梅花A', '梅花K', '梅花Q', '梅花J', '梅花10', '梅花9', '梅花8', '梅花7', '梅花6', '梅花5', '梅花4', '梅花3', '梅花2']
現在有個二維陣列,
每個元素皆為 [element_0, element_1, ......, ele_n], 長度皆為 n
element_0, ...... element_n 皆為可排序之數值, 請寫一個排序函數
依次比較 element_0, ......, ele_n, 進行上升排序
import random
datas = [[random.randint(0, 5) for x in range(10)] for y in range(10)]
for data in datas:
print(data)
[1, 2, 0, 0, 5, 2, 1, 4, 5, 5] [4, 5, 4, 2, 5, 3, 3, 5, 2, 3] [0, 5, 0, 4, 0, 3, 4, 2, 5, 5] [3, 1, 2, 1, 1, 3, 1, 3, 0, 0] [1, 4, 1, 5, 5, 4, 2, 0, 1, 3] [0, 4, 0, 3, 4, 0, 3, 0, 1, 5] [5, 5, 3, 4, 4, 2, 4, 4, 5, 4] [0, 3, 2, 4, 4, 1, 2, 4, 2, 2] [0, 3, 0, 5, 5, 3, 5, 1, 4, 1] [5, 4, 2, 1, 0, 1, 1, 4, 1, 4]
[[0, 1, 4, 1, 5, 3, 3, 4, 2, 5],
[0, 1, 4, 3, 2, 5, 5, 0, 3, 1],
[0, 2, 1, 1, 4, 3, 4, 1, 5, 1],
[0, 3, 2, 5, 4, 0, 0, 5, 4, 0],
[1, 5, 2, 1, 3, 0, 3, 2, 0, 2],
[2, 5, 2, 0, 2, 0, 0, 4, 4, 2],
[3, 3, 4, 5, 0, 5, 4, 0, 1, 1],
[5, 0, 5, 2, 4, 3, 1, 4, 0, 2],
[5, 4, 5, 4, 1, 5, 5, 3, 5, 3],
[5, 4, 5, 5, 1, 4, 1, 3, 3, 5]]