How to insert elements into an array - ruby

I have an array consisting of unknown elements:
myary = [100, "hello", 20, 40, "hi"]
I want to put integer 10 after each element to make it into this:
myary = [100, 10, "hello", 10, 20, 10, 40, 10, "hi", 10]
Is there a way or a method to do it?
Another problem is that I need to add integer 10 before a string "hello".
myary = [100, 10,"hello", 20, 40, "hi"]

Is this what you want ?
myary = [100, "hello", 20, 40, "hi"]
myary.flat_map { |i| [i, 10] }
# => [100, 10, "hello", 10, 20, 10, 40, 10, "hi", 10]
myary.flat_map { |i| i == 'hello' ? [10, i] : i }
# => [100, 10,"hello", 20, 40, "hi"]
Read #flat_map method.

Related

Ruby 100 doors returning 100 nil

I'm solving the '100 doors' problem from Rosetta Code in Ruby. Briefly,
there are 100 doors, all closed, designated 1 to 100
100 passes are made, designated 1 to 100
on the ith pass, every ith door is "toggled": opened if it's closed, closed if it's open
determine the state of each door after 100 passes have been completed.
Therefore, on the first pass all doors are opened. On the second pass even numbered doors are closed. On the third pass doors i for which i%3 == 0 are toggled, and so on.
Here is my attempt at solving the problem.
visit_number = 0
door_array = []
door_array = 100.times.map {"closed"}
until visit_number == 100 do
door_array = door_array.map.with_index { |door_status, door_index|
if (door_index + 1) % (visit_number + 1) == 0
if door_status == "closed"
door_status = "open"
elsif door_status == "open"
door_status = "closed"
end
end
}
visit_number += 1
end
print door_array
But it keeps printing me an array of 100 nil when I run it: Look at all this nil !
What am I doing wrong?
That's what your if clauses return. Just add a return value explicitly.
until visit_number == 100 do
door_array = door_array.map.with_index { |door_status, door_index|
if (door_index + 1) % (visit_number + 1) == 0
if door_status == "closed"
door_status = "open"
elsif door_status == "open"
door_status = "closed"
end
end
door_status
}
visit_number += 1
end
OR:
1.upto(10) {|i| door_array[i*i-1] = 'open'}
The problem is the outer if block doesn't explicitly return anything (thus returns nil implicitly) when the condition does not meet.
A quick fix:
visit_number = 0
door_array = []
door_array = 100.times.map {"closed"}
until visit_number == 100 do
door_array = door_array.map.with_index { |door_status, door_index|
if (door_index + 1) % (visit_number + 1) == 0
if door_status == "closed"
door_status = "open"
elsif door_status == "open"
door_status = "closed"
end
else #<------------- Here
door_status #<------------- And here
end
}
visit_number += 1
end
print door_array
Consider these approaches:
door_array.map { |door|
case door
when "open"
"closed"
when "closed"
"open"
end
}
or
rule = { "open" => "closed", "closed" => "open" }
door_array.map { |door| rule[door] }
or
door_array.map { |door| door == 'open' ? 'closed' : 'open' }
Code
require 'prime'
def even_nbr_divisors?(n)
return false if n==1
arr = Prime.prime_division(n).map { |v,exp| (0..exp).map { |i| v**i } }
arr.shift.product(*arr).map { |a| a.reduce(:*) }.size.even?
end
closed, open = (1..100).partition { |n| even_nbr_divisors?(n) }
closed #=> [ 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22,
# 23, 24, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40,
# 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57,
# 58, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
# 75, 76, 77, 78, 79, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
# 92, 93, 94, 95, 96, 97, 98, 99],
open #= [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Explanation
All doors are initially closed. Consider the 24th door. It is toggled during the following passes:
pass 1: opened
pass 2: closed
pass 3: opened
pass 4: closed
pass 6: opened
pass 8: closed
pass 12: opened
pass 24: closed
Notice that the door is toggled once for each of 24's divisors. Therefore, if we had a method divisors(n) that returned an array of n's divisors, we could determine the number of toggles as follows:
nbr_toggles = divisors(24).size
#=> [1,2,3,4,6,8,12,24].size
#=> 8
Since the door is toggled an even number of times, we conclude that it will be in its original state (closed) after all the dust has settled. Similarly, for n = 9,
divisors(9).size
#=> [1,3,9].size
#=> 3
We therefore conclude door #9 will be open at the end, since 3 is odd.
divisors can be defined as follows.
def divisors(n)
arr = Prime.prime_division(n).map { |v,exp| (0..exp).map { |i| v**i } }
arr.first.product(*arr[1..-1]).map { |a| a.reduce(:*) }
end
For example,
divisors 24
#=> [1, 3, 2, 6, 4, 12, 8, 24]
divisors 9
#=> [1, 3, 9]
divisors 1800
#=> [1, 5, 25, 3, 15, 75, 9, 45, 225, 2, 10, 50, 6, 30, 150, 18, 90, 450,
# 4, 20, 100, 12, 60, 300, 36, 180, 900, 8, 40, 200, 24, 120, 600, 72,
# 360, 1800]
Since we only care if there are an odd or even number of divisors, we can instead write
def even_nbr_divisors?(n)
return false if n==1
arr = Prime.prime_division(n).map { |v,exp| (0..exp).map { |i| v**i } }
arr.shift.product(*arr).map { |a| a.reduce(:*) }.size.even?
end
For n = 24, the steps are as follows:
n = 24
a = Prime.prime_division(n)
#=> [[2, 3], [3, 1]]
arr = a.map { |v,exp| (0..exp).map { |i| v**i } }
#=> [[1, 2, 4, 8], [1, 3]]
b = arr.shift
#=> [1, 2, 4, 8]
arr
#=> [[1, 3]]
c = b.product(*arr)
#=> [[1, 1], [1, 3], [2, 1], [2, 3], [4, 1], [4, 3], [8, 1], [8, 3]]
d = c.map { |a| a.reduce(:*) }
#=> [1, 3, 2, 6, 4, 12, 8, 24]
e = d.size
#=> 8
e.even?
#=> true
Lastly,
(1..100).partition { |n| even_nbr_divisors?(n) }
returns the result shown above.

Array deducting 1 from previous element when repeated more than once

I have the following array:
Input:
array = [211, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
Ouput:
array = [211, 200, 199, 198, 197, 196 ... ]
I've tried each_with_index but couldn't get the desired result.
I don't understand what are to be done with nils, so I haven't addressed that. Let arr be array or array.sort.reverse, depending on requirements. I think this is want you want to do? (See my comment on the question.)
def change_em(arr)
dup_indices = arr.each_index
.group_by { |i| arr[i] }
.values
.flat_map { |a| a.drop(1) }
puts "dup_indices = #{dup_indices}"
last = 0 # anything '-' responds to
arr.each_index.map { |i| last = dup_indices.include?(i) ? last-1 : arr[i] }
end
I've included the puts just to clarify what I'm doing here.
change_em [10, 8, 5, 5, 7]
#=> dup_indices = [3]
#=> [10, 8, 5, 4, 7]
change_em [10, 8, 7, 5, 5]
#=> dup_indices = [4]
#=> [10, 8, 7, 5, 4]
change_em [10, 9, 9, 8, 8, 8]
#=> dup_indices = [2, 4, 5]
#=> [10, 9, 8, 8, 7, 6]
change_em [211, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 196]
#=> dup_indices = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
#=> [211, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 196]
Notice that the statement
last = dup_indices.include?(i) ? last-1 : arr[i]
is doing double-duty: it updates the value of last and returns the mapped value for the index i. Note also that dup_indices cannot contain 0.
Not sure I fully understand your requirements, but here's my attempt:
# Transforms an array of numbers into a sorted array of the same length, where
# each successive element is always smaller than the preceding element.
def force_descending(array)
array.sort.reverse.each_with_object([]) do |element, collection|
collection << if collection.empty? || element < collection.last
element
else
collection.last-1
end
end
end
Sample inputs/outputs:
force_descending [211, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
#=> [211, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190]
force_descending [10, 8, 5, 5, 7]
#=> [10, 8, 7, 5, 4]
force_descending [211, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 196]
#=> [211, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189]
Wrote this is a more functional style.
def f(arr, dup_element = nil, dup_count = 0)
return generate_dup_array(dup_element, dup_count) if arr.empty?
if arr.head != arr.tail.head # Not duplicates
if dup_count == 0 # No duplicates to insert
[arr.head] + f(arr.tail)
else # There are duplicates to insert
generate_dup_array(dup_element, dup_count) + f(arr.tail)
end
else # Duplicate found, continue with tail of array and increase dup_count
f(arr.tail, arr.head, dup_count + 1)
end
end
def generate_dup_array(dup_element, dup_count)
return [] if dup_count == 0
(dup_element - dup_count..dup_element).to_a.reverse
end
class Array
def head; self.first; end
def tail; self[1..-1]; end
end
p f [211, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
# => [211, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190]
p f [10, 8, 5, 5, 7].sort.reverse
# => [10, 8, 7, 5, 4]
p f [9, 6, 6, 5, 5, 4, 4, 3, 3, 3, 2, 2, 1]
# => [9, 6, 5, 5, 4, 4, 3, 3, 2, 1, 2, 1, 1]
its already in decsending order
For the one-liner crowd:
results = numbers.chunk {|num| num}.flat_map {|num, group| (group.length == 1) ? num : ((num - (group.length-1))..num).to_a.reverse}
For sane programmers:
numbers = [211, 200, 200, 200]
start_of_dups = "_START_" #Something not in the array
dup_count = 0
results = numbers.map do |num|
if start_of_dups == num
dup_count += 1
num - dup_count
else
dup_count = 0
start_of_dups = num
end
end
p results
--output:--
[211, 200, 199, 198]
But if:
array = [10, 10, 10, 9]
--output:--
[10, 9, 8, 9]

How to find some arrays with certain value and sum their values?

I have an array of arrays with a lot of value. Where always the first and third element is repeated and I need to sum only the second element, because this is a number that I want totalizing.
Then make a new array with all values totalizing.
Example
# My array
=> [ ['abc', 13, 40 ], [ 'abc', 20, 40 ], [ 'abc', 2, 40 ], [ 'cde', 90, 20 ], [ 'cde', 60, 20 ], [ 'fgh', 20, 50 ] ]
# My Expected Result
=> [ ['abc', 35, 40 ], ['cde', 150, 20], ['fgh', 20, 50] ]
What would be the optimal way to do that? What functions could be used to reach that result?
You can do that like this:
arr = [[ 'abc', 13, 40 ], [ 'abc', 20, 40 ], [ 'abc', 2, 40 ],
[ 'cde', 90, 20 ], [ 'cde', 60, 20 ], [ 'fgh', 20, 50 ] ]
arr.group_by { |a,_,c| [a,c] }
.map { |(a,b),arr| [a,arr.reduce(0) { |t,(_,e,_)| t + e },b] }
#=> [["abc", 35, 40],
# ["cde", 150, 20],
# ["fgh", 20, 50]]
This is how this works:
f = arr.group_by { |a,_,c| [a,c] }
#=> {["abc", 40]=>[["abc", 13, 40], ["abc", 20, 40], ["abc", 2, 40]],
# ["cde", 20]=>[["cde", 90, 20], ["cde", 60, 20]],
# ["fgh", 50]=>[["fgh", 20, 50]]}
map passes the first key-value pair of the hash f into its block, assigning the block variables (using decomposition) as follows:
a,b = ["abc", 40]
arr = [["abc", 13, 40], ["abc", 20, 40], ["abc", 2, 40]]
and then computes:
[a,g,b]
#=> ["abc", g, 40]
where
g = arr.reduce(0) { |t,(_,e,_)| t + e }
#=> 35
so
["abc", 40]=>[["abc", 13, 40], ["abc", 20, 40], ["abc", 2, 40]]
is mapped to:
["abc", 35, 40]
The two other elements of the hash f are computed similarly.
arr.group_by { |a,_,c| [a,c] }.values.map{|x|[x[0][0],x.map{|n|n[1]}.reduce(&:+),x[0][2]]}

How do I make my class objects return an array instead of <Classobject:0x007fc94a0...]>?

!!!! I'm cleaning up my code and rethinking my question. I'll repost and edited version in a few minutes. Thanks for the responses!
Here's my code:
class Student
attr_accessor :scores, :first_name
def initialize(first_name, scores)
#first_name = first_name
#scores = scores
end
def average
#scores.inject {|sum, el| sum + el }.to_f / #scores.size
end
def letter_grade
case average
when (90..100)
"A"
when (80..89)
"B"
when (70..79)
"C"
when (60..69)
"D"
when (0..59)
"F"
end
end
end
me = Student.new("Alex", [100,100,100,0,100])
student_2 = Student.new('Hodor', [2,2,7,0,90])
student_3 = Student.new('Aria', [90,100,87,90,90])
student_4 = Student.new('Tyrion', [95,100,97,100,30])
student_5 = Student.new('Jaela', [100,100,100,100,100])
students = [me, student_2, student_3, student_4, student_5]
p students
Here's what I get back:
[#<Student:0x007f92a91e6070 #first_name="Alex", #scores=[100, 100, 100, 0, 100]>, #<Student:0x007f92a91e5ff8 #first_name="Hodor", #scores=[2, 2, 7, 0, 90]>, #<Student:0x007f92a91e5f80 #first_name="Aria", #scores=[90, 100, 87, 90, 90]>, #<Student:0x007f92a91e5f08 #first_name="Tyrion", #scores=[95, 100, 97, 100, 30]>, #<Student:0x007f92a91e5e90 #first_name="Jaela", #scores=[100, 100, 100, 100, 100]>]
I want something like [["Alex", [100, 100, 100, 0, 100], ["Hodor", [2..]..]]
The goal is to have these tests pass:
p linear_search(students, "Alex") == 0
p linear_search(students, "NOT A STUDENT") == -1
So I actually need this to happen within the Student class, I think.
I'm not sure what's the purpose of the exercise, but to get from your actual output to your expected output, you just have to go over your elements, and build an array out of each one (use map):
students.map { |x| [x.first_name, x.scores] }
# => [["Alex", [100, 100, 100, 0, 100]], ["Hodor", [2, 2, 7, 0, 90]], ["Aria", [90, 100, 87, 90, 90]], ["Tyrion", [95, 100, 97, 100, 30]], ["Jaela", [100, 100, 100, 100, 100]]]
If you try to output an instance of Student, ruby calls to_s() on the Student instance. If your class does not provide a to_s() method, then the inherited to_s() method(in class Object) is called, which provides the string you see. If you redefine Object#to_s, you can prove that:
#Your code here
class Object
def to_s
'hello from Object#to_s'
end
end
p students
--output:--
[hello from Object#to_s,
hello from Object#to_s,
hello from Object#to_s,
hello from Object#to_s,
hello from Object#to_s]
If you override the to_s() method inside Student, then ruby will call it and use its return value whenever you try to output a Student object:
require 'pp'
class Student
attr_accessor :scores, :first_name
...
def to_s
"#{first_name} #{scores.inspect}"
end
end
students = [
Student.new("Alex", [100,100,100,0,100]),
Student.new('Hodor', [2,2,7,0,90]),
Student.new('Aria', [90,100,87,90,90]),
Student.new('Tyrion', [95,100,97,100,30]),
Student.new('Jaela', [100,100,100,100,100]),
]
pp students
--output:--
[Alex [100, 100, 100, 0, 100],
Hodor [2, 2, 7, 0, 90],
Aria [90, 100, 87, 90, 90],
Tyrion [95, 100, 97, 100, 30],
Jaela [100, 100, 100, 100, 100]]
In the code fragment scores.inspect the inspect() method is what p uses, i.e. p scores is equivalent to print scores.inspect + "\n". But you can't write:
"some string #{p.scores}"
because string interpolation uses the return value of p.scores, and p, like puts, always returns nil.

Sort hash by key in descending order

I have the following hash:
{"2013-08-12"=> 10, "2013-08-13"=> 20, "2013-11-11"=>30, "2013-11-14"=> 40}
What I want to do is to sort it by key (dates in format yyyy-mm-dd) in descending order:
{"2013-11-14"=> 40, "2013-11-11"=>30, "2013-08-13"=> 20, "2013-08-12"=> 10}
Is this possible?
It is possible.
Hash[
{"2013-08-12"=> 10, "2013-08-13"=> 20, "2013-11-11"=>30, "2013-11-14"=> 40}
.sort_by{|k, _| k}.reverse
]
# => {
"2013-11-14" => 40,
"2013-11-11" => 30,
"2013-08-13" => 20,
"2013-08-12" => 10
}

Resources