Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 插入到嵌套数组中_Arrays_Ruby - Fatal编程技术网

Arrays 插入到嵌套数组中

Arrays 插入到嵌套数组中,arrays,ruby,Arrays,Ruby,我在本表格的文件中描述了汽车: CAR Audi 1x1 XXX XXXXX _X _X 我想将此文件中的汽车插入到一个10 x 10的嵌套数组中。 第二条线是从左上角点开始的坐标R x C,用于汽车起动。第三到第五行显示汽车的形状 OUTPUT= [["O","O","O","O","O","O","O","O","O","O"], ["O","X","X","X","O","O","O","O","O","O"], ["O","X","X","X","X","X","O",

我在本表格的文件中描述了汽车:

CAR
Audi
1x1
XXX
XXXXX
_X
_X
我想将此文件中的汽车插入到一个10 x 10的嵌套数组中。 第二条线是从左上角点开始的坐标R x C,用于汽车起动。第三到第五行显示汽车的形状

OUTPUT= [["O","O","O","O","O","O","O","O","O","O"],
    ["O","X","X","X","O","O","O","O","O","O"],
    ["O","X","X","X","X","X","O","O","O","O"],
    ["O","O","X","O","O","O","O","O","O","O"],
    ["O","O","X","O","O","O","O","O","O","O"]
    ["O","O","O","O","O","O","O","O","O","O"]
    ["O","O","O","O","O","O","O","O","O","O"]
    ["O","O","O","O","O","O","O","O","O","O"]
    ["O","O","O","O","O","O","O","O","O","O"]
    ["O","O","O","O","O","O","O","O","O","O"]

INPUT arr =[]

这应该行得通,相当棘手。当然有更好的办法

首先将文件映射到散列中


这是一个文件夹还是两个文件的内容?这只是一个csv文件,对不起,我不明白。如果是csv,逗号在哪里?预期产量是多少?像[奥迪,2,2],[宝马,5,1]?我不能理解Xs,对于奥迪来说是4条线,对于宝马来说是3条线。可以用更多的细节来编辑这篇文章吗?对不起,我把它和另一个作业弄错了。我已经修改了描述并添加了img。你能以Ruby的形式给出输入和输出吗?
car_array = []
File.open('car_array.txt').each_with_index do |line, idx|
    car_array << line.chomp
end
p car_array
# => ["CAR", "Audi", "1x1", "XXX", "XXXXX", "_X", "_X", "CAR", "BMW", "5x1", "__X", "XXX", "X", "XX"]

car_hash = {}
key = nil
car_array.each_with_index do |e, index|
  key = car_array[index-1].downcase.to_sym if car_array[index-2] == "CAR"
  car_hash[key] = [e] if car_array[index-2] == "CAR"
  car_hash[key] << e if car_hash[key] unless (car_array[index-2] == "CAR" || car_array[index-1] == "CAR" || car_array[index] == "CAR")
end

p car_hash
# => {:audi=>["1x1", "XXX", "XXXXX", "_X", "_X"], :bmw=>["5x1", "__X", "XXX", "X", "XX"]}

car_hash.transform_values do |array|
  h = {start: [], points: []}
  h[:start] = array[0].split('x').map(&:to_i).map!{ |e| e}
  (array.size - 1).times do |n|
    h[:points] << array[n+1]
  end
  array[0] = h
  (array.size - 1).times do |n|
    array.pop
  end
end

car_hash.transform_values! {|v| v[0]}

p car_hash
# => {:audi=>{:start=>[1, 1], :points=>["XXX", "XXXXX", "_X", "_X"]}, :bmw=>{:start=>[5, 1], :points=>["__X", "XXX", "X", "XX"]}}

car_hash.each_value do |car|
  car[:points].map! do |point|
    point.scan(/./)
  end
  row = car[:start][0] - 1
  col = car[:start][1] - 1
  car[:points].map! do |points|
  delta_col = 0
  row += 1
    points.map! do |point|
      delta_col +=1
      point == 'X' ? [row, col + delta_col] : nil
    end
    points.compact
  end
  car[:points] = car[:points].flatten(1)
end


p car_hash
# => {:audi=>{:start=>[1, 1], :points=>[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [3, 2], [4, 2]]}, :bmw=>{:start=>[5, 1], :points=>[[5, 3], [6, 1], [6, 2], [6, 3], [7, 1], [8, 1], [8, 2]]}}
table_model = Array.new(10) {Array.new(10,'O')}

def print_table (table)
  table.each {|row| p row}
end

audi_table = table_model.dup

car_hash[:audi][:points].each { |point| audi_table[point[0]][point[1]] = 'x' }

print_table audi_table

# => ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]
# => ["O", "x", "x", "x", "O", "O", "O", "O", "O", "O"]
# => ["O", "x", "x", "x", "x", "x", "O", "O", "O", "O"]
# => ["O", "O", "x", "O", "O", "O", "O", "O", "O", "O"]
# => ["O", "O", "x", "O", "O", "O", "O", "O", "O", "O"]
# => ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]
# => ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]
# => ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]
# => ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]
# => ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]