Read input as an instruction

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 654 Views - Last Post: 26 July 2012 - 12:27 PM Rate Topic: -----

#16 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • 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