---
# System prepended metadata

title: TQC Python3術科參考解答-2/2
tags: [Python, TQC]

---

[TOC]

## 前言
題目來自TQC網站公開的參考試題，可以自行去搜尋相關詳情。
自己研究的解法，不一定是最好或最正確的，歡迎留言討論!
若對你有幫助請幫忙按讚以及留言，轉載請註明出處，畢竟也是花費時間心血想出來的。
希望對學習有幫助~~~ ^ O ^

試卷編號:PYT-0001

考試時間100分鐘，滿分100分，合格分數70分。

## TQC Python3 術科參考試題

1. 數字、字串格式處理
![image](https://hackmd.io/_uploads/HJCftUfLC.png)


* 參考答案
```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:
![image](https://hackmd.io/_uploads/SylNfDzLA.png)


</br></br></br>

2. 選擇敘述與迴圈
![image](https://hackmd.io/_uploads/HJyzEDzUA.png)

* 參考答案
```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:
![image](https://hackmd.io/_uploads/rJNUnwzLR.png)</br>
![image](https://hackmd.io/_uploads/HyAInvG8A.png)</br>
![image](https://hackmd.io/_uploads/HkPD2DfUA.png)

</br></br></br>

3.函式與陣列
![image](https://hackmd.io/_uploads/rkUc2wzUC.png)

* 參考答案
```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:
![image](https://hackmd.io/_uploads/HJbzedzLC.png)</br>
![image](https://hackmd.io/_uploads/rkvzeuML0.png)</br>
![image](https://hackmd.io/_uploads/Hy0zldzI0.png)

</br></br></br>

4.字串與檔案處理
![image](https://hackmd.io/_uploads/ryJgXwzIC.png)

* 參考答案
```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:
![image](https://hackmd.io/_uploads/SyVQcnzUA.png)</br>
![image](https://hackmd.io/_uploads/rknXchz8C.png)</br>
![image](https://hackmd.io/_uploads/HkNVq3fIC.png)
</br></br></br>

5.數字相乘
![image](https://hackmd.io/_uploads/r1vzQvfIC.png)


* 參考答案
```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:
![image](https://hackmd.io/_uploads/rkavzsU8A.png)</br>
![image](https://hackmd.io/_uploads/SJCuzsI80.png)

</br></br></br>

6.字串拆解
![image](https://hackmd.io/_uploads/H1SSmwfUR.png)



* 參考答案
```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:
![image](https://hackmd.io/_uploads/SkWXAfmU0.png)


</br></br></br>

7.二進位轉十進位
![image](https://hackmd.io/_uploads/B16OXwGUR.png)

* 參考答案
```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:
![image](https://hackmd.io/_uploads/BkwGc88LR.png)</br>
![image](https://hackmd.io/_uploads/rkGm9LUL0.png)</br>
![image](https://hackmd.io/_uploads/Hy675U8LR.png)
</br></br></br>

---

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