Using each_with_index with map - ruby

I want to take a array and make it an order list. Currently I'm trying to do it in this way:
r = ["a", "b","c"]
r.each_with_index { |w, index| puts "#{index+1}. #{w}" }.map.to_a
# 1. a
# 2. b
# 3. c
#=> ["a", "b", "c"]
the output should be ["1. a", "2. b", "3. c"].
How do I get the proper output to be the new value for the r array?

a.to_enum.with_index(1).map { |element, index| "#{index}. #{element}" }
or
a.map.with_index(1) { |element, index| "#{index}. #{element}" }
with_index(1) makes the index of the first element 1.
In the first solution the array is converted to an enum, and in the second solution the array is directly mapped.

> => r.each_with_index.map { |w, index| "#{index+1}. #{w}" }
> => ["1. a", "2. b", "3. c"]

You need to map first, then puts:
r = %w[a b c]
r.map.with_index do |w, index|
"#{index + 1}. #{w}"
end.each do |str|
puts str
end
#=> ["1. a", "2. b", "3. c"]
# prints:
# 1. a
# 2. b
# 3. c
This is because each (and each_with_index) simply returns the original array.

Related

Getting a value from a string

I am trying to match one or more keywords within a string (str), but I have no luck. My method below is trying to match all keys when I only needed to match any (one or more).
#str = "g stands for girl and b is for boy"
def key1
%w[a, b, c, d, f, g]
end
def result
if #str.include?( key1 )
puts "happy days"
else
puts "bad days"
end
end
puts result # => bad days
How to make it show "happy days"?
PS: I have no idea what to name this title to. Maybe a mod could rename it?
You're asking if your string includes the Array: ["a", "b", "c", "d", "f", "g"]
What I think you're trying to ask is this: are there any elements in the array that also exist in the string? This is a good use case for Enumerable#any like this:
[2] pry(main)> #str = "g stands for girl and b is for boy"
=> "g stands for girl and b is for boy"
[3] key1 = %w[a b c d f g]
=> ["a", "b", "c", "d", "f", "g"]
[4] pry(main)> key1.any? { |letter| #str.include?(letter) }
=> true
So to refactor your code, it might look like this:
#str = "g stands for girl and b is for boy"
def key1
%w[a b c d f g]
end
def result
if key1.any? { |letter| #str.include?(letter) }
puts "happy days"
else
puts "bad days"
end
end
Something to note, with %w you don't need to use commas, you can simply separate the letters by a space (as outlined above).
I can't clearly understand your question, but I suspect you are looking for following:
key1.find { |k| #str.include? k }
I would use a regular expression:
MAGIC_CHARS = %w[a b c d f g]
#=> ["a", "b", "c", "d", "f", "g"]
def result(str)
(str =~ /#{MAGIC_CHARS.join('|')}/) ? "happy days" : "bad days"
end
result("g stands for girl and b is for boy")
#=> "happy days"
result("nothin' here")
#=> "bad days"
Note:
/#{MAGIC_CHARS.join('|')}/
#=> /a|b|c|d|f|g/
You could instead write:
Regexp.union(MAGIC_CHARS.map { |c| /c/ })
#=> /(?-mix:c)|(?-mix:c)|(?-mix:c)|(?-mix:c)|(?-mix:c)|(?-mix:c)/
or
/[#{MAGIC_CHARS.join('')}]/
#=> /[abcdfg]/
Another way:
def result(str)
(str.chars & MAGIC_CHARS).any? ? "happy days" : "bad days"
end
result("g stands for girl and b is for boy")
#=> "happy days"
result("nothin' here")
#=> "bad days"

Searching through two multidimensional arrays and grouping together similar subarrays

I am trying to search through two multidimensional arrays to find any elements in common in a given subarray and then put the results in a third array where the entire subarrays with similar elements are grouped together (not just the similar elements).
The data is imported from two CSVs:
require 'csv'
array = CSV.read('primary_csv.csv')
#=> [["account_num", "account_name", "primary_phone", "second_phone", "status],
#=> ["11111", "John Smith", "8675309", " ", "active"],
#=> ["11112", "Tina F.", "5551234", "5555678" , "disconnected"],
#=> ["11113", "Troy P.", "9874321", " ", "active"]]
# and so on...
second_array = CSV.read('customer_service.csv')
#=> [["date", "name", "agent", "call_length", "phone", "second_phone", "complaint"],
#=> ["3/1/15", "Mary ?", "Bob X", "5:00", "5551234", " ", "rude"],
#=> ["3/2/15", "Mrs. Smith", "Stew", "1:45", "9995678", "8675309" , "says shes not a customer"]]
# and so on...
If any number is present as an element in a subarray on both primary.csv and customer_service.csv, I want that entire subarray (as opposed to just the common elements), put into a third array, results_array. The desire output based upon the above sample is:
results_array = [["11111", "John Smith", "8675309", " ", "active"],
["3/2/15", "Mrs. Smith", "Stew", "1:45", "9995678", "8675309" , "says shes not a customer"]] # and so on...
I then want to export the array into a new CSV, where each subarray is its own row of the CSV. I intend to iterate over each subarray by joining it with a , to make it comma delimited and then put the results into a new CSV:
results_array.each do {|j| j.join(",")}
File.open("results.csv", "w") {|f| f.puts results_array}
#=> 11111,John Smith,8675309, ,active
#=> 3/2/15,Mrs. Smith,Stew,1:45,9995678,8675309,says shes not a customer
# and so on...
How can I achieve the desired output? I am aware that the final product will look messy because similar data (for example, phone number) will be in different columns. But I need to find a way to generally group the data together.
Suppose a1 and a2 are the two arrays (excluding header rows).
Code
def combine(a1, a2)
h2 = a2.each_with_index
.with_object(Hash.new { |h,k| h[k] = [] }) { |(arr,i),h|
arr.each { |e| es = e.strip; h[es] << i if number?(es) } }
a1.each_with_object([]) do |arr, b|
d = arr.each_with_object([]) do |str, d|
s = str.strip
d.concat(a2.values_at(*h2[s])) if number?(s) && h2.key?(s)
end
b << d.uniq.unshift(arr) if d.any?
end
end
def number?(str)
str =~ /^\d+$/
end
Example
Here is your example, modified somewhat:
a1 = [
["11111", "John Smith", "8675309", "", "active" ],
["11112", "Tina F.", "5551234", "5555678", "disconnected"],
["11113", "Troy P.", "9874321", "", "active" ]
]
a2 = [
["3/1/15", "Mary ?", "Bob X", "5:00", "5551234", "", "rude"],
["3/2/15", "Mrs. Smith", "Stew", "1:45", "9995678", "8675309", "surly"],
["3/7/15", "Cher", "Sonny", "7:45", "9874321", "8675309", "Hey Jude"]
]
combine(a1, a2)
#=> [[["11111", "John Smith", "8675309", "",
# "active"],
# ["3/2/15", "Mrs. Smith", "Stew", "1:45",
# "9995678", "8675309", "surly"],
# ["3/7/15", "Cher", "Sonny", "7:45",
# "9874321", "8675309", "Hey Jude"]
# ],
# [["11112", "Tina F.", "5551234", "5555678",
# "disconnected"],
# ["3/1/15", "Mary ?", "Bob X", "5:00",
# "5551234", "", "rude"]
# ],
# [["11113", "Troy P.", "9874321", "",
# "active"],
# ["3/7/15", "Cher", "Sonny", "7:45",
# "9874321", "8675309", "Hey Jude"]
# ]
# ]
Explanation
First, we define a helper:
def number?(str)
str =~ /^\d+$/
end
For example:
number?("8675309") #=> 0 ("truthy)
number?("3/1/15") #=> nil
Now index a2 on the values that represent numbers:
h2 = a2.each_with_index
.with_object(Hash.new { |h,k| h[k] = [] }) { |(arr,i),h|
arr.each { |e| es = e.strip; h[es] << i if number?(es) } }
#=> {"5551234"=>[0], "9995678"=>[1], "8675309"=>[1, 2], "9874321"=>[2]}
This says, for example, that the "numeric" field "8675309" is contained in elements at offsets 1 and 2 of a2 (i.e, for Mrs. Smith and Cher).
We can now simply run through the elements of a1 looking for matches.
The code:
arr.each_with_object([]) do |str, d|
s = str.strip
d.concat(a2.values_at(*h2[s])) if number?(s) && h2.key?(s)
end
steps through the elements of arr, assigning each to the block variable str. For example, if arr holds the first element of a1 str will in turn equals "11111", "John Smith", and so on. After s = str.strip, this says that if a s has a numerical representation and there is a matching key in h2, the (initially empty) array d is concatenated with the elements of a2 given by the value of h2[s].
After completing this loop we see if d contains any elements of a2:
b << d.uniq.unshift(arr) if d.any?
If it does, we remove duplicates, prepend the array with arr and save it to b.
Note that this allows one element of a2 to match multiple elements of a1.

Move elements of an array to a different array in Ruby

Simple ruby question. Lets say I have an array of 10 strings and I want to move elements at array[3] and array[5] into a totally new array. The new array would then only have the two elements I moved from the first array, AND the first array would then only have 8 elements since two of them have been moved out.
Use Array#slice! to remove the elements from the first array, and append them to the second array with Array#<<:
arr1 = ['Foo', 'Bar', 'Baz', 'Qux']
arr2 = []
arr2 << arr1.slice!(1)
arr2 << arr1.slice!(2)
puts arr1.inspect
puts arr2.inspect
Output:
["Foo", "Baz"]
["Bar", "Qux"]
Depending on your exact situation, you may find other methods on array to be even more useful, such as Enumerable#partition:
arr = ['Foo', 'Bar', 'Baz', 'Qux']
starts_with_b, does_not_start_with_b = arr.partition{|word| word[0] == 'B'}
puts starts_with_b.inspect
puts does_not_start_with_b.inspect
Output:
["Bar", "Baz"]
["Foo", "Qux"]
a = (0..9).map { |i| "el##{i}" }
x = [3, 5].sort_by { |i| -i }.map { |i| a.delete_at(i) }
puts x.inspect
# => ["el#5", "el#3"]
puts a.inspect
# => ["el#0", "el#1", "el#2", "el#4", "el#6", "el#7", "el#8", "el#9"]
As noted in comments, there is some magic to make indices stay in place. This can be avoided by first getting all the desired elements using a.values_at(*indices), then deleting them as above.
Code:
arr = ["null","one","two","three","four","five","six","seven","eight","nine"]
p "Array: #{arr}"
third_el = arr.delete_at(3)
fifth_el = arr.delete_at(4)
first_arr = arr
p "First array: #{first_arr}"
concat_el = third_el + "," + fifth_el
second_arr = concat_el.split(",")
p "Second array: #{second_arr}"
Output:
c:\temp>C:\case.rb
"Array: [\"null\", \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"s
even\", \"eight\", \"nine\"]"
"First array: [\"null\", \"one\", \"two\", \"four\", \"six\", \"seven\", \"eight
\", \"nine\"]"
"Second array: [\"three\", \"five\"]"
Why not start deleting from the highest index.
arr = ['Foo', 'Bar', 'Baz', 'Qux']
index_array = [2, 1]
new_ary = index_array.map { |index| arr.delete_at(index) }
new_ary # => ["Baz", "Bar"]
arr # => ["Foo", "Qux"]
Here's one way:
vals = arr.values_at *pulls
arr = arr.values_at *([*(0...arr.size)] - pulls)
Try it.
arr = %w[Now is the time for all Rubyists to code]
pulls = [3,5]
vals = arr.values_at *pulls
#=> ["time", "all"]
arr = arr.values_at *([*(0...arr.size)] - pulls)
#=> ["Now", "is", "the", "for", "Rubyists", "to", "code"]
arr = %w[Now is the time for all Rubyists to code]
pulls = [5,3]
vals = arr.values_at *pulls
#=> ["all", "time"]
arr = arr.values_at *([*(0...arr.size)] - pulls)
#=> ["Now", "is", "the", "for", "Rubyists", "to", "code"]

How do I create a histogram by iterating over an array in Ruby

So I was told to rewrite this question and outline my goal. They asked me to iterate over the array and "Use .each to iterate over frequencies and print each word and its frequency to the console... put a single space between the word and its frequency for readability."
puts "Type something profound please"
text = gets.chomp
words = text.split
frequencies = Hash.new 0
frequencies = frequencies.sort_by {|x,y| y}
words.each {|word| frequencies[word] += 1}
frequencies = frequencies.sort_by{|x,y| y}.reverse
puts word +" " + frequencies.to_s
frequencies.each do |word, frequencies|
end
Why can't it convert the string into an integer? What am I doing incorrectly?
Try this code:
puts "Type something profound please"
words = gets.chomp.split #No need for the test variable
frequencies = Hash.new 0
words.each {|word| frequencies[word] += 1}
words.uniq.each {|word| puts "#{word} #{frequencies[word]}"}
#Iterate over the words, and print each one with it's frequency.
I'd do as below :
puts "Type something profound please"
text = gets.chomp.split
I called here Enumerable#each_with_object method.
hash = text.each_with_object(Hash.new(0)) do |word,freq_hsh|
freq_hsh[word] += 1
end
I called below Hash#each method.
hash.each do |word,freq|
puts "#{word} has a freuency count #{freq}"
end
Now run the code :
(arup~>Ruby)$ ruby so.rb
Type something profound please
foo bar foo biz bar baz
foo has a freuency count 2
bar has a freuency count 2
biz has a freuency count 1
baz has a freuency count 1
(arup~>Ruby)$
chunk is a good method for this. It returns an array of 2-element arrays. The first of each is the return value of the block, the second is the array of original elements for which the block returned that value:
words = File.open("/usr/share/dict/words", "r:iso-8859-1").readlines
p words.chunk{|w| w[0].downcase}.map{|c, words| [c, words.size]}
=> [["a", 17096], ["b", 11070], ["c", 19901], ["d", 10896], ["e", 8736], ["f", 6860], ["g", 6861], ["h", 9027], ["i", 8799], ["j", 1642], ["k", 2281], ["l", 6284], ["m", 12616], ["n", 6780], ["o", 7849], ["p", 24461], ["q", 1152], ["r", 9671], ["s", 25162], ["t", 12966], ["u", 16387], ["v", 3440], ["w", 3944], ["x", 385], ["y", 671], ["z", 949]]

Why the functional differences in Hash initialization?

Is there documentation on the differences of initialization? The docs on Hash didn't have anything that would explain the difference.
foo = [1,2,3,4]
test1 = Hash.new([])
test2 = Hash.new{|h,k| h[k] = []}
foo.each do |i|
test1[i] << i
test2[i] << i
end
puts "test 1: #{test1.size}" #0
puts "test 2: #{test2.size}" #4
There is mentioning in the doc. Read the doc:
new(obj) → new_hash
new {|hash, key| block } → new_hash
[...] If obj is specified, this single object will be used for all default values. If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block’s responsibility to store the value in the hash if required.
h = Hash.new("Go Fish")
h["a"] = 100
h["b"] = 200
h["a"] #=> 100
h["c"] #=> "Go Fish"
# The following alters the single default object
h["c"].upcase! #=> "GO FISH"
h["d"] #=> "GO FISH"
h.keys #=> ["a", "b"]
# While this creates a new default object each time
h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
h["c"] #=> "Go Fish: c"
h["c"].upcase! #=> "GO FISH: C"
h["d"] #=> "Go Fish: d"
h.keys #=> ["c", "d"]
This is a common gotcha. With test1 (the non-block) you are modifying the default object, the thing which you get when the key does not exist in the hash.
foo = [1,2,3,4]
test1 = Hash.new([])
test2 = Hash.new{|h,k| h[k] = []}
foo.each do |i|
test1[i] << i
test2[i] << i
p test1['doesnotexist'] #added line
end
puts "test 1: #{test1.size}" #0
puts "test 2: #{test2.size}" #4
Output:
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
test 1: 0
test 2: 4
There's a difference, in some situation it could be significant
test1 = Hash.new([])
test2 = Hash.new{|h,k| h[k] = []}
test1['foo'] #=> []
test2['foo'] #=> []
test1.keys == test2.keys #=> false
The first construction just returns the default value but doesn't do anything with current hash but second construction initialize the hash with key/value where value is calculated by given block.

Resources