为什么我的 Python 代码出现运行时错误?
Why do I have a RunTime error for my Python code?
我正在做 Hackerrank 基础 Python 编程挑战。这个叫做 Mini-Max Sum* 而问题的 link 是:https://www.hackerrank.com/challenges/one-week-preparation-kit-mini-max-sum/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=one-week-preparation-kit&playlist_slugs%5B%5D=one-week-day-one
我能够为示例案例编译我的代码,但它不适用于任何其他案例。我很困惑为什么会收到 运行 时间错误。请帮忙!
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'miniMaxSum' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#
def miniMaxSum(arr):
# Find the minimum number:
minimum = 10^9
for integer in arr:
if integer < minimum:
minimum = integer
# Search the list to find the minimum and remove it:
arr.remove(minimum)
# Sum all of the entries in the array; this gives the maximum sum for four integers:
max_sum = 0
for number in arr:
max_sum += number
# --------------------------------------------------------
arr.append(minimum)
# Find the maximum number:
maximum = 1
for integer in arr:
if integer > maximum:
maximum = integer
# Search the list to find the maximum and remove it:
arr.remove(maximum)
# Sum all of the entries in the array; this gives the maximum sum for four integers:
min_sum = 0
for number in arr:
min_sum += number
print(min_sum, max_sum)
if __name__ == '__main__':
arr = list(map(int, input().rstrip().split()))
miniMaxSum(arr)
问题出在函数的第一行 miniMaxSum()
.
def miniMaxSum(arr):
# Find the minimum number:
minimum = 10^9
^
表示 xor operation 而不是 power/exponentiation。在 python 中使用 **
。
10^9 = 3
(二进制 1010 ^ 1001 = 0011
)。因此,您将 minimum
初始化为 3 而不是 1000,000,000
为什么会出现运行时错误?
arr.remove(minimum)
minimum
= arr
.
中可能不存在 3
您的 link 问题已过期,最好包含声明。当前 link 问题:https://www.hackerrank.com/challenges/mini-max-sum/problem
声明:
Given five positive integers, find the minimum and maximum values that
can be calculated by summing exactly four of the five integers. Then
print the respective minimum and maximum values as a single line of
two space-separated long integers.
我正在做 Hackerrank 基础 Python 编程挑战。这个叫做 Mini-Max Sum* 而问题的 link 是:https://www.hackerrank.com/challenges/one-week-preparation-kit-mini-max-sum/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=preparation-kits&playlist_slugs%5B%5D=one-week-preparation-kit&playlist_slugs%5B%5D=one-week-day-one
我能够为示例案例编译我的代码,但它不适用于任何其他案例。我很困惑为什么会收到 运行 时间错误。请帮忙!
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'miniMaxSum' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#
def miniMaxSum(arr):
# Find the minimum number:
minimum = 10^9
for integer in arr:
if integer < minimum:
minimum = integer
# Search the list to find the minimum and remove it:
arr.remove(minimum)
# Sum all of the entries in the array; this gives the maximum sum for four integers:
max_sum = 0
for number in arr:
max_sum += number
# --------------------------------------------------------
arr.append(minimum)
# Find the maximum number:
maximum = 1
for integer in arr:
if integer > maximum:
maximum = integer
# Search the list to find the maximum and remove it:
arr.remove(maximum)
# Sum all of the entries in the array; this gives the maximum sum for four integers:
min_sum = 0
for number in arr:
min_sum += number
print(min_sum, max_sum)
if __name__ == '__main__':
arr = list(map(int, input().rstrip().split()))
miniMaxSum(arr)
问题出在函数的第一行 miniMaxSum()
.
def miniMaxSum(arr):
# Find the minimum number:
minimum = 10^9
^
表示 xor operation 而不是 power/exponentiation。在 python 中使用 **
。
10^9 = 3
(二进制 1010 ^ 1001 = 0011
)。因此,您将 minimum
初始化为 3 而不是 1000,000,000
为什么会出现运行时错误?
arr.remove(minimum)
minimum
= arr
.
您的 link 问题已过期,最好包含声明。当前 link 问题:https://www.hackerrank.com/challenges/mini-max-sum/problem
声明:
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.