Transform Array ["1", "3", "2"] to [132] [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like this:
["1", "3", "2"]["2", "3", "1"]["3", "1", "2"]...
And want to transform it to an array that looks like this:
[132][231]..
What can i do? Thanks!

Using Enumerable#map, Array#join, and String#to_i:
a = ["1", "3", "2"],["2", "3", "1"],["3", "1", "2"]
a.map { |x| x.join.to_i } # => [132, 231, 312]
a.map { |x| [x.join.to_i] } # => [[132], [231], [312]]

a = [["1", "3", "2"],["2", "3", "1"],["3", "1", "2"]]
a.map{|e| [e.join.to_i]}
# => [[132], [231], [312]]

Ensure yourself that your variable has correct format and then as said
a = [["1", "3", "2"],["2", "3", "1"],["3", "1", "2"]]
a.map do |x|
x.join.to_i
end

Related

Divide a string into binary indexes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
a = '123712638126378123681273'
i have
=> a = ['12','37', ...... ]
I tried to use split but I do not know how to order it
help me
I would do:
a.scan(/\d{2}/)
#=> ["12", "37", "12", "63", "81", "26", "37", "81", "23", "68", "12", "73"]
Input
a = '123712638126378123681273'
Code
p a.gsub(/.{2}/).to_a
Or
p a.chars.each_slice(2).map(&:join)
Output
["12", "37", "12", "63", "81", "26", "37", "81", "23", "68", "12", "73"]

Why does it keep saying that the function Split does not exist? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
x = ["1", "2", "3", "4"]
#=> ["1", "2", "3", "4"]
x.split(", ")
#=> NoMethodError: undefined method `split' for ["1", "2", "3", "4"]:Array
The String#split method in ruby is used to divide a string into substrings
'a,b,c,d'.split(',') # => ["a", "b", "c", "d"]
You are trying to invoke Array#split (aka on an array object). As such array method doesn't exists, you get:
error undefined method split for ["1", "2", "3", "4"]:Array`
Arrays can't be split. You are probably thinking of splitting a string?
You will reach the desired value by simply x[0] for the 0 index, first object in the array.
This to any help?

Range of numbers to array of number strings? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I currently want to get the numbers from 1 to 23 as a string.
The method I arrived at feels slightly hard to read:
CHROMOSOME_NUMBERS = (1..23).to_a.map { |n| n.to_s }
Is there a prettier way of doing it?
There is map method defined for Range (because Range includes Enumerable module), so you don't have to convert it manually to array:
CHROMOSOME_NUMBERS = (1..23).map(&:to_s)
('1'..'23').to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"]

How to combination/permutation in ruby?

I've this familiar question that looks like permutation/combination of the Math world.
How can I achieve the following via ruby?
badges = "1-2-3"
badge_cascade = []
badges.split("-").each do |b|
badge_cascade << b
end
Gives: => ["1", "2", "3"]
But I want it to be is:
=> ["1", "2", "3",
"1-2", "2-3", "3-1", "2-1", "3-2", "1-3",
"1-2-3", "2-3-1", "3-1-2"]
Functional approach:
bs = "1-2-3".split("-")
strings = 1.upto(bs.size).flat_map do |n|
bs.permutation(n).map { |vs| vs.join("-") }
end
#=> ["1", "2", "3", "1-2", "1-3", "2-1", "2-3", "3-1", "3-2", "1-2-3", "1-3-2", "2-1-3", "2-3-1", "3-1-2", "3-2-1"]
You ned to use Array#permutation method in order to get all permutations:
arr = "1-2-3".split '-' # => ["1", "2", "3"]
res = (1..arr.length).reduce([]) { |res, length|
res += arr.permutation(length).to_a
}.map {|arr| arr.join('-')}
puts res.inspect
# => ["1", "2", "3", "1-2", "1-3", "2-1", "2-3", "3-1", "3-2", "1-2-3", "1-3-2", "2-1-3", "2-3-1", "3-1-2", "3-2-1"]
Let me explain the code:
You split string into array passing separator '-' to String#split method
You need all permutations of length 1, 2, 3. Range 1..arr.length represents all these lengths.
You collect an array of all permutations using Enumerable#reduce.
You will get array of arrays here:
[["1"], ["2"], ["3"], ["1", "2"], ["1", "3"], ["2", "1"], ["2", "3"], ["3", "1"], ["3", "2"], ["1", "2", "3"], ["1", "3", "2"], ["2", "1", "3"], ["2", "3", "1"], ["3", "1", "2"], ["3", "2", "1"]]
You transform all subarrays of this array into strings using Array#join with your '-' separator inside of Enumerable#map
Array#permutation(n) will give you all the permutations of length n as an Array of Arrays so you can call this with each length between 1 and the number of digits in badges. The final step is to map these all back into strings delimited with -.
badges = "1-2-3"
badges_split = badges.split('-')
permutations = []
(1..badges_split.size).each do |n|
permutations += badges_split.permutation(n).to_a
end
result = permutations.map { |permutation| permutation.join('-') }
Update: I think Alex's use of reduce is a more elegant approach but I'll leave this answer here for now in case it is useful.

How do I quickly reorder a Ruby Array given an order?

I have an array of values, and an array which determines the order.
How can I quickly re-arrange the array in the given order?
data = ['0','1','2','3','4','5']
order = [3,1,2,0,4,5]
I want:
data = ['3','1','2','0','4','5']
You can use the values_at method written for this kind of task:
data = ['0','1','2','3','4','5']
order = [3,1,2,0,4,5]
data.values_at *order
# => ["3", "1", "2", "0", "4", "5"]
data = ["0", "1", "2", "3", "4", "5"]
order = [3, 1, 2, 0, 4, 5]
> order.map{|x| data[x]}
=> ["3", "1", "2", "0", "4", "5"]
If you are not sure if the indices are correct, you can do this:
> order.map{|x| data.fetch(x)} # will raise an exception if index out of bounds
=> ["3", "1", "2", "0", "4", "5"]
Not as good as #Jakub's answer using Array#values_at (which I would argue should be the accepted answer) but here are some other fun alternatives:
p data.sort_by.with_index{ |d,i| order[i] }
p data.zip(order).sort_by(&:last).map(&:first)

Resources