0

I am trying to create a list of lists with the input of m and n, where m is the number of lists within the main list and n is the number of elements within each given list. The grid should contain the integers from start to start + rows * cols - 1 and be ascending. But, every odd numbered row should be descending instead.

The code I've written is returning the expected results, but my automated tester is saying it's incorrect. Maybe my logic is messed up somewhere?

inputs:

start = 1, m = 3, n = 5

expected:

[[1,2,3,4,5],[10,9,8,7,6],[11,12,13,14,15]]
result = []
mylist = []
start = 1
for x in range(0, rows):
 for x in range(0, cols):
 result.append(start)
 start += 1 
for y in range(0, rows):
 if y%2 != 0:
 mylist.append(result[cols - 1::-1])
 del result[cols - 1::-1]
 else:
 mylist.append(result[0:cols])
 del result[0:cols]
return mylist 
David Buck
3,88740 gold badges54 silver badges74 bronze badges
asked Nov 24, 2019 at 21:29

2 Answers 2

2

One possible solution, using itertools.count:

from itertools import count
def build(m, n, start=1):
 lst, c = [], count(start)
 for i in range(m):
 lst.append([next(c) for j in range(n)][::-1] if i % 2 else [next(c) for j in range(n)])
 return lst
print(build(3, 5, 1))

Prints:

[[1, 2, 3, 4, 5], [10, 9, 8, 7, 6], [11, 12, 13, 14, 15]]

print(build(3, 0, 1))

Prints:

[[], [], []]
answered Nov 24, 2019 at 22:11
Sign up to request clarification or add additional context in comments.

5 Comments

This also came up incorrect using the automated tester. I'm wondering if it could be that the expected result does not contain spaces between the lists.
@BeanieLeung What are allowed values for m, n and start?
It doesn't state any restrictions on the values
@BeanieLeung Hmm, I don't know then. Could be anything...Maybe the output formatting is wrong...
I must have typed something in incorrectly. This code did work!
1

just generate the list of numbers you need which will be n * m, in your case that would generate 0 to 14 in the python range function. However as we want to start at ` then we need to add the start offset too the range end.

Now we can generate all the numbers we need we just need to think about how to create them. well we can add numbers to the list until the list reaches the size of n, then we need to start a new list, However if the list we just finished is an even numbered row then we need to reverse that list.

def build_lists(m, n, start=1):
 data =[[]]
 for i in range(start, n * m + start):
 if len(data[-1]) < n:
 data[-1].append(i)
 else:
 if len(data) % 2 == 0:
 data[-1] = data[-1][::-1]
 data.append([i])
 if len(data) % 2 == 0:
 data[-1] = data[-1][::-1]
 return data
print(build_lists(3, 5))
print(build_lists(6, 3))
print(build_lists(6, 2, 100))

OUTPUT

[[1, 2, 3, 4, 5], [10, 9, 8, 7, 6], [11, 12, 13, 14, 15]]
[[1, 2, 3], [6, 5, 4], [7, 8, 9], [12, 11, 10], [13, 14, 15], [18, 17, 16]]
[[100, 101], [103, 102], [104, 105], [107, 106], [108, 109], [111, 110]]
answered Nov 24, 2019 at 21:43

1 Comment

The automated tester said this code is incorrect as well.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.