You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.
For example:
Let’s say you are given the array {1,2,3,4,3,2,1}:
Your function will return the index 3, because at the 3rd position of the array, the sum of left side of the index ({1,2,3}) and the sum of the right side of the index ({3,2,1}) both equal 6.
def find_even_index(arr):
list2 = arr.strip('{}').split(',')
list1 = list(map(int, list2))
len1 = len(list1)
for i in range(1, len1 - 1):
sum1 = sum(list1[0:i])
sum2 = sum(list1[i + 1:len1])
if sum1 == sum2:
return list1.index(list1[i])
arr = input('shuru:')
print(find_even_index(arr))
题目描述
回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度。一开始位于矩阵左上角,方向向下。
输入
输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列。接下来m行每行n个整数,表示这个矩阵。
输出
输出只有一行,共mn个数,为输入矩阵回形取数得到的结果。数之间用一个空格分隔,行末不要有多余的空格。
样例输入
3 3
1 2 3
4 5 6
7 8 9
样例输出
1 4 7 8 9 6 3 2 5
import math #导入math包
r,c=map(int,input().split())#其中r,c分别代表要输入数据的行数和列数
list1=[]#列表用来存放键盘输入的数据
ans=[]#用来存放要输出的数据
for i in range(0,r):#二维列表输入实例
list1.append(input().split())
for j in range(0,math.ceil(min(r,c)/2)):#math.ceil()函数是用来向上取整,向下取整直接整数相除即可,math.ceil(min(r,c)/2表示要转的圈数
for x in range(j,r-j):#将第j圈的左“1”字形x放入ans;
ans.append(list1[x][j])
for y in range(j+1,c-j):#将第j圈的下“一”字形x放入ans;
ans.append(list1[r-1-j][y])
if c-1>2*j:#判断一下是否还有多余的列需要转圈
for p in range(r-j-2,j-1,-1):#将第j圈的右“1”字形放入ans;
ans.append(list1[p][c-1-j])
if r-1>2*j:
for q in range(c-j-2,j,-1):#将第j圈的上“一”字形放入ans;
ans.append(list1[j][q])
for x in ans:
print(x,'',end='')
本文介绍了一种寻找数组中平衡索引的方法,即找到一个索引位置,使得该位置左边所有元素之和等于右边所有元素之和。此外,还详细解释了一个名为“回形取数”的算法,该算法用于沿矩阵边缘按特定顺序获取数值。
221

被折叠的 条评论
为什么被折叠?



