Last active
December 16, 2018 21:04
-
-
Save darya-makarenko/7f1c56149a9b41b8878364efb4086d71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Enter the elements of array: (f.e. 1 2 3 4)" | |
arr = gets.chomp.split.map(&:to_i) | |
#inserting the special value | |
#before every positive element | |
arr.map!{ |el| el > 0 ? [0, el] : el } | |
arr.flatten! | |
puts "The result array: #{arr.join(" ")}" |
aya-soft
commented
Nov 28, 2018
- в большинстве случаев лучше создавать новый массив, а не изменять старый (главное следить, не нужен ли старый вариант потом)
- используй тернарный оператор, Лея :)
- inserted_elem можно просто захардкодить в 0
- Скажи честно до flatten сама додумалась?
Сама, честно. Способ придумался сразу, оставалось найти средство. Для этого пришлось только прочитать документацию по методам массивов:)
🥇 🥇
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment