缺少期限等差 - 清理我的code我的、等差、期限、code

2023-09-11 02:19:09 作者:I服了You

我刚刚试用了一下在线编程测验是要我尽快解决这个问题。我得到了正确的答案,但我知道这是不是pretty的。我想成为一个更好的程序员,编写更干净,更高效的code,所以请给我一些建议。我已经包括下面的描述。 PS我觉得这个算法失败的情况下,N = 3

I just tried a little online programming quiz that asked me to solve this problem as quickly as possible. I got the right answer but I know it isn't pretty. I'm trying to become a better programmer and write cleaner, more efficient code so please give me some tips. I've included the description below. PS I think this algorithm fails for the case N=3

# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
N= int(sys.stdin.readline())
stringdata =  sys.stdin.readline()
array = stringdata.split(' ')
diff1=[0]*(N-1)
diff2 = [0]*(N-2)
index = 0
diff = 0
for i in  range(0,len(array)-1):
    first_diff[i] = int(array[i+1])-int(array[i])
for i in   range(0,len(diff1)-1):
    second_diff[i] = first_diff[i+1]-first_diff[i]
    if second_diff[i] == 0:
        diff = first_diff[i]
    else:
        index = i
print(int(array[index])+diff)

任务:找到失踪的长期算术级数

Task: Find the missing term in an Arithmetic Progression.

这是等差被定义为其中有连续的术语给定一系列数字之间的恒定差。为您提供了一个等差数列的连续元素。只有一个障碍:究竟从原来的系列一体期限为自组已给你的数字失踪。给定系列的其余部分是相同的原始的AP。寻找失踪的期限。

An Arithmetic Progression is defined as one in which there is a constant difference between the consecutive terms of a given series of numbers. You are provided with consecutive elements of an Arithmetic Progression. There is however one hitch: Exactly one term from the original series is missing from the set of numbers which have been given to you. The rest of the given series is the same as the original AP. Find the missing term.

输入格式 第一行包含一个整数n,它是将被作为输入提供的项数。 这之后是N个连续的整数,每对整数之间的空间。所有这些都是在一行,和它们在AP(比其中的一个整数是缺少点等)。

Input Format The first line contains an Integer N, which is the number of terms which will be provided as input. This is followed by N consecutive Integers, with a space between each pair of integers. All of these are on one line, and they are in AP (other than the point where an integer is missing).

输出格式 这是从系列缺失的整数之一号

Output Format One Number which is the missing integer from the series.

采样输入 五 1 3 5 9 11

Sample Input 5 1 3 5 9 11

样本输出 7

推荐答案

我觉得这$​​ C $ C能有所简化。首先,输入。没有太大的不同,但我用进行raw_input (或输入在Python 3),我马上的号码 INT

I think this code can be somewhat simplified. First, the input. Not much different, except I use raw_input (or input in Python 3), and I immediately map the numbers to int.

n = int(raw_input("Number of Numbers: "))
s = raw_input("List of Numbers, space-separated: ")
nums = map(int, s.split())
assert n == len(nums) and n > 2

现在的有趣的部分:需要注意的是(假设是良好的列表)有可能只是数字之间有两点不同:要么是正确的差别,或者两倍的差异。我用一个列表COM prehension创建元组的的名单(差异,在指数)的。现在,我可以简单地使用内置的最高函数来找到一个与两次正确区别和各自的指数( D 2,指数),并计算丢失的数量。

Now for the interesting part: Note that (assuming the list is well-formed) there can just be two differences between numbers: Either the correct difference, or two times that difference. I use a list comprehension to create a list of tuples (difference, at index). Now I can simply use the builtin max function to find the one with two times the correct difference and the respective index (d2, index) and calculate the missing number.

diffs = [(nums[i+1] - nums[i], i) for i in range(n-1)]
(d2, index) = max(diffs)
print nums[index] + d2 / 2

但问题是关于编码风格,不是算法,所以这里是我的想法:

But the question was about coding style, not about the algorithm, so here are my thoughts:

添加程序的逻辑块之间的一些空白行和注释(如#读取输入地图数组 INT 一次,而不是每次铸造的数字,你需要他们 您可以使用列表COM prehension创建 DIFF1 (又名 first_diff ),在我例如 您不需要 DIFF2 的所有的;只写如果DIFF1 [I + 1] - DIFF1 [I] == 0: 简洁:范围(0,len个(阵列)-1)是一样的范围(N-1) add some blank lines and comments between logical blocks of your program (e.g. # read input) map the array to int once, instead of casting the numbers each time you need them you can use a list comprehension to create diff1 (aka first_diff), as in my example you don't need diff2 at all; just write if diff1[i+1] - diff1[i] == 0: be concise: range(0,len(array)-1) is the same as range(N-1)
 
精彩推荐
图片推荐