# Read input as an instruction

• (2 Pages)
• 1
• 2

## 15 Replies - 1280 Views - Last Post: 26 July 2012 - 12:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286943&amp;s=2e8033d82425438658fa7401f22b3f0c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 carnivroar

• D.I.C Regular

Reputation: 29
• Posts: 387
• Joined: 18-September 11

## Re: Read input as an instruction

Posted 26 July 2012 - 12:27 PM

Well here's how I did it

```	def save(self, output):
file = open(output, "w")
file.write(".".join(",".join(str(self.MATRIX[i][j]) for j in range(self.COL)) for i in range(self.ROW)))
file.close()

def load(input):
file = open(input,"r")
contents = file.read().split(".")
file.close()
M = [[contents[i].split(",")[j] for j in range(len(contents[i].split(",")))] for i in range(len(contents))]
def nom(m):
if (not "/" in m):
return int(m)
return int(m[:m.index("/")])
def den(m):
if (not "/" in m):
return 1
return int(m[m.index("/")+1:])
return Matrix([[Rational(nom(M[i][j]), den(M[i][j])) for j in range(len(M[i]))] for i in range(len(M))])

```

Input and output look like this

Quote

1,-2,2.-1,1,3.1,-2,-4

I could use other characters than , and .

OOP speaking, does it make sense to include those 2 functions inside the Matrix class?

Also, would it be reasonable to implement a Vector class as a Matrix of one row? Is a vector a matrix, in other words? Or make an entire separate class?

This post has been edited by carnivroar: 26 July 2012 - 12:30 PM

Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }