NOJ-Python programming assignment of Northwest University of technology 71-80

Posted by jimson on Thu, 20 Jan 2022 00:53:22 +0100

Northwest University of technology NOJ-Python programming problem set:
Noj Python Programming: Season 1: season 1-easy (1-10)
Noj Python Programming: Season 2: Season 2 snippet (11-20)
Noj Python Programming: Season 3: season 3-loop (21-30)
NOJ-Python programming: Season 4: enumeration algorithm (31-40)
NOJ-Python programming: Season 5: modularization (41-50)
Noj Python Programming: Season 6: season 6-string (51-60)
NOJ-Python programming: Season 7: season 7-list and tuple (61-70)
Noj Python Programming: Season 8: season 8-sets and Dictionary (71-80)
Noj Python Programming: Season 9: class (81-90)
Noj Python Programming: Season 10: Season 10 challenges (91-100)

Season 8: season 8-sets and Dictionary (71-80)

Pre knowledge points

It is recommended to understand the basic application of the following function library before completing the topic.

What is the difference between copy() and deepcopy()

  • Replication in the ordinary sense is deep replication, that is, the copied object is completely replicated again and exists alone as an independent new individual. Therefore, changing the original copied object will not affect the copied new object.
  • Shallow copy does not produce an independent object. It just marks the original data block with a new label. Therefore, when one label is changed, the data block will change and the other label will change.

sorted function

sorted can sort all iteratable objects.

  • Syntax sorted(iterable, key=None, reverse=False)
  • Parameters:
    • Iteratable – iteratable object.
    • key – it is mainly used to compare elements. There is only one parameter. The parameters of the specific function are taken from the iteratable object. Specify an element in the iteratable object to sort.
    • Reverse – collation, reverse = True descending, reverse = False ascending (default).

Generate dictionary III

The copy library needs to be imported to copy the dictionary. Note that the deep copy is used to ensure that there are two dictionaries.

Current dic dictionary l 1 [ i ] l1[i] The value corresponding to l1[i] is the previous dictionary ans, then copy the dic dictionary to ans, and then empty the dic dictionary. Repeat len(l1) times, and the final answer is the ANS dictionary.

import copy
l1=list(input().split())
ans={}
dic={}
for i in range(len(l1)-1,-1,-1):
    dic[l1[i]]=ans
    ans=copy.deepcopy(dic)
    dic={}
print(ans)
# Code By Phoenix_ZH

Difference set

Directly use the set built-in function difference, and then output the answer. Note: be sure to sort in the output first, because the set itself is disordered and the tester has no special judge.

s1=set(input().split())
s2=set(input().split())
ans=list(s1.difference(s2))
ans.sort()#You need to sort from small to large. set itself is out of order
for it in ans:
    print(it,end=' ')
# Code By Phoenix_ZH

Generate dictionary I

Directly create a dictionary, and then assign key values to pairs.

n=int(input())
dic=dict()
for i in range(1,n+1):
    dic[i]=i*i
print(dic)

# Code By Phoenix_ZH

Dictionary addition

First get a general list, and then add it to the dictionary one by one. If the key does not exist, add the new key / value pair. If the key exists, overlay the value, and finally output the dictionary.

l1=list(input().split(','))
l2=list(input().split(','))
dic=dict()
l1+=l2
for it in l1:
    x=it.split(':')
    if(x[0] in dic):
        dic[x[0]]+=int(x[1])
    else:
        dic[x[0]]=int(x[1])
print(dic)
# Code By Phoenix_ZH

Symmetric difference set

Method 1: directly use symmetric_ The difference function obtains the symmetric difference set, which is finally converted into a list and sorted.

s1=set(input().split())
s2=set(input().split())
s3=list(s1.symmetric_difference(s2))
s3.sort()
print(' '.join(s3))

Method 2: Union intersection, finally converted into a list and sorted.

s1=set(input().split())
s2=set(input().split())
s3=s1.union(s2)
s4=s1.intersection(s2)
s3=list(s3-s4)
s3.sort()
print(' '.join(s3))

Union

Directly use the union function, and finally convert it into a list and sort it.

s1=set(input().split())
s2=set(input().split())
ans=list(s1.union(s2))
ans.sort()
print(' '.join(ans))
# Code By Phoenix_ZH

intersection

Use the intersection function directly, and finally convert it into a list and sort it.

s1=set(input().split())
s2=set(input().split())
ans=list(s1.intersection(s2))
ans.sort()
print(' '.join(ans))
# Code By Phoenix_ZH

Dictionary sort

Create a dictionary and add key value pairs, and then use the sorted function to sort the second element and the first element of the dictionary element pair respectively.

d1={}
while(1):
    s=input()
    if(s==''):
        break
    x=s.split()
    d1[x[0]]=x[1]
ans=sorted(d1.items(),key=lambda x:(x[1]))
print(ans)
ans=sorted(d1.items(),key=lambda x:(x[0]),reverse=True)
print(ans)
'''
Language 89
 Mathematics 91
 English 93
 Biology 77
 Geography 86
 History 85
'''
# Code By Phoenix_ZH

Dictionary max min

Create a dictionary, access the list linearly, and update maxx and minn. If the key does not exist in the dictionary, add it, and finally output maxx and minn.

l=list(input().split(','))
dic={}
minn,maxx=1e9,-1e9
for it in l:
    x=it.split(':')
    maxx,minn=max(maxx,int(x[1])),min(minn,int(x[1]))
    if(x[0] in dic):
        continue
    else:
        dic[x[0]]=x[1]
print(maxx,minn)
'''
a:500,b:5874,c:560
'''
# Code By Phoenix_ZH

Generate Dictionary II

Save the key and value in l1 and l2 respectively, and then add the key value pair to the dictionary. In fact, you can also use the zip function.

l1=list(input().split())
l2=list(input().split())
dic={}
for i in range(len(l1)):
    dic[l1[i]]=l2[i]
print(dic)
# Code By Phoenix_ZH

Topics: Python Algorithm