I have the following array:
array = [["Group EX (Instructor)", 0.018867924528301886], ["Personal Reasons", 0.018867924528301886]]
and I need to split this array up, dynamically, into two arrays:
text_array = ["Group EX (Instructor)", "Personal Reasons"]
number_array = [0.018867924528301886,0.018867924528301886]
I'm currently doing this, which can't be the right way:
array.each do |array|
text_array << array[0]
number_array << array[1]
end
Simply use #transpose.
array = [["Group EX (Instructor)", 0.018867924528301886], ["Personal Reasons", 0.018867924528301886]]
a1, a2 = array.transpose
#=> [["Group EX (Instructor)", "Personal Reasons"],
[0.018867924528301886, 0.018867924528301886]]
Repairing your existing code,
text_array = array.map { |x| x[0] } #give back first element of each subarray
number_array = array.map { |x| x[1] } #give back second element of each subarray
I would do as below :
array = [["Group EX (Instructor)", 0.018867924528301886], ["Personal Reasons", 0.018867924528301886]]
text_array,number_array = array.flatten.partition{|e| e.is_a? String }
text_array # => ["Group EX (Instructor)", "Personal Reasons"]
number_array # => [0.018867924528301886, 0.018867924528301886]
This too works:
text_array, number_array = array.first.zip(array.last)
but transpose clearly is what you want.
Related
String:
string = "this;is;a;string;yes"
I can split the string and append each element to an array like this
arr = []
string.split(";").each do |x|
arr << x
end
Is there an easy way to take the first third and fourth values other than something like this.
x = 0
string.split(";").each do |x|
if x == 0 or x == 2 or x == 3 then arr << x end
x += 1
end
Sure. Use Array#values_at:
string = "this;is;a;string;yes"
string.split(";").values_at(0, 2, 3)
# => ["this", "a", "string"]
See it on repl.it: https://repl.it/#jrunning/FussyRecursiveSpools
I am trying to transform a given string into a hash with each its character = key and index = value.
For example, if I have str = "hello", I would like it to transform into {"h"=>0, "e"=>1, "l"=>2, "l"=>3, "o"=>4}.
I created a method as such:
def map_indices(arr)
arr.map.with_index {|el, index| [el, index]}.to_h
end
#=> map_indices('hello'.split(''))
#=> {"h"=>0, "e"=>1, "l"=>3, "o"=>4}
The problem is it skips the first l. If I reverse the order of el and index: arr.map.with_index {|el, index| [index, el]}.to_h, I get all the letters spelled out: {0=>"h", 1=>"e", 2=>"l", 3=>"l", 4=>"o"}
But when I invert it, I get the same hash that skips one of the l's.
map_indices('hello'.split('')).invert
#=> {"h"=>0, "e"=>1, "l"=>3, "o"=>4}
Why is this behaving like such? How can I get it to print {"h"=>0, "e"=>1, "l"=>2, "l"=>3, "o"=>4}?
It can be done, but will confuse other Ruby programmers.A normal hash treats a key "a" as identical to another "a". Unless a little known feature .compare_by_identity is used:
h = {}.compare_by_identity
"hello".chars.each_with_index{|c,i| h[c] = i}
p h # => {"h"=>0, "e"=>1, "l"=>2, "l"=>3, "o"=>4}
Any of the following could be used. For
str = "hello"
all return
{"h"=>[0], "e"=>[1], "l"=>[2, 3], "o"=>[4]}
str.each_char
.with_index
.with_object({}) { |(c,i),h| (h[c] ||= []) << i }
See String#each_char, Enumerator#with_index and Enumerator#with_object. The block variables have been written to exploit array decomposition.
str.each_char
.with_index
.with_object(Hash.new { |h,k| h[k] = [] }) { |(c,i),h| h[c] << i }
See the form of Hash::new that takes a block and no argument. If a hash has been defined
h = Hash.new { |h,k| h[k] = [] }
and later
h[c] << i
is executed, h[c] is first set equal to an empty array if h does not have a key c.
str.size
.times
.with_object(Hash.new { |h,k| h[k] = [] }) { |i,h| h[str[i]] << i }
str.each_char
.with_index
.group_by(&:first)
.transform_values { |a| a.flat_map(&:last) }
See Enumerable#group_by, Hash#transform_values (introduced in Ruby v2.5) and Enumerable#flat_map.
Note that
str.each_char
.with_index
.group_by(&:first)
#=> {"h"=>[["h", 0]], "e"=>[["e", 1]], "l"=>[["l", 2], ["l", 3]],
# "o"=>[["o", 4]]}
Another option you can use is zipping two enumerations together.
s = "hello"
s.chars.zip(0..s.size)
This yields: [["h", 0], ["e", 1], ["l", 2], ["l", 3], ["o", 4]]
I am new to Ruby and I am sure this can be refactored, but another alternative might be:
arr1 = "Hello".split(%r{\s*})
arr2 = []
for i in 0..arr1.size - 1
arr2 << i
end
o = arr1.zip(arr2)
a_h = []
o.each do |i|
a_h << Hash[*i]
end
p a_h.each_with_object({}) { |k, v| k.each { |kk,vv| (v[kk] ||= []) << vv } }
=> {"H"=>[0], "e"=>[1], "l"=>[2, 3], "o"=>[4]}
I want to change the value of an array via a hash, for example:
arr = ['g','g','e','z']
positions = {1 => arr[0], 2 => arr[1]}
positions[1] = "ee"
Problem is that the one that changed is hash and not array. When I do p arr It still outputs ['g','g','e','z']. Is there a way around this?
You're going to need to add another line of code to do what you want:
arr = ['g','g','e','z']
positions = {1 => arr[0], 2 => arr[1]}
positions[1] = "ee"
arr[0] = positions[1]
Another option would be to make a method that automatically updated the array for you, something like this:
def update_hash_and_array(hash, array, val, index)
# Assume that index is not zero indexed like you have
hash[index] = val
array[index - 1] = val
end
update_hash_and_array(positions, arr, "ee", 1) # Does what you want
This is possible to code into your hash using procs.
arr = ['g','g','e','z']
positions = {1 => -> (val) { arr[0] = val } }
positions[1].('hello')
# arr => ['hello', 'g', 'e', 'z']
You can generalize this a bit if you want to generate a hash that can modify any array.
def remap_arr(arr, idx)
(idx...arr.length+idx).zip(arr.map.with_index{|_,i| -> (val) {arr[i] = val}}).to_h
end
arr = [1,2,3,4,5,6]
positions = remap_arr(arr, 1)
positions[2].('hello')
# arr => [1,'hello',3,4,5,6]
positions[6].('goodbye')
# arr => [1,'hello',3,4,5,'goodbye']
But I'm hoping this is just a thought experiment, there is no reason to change the way array indexing behavior works to start from 1 rather than 0. In such cases, you would normally just want to offset the index you have to match the proper array indexing (starting at zero). If that is not sufficient, it's a sign you need a different data structure.
#!/usr/bin/env ruby
a = %w(q w e)
h = {
1 => a[0]
}
puts a[0].object_id # 70114787518660
puts h[1].object_id # 70114787518660
puts a[0] === h[1] # true
# It is a NEW object of a string. Look at their object_ids.
# That why you can not change value in an array via a hash.
h[1] = 'Z'
puts a[0].object_id # 70114787518660
puts h[1].object_id # 70114574058580
puts a[0] === h[1] # false
h[2] = a
puts a.object_id # 70308472111520
puts h[2].object_id # 70308472111520
puts h[2] === a # true
puts a[0] === h[2][0] # true
# Here we can change value in the array via the hash.
# Why?
# Because 'h[2]' and 'a' are associated with the same object '%w(q w e)'.
# We will change the VALUE without creating a new object.
h[2][0] = 'X'
puts a[0] # X
puts h[2][0] # X
puts a[0] === h[2][0] # true
I'm having a lot of issues writing a Quicksort algorithm in Ruby. I'm coming from C++/ Java, so my code could be completely wrong:
def quicksort(*list)
if list.empty?
return list
end
$pivot = list.sample
list.delete_at(list.index($pivot))
current_element = list[0]
$smaller = Array.new
$larger = Array.new
list.each do |x|
if (list[x] <= $pivot)
$smaller << list[x]
else
$larger << list[x]
end
end
$sorted = Array.new
$sorted << self.quicksort(*$smaller)
$sorted << pivot
$sorted << self.quicksort(*$larger)
$sorted.flatten!
return *$sorted
end
myArray = [5, 4, 3, 2, 1]
sorted = Array.new(quicksort(myArray))
myArray.each do |x|
print x
end
print "\n"
This is the error that I'm getting:
My error
Can you not set an array equal to a function that returns an array, like you could in C++ or Java?
Editing your code you must have a class to define methods in IRB. In addition I fixed the bug with missing dollar before pivot.
class SortingAlgorithms
def self.quicksort(*list)
if list.empty?
return list
end
$pivot = list.sample
list.delete_at(list.index($pivot))
current_element = list[0]
$smaller = Array.new
$larger = Array.new
list.each do |x|
if (list[x] <= $pivot)
$smaller << list[x]
else
$larger << list[x]
end
end
$sorted = Array.new
$sorted << self.quicksort(*$smaller)
$sorted << $pivot
$sorted << self.quicksort(*$larger)
$sorted.flatten!
return *$sorted
end
end
myArray = [5, 4, 3, 2, 1]
sorted = Array.new(SortingAlgorithms.quicksort(myArray))
myArray.each do |x|
print x
end
print "\n"
But, beautiful ruby way provided in another thread.
I have an array:
[1,2,3,4,5,6,7,8,9,10]
and I need to count three separate things.
1) All values less than or equal to 6.
2) All values equal to 7 or 8.
3) All values greater than 8.
What is the best way to do this without counting individual values and adding them all together?
Use Enumerable#count with a block.
[1,2,3,4,5,6,7,8,9,10].count { |val| val <= 6 }
[1,2,3,4,5,6,7,8,9,10].count { |val| val == 7 || val == 8 }
[1,2,3,4,5,6,7,8,9,10].count { |val| val > 8 }
arr = [1,2,3,4,5,6,7,8,9,10]
arr.select {|e| e <= 6}.size
#=> 6
arr.select {|e| e == 7 || e == 8}.size
#=> 2
arr.select {|e| e > 8}.size
#=> 2
How about this:
>> hash = Hash.new(0)
>> arr = [1,2,3,4,5,6,7,8,9,10]
>> arr.each { |e| hash[(e-7.5).truncate<=>0]+=1 }
>> hash
=> {-1=>6, 0=>2, 1=>2}
Or even more succinct:
>> arr = [1,2,3,4,5,6,7,8,9,10]
>> arr.each_with_object(Hash.new(0)) { |e,h| h[(e-7.5).truncate<=>0]+=1 }
=> {-1=>6, 0=>2, 1=>2}
More interesting, imo, is to answer #Luigi's three questions with a single statement (containing no semicolons). Here are two ways to do it:
a = [1,2,3,4,5,6,7,8,9,10]
a.inject([0,0,0]) {|arr,i| [arr[0]+(i<7 ? 1:0), arr[1]+((i>6 and i<9) ? 1:0), arr[2]+(i>8 ? 1:0)]}
a.slice_before(7).to_a.map {|arr| arr.slice_before(9).to_a}.flatten(1).map(&:size)
# => [6, 2, 2]
Edit: another, inspired by #staafl's answer:
arr.group_by {|e| (e+1)/2-4<=>0}.values.map(&:size)
Can you suggest others?