Quick actions

cmd+k|ctrl+k

Navigation

Languages

lab1212

Snippet info

Language

Crystal

Visibility

public

Author

nekokat89

Created

2017-12-14T09:15:10Z

Updated

2017-12-14T09:19:01Z

def random_array(size)
  (1..size).map{|i| rand(-9.0..9.0).round(5)}
end

def to_array(file_name)
  File.read(file_name).scan(/-?\d+[.,]\d+/).map{|i| i[0].to_f}
end

class Array
  def fstnum_half
    self.select{|i| i > 0}[0].fdiv(2)
  end

  def mod_array1(size)
    self.first(size).map{|i| (i + fstnum_half).round(5)}
  end

  def mod_array2(min, max)
    self[min..max].map{|i| (i + fstnum_half).round(5)}
  end
end

array1,array2 = to_array("array.txt"), random_array(20)
modarr1, modarr2 = array1.mod_array1(6),array2.mod_array2(5, 12)
File.write("modarray1.cr", modarr1)
File.write("modarray2.cr", modarr2)
puts "#{array1}\n\n#{array2}\n\n#{modarr1}\n\n#{modarr2}"
INFO