Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flipping matrices problem
#1
My question is:
A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix

1 2 3
4 5 6
7 8 9
would be represented as [[1, 2, 3], [4, 5, 6], [7, 8, 9]].

A horizonatal flip reflects each row. For instance, if we flip the previous matrix horizontally, we get

3 2 1
6 5 4
9 8 7
which would be represented as [[3, 2, 1], [6, 5, 4], [9, 8, 7]].

A vertical flip reflects each column. For instance, if we flip the previous matrix that has already been flipped horizontally, we get

9 8 7
6 5 4
3 2 1
which would be represented as [[9, 8, 7], [6, 5, 4], [3, 2, 1]].

Write a Python function matrixflip(m,d) that takes as input a two dimensional matrix m and a direction d, where d is either 'h' or 'v'. If d == 'h', the function should return the matrix flipped horizontally. If d == 'v', the function should retun the matrix flipped vertically. For any other value of d, the function should return m unchanged. In all cases, the argument m should remain undisturbed by the function.

So I figured out the solution:
def matrixflip(m,d):
  newm=m[:]
  if d=='v':
    n=len(m)-1
    for i in range(0,len(m)//2):
      newm[i],newm[n-i]=newm[n-i],newm[i]
  elif d=='h':
    n=len(m[0])-1
    for i in range(0,len(m)):
      for j in range(0,len(m)//2):
        newm[i][j],newm[i][n-j]=newm[i][n-j],newm[i][j]
  return newm
The problem is that if I submit my answer, out of the two test cases when it is flipped horizontally i.e d='h' it shows side effect but it works in python interpreter in my laptop without any issues
Reply
#2
Are the input matrices guaranteed to be square? The problem doesn't say they are. In the d == 'h' section you are using len(m) for both the iteration over rows and the iteration over columns.

You should generally iterate over the list, not the indexes of the list, as shown here. Is there some reason you are not using list.reverse() or list[::-1] to reverse the list or sub-lists?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you!
Sorry didn't know that.
I'll make sure to add python tag in the future

(Feb-20-2019, 04:03 PM)ichabod801 Wrote: Are the input matrices guaranteed to be square? The problem doesn't say they are. In the d == 'h' section you are using len(m) for both the iteration over rows and the iteration over columns.

You should generally iterate over the list, not the indexes of the list, as shown here. Is there some reason you are not using list.reverse() or list[::-1] to reverse the list or sub-lists?
I got the matrix they are using for tests.
[[1,2], [3,4]] this when flipped vertically works without any problem
But when flipped horizontally it shows side effect.(These are in the website in which I have to submit my assignment. The website provides a python 3 terminal to paste the codes and run the script)
So I tried running the same script in my laptop. Both the cases worked without any errors and I get the correct output too!
Reply
#4
What side effect is it showing on the web site? I'm getting the correct answer on my laptop as well.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Feb-20-2019, 04:26 PM)ichabod801 Wrote: What side effect is it showing on the web site? I'm getting the correct answer on my laptop as well.

Thank you for trying it in your laptop as well, so the code works then.
The website doesn't show what side effect it is, just shows "side effect" [Image: GZ8hdWE]
Reply
#6
Got it: You're modifying m. Check m after your run and you will see that it is flipped. You are only making a shallow copy of m, you need to make a deep copy.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Feb-20-2019, 05:19 PM)ichabod801 Wrote: Got it: You're modifying m. Check m after your run and you will see that it is flipped. You are only making a shallow copy of m, you need to make a deep copy.

I didn't get it, passing the old_list to the constructor returns a new list right?
Can you please tell what should I edit?
-Thanks
Reply
#8
You need to change line 2. n = m[:] means that n is a different list than m. However, n[0] is still the same list as m[0]. With the vertical flip, you are only modifying n, so it doesn't mess with m. But with the horizontal flip, you are changing n[0], which changes m[0], and thereby changes the overall m. You've got the right technique with the default slice [:], but you need to apply it to each sub-list of m.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(Feb-20-2019, 05:37 PM)ichabod801 Wrote: You need to change line 2. n = m[:] means that n is a different list than m. However, n[0] is still the same list as m[0]. With the vertical flip, you are only modifying n, so it doesn't mess with m. But with the horizontal flip, you are changing n[0], which changes m[0], and thereby changes the overall m. You've got the right technique with the default slice [:], but you need to apply it to each sub-list of m.

Oh I totally forgot about memory management in python while solving this tricky problem.
So now I split every sublist in m and assigned that to corresponding new list and it worked.
Finally I am so relieved Dance , was scratching my head Think so long for this.
Thanks a million! So glad I joined this forum. [Image: Cc8niZI]
Reply
#10
Cool, but as I said, doing stuff index by index is not pythonic. Here's two other ways to solve the problem:

def matrixflip(m, d):
    if d == 'v':
        return m[::-1]
    elif d == 'h':
        return [row[::-1] for row in m]
    else:
        return m[:]
or:

def matrixflip(m, d):
    if d == 'v':
        return reversed(m)
    elif d == 'h':
        return [reversed(row) for row in m]
    else:
        return m[:]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  4D matrices ali1almakhmari 0 761 Jun-13-2022, 09:21 AM
Last Post: ali1almakhmari
  Unable to use Pauli Matrices in QNET Package Rupayan 2 1,923 Sep-25-2021, 06:02 AM
Last Post: Rupayan
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,240 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Application of dilute matrices chris_drak 0 1,351 Mar-29-2020, 03:04 PM
Last Post: chris_drak
  Partial "visual" Matching of matrices masteripper 15 5,318 Nov-03-2019, 05:41 PM
Last Post: masteripper
  flipping the for loop in file juniorcoder 2 2,224 Oct-21-2018, 01:47 PM
Last Post: wavic
  matrices math problem lokoprof 1 2,220 Aug-27-2018, 07:48 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020