close

701 串列數組轉換

說明:

請撰寫一程式,輸入數個整數並儲存至串列中,以輸入-9999為結束點(串列中不包含-9999),再將此串列轉換成數組,最後顯示該數組以及其長度(Length)、最大值(Max)、最小值(Min)、總和(Sum)。

範例輸入:

7
6
91
-8
-9999

輸入/輸出

image

 

Code:

L=[]
while True:
  x=int(input())
  if x==-9999:
    break
  else:
    L.append(x)
print(len(tuple(L)),max(tuple(L)),min(tuple(L)),sum(tuple(L)))

702 數組合併排序

說明:

請撰寫一程式,輸入並建立兩組數組,各以-9999為結束點(數組中不包含-9999)。將此兩數組合併並從小到大排序之,顯示排序前的數組和排序後的串列。

輸入/輸出

範例輸入:

2
4
6
-9999

3
1
7
-999
-9999

範例輸出:

image

Code:

tuple1 = ()

print('Create tuple 1:')
num = eval(input())

while num !=-9999:
    tuple1 +=(num,)
    num = eval(input())
tuple2 = ()
print('Create tuple 2:')
num = eval(input())
while num !=-9999:
    tuple2 +=(num,)
    num = eval(input())
tuple1 += tuple2

print('Combined tuple before sorting:',tuple1)

LIST = list(tuple1)
LIST.sort()

print('Combined list after sorting:',LIST)

 

703 數組條件判斷

說明:

請撰寫一程式,輸入一些字串至數組(至少輸入五個字串),以字串"end"為結束點(數組中不包含字串"end")。接著輸出該數組,再分別顯示該數組的第一個元素到第三個元素和倒數三個元素。

輸入/輸出

範例輸入:

how
dare
you
python
learner
end

範例輸出:

 

image

 

Code:

tmp=()
while True:
  x=input()
  if x=="end":
    break
  else:
    tmp+=(x,)
print(tmp[0:3],tmp[-3:])

704 集合條件判斷

說明:

請撰寫一程式,輸入數個整數並儲存至集合,以輸入-9999為結束點(集合中不包含-9999),最後顯示該集合的長度(Length)、最大值(Max)、最小值(Min)、總和(Sum)。

輸入/輸出

範例輸入:

1

3

5

-9

-11

991

-9999

範例輸出:

image

Code:

set1= set()
while True:
    x=int(input())
    if x!=-9999:
        set1.add(x)
    else:
        break
print(len(set1),max(set1),min(set1),sum(set1))

705 子集合與超集合

說明:

請撰寫一程式,依序輸入五個、三個、九個整數,並各自儲存到集合set1、set2、set3中。接著回答:set2是否為set1的子集合(subset)?set3是否為set1的超集合(superset)?

輸出浮點數到小數點後第二位。

輸入/輸出

範例輸入:

1
3
5
7
11
1
3
11
1
3
5
7
11
17
19
21
777

範例輸出:

image

Code:

set1=set()
set2=set()
set3=set()
for i in range(5):
    x=int(input())
    set1.add(x)
for i in range(3):
    x=int(input())
    set2.add(x)
for i in range(9):
    x=int(input())
    set3.add(x)
print("set2 is subset of set1:", set2.issubset(set1))
print("set3 is superset of set1:", set3.issuperset(set1))

706 全字母句

說明:

全字母句(Pangram)是英文字母表所有的字母都出現至少一次(最好只出現一次)的句子。請撰寫一程式,要求使用者輸入一正整數k(代表有k筆測試資料),每一筆測試資料為一句子,程式判斷該句子是否為Pangram,並印出對應結果True(若是)或False(若不是)。欄寬為4。

輸入/輸出

範例輸入:

1

Jock nymphs waqf drug vex blitz

範例輸出:

image

 

Code:

count=eval(input())
alpha=26
for i in range(count):
    sentence=input()
    set1=set(sentence.lower())
    if " " in set1:
        set1.remove(" ")
    if len(set1)>=alpha:
        print("True")
    else:
        print("False")

707 共同科目

說明:

請撰寫一程式,輸入X組和Y組各自的科目至集合中,以字串"end"作為結束點(集合中不包含字串"end")。請依序分行顯示(1) X組和Y組的所有科目、(2)X組和Y組的共同科目、(3)Y組有但X組沒有的科目,以及(4) X組和Y組彼此沒有的科目(不包含相同科目)。 

輸入/輸出

範例輸入:

math

art

english

chinese

end

chinese

graphic

physic

end

範例輸出:

image

Code:

Xset,Yset=set(),set()
while True:
    x=input()
    if x=="end":
        break
    else:
        Xset.add(x)
while True:
    x=input()
    if x == "end":
        break
    else:
        Yset.add(x)
print(sorted(Xset.union(Yset)))
print(sorted(Yset.intersection(Xset)))
print(sorted(Yset.difference(Xset)))
print(sorted(Yset.symmetric_difference(Xset)))

708 詞典合併

說明:

請撰寫一程式,自行輸入兩個詞典(以輸入鍵值"end"作為輸入結束點,詞典中將不包含鍵值"end"),將此兩詞典合併,並根據key值字母由小到大排序輸出,如有重複key值,後輸入的key值將覆蓋前一key值。

輸入/輸出

範例輸入:

Key:A
Value:apple
Key:c
Value:cat
Key:d
Value:dog
Key:e
Value:egg
Key:p
Value:pig
Key:end
Key:g
Value:goose
Key:k
Value:kitty
Key:b
Value:book
Key:end
 

範例輸出:

image

 

Code:

 

dict1={}
dict2={}
while True:
    key = input('Key:')
    if key == 'end':
        break
    dict1[key] = input('Value:')


while True:
    key = input('Key:')
    if key == 'end':
        break
    dict2[key] = input('Value:')

dict1.update(dict2)

for i in sorted(dict1.keys()):
    print(i, ': ', dict1[i])

709 詞典排序

說明:

請撰寫一程式,讓使用者建立兩個2*2的矩陣,其內容為從鍵盤輸入的整數,接著輸出這兩個矩陣的內容以及它們相加的結果。

輸入/輸出

Key:a
Value:apple
Key:e
Value:eg
Key:p
Value:pond
Key:b
Value:book
Key:end
 

範例輸出:

image

Code:

color_dict = {}

while True:
    key = input('Key:')
    if key == 'end':
        break
    color_dict[key] = input('Value:')

for i in sorted(color_dict.keys()):
    print(i,': ',color_dict[i])

710 詞典搜尋

說明:

請撰寫一程式,為一詞典輸入資料(以輸入鍵值"end"作為輸入結束點,詞典中將不包含鍵值"end"),再輸入一鍵值並檢視此鍵值是否存在於該詞典中。

輸入/輸出

範例輸入:

Key:a
Value:apple
Key:c
Value:cake
Key:d
Value:duck
Key:s
Value:snake
Key:end
Search key:e

範例輸出:

image

 

Code:

data_dist = {}

while True:
    key = input('Key:')

    if key == 'end':
        break
    data_dist[key] = input('Value:')

search = input('Search key:')

print(search in data_dist.keys())

系列文章:

101-110

201-210

301-310

401-410

501-510

601-610

701-710

801-810

901-910


arrow
arrow

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()