Computing desk | ||
---|---|---|
< December 18 | << Nov | December | Jan >> | Current desk > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
There are some websites which can be found through Tor only. — Preceding unsigned comment added by Темная планета ( talk • contribs) 06:38, 19 December 2019 (UTC)
What's wrong with this code? :
def snail(snail_map): newArray = [] a = 0 b = 0 c = 0 d = len(snail_map[a])-1 while len(snail_map) > 0: while b < len(snail_map[a]): newArray.append(snail_map[a][b]) snail_map[a].pop(b) if len(snail_map[a]) == 0: snail_map.pop(a) while c < len(snail_map): newArray.append(snail_map[c][d]) snail_map[c].pop(d) c += 1 c -= 1 d -= 1 while d >= 0: print(snail_map) print(newArray) print(c,d) print(snail_map[c][d]) newArray.append(snail_map[c][d]) snail_map[c].pop(d) if len(snail_map[c]) == 0: snail_map.pop(c) d -= 1 c -= 1 while c >= 0: newArray.append(snail_map[c][0]) snail_map[c].pop(0) c -= 1 snail([[1,2,3],[8,9,4],[7,6,5]]) snail([[1,2,3,4],[12,13,14,5],[11,16,15,6],[10,9,8,7]])
Basically, this code is meant to fulfill this exercise:
https://www.codewars.com/kata/snail/train/python
For some reason, however, I get this error message:
"Traceback (most recent call last): File "C:/Users/Josh/AppData/Local/Programs/Python/Python37-32/snail_map.py", line 37, in <module> snail([[1,2,3],[8,9,4],[7,6,5]]) File "C:/Users/Josh/AppData/Local/Programs/Python/Python37-32/snail_map.py", line 14, in snail newArray.append(snail_map[c][d]) IndexError: list index out of range"
I don't know why exactly it states that the list index here is out of range considering that "snail_map[c][d]" means "snail_map[1][0]" and considering that the value of "snail_map[c][d]" is "7". So, why exactly does it state that the list index here is out of range?
Any thoughts on this? Futurist110 ( talk) 23:15, 19 December 2019 (UTC)
print (c,d)
before line 16, and c and d both have the value -1 when the error occurs.
AndrewWTaylor (
talk)
11:27, 20 December 2019 (UTC)It looks like 135.84.167.41 has done a good job analyzing this issue. At a wider level, part of the reason this code is so confusing is because of all the values (variables and arrays) that keep changing. That is considered an old-fashioned style these days, and while going full-on functional programming (making everything immutable) creates its own problems, you will generally have an easier time debugging if you use immutable values instead of mutable ones when it's convenient to do so. Python itself makes that difficult because its iterators mutate, but if you only use them once you can look the other way. So my version of this code goes:
from typing import Tuple,Iterator,List
def squares(n: int) -> IteratorTupleint,int]]:
yield from [(0, i) for i in range(n)] # go along the top, left to right
yield from [(i, n-1) for i in range(1,n)] # then down the right edge
yield from [(n-1, i) for i in range(n-2,-1,-1)] # then along bottom, right to left
yield from [(i, 0) for i in range(n-2,0,-1)] # then up the left edge
if n > 2: # now recursively do the nested square inside the edge
yield from [(a+1,b+1) for a,b in squares(n-2)]
def snail(array: ListListint]]) -> Listint]:
n = len(array)
assert all(len(a) == n for a in array)
return list(arrayi][j for i,j in squares(n))
array = [[1,2,3],
4,5,6],
7,8,9]]
print (snail(array))
I made some errors getting the ranges to not overlap, but they were very easy to debug by just examining the output.
With python 3, using type annotations (like above) can help keep your thoughts clear, and Mypy can check the annotations and frequently spot bugs in your code before you even try to run the code.
I noticed just now that the code above only does square arrays rather than arbitrary rectangular ones that might have been expected. However, that should be straightforwardly fixable. 173.228.123.190 ( talk) 12:07, 21 December 2019 (UTC)
Computing desk | ||
---|---|---|
< December 18 | << Nov | December | Jan >> | Current desk > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
There are some websites which can be found through Tor only. — Preceding unsigned comment added by Темная планета ( talk • contribs) 06:38, 19 December 2019 (UTC)
What's wrong with this code? :
def snail(snail_map): newArray = [] a = 0 b = 0 c = 0 d = len(snail_map[a])-1 while len(snail_map) > 0: while b < len(snail_map[a]): newArray.append(snail_map[a][b]) snail_map[a].pop(b) if len(snail_map[a]) == 0: snail_map.pop(a) while c < len(snail_map): newArray.append(snail_map[c][d]) snail_map[c].pop(d) c += 1 c -= 1 d -= 1 while d >= 0: print(snail_map) print(newArray) print(c,d) print(snail_map[c][d]) newArray.append(snail_map[c][d]) snail_map[c].pop(d) if len(snail_map[c]) == 0: snail_map.pop(c) d -= 1 c -= 1 while c >= 0: newArray.append(snail_map[c][0]) snail_map[c].pop(0) c -= 1 snail([[1,2,3],[8,9,4],[7,6,5]]) snail([[1,2,3,4],[12,13,14,5],[11,16,15,6],[10,9,8,7]])
Basically, this code is meant to fulfill this exercise:
https://www.codewars.com/kata/snail/train/python
For some reason, however, I get this error message:
"Traceback (most recent call last): File "C:/Users/Josh/AppData/Local/Programs/Python/Python37-32/snail_map.py", line 37, in <module> snail([[1,2,3],[8,9,4],[7,6,5]]) File "C:/Users/Josh/AppData/Local/Programs/Python/Python37-32/snail_map.py", line 14, in snail newArray.append(snail_map[c][d]) IndexError: list index out of range"
I don't know why exactly it states that the list index here is out of range considering that "snail_map[c][d]" means "snail_map[1][0]" and considering that the value of "snail_map[c][d]" is "7". So, why exactly does it state that the list index here is out of range?
Any thoughts on this? Futurist110 ( talk) 23:15, 19 December 2019 (UTC)
print (c,d)
before line 16, and c and d both have the value -1 when the error occurs.
AndrewWTaylor (
talk)
11:27, 20 December 2019 (UTC)It looks like 135.84.167.41 has done a good job analyzing this issue. At a wider level, part of the reason this code is so confusing is because of all the values (variables and arrays) that keep changing. That is considered an old-fashioned style these days, and while going full-on functional programming (making everything immutable) creates its own problems, you will generally have an easier time debugging if you use immutable values instead of mutable ones when it's convenient to do so. Python itself makes that difficult because its iterators mutate, but if you only use them once you can look the other way. So my version of this code goes:
from typing import Tuple,Iterator,List
def squares(n: int) -> IteratorTupleint,int]]:
yield from [(0, i) for i in range(n)] # go along the top, left to right
yield from [(i, n-1) for i in range(1,n)] # then down the right edge
yield from [(n-1, i) for i in range(n-2,-1,-1)] # then along bottom, right to left
yield from [(i, 0) for i in range(n-2,0,-1)] # then up the left edge
if n > 2: # now recursively do the nested square inside the edge
yield from [(a+1,b+1) for a,b in squares(n-2)]
def snail(array: ListListint]]) -> Listint]:
n = len(array)
assert all(len(a) == n for a in array)
return list(arrayi][j for i,j in squares(n))
array = [[1,2,3],
4,5,6],
7,8,9]]
print (snail(array))
I made some errors getting the ranges to not overlap, but they were very easy to debug by just examining the output.
With python 3, using type annotations (like above) can help keep your thoughts clear, and Mypy can check the annotations and frequently spot bugs in your code before you even try to run the code.
I noticed just now that the code above only does square arrays rather than arbitrary rectangular ones that might have been expected. However, that should be straightforwardly fixable. 173.228.123.190 ( talk) 12:07, 21 December 2019 (UTC)