Demo entry 5923209

кк

   

Submitted by Oleg on Sep 03, 2016 at 16:42
Language: Ruby. Code size: 1.7 kB.

#encoding: cp866
def readFiles path
l = -1
data = []
	File.open(path, 'r') do |file|
		file.readlines.each do |line|
			line = line.chomp.split(',')
			l += 1	
			data[l] = []
				line.each do |char|
					data[l] << char.to_f	
				end
		end
 	data
	end
end

def   math  mass
	sred = []
	(0...@z.size).each do |l|
		p = Array.new(mass.size){0} 
		sqrt = 0
		(0...mass.size).each do |i|
	         
			(0...mass[0].size).each do |j|
			p[i] += ((mass[i][j] - @z[l][j]).to_f/(@maxx[j] - @minn[j]).to_f)**2
			
			end
		
			sqrt += Math.sqrt(p[i])
		
		end
        sred[l] = sqrt/mass.size
	end
	sred
end
#============Считывание и запись объектов из базы данных в массив =============================================== 
x = readFiles 'rud.txt'
y = readFiles 'NoRud.txt'
@z = readFiles 'investigate.txt'

(0...x.size).each do |i|
   (0...x[0].size).each do |j|
		print " #{x[i][j]} "
	end
	
	print "\n"
end

#============Нахождение максимальных и минимальных свойств===============================================
  @maxx = []
  @minn = []
 (0...x[0].size).each do |i| 
	box = []
	(0...x.size).each do |j|
		box << x[j][i] 
	        
	end
	(0...y.size).each do |j|                         
		box << y[j][i]
	end
	
	@maxx[i] = box.max
	@minn[i] = box.min
	
end
print @maxx, @minn, "\n"

#============Определение к какому из облаков исследуемый объект ближе===============================================
sredx = math x
sredy = math y
@z.size.times do |i|
	puts "Объект номер #{i} ближе к рудным" if sredy[i] > sredx[i]
	puts "Объект номер #{i} ближе к безрудным" if sredy[i] < sredx[i]
end

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).