追蹤者

2018年11月27日 星期二

Python 商管程設(一) 第四周練習 + 作業

---
Code
---
import math

c = int(input())
r = int(input())
N = int(input())
q = int(input())
list = []
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))

exp = 0
sum = 0.0
D = 0.0
maxP = 0

for p in range(0,q+1) :
exp = r * p - c * q
if p != q :
sum += list[p] * exp
D += list[p]
else :
sum += (1-D) * exp
break

print(int(sum))

**************** 第四周作業 ******************
承上題,現在不給定進貨量,求在此機率分布下,各種進貨量(0到8)
所能得到之做大收益為何?  在何進貨量能夠得到此收益?
輸出 :
可獲得做大收益之進貨量 空格 最大收益
---
code
---
import math

c = int(input())
r = int(input())
N = int(input())
list = []
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))
list.append(float(input()))

exp = 0 # 該進貨量下之預期收益
sum = 0.0
D = 0.0
maxP = 0.0 # 最大利潤
qMax = 0 # 可達最大利潤之進貨數
N = 8


for q in range(0,N+1) :
sum = 0
D = 0.0
for p in range(0,q+1) :
# p 為 不大於進貨量下之需求量
exp = r * min(p,q) - c * q
if p != q :
sum += list[p] * exp
D += list[p]
else:
sum += (1-D) * exp
break
if sum > maxP :
maxP = sum
qMax = q


profit = int(maxP)
print(str(qMax)+" "+str(profit))














2018年9月6日 星期四

Python 商管程設(一) 第三周作業

題目敘述
如果你在一家零售店幫消費的客人結帳,你可能需要快速地挑出合適且數量正確的鈔票與零錢。假設客人的消費金額 aa 一定是 1 到 1000 之間的整數,而你有無限量的 500、100、50、10、5、1 這些面額的鈔票和零錢,我們希望你能依照下面的規則找錢:

你找的錢的總額要是 1000 - a1000−a。
與其給客人五張 100 元,不如給他一張 500 元;與其給客人兩個 50 元,不如給他一張 100 元……依此類推。
以下是一些範例:

如果客人消費 200 元,你應該找給他 1 張 500 元和 3 張 100 元。
如果客人消費 286 元,你應該找給他 1 張 500 元、2 張 100 元、1 個 10 元和 4 個一元。
如果客人消費 925 元,你應該找給他 1 個 50 元、2 個 10 元和 1 個 5 元。
在本題中,你將會被給予上述的整數 aa,而你要找出符合上述規則的唯一找錢方式。

輸入輸出格式
在每筆測試資料中,會有一個整數 aa 代表客人的消費金額,aa 會介於 1 到 999 之間(包含 1 跟 999)。讀入 aa 之後,你會依照題目指定的規則找出每種面額的鈔票或銅板應該要給幾張或幾個,然後由面額大至面額小依序輸出所需鈔票張數或銅板個數,但如果不應該找給客人某個面額的鈔票或銅板,就跳過該面額不要輸出。因為這樣一來可能只輸出少於 6 個數字,會不知道怎麼對應到面額,因此現在要把面額與所需張數(個數)成對地輸出,中間用一個逗點和一個空格隔開,而面額與面額之間用一個分號和一個空格隔開。



**************
CODE :
a = int(input()) # a介於1~999
tmp = 1000 - a
cnt = 0 # 計算非零面額數
p1 = tmp // 500
if p1 != 0:
cnt += 1
tmp = tmp - p1 * 500
p2 = tmp // 100
if p2 != 0:
cnt += 1
tmp = tmp - p2 * 100
p3 = tmp // 50
if p3 != 0:
cnt += 1
tmp = tmp - p3 * 50
p4 = tmp // 10
if p4 != 0:
cnt += 1
tmp = tmp - p4 * 10
p5 = tmp // 5
if p5 != 0:
cnt += 1
tmp = tmp - p5 * 5
p6 = tmp
if p6 != 0:
cnt += 1


if p1 != 0:
print("500, "+str(p1), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p2 != 0:
print("100, "+str(p2), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p3 != 0:
print("50, "+str(p3), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p4 != 0:
print("10, "+str(p4), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p5 != 0:
print("5, "+str(p5), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p6 != 0:
print("1, "+str(p6), end="")
p6 = 0
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")

*** End Code ***
其實P6 那也可以不用判斷cnt 直接印出空白









2018年8月30日 星期四

Python 商管程設(一) 第二周作業

如果你在一家零售店幫消費的客人結帳,你可能需要快速地挑出合適且數量正確的鈔票與零錢。假設客人的消費金額 a 一定是 1 到 1000 之間的整數,而你有無限量的 500、100、50、10、5、1 這些面額的鈔票和零錢,我們希望你能依照下面的規則找錢:
  1. 你找的錢的總額要是 1000 - a
  2. 與其給客人五張 100 元,不如給他一張 500 元;與其給客人兩個 50 元,不如給他一張 100 元……依此類推。
以下是一些範例:
  1. 如果客人消費 200 元,你應該找給他 1 張 500 元和 3 張 100 元。
  2. 如果客人消費 286 元,你應該找給他 1 張 500 元、2 張 100 元、1 個 10 元和 4 個一元。
  3. 如果客人消費 925 元,你應該找給他 1 個 50 元、2 個 10 元和 1 個 5 元。
在本題中,你將會被給予上述的整數 a,而你要找出符合上述規則的唯一找錢方式。

輸入輸出格式

在每筆測試資料中,會有一個整數 a 代表客人的消費金額,a 會介於 1 到 1000 之間(包含 1 跟 1000)。讀入 a 之後,你會依照題目指定的規則找出每種面額的鈔票或銅板應該要給幾張或幾個,然後在一行中依序輸出六個整數,分別代表面額由大到小的鈔票或零錢所需的張數或個數。如果某種面額的鈔票或銅板是不需要的,針對該面額就輸出 0。兩個整數之間用一個空白字元隔開。請注意最後面一個數字後面沒有空格。
--------------------
注意: 空白字元隔開:
print(b, end=' ')
--------------------
# code

a = int(input())
p1 = int()
p2 = int()
p3 = int()
p4 = int()
p5 = int()
p6 = int()
o = 1000 - a
p6 = o%10
if(p6==0):
p5 = 0
elif(p6>=5):
p5 = 1
p6 = p6 - 5
else:
p6 = p6
p5 = 0

p4 = (o%100)
if(p4==0):
p3 = 0
elif(p4>=50):
p3 = 1
p4 = (p4 - 50)//10
else:
p4 = p4//10
p3 = 0
p2 = o%1000
if(p2==0):
p1 = 0
elif(p2>=500):
p1 = 1
p2 = (p2 - 500)//100
else:
p2 = p2//100
p1 = 0

print(p1, end=' ')
print(p2, end=' ')
print(p3, end=' ')
print(p4, end=' ')
print(p5, end=' ')
print(p6)





2018年8月24日 星期五

用 Python 做商管程式設計(一)

02-05 跳脫字元&基本運算
    print("hello\nworld") => 換行
    \t 是空格
 *乘除運算
3**(5/2)   => 3的2.5次方 = 15.588
3**(5//2)  => 5//2 為取floor 即 2 = 3^2 = 9

02-06 input() 由keyboard 輸入
type() => 取該變數的類別

ex:
num1 = int()
num2 = int()
num1 = int(input()) => 要有宣告int,不然就會被視為字串
num2 = int(input())
print(num1+num2)

02-08 除錯
*syntex error : 語意(法)錯誤
*logic error :  邏輯錯誤

02-09 notepad++ 執行python
點執行輸入 : cmd /k C:/Python36/python "$(FULL_CURRENT_PATH)" $ PAUSE $ EXIT
若已設置好環境變數 輸入: cmd /k python "$(FULL_CURRENT_PATH)" $ PAUSE $ EXIT
儲存執行 > 設定快捷鍵(F10)

02-練習
記得處理型態轉換時,變數放在int的括號內,如 : int(x)

03-a04 字串
chr() : function 丟 -127~128 查詢ASCII CODE的值為何?
len() : 回傳字串長度

03-b01 型態轉換
int() : 將數字轉為整數型態
float() : 將數字轉為浮點數型態
str() : 將數字轉為字串

03-b02 輸入輸出
print() : 輸出
逗點區隔,例如:
    print("a+b=", a+b)  //a+b本來是一個數字,它會被先轉成字串,再和前面接起來。
*注意: 當用逗點隔開時,兩者中間會自動加一個空白,若不想要空白記得用+號
    print("My income is $"+str(income))

03-c01 條件式判斷
if , else , elif
if 或else 後面要冒號,敘述句要縮排(indention)
縮排大小要一致,tab和空四格不同,
if a:
    then b
if c:
    then d
03-d01 註解
# : 一行
""" : 一區塊



2018年1月18日 星期四

精通Python(二)

第三章
✪ 串列、Tuple、字典、集合
*Tuple 是不可變的,當你只派一個元素給它就不能更改了,串列是可變的,可以插入、刪除元素。

*串列
-用[]或list()建立
>>>weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday']
>>>another_empty_list = list()
-用list()將其他資料類型轉換成串列
>>>list('cat')
['c', 'a', 't']
-用一個範圍的位移值來以slice取出項目
將串列反過來
>>> marxes = ['gg','hh','cc']
>>> marxes[::-1]
['cc', 'hh', 'gg']
-用append() 將項目附加到結尾,和用+=結合串列
>>> others = ['gu','kk']
>>> marxes + others
['gg', 'hh', 'cc', 'gu', 'kk']
>>> marxes.append(others)    #(會被加成一個串列項目)
>>> marxes
['gg', 'hh', 'cc', ['gu', 'kk']]
-用insert()與位移值加入項目
>>> marxes.insert(3,'apple')
>>> marxes
['gg', 'hh', 'cc', 'apple', ['gu', 'kk']]
-用del 與 位移值刪除一個項目
>>> del marxes[-1]
>>> marxes
['gg', 'hh', 'cc', 'apple']
-用remove 與值刪除項目
>>> marxes.remove('apple')
>>> marxes
['gg', 'hh', 'cc']
-用pop()與位移值取得一個項目並刪除他
 # 如果呼叫pop() 並使用一個位移值他會回傳該位移值的項目,如果沒有使用引數,他會使用-1
 # pop(0)會回傳串列開頭,pop()和pop(-1)回傳串列結尾
-用index 與值 來尋找某項目的位移值
>>> marxes.index('hh')
1
-用in 來測試值
>>> 'hh' in marxes
True
>>> 'bird' in marxes
False
-用count()算出某個值出現次數
-用join()來轉換字串
-用sort() 排序項目
@ sort()會就地排序串列本身
@ sorted() 會排序串列並回傳副本
>>> sorted_marxes = sorted(marxes)
>>> sorted_marxes
['cc', 'gg', 'hh']
>>> marxes # sorted_marxes 是一個副本,建立他不會更改原先marxes內順序
['gg', 'hh', 'cc']
>>> marxes.sort() # sort()則會改變
>>> marxes
['cc', 'gg', 'hh']
-多類型sort
>>> numbers = [2,1,4.0,3]
>>> numbers.sort()
>>> numbers
[1, 2, 3, 4.0]
-用=來指派,用copy來複製
>>> a= [1,2,3]
>>> a
[1, 2, 3]
>>> b=a
>>> b
[1, 2, 3]
>>> a[0] = 'hello'
>>> a
['hello', 2, 3]
>>> b # 注意 b 只是參考 與 a 同樣的串列物件無論我們用名稱a,b來變更串列內容,兩者都會反映出來
['hello', 2, 3]
>>> b[1] = 'josh'
>>> b
['hello', 'josh', 3]
>>> a
['hello', 'josh', 3]

* Tuple
-用()建立 Tuple
>>>empty_tuple = ()
-要讓tuple 有一個或多個元素,則在每個元素後面加上一個逗號
>>> one_marx = 'gg',
>>> one_marx
('gg',)
 多個元素
>>> marx_tuple = 'gg','cc','hh'
>>> marx_tuple
('gg', 'cc', 'hh')
>>> marx_tuple = ('zz','nn','mm')   # 可用括號括起來
>>> marx_tuple
('zz', 'nn', 'mm')

*字典(dictionary) - 不在乎順序,每個值有獨一無二的鍵
-用{}建立字典
>>> empty_dict = {}
>>> empty_dict
{}
>>> bierce = {"book" : "something to read","banana":"a fruit","man":"a  gender"}
>>> bierce
{'book': 'something to read', 'banana': 'a fruit', 'man': 'a  gender'}
-用dict()轉換,每個序列的第一個項目都會被當成鍵,第二個項目會被當成值
-用[鍵]添加或變更項目
-用update()合併
>>> first = {'a':'1','b':'2'}
>>> first
{'a': '1', 'b': '2'}
>>> second = {'b':'hello'}
>>> first.update(second)
>>> first
{'a': '1', 'b': 'hello'} #此合併第二個字典會勝出
-用del 與鍵刪除項目
>>> del first['b']
>>> first
{'a': '1'}
-用clear()刪除所有項目
-用 in 來測試鍵
-用 [鍵]來取得項目 或用 get()
>>> first['a']
'1'
>>> first.get('a')
'1'
>>> first.get('b')
>>> # 得到 none
-用keys()得到所有的鍵
-用values()得到所有的值
-用items()取得所有的鍵/值對

*集合 像是被移除值,只留下鍵的字典
-以set()來建立 或將一個或多個以逗號分隔的值包在大括號
>>> even_numbers = {0,2,4,6,8}
>>> even_numbers
{0, 2, 4, 6, 8}
>>> odd_numbers = {1,3,5,7,9}
>>> odd_numbers
{1, 3, 5, 7, 9}
-轉換其他類型
>>> set('letters')
{'r', 'e', 's', 'l', 't'}
-結合與運算子
集合交集運算子 &
>>>for name, contents in drinks.items():
if contents & {'vermouth','orange juice'}:
print(name)
# & 運算子會產生一個集合,存有你所比較的兩個串列內都有的項目
-取得交集
>>> a = {1,2}
>>> b = {2,3}
>>> a&b   # 或者a.intersection(b)
{2}
-取得聯集 | # a|b  或者 a.union(b)
-取得差集 - # a-b  或者 a.difference(b)
-互斥或   ^ 只屬於其中一個的項目 # a^b  或者 a.symmetric_difference(b)
>>> a^b
{1, 3}
-用 <= 或 issubset檢查某集合是否為另一集合之子集

2018年1月17日 星期三

精通Python(一)

第二章
✪ 數字、字串、變數
*Python 是 強類型(strong typing) 物件的類型無法變更,即使值是可變的
*Python 變數 :  變數只是個名稱。賦值不會將值複製,他只會指派一個"名稱"給含有該資料的物件而已。
*名稱的第一個字元不能使用數字
*除法有兩種
    -算出浮點除法 :
    >>> 9/5
    1.8
    -算出整數除法 :
 >>> 9//5
    1
*同時取得商和餘數 :  divmod
    >>> divmod(9,5)
    (1,4)
基數
1. 二進位 :
>>>0b10
2
2.八進位 :
>>>0o10
8
3.十六進位 :
>>>0x10
16
*類型轉換
>>> int(True)
1
>>> int(98.6)
98
>>> int('100')  (只包含純整數的字串)
100
>>> int('98.6')  error
>>> int('44 is good') error
*int 大小 : 32位元 可儲存 -(2^31)~2^31-1
*字串
- str() 轉換資料類型 :
>>>str(98.6)
'98.6'
- 用\轉義 :
>>> pd = 'a for apple, \nb for bird, \nc for cat.'
>>> print(pd)
a for apple,
b for bird,
c for cat.
\n 換行 \t tab
-用+結合常值字串或字串變數
>>> "hello"+"how are you"
'hellohow are you'
- 用* 複製
>>> start = 'na' * 4 + '\n'
>>> print(start)
nananana

-使用 [開始:結束:間隔] 的 slice
  • [:] 會擷取整個序列,從開始到結束
  • [start:] 會指定從 start 到結束 
  • [:end] 會指定從開始到end位移值-1
  • [start:end] 會指定從start 位移值 到 end 位移值-1
  • [start: end : step] 會擷取從start位移值到end位移值-1,跳過step元
>>> letters = 'abcdefghijklmnopqrstuvwxyz'
>>> letters[20:]
'uvwxyz'
>>> letters[12:15]
'mno'
>>> letters[-3:]
'xyz'
>>> letters[18:-3]
'stuvw'
>>> letters[::7]
'ahov'
>>> letters[4:20:3] 從位移值 4到 19 間隔3個字元
'ehknqt'
- len() 取得長度 
>>>len(letters)
26
- split()分割
>>> todos = 'hello,ready,final'
>>> todos.split(',')
['hello', 'ready', 'final']
- join 結合
>>> cp_list = ['arr','bww','ckk']
>>> cp_string = ','.join(cp_list)
>>> print(cp_string)
arr,bww,ckk
- find() 找某字在字串中第一次出現的位移植
>>> word  ='the'
>>>poem.find(word)
73
- count() 數某字出現幾次
-strip() 移除某序列 
>>> setup = 'a duck goes into a bar...'
>>> setup.strip('.')
'a duck goes into a bar'
-將所有字元改為大寫
>>> setup.upper()
'A DUCK GOES INTO A BAR...'
- replace() 替換
>>> setup.replace('duck','rabbit')
'a rabbit goes into a bar...'
# 注意 因為字串是不可變的,這些範例都不會實際改變setup字串。每個範例都只是取出setup的值,做一些事情後,再以新字串回傳結果