Question

Python HELP How can a get the diagonals of an n x n list without using...

Python HELP

How can a get the diagonals of an n x n list without using numpy.

For example if have a list: L = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Then I can get the diagonals:

1, 6, 11, 16

2, 7 , 12

3, 8

5 , 10 ,15

9, 14

2, 5

3, 6, 9

4, 7, 10, 13

8, 11, 14

12, 15

0 0
Add a comment Improve this question Transcribed image text
Answer #1

col data = [ 18 Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux [1, 6, 11, 16] [2, 7, 12] [3, 8] 19 while row

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

size = len(L)

for i in range(size - 1):
        row = 0
        col = i
        data = []
        while row < size and col < size:
                data.append(L[row][col])
                row += 1
                col += 1
        print(data)

for i in range(1, size - 1):
        row = i
        col = 0
        data = []
        while row < size and col < size:
                data.append(L[row][col])
                row += 1
                col += 1
        print(data)


for i in range(1, size):
        row = 0
        col = i
        data = []
        while row < size and col >= 0:
                data.append(L[row][col])
                row += 1
                col -= 1
        print(data)


for i in range(1, size - 1):
        row = i
        col = size-1
        data = []
        while row < size and col >= 0:
                data.append(L[row][col])
                row += 1
                col -= 1
        print(data)


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Python HELP How can a get the diagonals of an n x n list without using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python HELP How can a get the diagonals of an n x n list without using...

    Python HELP How can a get the diagonals of an n x n list without using numpy. For example if have a list: L = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Then I can get the diagonals: 1, 6, 11, 16 2, 7 , 12 3, 8 5 , 10 ,15 9, 14 2, 5 3, 6, 9 4, 7, 10, 13 8, 11, 14 12,...

  • I really do not understand how to read these.... If I can get some help that...

    I really do not understand how to read these.... If I can get some help that would be amazing!!! DLL 00 KIMAX an w M 9 ma N N 00 a w 00 N O 16 16 15 - 15 0.6 14 14 13 13 0.5 12 12 11 11 10 10 10 KIMAX 10.4 9 8 0.3 பாப்பா பாடல் 7 6 5 0.2 3 0.1 2 1 1 cm A cm B cm C m Gradations: A B C...

  • Java Magic Square: A n x n matrix that is filled with the numbers 1, 2,...

    Java Magic Square: A n x n matrix that is filled with the numbers 1, 2, 3,.... n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. I need to write an application that gets 16 values as user input, in any order. Store these values in an ArrayList. When the numbers are put into a square, this would be a 4x4 two-dimensional array....

  • using Python --- from typing import List THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7,...

    using Python --- from typing import List THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7, 8, 9]] FOUR_BY_FOUR = [[1, 2, 6, 5], [4, 5, 3, 2], [7, 9, 8, 1], [1, 2, 1, 4]] UNIQUE_3X3 = [[1, 2, 3], [9, 8, 7], [4, 5, 6]] UNIQUE_4X4 = [[10, 2, 3, 30], [9, 8, 7, 11], [4, 5, 6, 12], [13, 14, 15, 16]] def find_peak(elevation_map: List[List[int]]) -> List[int]: """Return the cell that is the highest point in the...

  • Sisay Putty: Attempt 1 Use the roster method to list all the elements in the set...

    Sisay Putty: Attempt 1 Use the roster method to list all the elements in the set {x | xe N and x > 41 O{4} {5, 6, 7, 8, 9, 10,...) {1,2,3,4} 10 Question 4 (1 point) Use the roster method to list all the elements in the set {x|x N and x < 4 and x 2 12) 13 15,6, 7, 8, 9, 10, 11, 12) (1,2,3,4) 16, 7, 8, 9, 10....) BO 3 4 5 6 7 8...

  • How would I type this up in python to get spiral circles in turtle 1 #...

    How would I type this up in python to get spiral circles in turtle 1 # This program draws a design using repeated circles. 2 import turtle 3 4 # Named constants 5 NUM_CIRCLES = 36    # Number of circles to draw 6 RADIUS = 100        # Radius of each circle 7 ANGLE = 10          # Angle to turn 8 ANIMATION_SPEED = 0 # Animation speed 9 10 # Set the animation speed. 11 turtle.speed(ANIMATION_SPEED) 12 13 # Draw 36...

  • I NEED HELP WITH NUMBER 8 & 9 Get a random sample of n=29 from the...

    I NEED HELP WITH NUMBER 8 & 9 Get a random sample of n=29 from the data set. H0: ? = 15 H1: ? ≠ 15 x f 10 4 11 6 12 7 13 5 14 4 15 2 16 1 Sampling distribution (assuming the null is true): 4. What is the mean of the sampling distribution? (+1) mean= 15 5. What is standard error of the sampling distribution? (+1) Find it on the output. (+1) standard error= 1.63...

  • I need to create a C++ program to simulate a Round Robin Tournament. For example: if...

    I need to create a C++ program to simulate a Round Robin Tournament. For example: if a user enters 4, a 4 team tournament would output: 1 2 3 4 2 1 4 3 3 4 1 2 4 3 2 1 My goal is to create this program using a two dimentional array, however I am unsure how to go about doing initializing everything. How do I write a constructor for this? The following is the class declaration I...

  • Magic Square question for Python

    You have this solution in Java, I am interested in this same solution for Python.One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.You should find that the first, second, and...

  • Problem 6. The set (Z19 − {0}, ·19) is a group with the indicated operation; see...

    Problem 6. The set (Z19 − {0}, ·19) is a group with the indicated operation; see the attached table. a.) Show that H = {1, 7, 8, 11, 12, 18} is a subgroup. b.) List all the right cosets of H. c.) Show that if Hy = Hx then xy−1 ∈ H. [Make sure to give a reason for each step.] d.) Show that φ : H → Hx defined by φ(h) = hx is one-to-one and onto. [Use the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT