Найти - Пользователи
Полная версия: перевести код на Python
Начало » Python для новичков » перевести код на Python
1
kate
Всем привет, имеется вот такой код написанный на C#
 public static int[,] funk(int[,] S)
{
int width = S.GetLength(0);
int height = S.GetLength(1);

int[,] result = new int[width, height];

result[0, 0] = S[0, 0];

for (int x = 1; x < width; x++)
result[x, 0] = S[x, 0] + result[x - 1, 0];

for (int y = 1; y < height; y++)
result[0, y] = S[0, y] + result[0, y - 1];

for (int y = 1; y < height; y++)
for (int x = 1; x < width; x++)
result[x, y] = S[x, y] + result[x - 1, y] + result[x, y - 1] - result[x - 1, y - 1];

return result;
}
Ребят помогите пожалуйста перевести его на python, я пыталась, но что-то делаю не так…
def funk():
w = 100.0
h = 100.0
x = 1.0
y = 1.0
si = [0.0, 0.0]
result = [w, h]
result [0.0, 0.0] = si[0.0, 0.0]

for x in range(w):
x +=1
result[x, 0] = si[x, 0] + result[x - 1, 0]

for y in range(h):
y +=1
result[0, y] = si[0, y] + result[0, y - 1]

for x in range(w):
x +=1
for y in range(h):
y +=1
result[x, y] = si[x, y] + result[x - 1, y] + result[x, y - 1] - result[x - 1, y - 1]
return result
За ранее спасибо….
pyuser
def funk(S):
height = len(S)
width = len(S[0])

result = [[0 for j in range(width)] for i in range(height)]

result[0][0] = S[0][0]

for i in range(1, height):
result[i][0] = S[i][0] + result[i - 1][0]

for i in range(1, width):
result[0][i] = S[0][i] + result[0][i - 1]

for i in range(1, height):
for j in range(1, width):
result[i][j] = S[i][j] + result[i - 1][j] + \
result[i][j - 1] + result[i - 1][j - 1]

return result
kate
спасибо pyuser, вы очень помогли.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB