Answers to the 11th Blue Bridge Cup Python Group Test Questions

Answers to the 11th Blue Bridge Cup Python Group Test Questions

House number making

Insert picture description here

The meaning of this question is to find the center of the number 2 in all the numbers between [1, 2020] ( note that it is a closed interval ).
That is to say, start from 1, find the number that contains 2, and then see how many 2s in this number.

For example, 2021 will contribute two 2.

Method 1 : Digital Separation

count = 0

for i in range(1, 2021):
    n = i
    while n != 0:
        m = n % 10
        n = n // 10
        if m == 2:
            count += 1

print(count)
# 624

Method 2 : Forcibly convert the string

count = 0

for i in range(1, 2021):
    count += str(i).count('2')

print(count)
# 624

Looking for 2020

Insert picture description here

The data is a txt file that contains 300 rows and 300 columns of data, all of which are numbers 0 and 2. I didn't find this txt file on the official website or elsewhere, so let's test it with sample data.

220000
000000
002202
000000
000022
002020

Note: The data read from the file are all character strings.

def check_s(s):
    return s == '2020'


data = []
# test.txt
# 220000
# 000000
# 002202
# 000000
# 000022
# 002020

# 读取数据
with open('test.txt', mode='r', encoding='utf-8') as fp:
    for line in fp:
        line = list(line.strip())
        data.append(line)

print(data)
# 读出来的全是字符串
# [['2', '2', '0', '0', '0', '0'], 
#  ['0', '0', '0', '0', '0', '0'], 
#  ['0', '0', '2', '2', '0', '2'], 
#  ['0', '0', '0', '0', '0', '0'], 
#  ['0', '0', '0', '0', '2', '2'], 
#  ['0', '0', '2', '0', '2', '0']]

m, n = len(data), len(data[0])
count = 0

for i in range(m):
    for j in range(n):
        # 行
        if i + 3 < n and check_s(data[i][j] + data[i+1][j] + data[i+2][j] + data[i+3][j]):
            count += 1
        # 列
        if j + 3 < m and check_s(data[i][j] + data[i][j+1] + data[i][j+2] + data[i][j+3]):
            count += 1
        # 斜
        if i + 3 < n and j + 3 < m and check_s(data[i][j] + data[i+1][j+1] + data[i+2][j+2] + data[i+3][j+3]):
            count += 1

print(count)
# 5

Divergent thinking : consider using the count() function of the string to process, the string in each row and column is easy to read, and then use line.count('2020') to get the number of lines in the string The string is '2020', but there is no good way to get the corresponding string out at an angle, and I will update it when I have time. You can leave a comment in the comment area where you can think of a good way.

Running exercise

Insert picture description here

It can be easily obtained by directly using the datetime timedelta weekday in the python standard library datetime. The key position has been analyzed.

import datetime

distances = 0
start_time = datetime.datetime(year=2000, month=1, day=1)  # 2000年1月1日00:00 周六
dela = datetime.timedelta(days=1)
end_time = datetime.datetime(year=2020, month=10, day=1)  # 到2020年10月2日00:00 这样包括10月1号 周四
time = end_time - start_time  # time为一个timedelta对象
# start_time.weekday() 返回数字0-6,0代表星期一,依次类推  5 代表星期六

while start_time <= end_time:
    if start_time.day == 1 or start_time.weekday()  == 0:
        # 月初或周一
        distances += 2
    else:
        distances += 1
    start_time += dela

print(distances)  # 8879

Snake skin walking position

Insert picture description here

It is recommended to calculate by hand, it is very fast. It's not impossible to use code. I have been very busy recently. My personal website http://47.116.137.43/ will be launched soon. The domain name liboer.top cannot be accessed as a domain name for the time being during the filing, and CDN acceleration has not been done yet. The loading of the first visit will be slightly slower. Welcome. Everyone is visiting. Here is a manual calculation method. Regarding the code, you can add it when you have time. You are welcome to add it in the comment area.
The idea of ​​solving the problem.
We can see that the cumulative number of numbers is incremented by n(n+1)/2, but the final number is divided into odd and even order: odd columns, even rows. For example: the first number in the three columns of 3 is 3(3+1)/2=6 and the first number in the fourth row is 4(4+1)/2=10. Therefore, we need to know the number in the 20th row and 20th column that it is the number of the diagonal number. We can see that it is symmetrical, so there are (20-1)=19 columns on the right side of the 20th column, so the first number in the 39th column is the last number in the 20th row and 20th column. . 39 is an odd number, so 39(39+1) /20=780 780 and the extra (39-20)=19 numbers are subtracted, so the final result is 780-19 = 761.
My problem-solving steps
Insert picture description here

Sort

Insert picture description here

Bubble sorting
Condition: 100 times of
adjacent exchanges, only lowercase letters are not repeated, and the shortest dictionary order is the smallest. First, adjacent exchanges, only lowercase letters, no repetition, and shortest can be preliminarily judged and directly reverse the order, because the shortest string can be exchanged the most times. . It is easy to know that the number of exchanges is n(n-1)/2. When n=15, all reverse order can be exchanged 105 times.
That is: onmlkjihgfedcba , exchange 105 times and
now 100 exchanges, so this o must move to the right, very It’s easy to think of nmlkjoihgfedcba . This is 100 times.
However, look at the condition: the lexicographical order is the smallest, so move the smallest one to the left as much as possible. Taking into account five times more, we put the fifth letter j at the end, so jonmlkihgfedcba , Each exchange moves five times less, and finally returns to the reverse order when j is reached.
For example:
jonmlkihgfedcba is to put o at the end to get jnmlkihgfedcbao . Compared with onmlkjihgfedcba, putting o at the end is one less time, nmlkjihgfedcbao
jnmlkihgfedcbao means putting n at the end to get jmlkihgfedcbano. Compared nmlkjihgfedcbao put n place last less time
...
jonmlkihgfedcba to jihgfedcbaklmno just less five times, five times after onmlkjihgfedcba has become jihgfedcbaklmno back to the same
result: jonmlkihgfedcba

Code explanation

def bubbl_sort(alist, count):
    for i in range(len(alist) - 1):
        for j in range(len(alist) - 1 - i):
            if alist[j] > alist[j+1]:
                count += 1
                alist[j], alist[j+1] = alist[j+1], alist[j]
        print(alist)
    return (alist, count)

count = 0
print(bubbl_sort(list('jonmlkihgfedcba'), count))


# 每趟循环结果
['j', 'n', 'm', 'l', 'k', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'o']
['j', 'm', 'l', 'k', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'n', 'o']
['j', 'l', 'k', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'm', 'n', 'o']
['j', 'k', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'l', 'm', 'n', 'o']
['j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'k', 'l', 'm', 'n', 'o']
['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'k', 'l', 'm', 'n', 'o']
['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['g', 'f', 'e', 'd', 'c', 'b', 'a', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['f', 'e', 'd', 'c', 'b', 'a', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['e', 'd', 'c', 'b', 'a', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['d', 'c', 'b', 'a', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['c', 'b', 'a', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['b', 'a', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']


(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'], 100)


Guess you like

Origin blog.csdn.net/qq_31910669/article/details/114843073