Preface
The work of this group does not specify Python direction, but to improve the language skills of the members of the group, so that we can go further in other directions of Python.
We welcome Python learners of any degree
Python programming group number: 651707058
Question 1: number of Narcissus
Print out all the "narcissus number" of 100-1000. The so-called "narcissus number" refers to a three digit number, and the sum of the cubes of each digit is equal to the number itself.
For example: 153 is a "narcissus number", because 153 = 1 to the third power + 5 to the third power + 3 to the third power.
def get_flower_number(start,end):
'''
:param start:Start of narcissus number range
:param end: End of narcissus number range
:return: list[int] Save the number of Narcissus list
'''
res = []
for i in range(start,end):
if i == (i//100)**3+(i//10%10)**3+(i%10)**3:
res.append(i)
return res
print(get_flower_number(100,1000))
Question 2: reverse the key value pairs of the dictionary
Invert key value pairs of a dictionary
For example, the original dictionary is: {'a':'a ',' B ':'b', 'C':'c '}
Falling down is {'a': 'a', 'B': 'B', 'C': 'C'}
D = {'A':'a', 'B':'b', 'C':'c'}
res = {v:k for k,v in D.items()}
print(res)
Question 3: dichotomy exercise
Use dichotomy to insert a number into an already ascending list. When an equal number is encountered, insert it to the right
For example: [1,4,7,11] to insert 2
Results: [1,2,4,11]
For example: [1,4,7,11] to insert 4.0
Results: [1,4,4.0,7,11] encountered an equal insert to its right
ps: it must be done by dichotomy, not by Python's own sorting
def insert_list(L,num):
'''
:param num:List to insert
:param num: Number to be inserted
:return: list[]
'''
p1,p2 = 0,len(L)-1 #Boundary value
while p1<=p2:
m = (p1+p2)//2 #Intermediate value
if num<L[m]:p2 = m #Update right boundary
else:p1 = m+1 #Update the left boundary. To insert it right, + 1 needs to insert it right when l [M] = = num or l [M] < num
L.insert(p1,num)
return L
print(insert_list([1,4],4.0))
Question 4: effective Sudoku
Judge whether a 9x9 Sudoku is valid. Just verify that the numbers you have filled are valid according to the following rules.
The numbers 1-9 can only appear once per line.
The numbers 1-9 can only appear once in each column.
The numbers 1-9 can only appear once in every 3 x 3 uterus separated by thick solid lines.
Please design a function to determine whether the following M is a valid Sudoku, return True or return False
Where there are no numbers, they are represented by "."
"'
M = [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
def isValidSudoku(board):
"""
:type board: List[List[str]]
:rtype: bool
"""
from math import ceil # For rounding up, 1-3 4-6 7-9 is rounded up by x/3 y/3 to obtain 1, 23 and three types
D = {}
for i in range(1, 10):
for j in range(1, 10):
temp = board[i - 1][j - 1]
if temp != '.' and temp not in D:
D[temp] = [[i, j]]
elif temp != '.':
D[temp].append([i, j])
for k in D:
L = len(D[k])
setx, sety, setxy = set(), set(), set()
for it in D[k]:
setx.add(it[0])
sety.add(it[1])
setxy.add((ceil(it[0] / 3), ceil(it[1] / 3)))
if len(setx) != L or len(sety) != L or len(setxy) != L:
return False
return True
print(isValidSudoku(M))
# Code Description:
# Rewrite Sudoku into this data structure to judge:
# {'5': [[1, 1], [2, 6], [8, 9]], '3': [[1, 2], [4, 9], [5, 6]], '7': [[1, 5], [6, 1], [9, 8]], '6': [[2, 1], [3, 8], [4, 5], [6, 9], [7, 2]], '1': [[2, 4], [5, 9], [8, 5]], '9': [[2, 5], [3, 2], [8, 6], [9, 9]], '8': [[3, 3], [4, 1], [5, 4], [7, 8], [9, 5]], '4': [[5, 1], [8, 4]], '2': [[6, 5], [7, 7]]}