---
# System prepended metadata

title: 311159023徐若曦_程式設計期中考試

---

# 311159023徐若曦_程式設計期中考試
## Python
### midterm_113.py
#### All
##### 驗證程式
```
def is_positive_int(value):
    try:
        num=int(value)
        if num>0:
            return True
        else:
            return False
    except ValueError:
        return False
```
#### 1.
##### 程式設計
```
# 1
print("第1題")
list1=list(range(1001, 1, -5))
# 1.1
print("第1.1題")
print(sorted(list1))
print(max(list1))
print(min(list1))
print(sum(list1))
# 1.2
print("第1.2題")
list1.pop(18)
list1.pop(91) # index應為91
print(list1)
# 1.3
print("第1.3題")
list2=[25, 173, 411]
print(list2)
# 1.4
print("第1.4題")
for i in range(len(list2)):
    list2[i]=list2[i]*3
list3=list1+list2
print(list3)
# 1.5
print("第1.5題")
list3.sort(reverse=True)
print(list3)
```
##### 執行結果
```
第1題
第1.1題
[6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96, 101, 106, 111, 116, 121, 126, 131, 136, 141, 146, 151, 156, 161, 166, 171, 176, 181, 186, 191, 196, 201, 206, 211, 216, 221, 226, 231, 236, 241, 246, 251, 256, 261, 266, 271, 276, 281, 286, 291, 296, 301, 306, 311, 316, 321, 326, 331, 336, 341, 346, 351, 356, 361, 366, 371, 376, 381, 386, 391, 396, 401, 406, 411, 416, 421, 426, 431, 436, 441, 446, 451, 456, 461, 466, 471, 476, 481, 486, 491, 496, 501, 506, 511, 516, 521, 526, 531, 536, 541, 546, 551, 556, 561, 566, 571, 576, 581, 586, 591, 596, 601, 606, 611, 616, 621, 626, 631, 636, 641, 646, 651, 656, 661, 666, 671, 676, 681, 686, 691, 696, 701, 706, 711, 716, 721, 726, 731, 736, 741, 746, 751, 756, 761, 766, 771, 776, 781, 786, 791, 796, 801, 806, 811, 816, 821, 826, 831, 836, 841, 846, 851, 856, 861, 866, 871, 876, 881, 886, 891, 896, 901, 906, 911, 916, 921, 926, 931, 936, 941, 946, 951, 956, 961, 966, 971, 976, 981, 986, 991, 996, 1001]
1001
6
100700
第1.2題
[1001, 996, 991, 986, 981, 976, 971, 966, 961, 956, 951, 946, 941, 936, 931, 926, 921, 916, 906, 901, 896, 891, 886, 881, 876, 871, 866, 861, 856, 851, 846, 841, 836, 831, 826, 821, 816, 811, 806, 801, 796, 791, 786, 781, 776, 771, 766, 761, 756, 751, 746, 741, 736, 731, 726, 721, 716, 711, 706, 701, 696, 691, 686, 681, 676, 671, 666, 661, 656, 651, 646, 641, 636, 631, 626, 621, 616, 611, 606, 601, 596, 591, 586, 581, 576, 571, 566, 561, 556, 551, 546, 536, 531, 526, 521, 516, 511, 506, 501, 496, 491, 486, 481, 476, 471, 466, 461, 456, 451, 446, 441, 436, 431, 426, 421, 416, 411, 406, 401, 396, 391, 386, 381, 376, 371, 366, 361, 356, 351, 346, 341, 336, 331, 326, 321, 316, 311, 306, 301, 296, 291, 286, 281, 276, 271, 266, 261, 256, 251, 246, 241, 236, 231, 226, 221, 216, 211, 206, 201, 196, 191, 186, 181, 176, 171, 166, 161, 156, 151, 146, 141, 136, 131, 126, 121, 116, 111, 106, 101, 96, 91, 86, 81, 76, 71, 66, 61, 56, 51, 46, 41, 36, 31, 26, 21, 16, 11, 6]
第1.3題
[25, 173, 411]
第1.4題
[1001, 996, 991, 986, 981, 976, 971, 966, 961, 956, 951, 946, 941, 936, 931, 926, 921, 916, 906, 901, 896, 891, 886, 881, 876, 871, 866, 861, 856, 851, 846, 841, 836, 831, 826, 821, 816, 811, 806, 801, 796, 791, 786, 781, 776, 771, 766, 761, 756, 751, 746, 741, 736, 731, 726, 721, 716, 711, 706, 701, 696, 691, 686, 681, 676, 671, 666, 661, 656, 651, 646, 641, 636, 631, 626, 621, 616, 611, 606, 601, 596, 591, 586, 581, 576, 571, 566, 561, 556, 551, 546, 536, 531, 526, 521, 516, 511, 506, 501, 496, 491, 486, 481, 476, 471, 466, 461, 456, 451, 446, 441, 436, 431, 426, 421, 416, 411, 406, 401, 396, 391, 386, 381, 376, 371, 366, 361, 356, 351, 346, 341, 336, 331, 326, 321, 316, 311, 306, 301, 296, 291, 286, 281, 276, 271, 266, 261, 256, 251, 246, 241, 236, 231, 226, 221, 216, 211, 206, 201, 196, 191, 186, 181, 176, 171, 166, 161, 156, 151, 146, 141, 136, 131, 126, 121, 116, 111, 106, 101, 96, 91, 86, 81, 76, 71, 66, 61, 56, 51, 46, 41, 36, 31, 26, 21, 16, 11, 6, 75, 519, 1233]
第1.5題
[1233, 1001, 996, 991, 986, 981, 976, 971, 966, 961, 956, 951, 946, 941, 936, 931, 926, 921, 916, 906, 901, 896, 891, 886, 881, 876, 871, 866, 861, 856, 851, 846, 841, 836, 831, 826, 821, 816, 811, 806, 801, 796, 791, 786, 781, 776, 771, 766, 761, 756, 751, 746, 741, 736, 731, 726, 721, 716, 711, 706, 701, 696, 691, 686, 681, 676, 671, 666, 661, 656, 651, 646, 641, 636, 631, 626, 621, 616, 611, 606, 601, 596, 591, 586, 581, 576, 571, 566, 561, 556, 551, 546, 536, 531, 526, 521, 519, 516, 511, 506, 501, 496, 491, 486, 481, 476, 471, 466, 461, 456, 451, 446, 441, 436, 431, 426, 421, 416, 411, 406, 401, 396, 391, 386, 381, 376, 371, 366, 361, 356, 351, 346, 341, 336, 331, 326, 321, 316, 311, 306, 301, 296, 291, 286, 281, 276, 271, 266, 261, 256, 251, 246, 241, 236, 231, 226, 221, 216, 211, 206, 201, 196, 191, 186, 181, 176, 171, 166, 161, 156, 151, 146, 141, 136, 131, 126, 121, 116, 111, 106, 101, 96, 91, 86, 81, 76, 75, 71, 66, 61, 56, 51, 46, 41, 36, 31, 26, 21, 16, 11, 6]
```
#### 2.
##### 程式設計
```
# 2
print("第2題") # midterm_110.py
def fac(maxi):
    # maxi = input('請輸入一個正整數值: ') # 應為正整數值（優化事項）, 缺少)
    while is_positive_int(maxi)==False: # 缺少驗證程式（優化事項）
        print("請輸入正整數")
        maxi = input('請輸入一個正整數值: ') # 應為正整數值（優化事項）
    else:
        maxi=int(maxi)
    fac = 1
    i = 1
    while (i < maxi): # 應為i, maxi, 應為<（新增）
        i += 1
        fac = fac*i # 應為*
    # print('滿足最小大於整數值', maxi, '的階乘為: ', fac, ', ===> ', i, '! = ', fac, sep="") # 缺少sep=""（優化事項）
    return fac
print(fac(9)/(fac(4)*fac(6)))
```
##### 執行結果
```
第2題
21.0
```
#### 3.
##### 程式設計
```
# 3
print("第3題")
import math
ans=pow(abs((math.pi)**((2)**(1/2))+math.log10(253*math.sin(2.1))-3**2.5), 3)
print("%.3f" %ans)
print("{:.3f}".format(ans))
```
##### 執行結果
```
第3題
551.716
551.716
```
#### 4.
##### 程式設計
```
# 4
print("第4題")
# 4.1
print("第4.1題")
dic={"Pig":"豬", "Deer":"鹿", "Cow":"牛", "Dog":"狗"}
for i in dic:
    print(i, dic[i])
# 4.2
print("第4.2題")
for _ in range(2):
    word=input("請輸入查詢英文: ")
    while word not in dic:
        print("請輸入正確英文")
        word=input("請輸入查詢英文: ")
    else:
        print(dic[word])
# 4.3
print("第4.3題")
dic["Cat"]="鼠"
dic["Rabbit"]="兔"
print(dic)
# 4.4
print("第4.4題")
dic["Cat"]="貓"
print(dic)
# 4.5
print("第4.5題")
dic.pop("Deer")
print(dic)
```
##### 執行結果
```
第4題
第4.1題
Pig 豬
Deer 鹿
Cow 牛
Dog 狗
第4.2題
請輸入查詢英文: Pig
豬
請輸入查詢英文: Deer
鹿
第4.3題
{'Pig': '豬', 'Deer': '鹿', 'Cow': '牛', 'Dog': '狗', 'Cat': '鼠', 'Rabbit': '兔'}
第4.4題
{'Pig': '豬', 'Deer': '鹿', 'Cow': '牛', 'Dog': '狗', 'Cat': '貓', 'Rabbit': '兔'}
第4.5題
{'Pig': '豬', 'Cow': '牛', 'Dog': '狗', 'Cat': '貓', 'Rabbit': '兔'}
```
#### 5.
##### 程式設計
```
# 5
print("第5題")
# python program to find H.C.F. of two numbers # 缺少.（優化事項）

# define a function
def compute_hcf(x, y):
    # choose the smaller number # 缺少縮排（優化事項）
    if x > y:
        smaller=y # 應為=
    else:
        smaller=x
    for i in range(1, smaller+1): # 缺少in
        if x % i == 0 and y % i == 0: # 應為%, 不用()
            hcf = i
    return hcf

num1 = 54
num2 = 24
print("The H.C.F. is ", compute_hcf(num1, num2), ".", sep="") # 應為compute_hcf, 缺少.（優化事項）, 缺少sep=""（優化事項）
```
##### 執行結果
```
第5題
The H.C.F. is 6.
```
## JavaScript
### midterm_113.js
#### All
##### 讀取介面
```
// 讀取介面
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
function getInput(question) {
    return new Promise((resolve) => {
        rl.question(question, (answer) => {
            resolve(answer);
        });
    });
}
```
##### 驗證程式
```
// 驗證程式
function isPositiveInt(value) {
    const num = parseInt(value);
    return !isNaN(num) && num > 0;
}
```
##### 程式設計
```
// main
async function main() {
    rl.close();
}
main();
```
#### 1.
##### 程式設計
```
// 1
console.log("第1題");
let list1 = [];
for (let i = 1001; i > 5; i -= 5) {
    list1.push(i)
}
console.log(list1)
// 1.1
console.log("第1.1題");
console.log(list1.sort(function (a, b) {
    return a - b;
}));
console.log(Math.max(...list1));
console.log(Math.min(...list1));
sum = 0
for (let i = 0; i < list1.length; i++) {
    sum += list1[i]
}
console.log(sum);
// 1.2
console.log("第1.2題");
list1.splice(18, 1);
list1.splice(91, 1);
console.log(list1);
// 1.3
console.log("第1.3題");
let list2 = [25, 173, 411];
console.log(list2);
// 1.4
console.log("第1.4題");
for (let i = 0; i < list2.length; i++) {
    list2[i] = list2[i] * 3;
}
let list3 = list1.concat(list2);
console.log(list3);
// 1.5
console.log("第1.5題");
list3 = list3.sort(function (a, b) {
    return b - a;
});
console.log(list3);
```
##### 執行結果
```
第1題
[
  1001, 996, 991, 986, 981, 976, 971, 966, 961, 956, 951,
   946, 941, 936, 931, 926, 921, 916, 911, 906, 901, 896,
   891, 886, 881, 876, 871, 866, 861, 856, 851, 846, 841,
   836, 831, 826, 821, 816, 811, 806, 801, 796, 791, 786,
   781, 776, 771, 766, 761, 756, 751, 746, 741, 736, 731,
   726, 721, 716, 711, 706, 701, 696, 691, 686, 681, 676,
   671, 666, 661, 656, 651, 646, 641, 636, 631, 626, 621,
   616, 611, 606, 601, 596, 591, 586, 581, 576, 571, 566,
   561, 556, 551, 546, 541, 536, 531, 526, 521, 516, 511,
   506,
  ... 100 more items
]
第1.1題
[
    6,  11,  16,  21,  26,  31,  36,  41,  46,  51,  56,  61,
   66,  71,  76,  81,  86,  91,  96, 101, 106, 111, 116, 121,
  126, 131, 136, 141, 146, 151, 156, 161, 166, 171, 176, 181,
  186, 191, 196, 201, 206, 211, 216, 221, 226, 231, 236, 241,
  246, 251, 256, 261, 266, 271, 276, 281, 286, 291, 296, 301,
  306, 311, 316, 321, 326, 331, 336, 341, 346, 351, 356, 361,
  366, 371, 376, 381, 386, 391, 396, 401, 406, 411, 416, 421,
  426, 431, 436, 441, 446, 451, 456, 461, 466, 471, 476, 481,
  486, 491, 496, 501,
  ... 100 more items
]
1001
6
100700
第1.2題
[
    6,  11,  16,  21,  26,  31,  36,  41,  46,  51,  56,  61,
   66,  71,  76,  81,  86,  91, 101, 106, 111, 116, 121, 126,
  131, 136, 141, 146, 151, 156, 161, 166, 171, 176, 181, 186,
  191, 196, 201, 206, 211, 216, 221, 226, 231, 236, 241, 246,
  251, 256, 261, 266, 271, 276, 281, 286, 291, 296, 301, 306,
  311, 316, 321, 326, 331, 336, 341, 346, 351, 356, 361, 366,
  371, 376, 381, 386, 391, 396, 401, 406, 411, 416, 421, 426,
  431, 436, 441, 446, 451, 456, 461, 471, 476, 481, 486, 491,
  496, 501, 506, 511,
  ... 98 more items
]
第1.3題
[ 25, 173, 411 ]
第1.4題
[
    6,  11,  16,  21,  26,  31,  36,  41,  46,  51,  56,  61,
   66,  71,  76,  81,  86,  91, 101, 106, 111, 116, 121, 126,
  131, 136, 141, 146, 151, 156, 161, 166, 171, 176, 181, 186,
  191, 196, 201, 206, 211, 216, 221, 226, 231, 236, 241, 246,
  251, 256, 261, 266, 271, 276, 281, 286, 291, 296, 301, 306,
  311, 316, 321, 326, 331, 336, 341, 346, 351, 356, 361, 366,
  371, 376, 381, 386, 391, 396, 401, 406, 411, 416, 421, 426,
  431, 436, 441, 446, 451, 456, 461, 471, 476, 481, 486, 491,
  496, 501, 506, 511,
  ... 101 more items
]
第1.5題
[
  1233, 1001, 996, 991, 986, 981, 976, 971, 966, 961, 956,
   951,  946, 941, 936, 931, 926, 921, 916, 911, 906, 901,
   896,  891, 886, 881, 876, 871, 866, 861, 856, 851, 846,
   841,  836, 831, 826, 821, 816, 811, 806, 801, 796, 791,
   786,  781, 776, 771, 766, 761, 756, 751, 746, 741, 736,
   731,  726, 721, 716, 711, 706, 701, 696, 691, 686, 681,
   676,  671, 666, 661, 656, 651, 646, 641, 636, 631, 626,
   621,  616, 611, 606, 601, 596, 591, 586, 581, 576, 571,
   566,  561, 556, 551, 546, 541, 536, 531, 526, 521, 519,
   516,
  ... 101 more items
]
```
#### 2.
##### 程式設計
```
// 2
console.log("第2題"); // midterm_110.py
function computeFac(maxi) {
    if (!isPositiveInt(maxi)) {
        console.log("請輸入正整數");
        return computeFac(maxi);
    }
    maxi = parseInt(maxi);
    let fac = 1;
    for (let i = 2; i <= maxi; i++) {
        fac *= i;
    }
    return fac;
}
console.log(computeFac(9) / (computeFac(4) * computeFac(6)));
```
##### 執行結果
```
第2題
21
```
#### 3.
##### 程式設計
```
// 3
console.log("第3題");
const ans = Math.pow(Math.abs(Math.pow(Math.PI, Math.sqrt(2)) + Math.log10(253 * Math.sin(2.1)) - Math.pow(3, 2.5)), 3);
console.log(ans.toFixed(3));
```
##### 執行結果
```
第3題
551.716
```
#### 4.
##### 程式設計
```
// 4
console.log("第4題");
// 4.1
console.log("第4.1題");
let dic = { "Pig": "豬", "Deer": "鹿", "Cow": "牛", "Dog": "狗" };
for (let word in dic) {
    console.log(word, dic[word]);
}
// 4.2
console.log("第4.2題");
for (let i = 0; i < 2; i++) {
    let word = await getInput("請輸入查詢英文: ");
    while (!(word in dic)) {
        console.log("請輸入正確英文");
        word = await getInput("請輸入查詢英文: ");
    }
    console.log(dic[word]);
}
// 4.3
console.log("第4.3題");
dic["Cat"] = "鼠";
dic["Rabbit"] = "兔";
console.log(dic);
// 4.4
console.log("第4.4題");
dic["Cat"] = "貓";
console.log(dic);
// 4.5
console.log("第4.5題");
delete dic["Deer"];
console.log(dic);
```
##### 執行結果
```
第4題
第4.1題
Pig 豬
Deer 鹿
Cow 牛
Dog 狗
第4.2題
請輸入查詢英文: Pig
豬
請輸入查詢英文: Deer
鹿
第4.3題
{ Pig: '豬', Deer: '鹿', Cow: '牛', Dog: '狗', Cat: '鼠', Rabbit: '兔' }
第4.4題
{ Pig: '豬', Deer: '鹿', Cow: '牛', Dog: '狗', Cat: '貓', Rabbit: '兔' }
第4.5題
{ Pig: '豬', Cow: '牛', Dog: '狗', Cat: '貓', Rabbit: '兔' }
```
#### 5.
##### 程式設計
```
// 5
console.log("第5題");
// javascript program to find H.C.F. of two numbers

// define a function
function computeHCF(x, y) {
    // choose the smaller number
    const smaller = Math.min(x, y);
    for (let i = 1; i <= smaller; i++) {
        if (x % i === 0 && y % i === 0) {
            hcf = i;
        }
    }
    return hcf;
}
const num1 = 54;
const num2 = 24;
console.log(`The H.C.F. is ${computeHCF(num1, num2)}.`);
```
##### 執行結果
```
第5題
The H.C.F. is 6.
```