[TOC]
## 前言
題目來自TQC網站公開的參考試題,可以自行去搜尋相關詳情。
自己研究的解法,不一定是最好或最正確的,歡迎留言討論!
若對你有幫助請幫忙按讚以及留言,轉載請註明出處,畢竟也是花費時間心血想出來的。
希望對學習有幫助~~~ ^ O ^
試卷編號:PYT-0001
考試時間100分鐘,滿分100分,合格分數70分。
## TQC Python3 術科參考試題
1. 數字、字串格式處理

* 參考答案
```python=
"""This module print out number of bottles of juice and total price"""
import sys
APPLE_JUICE = 23.34
quantity = input("")
if quantity.isnumeric():
quantity = int(quantity)
print(f"{APPLE_JUICE* quantity:0.2f}")
else:
sys.exit("Enter purchase quantity")
```
* result:

</br></br></br>
2. 選擇敘述與迴圈

* 參考答案
```python=
"""This module print out adjusted score"""
score=input("")
if score.isdigit():
score = int(score)
if score > 100 or score < 0:
print("error")
elif score > 60:
print(score + 10)
else:
print(score + 5)
```
* result:
</br>
</br>

</br></br></br>
3.函式與陣列

* 參考答案
```python=
"""This module calculates the adjusting score"""
def compute(num):
"""compute score"""
if num < 0 or num >100:
return -1
if num > 60:
return num + 5
return num + 10
def main():
"""print out result"""
score = input("")
if score.isdigit():
score = int(score)
print(compute(score))
main()
```
* result:
</br>
</br>

</br></br></br>
4.字串與檔案處理

* 參考答案
```python=
"""This module compares two strings in limit length"""
import sys
def get_input():
"""get two strings from user, both length < 128 iacters"""
strlist = [input("") for _ in range(3)]
if len(strlist[0]) < 128 and len(strlist[1]) < 128:
pass
else:
print("String length should less than 128")
return strlist
def compare(a, b, c):
"""compare 2 strings' overlapping number of is"""
overlape = 0
for i in range(c) :
if a[i] in b[i]:
overlape += 1
if overlape == c:
text = f"{a}"+ " = " + f"{b}"
print(text)
elif overlape < c:
text = f"{a}"+ " < " + f"{b}"
print(text)
def main():
"""print out strings comparing result"""
string1, string2, n = get_input()
if len(string1) < int(n) or len(string2) < int(n) or len(string1) != len(string2):
sys.exit("error")
compare(string1, string2, int(n))
main()
```
* result:
</br>
</br>

</br></br></br>
5.數字相乘

* 參考答案
```python=
"""This module calculates multiplication of a number digit by digit"""
number = input("")
number = list(number)
TEMP = 1
if len(number) <= 9:
for count, num in enumerate(number):
result = TEMP * int(num)
TEMP = result
if count == (len(number) -1):
print(f"{num}" + "=",end="")
else:
print(f"{num}" + "*",end="")
print(result)
```
* result:
</br>

</br></br></br>
6.字串拆解

* 參考答案
```python=
"""This module print out uppercase and lowercase characters
and number of uppercase characters"""
import sys
string = input("")
l_upper = []
l_lower = []
if string.isalpha():
l_upper = [string[i] for i in range(len(string)) if string[i].isupper()]
l_lower = [string[i] for i in range(len(string)) if string[i].islower()]
else:
sys.exit("Enter alphabetical characters")
print(''.join(l_upper))
print(''.join(l_lower))
print(len(l_upper))
```
* result:

</br></br></br>
7.二進位轉十進位

* 參考答案
```python=
"""This module change string from binary to decimal"""
import sys
def get_input():
"""get a binary string less or equal to 10 characters"""
binum = {'0', '1'}
bi_string = input("")
if len(bi_string) <= 10 and set(bi_string)| set(binum) == binum:
length = len(bi_string)
output = 0
for i in range(length):
output = pow(2, length - 1- i)*int(bi_string[i]) + output
else:
sys.exit("Enter a binary string <= 10 chars")
return output
def main():
"""print result of decimal number"""
print(get_input())
main()
```
* result:
</br>
</br>

</br></br></br>
---
## Reference material
* [python doc/re](https://docs.python.org/3/library/re.html)
* [enumerate](https://www.geeksforgeeks.org/enumerate-in-python/)