Question

Python code: using only len(), range(), append(), and del keyword when needed as the directions state?...

  1. Python code: using only len(), range(), append(), and del keyword when needed as the directions state? No list comprehension, slicing, or importing modules. Please and thank you.

  2. def diagonal_matrix(mat)

    • Converts a square matrix mat into a lower diagonal matrix https://en.wikipedia.org/wiki/Triangular_matrix

    • After the conversion the values on the diagonal will be the sum of the deleted elements in the respective row

    • Return value is None. Variable mat is directly modi ed in memory

    • mat is a nested list (e.g. [[1,2,3], [4,5,6], [7,8,9]] and is guaranteed to have a square

      shape of size at least 2x2

    • You’re not allowed to use slicing, or import modules, or use the list comprehension syntax

    • The only functions you’re allowed to use are: len() , range() and the del keyword

    • Examples:

      ◦ diagonal_matrix([[1,2,3], [4,5,6], [7,8,9]]) → [[6], [4, 11], [7, 8, 9]]

    • ◦ diagonal_matrix([[1,2],[3,4]]) → [[3], [3,4]]

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

OUTPUT :

CODE :

def diagonal_matrix(mat):
    #Initialize ans
    ans = []
    #Iterate through all arrays in 2d matrix
    for i in range(len(mat)):
        #Initialize array
        f = []
        #take first i elements
        f.extend(mat[i][0:i])
        #Find sum from i+1th element
        f.append(sum(mat[i][i:]))
        ans.append(f)
    return ans

Add a comment
Know the answer?
Add Answer to:
Python code: using only len(), range(), append(), and del keyword when needed as the directions state?...
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
  • Can someone solve these for me using only len(), range(), append(), and del keyword when needed...

    Can someone solve these for me using only len(), range(), append(), and del keyword when needed as the directions state? No list comprehension, slicing, or importing modules. No chr or index(i) functions. def reduce_matrix(mat) • Reduces the size of matrix mat in half, in both dimensions, by merging adjacent elements • The new values of the matrix are the average of the respective merged elements • Return value is None. Variable mat is directly modified in memory • mat is...

  • PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the...

    PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the map referred to is pictured in the screenshot. Need help with these 4 tasks: Function 4: def is_map(map) : given a map (see screenshot), does it meet the following criteria? 1) it is a list of lists of values of type int; 2) it has at least one row and one column; 3) it’s rectangular (all sub-lists are same length); 4) only non-negative ints...

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