python作業3


第一題和第三題c的隔壁同學的。我寫了兩個都運行不通過,很絕望。

7-1 圖的字典表示 (20 分)

圖的字典表示。輸入多行字符串,每行表示一個頂點和該頂點相連的邊及長度,輸出頂點數,邊數,邊的總長度。比如上圖0點表示:
{'O':{'A':2,'B':5,'C':4}}

輸入格式:
第一行表示輸入的行數 下面每行輸入表示一個頂點和該頂點相連的邊及長度的字符串

輸出格式:
在一行中輸出頂點數,邊數,邊的總長度

輸入樣例:
在這里給出一組輸入。例如:

4
{'a':{'b':10,'c':6}}
{'b':{'c':2,'d':7}}
{'c':{'d':10}}
{'d':{}}
輸出樣例:
在這里給出相應的輸出。例如:

4 5 35

num = input();
vnum = 0;
adnum = 0;
adsum = 0;
for i in range(int(num)):
    vnum = vnum + 1;
    dictt = eval(input());
    for te in dictt:
        temp = dictt[te];
        for key in temp:
          adnum = adnum+1;
          adsum = adsum+temp[key];
print(str(num)+" "+str(adnum)+" "+str(adsum));

7-11 jmu-python-逆序輸出 (5 分)

輸入一行字符串,然后對其進行如下處理。

輸入格式:
字符串中的元素以空格或者多個空格分隔。

輸出格式:
逆序輸出字符串中的所有元素。
然后輸出原列表。
然后逆序輸出原列表每個元素,中間以1個空格分隔。注意:最后一個元素后面不能有空格。

輸入樣例:
a b c e f gh
輸出樣例:
ghfecba
['a', 'b', 'c', 'e', 'f', 'gh']
gh f e c b a

b = input().split()
a=b[::-1]
str=""
str2=""
for i in a:
   str+=" "+i
   str2+=i
print(str2)
print(b)
print(str[1:])

7-12 jmu-python-班級人員信息統計 (15 分)

輸入a,b班的名單,並進行如下統計。

輸入格式:
第1行::a班名單,一串字符串,每個字符代表一個學生,無空格,可能有重復字符。
第2行::b班名單,一串字符串,每個學生名稱以1個或多個空格分隔,可能有重復學生。
第3行::參加acm競賽的學生,一串字符串,每個學生名稱以1個或多個空格分隔。
第4行:參加英語競賽的學生,一串字符串,每個學生名稱以1個或多個空格分隔。
第5行:轉學的人(只有1個人)。

輸出格式
特別注意:輸出人員名單的時候需調用sorted函數,如集合為x,則print(sorted(x))
輸出兩個班級的所有人員數量
輸出兩個班級中既沒有參加ACM,也沒有參加English的名單和數量
輸出所有參加競賽的人員的名單和數量
輸出既參加了ACM,又參加了英語競賽的所有人員及數量
輸出參加了ACM,未參加英語競賽的所有人員名單
輸出參加英語競賽,未參加ACM的所有人員名單
輸出參加只參加ACM或只參加英語競賽的人員名單
最后一行:一個同學要轉學,首先需要判斷該學生在哪個班級,然后更新該班級名單,並輸出。如果沒有在任何一班級,什么也不做。

輸入樣例:
abcdefghijab
1 2 3 4 5 6 7 8 9 10
1 2 3 a b c
1 5 10 a d e f
a
輸出樣例:
Total: 20
Not in race: ['4', '6', '7', '8', '9', 'g', 'h', 'i', 'j'], num: 9
All racers: ['1', '10', '2', '3', '5', 'a', 'b', 'c', 'd', 'e', 'f'], num: 11
ACM + English: ['1', 'a'], num: 2
Only ACM: ['2', '3', 'b', 'c']
Only English: ['10', '5', 'd', 'e', 'f']
ACM Or English: ['10', '2', '3', '5', 'b', 'c', 'd', 'e', 'f']
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

ac = input();
bc = input();
acm = input();
eng = input();
zz = input();

as1 = set(ac);
bs1 = set(bc.split(' '));
acm2 = set(acm.split(' '));
eng2 = set(eng.split(' '));

if '' in bs1:
  bs1.remove('');
if '' in acm2:  
  acm2.remove('');
if '' in eng2:  
  eng2.remove('');

#人數
print('Total: '+str(len(as1)+len(bs1)));
#不參加競賽的
notrace = list();
for temp in as1:
    if temp not in acm2 and temp not in eng2:
        notrace.append(temp);
for temp in bs1:
    if temp not in acm2 and temp not in eng2:
        notrace.append(temp);
print('Not in race: '+str(sorted(notrace))+', num: '+str(len(notrace)));
#所有參賽的
allracers = list();
for temp in as1:
    if temp in acm2 or temp in eng2:
        allracers.append(temp);
for temp in bs1:
    if temp in acm2 or temp in eng2:
        allracers.append(temp);
print('All racers: '+str(sorted(allracers))+', num: '+str(len(allracers)));
#都參加了的
both = list();
for temp in as1:
    if temp in acm2 and temp in eng2:
        both.append(temp);
for temp in bs1:
    if temp in acm2 and temp in eng2:
        both.append(temp);
print('ACM + English: '+str(sorted(both))+', num: '+str(len(both)));
#只參加了ACM的
acm3 = list();
for temp in as1:
    if temp in acm2 and temp not in eng2:
        acm3.append(temp);
for temp in bs1:
    if temp in acm2 and temp not in eng2:
        acm3.append(temp);
print('Only ACM: '+str(sorted(acm3)));
#只參加了英語的
eng3 = list();
for temp in as1:
    if temp not in acm2 and temp in eng2:
        eng3.append(temp);
for temp in bs1:
    if temp not in acm2 and temp in eng2:
        eng3.append(temp);
print('Only English: '+str(sorted(eng3)));
#只參加ACM或英語的
dd = eng3+acm3;
print('ACM Or English: '+str(sorted(dd)));
#判斷轉學
if zz in as1:
    new = list(as1);
    new.remove(zz)
    print(sorted(new));
elif zz in bs1:
    new = list(bs1);
    new.remove(zz);
    print(sorted(new));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM