Home
Preface
Chapter Selection
Programming Examples
Homework Problems
Homework Solutions MEA's
Search Useful Links

Homework Solutions

Chapter 7 Homework Solutions

7.1
#!/usr/bin/ruby
require 'file_writer.rb'
outfile = FileWriter.new("game.txt")
print "Please specify the first move for player one; using two digits for each co-ordinate.
	 For example if the player placed a stone of the third horizontal and 2nd vertical 
	lines please enter 0302. Enter S to indicate skipping a turn. Enter Q to quit.\n"

i=gets.to_s
until i == "Q\n"
  outfile.writeLine(i)
  i=gets.to_s
end
outfile.close()
 
7.2
#/usr/bin/ruby

require 'file_reader.rb'

infile = FileReader.new("game.txt")

board = Array.new(19)
for i in 0..18
  board[i]=["+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+"]
end


counter = 0

while ((inputLine = infile.readLine) && (inputLine.to_s[0,1]!="Q"))
  if(inputLine[0,1]=="S") 
    counter += 1
  else
    i=inputLine[0,2].to_i
    j=inputLine[2,2].to_i
    currentRow = board[i]
    currentRow[j] = ["x","o"][counter % 2]
    board[i]=currentRow
    counter += 1
  end
end

for i in 0..18 
  currentRow = board[i]
  for j in 0..18
    print currentRow[j]
  end
  print "\n"
end
Solution 3 data.sgf
(;
FF[3]
EV[First Kisei title]
RO[1]
ID[ 1/1]
PB[Fujisawa Shuko]
BR[9d]
PW[Hashimoto Utaro]
WR[9d]
TM[9h]
KM[5.5]
RE[B+R]
DT[1976-12-02,03]
PC[Tokyo, Japan]

;B[pd];W[cq];B[pq];W[po];B[dd];W[oq];B[or];W[op];B[nq];W[nr]
;B[mr];W[pr];B[ns];W[qq];B[mo];W[jc];B[qm];W[mn];B[nn];W[pm]
;B[pl];W[om];B[nm];W[ol];B[pk];W[qi];B[ok];W[nl];B[mm];W[qf]
;B[qe];W[pf];B[nd];W[gc];B[eq];W[do];B[gp];W[cf];B[cm];W[cn]
;B[dm];W[fo];B[ci];W[go];B[hp];W[ee];B[ed];W[bd];B[gd];W[fd]
;B[fe];W[fc];B[de];W[ch];B[ef];W[di];B[cj];W[dh];B[dj];W[rk]
;B[rl];W[qk];B[ql];W[ge];B[gf];W[hd];B[pi];W[fg];B[cc];W[qh]
;B[re];W[ml];B[ph];W[rf];B[lj];W[nf];B[ro];W[rp];B[qo];W[kl]
;B[lm];W[ll];B[bc];W[oc];B[pc];W[nh];B[qp];W[pp];B[rq];W[rr]
;B[sp];W[od];B[ob];W[nb];B[oe];W[nc];B[pe];W[ne];B[of];W[og]
;B[pg];W[qg];B[pb];W[se];B[md];W[lb];B[sd];W[sf];B[rc];W[lg]
;B[kg];W[kh];B[jh];W[kf];B[jg];W[lh];B[fi];W[fh];B[gi];W[hf]
;B[cr];W[dq];B[dr];W[ep];B[fq];W[gm];B[hh];W[bg];B[ff];W[hg]
;B[ei];W[jo];B[jn];W[io];B[jm];W[jl];B[il];W[in];B[ik];W[ko]
;B[dg];W[bi];B[be];W[eh];B[gg];W[bj];B[bk];W[gh];B[aj];W[hi]
;B[ih];W[ej];B[hj])
7.3
#/usr/bin/ruby

require 'file_reader.rb'

print "Please enter the path to the sgf file you would like to display\n"
filename = gets.to_s[0..-2]

infile = FileReader.new(filename)


board = Array.new(19)

for i in 0..18
  board[i]=["+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+"]
end

A = Hash.new
A["a"] = 0
A["b"] = 1
A["c"] = 2
A["d"] = 3
A["e"] = 4
A["f"] = 5
A["g"] = 6
A["h"] = 7
A["i"] = 8
A["j"] = 9
A["k"] = 10
A["l"] = 11
A["m"] = 12
A["n"] = 13
A["o"] = 14
A["p"] = 15
A["q"] = 16
A["r"] = 17
A["s"] = 18
A["t"] = 19
A["u"] = 20
A["v"] = 21
A["w"] = 22
A["x"] = 23
A["y"] = 24
A["z"] = 25
A["B"] = "x"
A["W"] = "o"


while(currentLine = infile.readLine)

  if(currentLine[0,3]==";B[")
    counter = 0
    while(counter < currentLine.length-3)
      board[A[currentLine[counter+3,1]]][A[currentLine[counter+4,1]]]=
		A[currentLine[counter+1,1]]
      counter += 6
    end
  end
end

for i in 0..18 
  currentRow = board[i]
  for j in 0..18
    print currentRow[j]
  end
  print "\n"
end
7.4
	
#/usr/bin/ruby

require 'file_writer.rb'

outfile = FileWriter.new("CA.txt")

cells = Array.new(32)

for i in 0..31
  cells[i]=Array.new
  for j in 0..66
    cells[i].unshift(0)
  end
end

cells[0][33]=1

for t in 1..31
  for i in 1..64
    if(cells[t-1][i]+cells[t-1][i-1]+cells[t-1][i+1]==1 ||
		 cells[t-1][i]+cells[t-1][i-1]+cells[t-1][i+1]==2)
      cells[t][i]=1
    end
  end
end

A = Hash.new

A[1]="X"
A[0]=" "

for t in 0..31
  line = ""
  for i in 0..65
       line = line + A[cells[t][i]]
  end
  outfile.writeLine(line)
end

outfile.close()
7.5
Example definition file: data.txt
woolgathering:Indulgence in idle daydreaming
sciolism:Superficial knowledge; a superficial show of learning
bouleversement:Complete overthrow; a reversal
supervene:To take place or occur as something additional, extraneous, or unexpected
redoubtable:formidable; also illustrious; eminent
paroxysm:An outburst; a fit


#/usr/bin/ruby
require 'file_reader.rb'
require 'file_writer.rb'

print "Please speciy the path of the file with the questions\n"

filename = gets.to_s[0..-2]

infile = FileReader.new(filename)

questions = Array.new

while(line=infile.readLine)
  questions.push(line.split(":"))
end

print "We will now begin the quiz. Enter Q to quit.\n"

input = ""

while(input!="Q")
  i = rand(questions.size-1)
  print questions[i][1]+"\n"
  input = gets.to_s[0..-2]
  print "\n"
  if(input==questions[i][0])
    print "You are correct!\n"
  else
    print "Sorry that is incorrect.\n"
  end
end

7.6
fileContents=""
selection = 1

while(selection!=3)
puts "would you like to \n
	1.View past oil changes.\n2.Add an entry for a new oil change\n3.quit"
selection=gets.to_i
fileContents=""
puts ""
if(selection == 1)


	if(File.exist?("mileage.txt"))
		readFile=File.open("mileage.txt","r")
		
		while( readline= readFile.gets)
			puts readline.to_s
		end
		readFile.close
		puts "\ndone\n\n"
	else
	puts "\nno previous entries\n\n"
	
	end


end

if(selection == 2)
	if(File.exist?("mileage.txt"))
		readFile=File.open("mileage.txt","r")
		
		while(readline=readFile.gets)
			fileContents = fileContents + readline.to_s
		end		
		readFile.close
	else
		
		fileContents="Car Mileage for last Oil Change\n
			      -------------------------------\n"
		
		
	end
	writeFile=File.open("mileage.txt", "w")
	puts("Enter car mileage for this oil change: ")
	readline=gets
	fileContents=fileContents + readline.to_s
	writeFile.puts(fileContents)
	writeFile.close
	puts"New mileage is now saved.\n\n"



	end
end



7.7
puts "enter the file to be read"
fileName=gets.to_s.chomp 

if(File.exist?(fileName))
	readFile=File.open(fileName,"r")	
	while( readline= readFile.gets)
		puts readline
	end

7.8
puts "enter the file to be written to."
fileName=gets.to_s.chomp
puts "enter text to be written to the file.  Enter a blank line when you are done."
	
writeFile=File.open(fileName,"w")
readline= gets.to_s
while( readline.chomp!="")
	writeFile.puts(readline)
	readline=gets.to_s
end
writeFile.close

7.9
puts "Enter the source file name:"
readFileName=gets.to_s.chomp
puts "enter the destination file name:"
writeFileName=gets.to_s.chomp

if(File.exist?(readFileName))
	readFile=File.open(readFileName ,"r")
	writeFile=File.open(writeFileName , "w")
	while( readline= readFile.gets)
		writeFile.puts(readline.to_s)
	end
	readFile.close
	puts "The file has been copied."
else
	puts "The file \"" + readFileName + "\" doesn't exist"
	
end
7.10
writeFile=File.open("outputFile.txt" , "w")
writeFile.puts("Bob said, \"I don't want\\need that\".")
writeFile.close
7.11
selection = 1
while(selection !=3)
	puts "would you like to \n1.Create a new file.\n2.Open an existing file\n3.quit"
	selection=gets.to_i
	

	if(selection==1)
		puts "Enter the file name"
		fileName=gets.to_s.chomp
		puts "Enter a password for this file"
		password=gets.to_s.chomp
		
		puts "enter text to be written to the file.  
			Enter a blank line when you are done."
		writeFile=File.open(fileName,"w")
		writeFile.puts(password)
		readline= gets.to_s.chomp

		while(readline!="")
			writeFile.puts(readline)
			readline=gets.to_s.chomp
		end
		writeFile.close
		puts "File successfully created."


	end
	if(selection==2)
		puts "Enter the file name"
		fileName=gets.to_s.chomp
		puts "Enter the password"
		password=gets.to_s

		if(File.exist?(fileName))
			readFile=File.open(fileName,"r")
			readline=readFile.gets
			if(readline==password)
				puts "Password accepted\n\n"
				while( readline= readFile.gets)
				puts readline.to_s
				end
				readFile.close
			else
				puts "Incorrect password"
			end
		else
			puts "File doesn't exist."
	
		end
	end
end

7.12
selection = 1

while(selection!=3)
puts "would you like to \n1.View address book.\n2.Add a new address\n3.quit"
selection=gets.to_i
fileContents=""
puts ""
if(selection == 1)


	if(File.exist?("addressBook.txt"))
		readFile=File.open("addressBook.txt","r")
		
		while( readline= readFile.gets)
			puts readline.to_s
		end
		readFile.close
		puts "\ndone\n\n"
	else
	puts "\nno entries\n\n"
	
	end


end

if(selection == 2)
	if(File.exist?("addressBook.txt"))
		readFile=File.open("addressBook.txt","r")
		
		while(readline=readFile.gets)
			fileContents = fileContents + readline.to_s
		end		
		readFile.close
	else
		
		fileContents="               Address Book\n
			------------------------------------------\n\n"
		
		
	end
	writeFile=File.open("addressBook.txt", "w")

	puts("Enter the name for this entry")
	readline=gets
	fileContents=fileContents + "          Name: " + readline.to_s 
	
	puts("Enter the street address for this entry")
	readline=gets
	fileContents=fileContents + "Street Address: " + readline.to_s 

	puts("Enter the City/State/Zip for this entry")
	readline=gets
	fileContents=fileContents + "City/State/Zip: " + readline.to_s

	puts("Enter the phone number for this entry")
	readline=gets
	fileContents=fileContents + "  Phone Number: " + readline.to_s + "\n\n"

	writeFile.puts(fileContents)
	writeFile.close
	puts"New entry is now saved.\n\n"

7.13
Sample input data:
 1 2
-1 3


#/usr/bin/ruby

require 'file_reader.rb'

print "Please specify which file contains the equations.\n"

filename = gets.to_s[0..-2]

infile = FileReader.new(filename)

A = Array.new
A[0] = infile.readLine.split
A[1] = infile.readLine.split

for i in 0..1
  for j in 0..1
    A[i][j] = A[i][j].to_i
  end
end

print "The determinant is " + (A[0][0]*A[1][1]-A[0][1]*A[1][0]).to_s + ".\n"



7.14
Sample solution data
 -3  5   1 -12  20
  1  6  -1 -16 -10
 10 -8 -15   0  14
 -2 -4  -2  14  12
-12  4  18  19  -8

#/usr/bin/ruby

def determinant(mat)
  if(mat.size == 2)
    return(mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0])
  else
    sum = 0
    for i in 0..(mat.size-1)
      sum += mat[0][i]*((-1)**(i))*determinant(minor(mat,i))
    end
    return(sum)
  end
end

def minor(mat,i)
  matnew = Array.new
  for j in 1..(mat.size-1)
    row = Array.new
    for k in 0..(mat.size-1)
      if(k!=i)
         row.push(mat[j][k]) 
      end
    end
  matnew.push(row)
 end
 return(matnew)
end



require 'file_reader.rb'

print "Please specify which file contains the equations.\n"

filename = gets.to_s[0..-2]

infile = FileReader.new(filename)

matrix = Array.new

while(line=infile.readLine)
  matrix.push(line.split)
end

for i in 0..(matrix.size-1)
  for j in 0..(matrix.size-1)
    matrix[i][j]=matrix[i][j].to_i
  end
end

print "The determinant is " + (determinant(matrix)).to_s + ".\n"

7.15
#/usr/bin/ruby

def determinant(mat)
  if(mat.size == 1)
    return(mat[0][0])
  end
  if(mat.size == 2)
    return(mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0])
  else
    sum = 0.0
    for i in 0..(mat.size-1)
      sum += mat[0][i]*((-1.0)**(i))*determinant(minor(mat,0,i))
    end
    return(sum)
  end
end

def minor(mat,l,i)
  matnew = Array.new
  for j in 0..(mat.size-1)
    row = Array.new
    for k in 0..(mat.size-1)
      if(k!=i)
         row.push(mat[j][k]) 
      end
    end
  if(j!=l)
    matnew.push(row)
  end
 end
 return(matnew)
end

def adjugate(mat)
  matnew = Array.new
  for i in 0..(mat.size-1)
    row = Array.new
    for j in 0..(mat.size-1)
      row.push(((-1)**(i+j))*determinant(minor(mat,i,j)))
    end
    matnew.push(row)
  end
  return(transpose(matnew))
end

def transpose(mat)
  matnew = Array.new()
  for i in 0..(mat.size-1)
    matnew.push(Array.new(mat.size-1))
  end
  for i in 0..(mat.size-1)
    for j in 0..(mat.size-1)
      matnew[i][j]=mat[j][i]
    end
  end
  return(matnew)
end
    


require 'file_reader.rb'

print "Please specify which file contains the equations.\n"

filename = gets.to_s[0..-2]

infile = FileReader.new(filename)

matrix = Array.new

while(line=infile.readLine)
  matrix.push(line.split)
end

for i in 0..(matrix.size-1)
  for j in 0..(matrix.size-1)
    matrix[i][j]=matrix[i][j].to_f
  end
end


adj = adjugate(matrix)
det = determinant(matrix)
inv = adj

for i in 0..(matrix.size-1)
  for j in 0..(matrix.size-1)
    inv[i][j]=(inv[i][j]/det)
  end
end

for i in 0..(matrix.size-1)
  for j in 0..(matrix.size-1)
    print inv[i][j].to_s + " "
  end
  print "\n"
end