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


Messages In This Thread
Flipping matrices problem - by Qmohankumar0017 - Feb-20-2019, 03:52 PM
RE: Flipping matrices problem - by ichabod801 - Feb-20-2019, 04:03 PM
RE: Flipping matrices problem - by Qmohankumar0017 - Feb-20-2019, 04:04 PM
RE: Flipping matrices problem - by ichabod801 - Feb-20-2019, 04:26 PM
RE: Flipping matrices problem - by Qmohankumar0017 - Feb-20-2019, 04:44 PM
RE: Flipping matrices problem - by ichabod801 - Feb-20-2019, 05:19 PM
RE: Flipping matrices problem - by Qmohankumar0017 - Feb-20-2019, 05:30 PM
RE: Flipping matrices problem - by ichabod801 - Feb-20-2019, 05:37 PM
RE: Flipping matrices problem - by Qmohankumar0017 - Feb-20-2019, 06:00 PM
RE: Flipping matrices problem - by ichabod801 - Feb-20-2019, 06:44 PM
RE: Flipping matrices problem - by buran - Feb-20-2019, 06:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  4D matrices ali1almakhmari 0 798 Jun-13-2022, 09:21 AM
Last Post: ali1almakhmari
  Unable to use Pauli Matrices in QNET Package Rupayan 2 1,977 Sep-25-2021, 06:02 AM
Last Post: Rupayan
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,394 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Application of dilute matrices chris_drak 0 1,385 Mar-29-2020, 03:04 PM
Last Post: chris_drak
  Partial "visual" Matching of matrices masteripper 15 5,550 Nov-03-2019, 05:41 PM
Last Post: masteripper
  flipping the for loop in file juniorcoder 2 2,292 Oct-21-2018, 01:47 PM
Last Post: wavic
  matrices math problem lokoprof 1 2,261 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