• 1
    数据输入输出
    • 任务
  • 2
    条件语句
    • 任务
  • 3
    for循环
    • 任务
  • 4
    字符串
    • 任务
  • 5
    while循环
    • 任务
  • 6
    列表
    • 任务
  • 7
    二维数组
    • 任务
  • 8
    字典
    • 任务
  • 9
    集合
    • 任务
  • 10
    函数与递归
    • 任务
  • к

课程 7. Two -dimensional arrays

难度:

任务«你需要推迟多久?»

💻 Python
你需要为10年制定预算。年度支出以二维数组的形式给出   Expenses [year] [Category] ,其中   year — 从0到9  ( categories 支出类别数为整数,始终等于5)。当前薪资 —   salary 。第   n   年的薪资计算公式为   salary * (1 + p)^n ,其中   p   — 薪资增长率(以小数表示,例如0.05代表5%)。找到最小的   p (精度为0.001),使得10年的总薪资不低于这10年的总支出。

输入格式

salry (物料编号)。以下是10行,每行包含5个物料编号 & mdash; 按类别划分的年度支出

输出格式

推导满足条件的最小值   p (材料编号),精度达到小数点后三位

示例

输入

3000
RN
200 200 200 200 200
RN
300 300 300 300 300
RN
400 400 400 400
RN
500 500 500 500 500
RN
600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600 600
RN
700 700 700 700 700
RN
800 800 800 800 800
RN
900 900 900 900
RN
1000 1000 1000 1000 1000
RN
1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 1100

输出

0.018

提示

min function

The min() function is your tool for quickly searching for the smallest element of several values or within a collection.

Syntax

min(iterable, *[, key, default])
min(arg1, arg2, *args[, key])
  • iterable: An iterable object (for example, a list, tuple, string, set).
  • arg1, arg2, *args: One or more arguments passed directly for comparison.
  • key (optional): A function that is applied to each element before comparison. The minimum will be searched based on the results of this function.
  • default (optional): The value that will be returned if iterable is empty. If default is not specified, an attempt to find the minimum in an empty collection will cause the error ValueError.

Usage examples

  • With a single iterable object:

    numbers = [3, 1, 4, 1, 5, 9]
    print(min(numbers))  # Output: 1
    
  • With multiple arguments:

    print(min(3, 1, 4, 1, 5, 9)) # Output: 1
    
  • With the key parameter: Searching for an element by any of its properties.

    words = ["apple", "banana", "cherry"]
    # Finding the word with the shortest length
    print(min(words, key=len))  # Output: 'apple'
    
  • With the default parameter: Safe handling of potentially empty collections.

    empty_list = []
    print(min(empty_list, default="empty"))  # Output: 'empty'
    

Useful tips and subtleties:

  • Comparing different types: Incomparable data types, such as a number and a string, cannot be compared. min(5, "hello") will cause the error TypeError.
  • How the key works: It is important to understand that key only changes the value for comparison, but returns the original element. In the example min(words, key=len), the lengths (5, 6, 6) are compared, but the word 'apple' itself is returned.
  • Using lambda functions with key: For complex comparisons, it is convenient to use anonymous lambda functions. For example, to find a dictionary with the minimum value by the key 'age':
    people = [{'name': 'John', 'age': 25}, {'name': 'Jane', 'age': 22}]
    print(min(people, key=lambda person: person['age']))  # Output: {'name': 'Jane', 'age': 22}
    
  • String comparison: The strings are compared lexicographically (in alphabetical order). min("a", "b", "A") returns 'A', since capital letters have a smaller code in the symbol table.

Function max()

The max() function works similarly to min(), but returns the largest element from the passed arguments or collection.

Syntax

max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])

The parameters (iterable, arg1, arg2, *args, key, default) have the same value as in the min() function.

Usage examples

  • With a single iterable object:

    numbers = [3, 1, 4, 1, 5, 9]
    print(max(numbers))  # Output: 9
    
  • With multiple arguments:

    print(max(3, 1, 4, 1, 5, 9)) # Output: 9
    
  • With the key parameter:

    words = ["apple", "banana", "cherry"]
    # Finding the word with the longest length
    print(max(words, key=len))  # Output: 'banana'
    
  • With the default parameter:

    empty_list = []
    print(max(empty_list, default='N/A'))  # Output: 'N/A'
    

Useful tips and subtleties:

  • All the tips and subtleties applicable to min() are also relevant for max(): avoid mixing types, use lambda functions for complex keys and keep in mind the default parameter for empty collections.
  • If there are several identical maximum/minimum elements, the first one is returned. In the example, max(words, key=len) the words "banana" and "cherry" have the same length of 6, but max returns the first encountered 'banana'.

Function sum()

The sum() function is used to find the sum of numeric elements in an iterable object.

Syntax

sum(iterable, start=0)
  • iterable: An iterable object containing numbers (for example, a list, a tuple, a set).
  • start (optional): The initial value that is added to the total amount. The default value is 0.

Usage examples

  • Sum of list/tuple/set elements:

    numbers_list = [1, 2, 3, 4, 5]
    print(sum(numbers_list))  # Output: 15
    
  • Using the start parameter:

    numbers = [1, 2, 3, 4, 5]
    # We start summing not from 0, but from 10
    total = sum(numbers, 10)
    print(total) # Output: 25 (10 + 1 + 2 + 3 + 4 + 5)
    

Application to two-dimensional arrays (lists of lists)

sum() can be effectively used to process matrices, but with one important caveat.

  • Wrong approach: sum(matrix) will cause the error TypeError, as Python will try to add the initial value 0 with the list [1, 2, 3].
  • The right approach: You must first calculate the sum of each row, and then add these amounts.
    # Two-dimensional array
    matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    
    # Sum of all array elements using a generator
    total_sum = sum(sum(row) for row in matrix)
    print(total_sum) # Output: 45
    

Useful tips and subtleties:

  • For numbers only: sum() only works with numeric types (int, float). An attempt to summarize a list of strings (sum(["a", "b"])) will result in a TypeError.
  • Summation True/False: True is interpreted as 1, and False as 0. This can be used to count the true values in the collection.
    bools = [True, False, True, True] # 1 + 0 + 1 + 1
    print(sum(bools)) # Output: 3
    
  • "Flattening" the list of lists: The start parameter can be cunningly used to combine lists, although it is less readable and efficient than other methods.
    list_of_lists = [[1, 2], [3, 4], [5]]
    flat_list = sum(list_of_lists, [])
    print(flat_list) # Output: [1, 2, 3, 4, 5]
    
    Warning: This method is not recommended for large lists due to poor performance.
main.py
测试 1
测试 2
测试 3
测试 4
测试 5
测试 6
测试 7
测试 8
测试 9
测试 10
开发者解答

🎉 恭喜! 🎉

你出色地完成了任务!这是一个不小的挑战,但你找到了正确答案。你离编程大师又近了一步!继续保持,每一步都让你更强大。

AD

广告

red-snake blue-snake green-snake

正在运行代码...

AI助手

你好!我是你的编程助手。随时向我提问关于 Python 的问题——我可以解释函数、方法,并帮助你完成当前任务!