NOJ-Python programming assignment of Northwest University of technology 91-100

Posted by luke_barnes on Sat, 22 Jan 2022 21:44:30 +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 10: Season 10 challenges (91-100)

Pre knowledge points

This part mainly uses the previous knowledge to realize the algorithm problem, without pre knowledge points. This part of the topic is relatively simple, mainly focusing on the algorithm ideas and code specifications.

Mirror character

The mirroring requirement is o r d ( ′ a ′ ) + o r d ( ′ z ′ ) = o r d ( ′ b ′ ) + o r d ( ′ y ′ ) = . . . ord('a')+ord('z')=ord('b')+ord('y')=... ord(′a′)+ord(′z′)=ord(′b′)+ord(′y′)=..., So calculate t = o r d ( ′ a ′ ) + o r d ( ′ z ′ ) t=ord('a')+ord('z') t=ord('a ') + ord('z'), and then n − 1 n-1 The n − 1 bit starts with the image exchange character, which is still the original character before that.

t=ord('a')+ord('z')
s=input()
n=int(input())-1
ans=''
for i in range(len(s)):
    if(i<n):
        ans=ans+s[i]
    else:
        ans=ans+chr(t-ord(s[i]))
print(ans)
# Code By Phoenix_ZH

Matchstick

The number of triangles can be seen from the picture: 1 + 2 + 3 + . . . + n 1+2+3+...+n 1+2+3+...+n. So the number of matchsticks is ( n + 1 ) ∗ n / 2 ∗ 3 (n+1)*n/2*3 (n+1)∗n/2∗3

n=int(input())
ans=(n+1)*n//2*3
print(ans)
# Code By Phoenix_ZH

24-hour format

Note that when AM 12 turns to 24 hours, it is 00; When PM 12 turns to 24 hours, it is still 12.

l=input().split()
t=l[0].split(':')
if(t[0]=='12' and l[1]=='AM'):
    t[0]='00'
elif(t[0]!='12' and l[1]=='PM'):
    t[0]=str(int(t[0])+12)
print(':'.join(t))
# Code By Phoenix_ZH

Array rotation

The list's [ 0 : n ] [0:n] [0:n] add to the end, and then add [ 0 : n ] [0:n] [0:n] can be deleted. Or traverse to get a new list.

l=list(map(int,input().split()))
n=int(input())
for i in range(n):
    l.append(l[i])
del l[0:n]
l=map(str,l)
print(' '.join(l))
# Code By Phoenix_ZH

Reference sort

Create a dictionary, get the key value pair according to the corresponding list, then call the sorted function, and sort it by value, get the list, then convert it into dictionary by dict(), then export all the keys of the dictionary.

key=input().split()
value=input().split()
dic=dict(zip(key,value))
dic=dict(sorted(dic.items(),key=lambda x:(x[1])))
print(' '.join(dic.keys()))
# Code By Phoenix_ZH

Z igzag output

Simulate Z-shaped movement.

n=int(input())
a=[]
for i in range(n):
    a2=list(input().split())
    a.append(a2)
ans=[]
for i in range(n-1):
    ans.append(a[0][i])
for i in range(n):
    ans.append(a[i][n-i-1])
for i in range(1,n):
    ans.append(a[n-1][i])
print(' '.join(ans))
# Code By Phoenix_ZH

Fibonacci sequence multiples

Construct Fibonacci sequence, once the current number is a multiple of k n − = 1 n-=1 n − = 1 until n = 0 n=0 n=0. Finally, check the length of Fibonacci sequence.

f=[0,1,1]
n,k=map(int,input().split())
while(n):
    x=f[len(f)-1]+f[len(f)-2]
    f.append(x)
    if(x%k==0):
        n-=1
print(len(f)-1)
# Code By Phoenix_ZH

matching

The question is not clear: is the same character allowed in a string?
If it is ensured that the characters in a string are different from each other, the intersection can be obtained directly, and then the length can be output. So I messed with one hair, AC. The data is fake
If you don't guarantee that they are different from each other, I'm afraid the easiest way is to search again with O(n^2)

l1,l2=input().split()
l1,l2=set(l1),set(l2)
l1=l1.intersection(l2)
print(len(l1))
# Code By Phoenix_ZH

Maximum element

Get the list directly, and then directly traverse linearly to update maxx. Or sort directly.

maxx=-1e9
l=list(map(int,input().split()))
for it in l:
    maxx=max(maxx,it)
print(maxx)
# Code By Phoenix_ZH

URL

You need to construct regular expressions, and then call the findall function in the string st to output the answer list.

import re
st = input()
result=re.findall(r'(?:https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]', st)
print(result)
# Code By Phoenix_ZH

Topics: Python Algorithm