Skip to content

Instantly share code, notes, and snippets.

@shamritskiy3468
Last active December 22, 2018 20:35
Show Gist options
  • Save shamritskiy3468/b03c5c0d6d6114364652e681a439330b to your computer and use it in GitHub Desktop.
Save shamritskiy3468/b03c5c0d6d6114364652e681a439330b to your computer and use it in GitHub Desktop.
Task5
def generate_array(size)
array = []
size.to_i.times { array << rand(-10..20) }
array
end
#have some questions accroding to this task (probably, it can be done with 'each'?!)
def insert_zeros_to_array(array)
i = 0
while(i < array.size) do
if array[i] > 0
array.insert(i, 0)
i += 2
end
i += 1
end
array
end
puts "Enter Array size: "
size = gets.chomp
start_array = generate_array(size)
puts "Start Array = #{start_array.join(" ~ ")} "
result = insert_zeros_to_array(start_array)
puts "Result Array = #{result.join(" ~ ")} "
@aya-soft
Copy link

Очень длинно, а как короче это записать? Чтобы массив внутри итератора варился :)

def generate_array(size)
  array = []
  size.to_i.times { array << rand(-10..20) }
  array
end

@aya-soft
Copy link

aya-soft commented Dec 22, 2018

Тут мы из одного массива мы пытаемся сделать другой. А если метод flatten использовать, какое решение получится?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment