update: sorry, I fixed my program:
a = [ 'str1' , 'str2', 'str2', 'str3' ]
name = ''
a.each_with_index do |x, i |
if x == name
puts "#{x} found duplicate."
else
puts x
name = x if i!= 0
end
end
output:
str1
str2
str2 found duplicate.
str3
Is there another beautiful way in ruby language to do the same thing ?
btw, actually. a is a ActiveRecord::Relation in my real case.
Thanks.
The problem you might have with each_cons is that it iterates through n-1 pairs (if the length of the Enumerable is n). In some cases this means you have to separately handle edge cases for the first (or last) element.
In that case, it's really easy to implement a method similar to each_cons, but which would yield (nil, elem0) for the first element (as opposed to each_cons, which yields (elem0, elem1):
module Enumerable
def each_with_previous
self.inject(nil){|prev, curr| yield prev, curr; curr}
self
end
end
you can use each_cons:
irb(main):014:0> [1,2,3,4,5].each_cons(2) {|a,b| p "#{a} = #{b}"}
"1 = 2"
"2 = 3"
"3 = 4"
"4 = 5"
You can use each_cons
a.each_cons(2) do |first,last|
if last == name
puts 'got you!'
else
name = first
end
end
You may use Enumerable#each_cons:
a = [ 'str1' , 'str2', 'str3' , ..... ]
name = ''
a.each_cons(2) do |x, y|
if y == name
puts 'got you! '
else
name = x
end
end
As you probably want to do more than puts with the duplicates, I would rather keep the duplicates in a structure:
### question's example:
a = [ 'str1' , 'str2', 'str2', 'str3' ]
# => ["str1", "str2", "str2", "str3"]
a.each_cons(2).select{|a, b| a == b }.map{|m| m.first}
# => ["str2"]
### a more complex example:
d = [1, 2, 3, 3, 4, 5, 4, 6, 6]
# => [1, 2, 3, 3, 4, 5, 4, 6, 6]
d.each_cons(2).select{|a, b| a == b }.map{|m| m.first}
# => [3, 6]
More on at: https://www.ruby-forum.com/topic/192355 (cool answer of David A. Black)
Related
trio = Proc.new do |x|
tf = true
puts x
if tf
puts "ai yo"
end
end
trio.call([1, 2, 3, 4, 5])
output:
1 2 3 4 5 ai yo #its only doing the ai yo part only once when I believe it
should do it after every number
but What I am expecting of output is:
1 ai yo 2 ai yo 3 ai yo 4 ai yo 5 ai yo
I still cant wrap my head around why this is happening.
Im trying to get this program to work that i thought would be a cool way to use procs although in this specific problem i dont need to basically:
#The prime factors of 13195 are 5, 7, 13 and 29.
#What is the largest prime factor of the number 600851475143 ?
number = 13195
def factorsAndOptions(num, proc = Proc.new {|x|return x})
factorsArray = []
for i in 1..num
factorsArray.push(i) if num % i == 0
end
proc.call(factorsArray)
end
largestPrime = Proc.new do |x|
prime = true
for j in 2...x
if (x % x == 0)
prime = false
end
end
larger = x if prime && larger > x
puts larger
larger
end
factorsAndOptions(number, largestPrime)
call won't iterate over arguments. What you've written is, effectively:
puts [1, 2, 3, 4, 5]
puts "ai yo"
If you want to iterate, use each:
[1, 2, 3, 4, 5].each(&trio)
Or:
[1, 2, 3, 4, 5].each { |i| trio.call(i) }
As has been mentioned, you do not have any looping block. Your proc - trio is acting on the whole array as one single element.
In your example: x becomes [1, 2, 3, 4, 5] and not individual elements of the array as you are expecting.
To circumvent this you can either loop inside your Proc or create a separate Proc that will loop over the elements of the array and call the first Proc.
Example 1
trio = Proc.new do |arr|
arr.each do |elem|
puts elem
if tf
puts "ai yo"
end
end
end
This assumes that arr is an array
Example 2
trio = Proc.new do |x|
tf = true
puts x
if tf
puts "ai yo"
end
end
trio_helper = Proc.new do |x|
arr = x.to_a
arr.each do |elem|
trio.call(elem)
end
end
trio_helper.call([1, 2, 3, 4, 5])
This utilizes the original Proc you have written and uses another Proc to iterate over the array and call the first one on each element.
I have a simple method that iterates through an array and returns a duplicate. (Or duplicates)
def find_dup(array)
duplicate = 0
array.each { |element| duplicate = element if array.count(element) > 1}
duplicate
end
It works, but I'd like to express this more elegantly.
The reason it is three lines is that the variable "duplicate", which the method must return, is not visible to the method if I introduce it inside the block, i.e,
def find_dup(array)
array.each { |element| duplicate = element if array.count(element) > 1}
duplicate
end
I've tried a few ways to define "duplicate" as the result of a block, but to no avail.
Any thoughts?
It's a little too much to do cleanly in a one-liner, but this is a more
efficient solution.
def find_dups(arr)
counts = Hash.new { |hash,key| hash[key] = 0 }
arr.each_with_object(counts) do |x, memo|
memo[x] += 1
end.select { |key,val| val > 1 }.keys
end
The Hash.new call instantiates a hash where the default value is 0.
each_with_object modifies this hash to track the count of each element in arr, then at the
end the filter is used to select only those having a count greater than one.
The benefit of this approach over a solution using Array#includes? or Array#count is that it only scans the array a single time. Thus it is a O(N) time instead of O(N^2).
Your method is only finding the last duplicate in the array. If you want all the duplicates, I would do something like this:
def find_dups(arr)
dups = Hash.new { |h, k| h[k] = 0 }
arr.each { |el| dups[el] += 1 }
dups.select { |k, v| v > 1 }.keys
end
If what you really want is a one-liner that isn't concerned with big-O complexity and only returns the last duplicate in the array, I would do this:
def find_last_dup(arr)
arr.reverse_each { |el| return el if arr.count(el) > 1 }
end
You can do this as one line and it flows a bit nicer. Though this would find the first instance of a duplicate whereas your code is returning the last instance of a duplicate, not sure if that's part of your requirement.
def find_dup(array)
array.group_by { |value| value }.find { |_, groups| groups.count > 1 }.first
end
Also, note that making things one line doesn't strictly mean is better. I'd find the code more readable split over more lines, but that's just my opinion.
def find_dup(array)
array.group_by { |value|
value
}.find { |_, groups|
groups.count > 1
}.first
end
Just want to add one more approach to the mix.
def find_last_dup(arr)
arr.reverse_each.detect { |x| arr.count(x) > 1 }
end
Alternatively, you can get linear time complexity in two lines.
def find_last_dup(arr)
freq = arr.each_with_object(Hash.new(0)) { |x, obj| obj[x] += 1 }
arr.reverse_each.detect { |x| freq[x] > 1 }
end
For the sake of argument, the latter approach can be reduced to one line as well, but this would be unidiomatic and confusing.
def find_last_dup(arr)
arr.each_with_object(Hash.new(0)) { |x, obj| obj[x] += 1 }
.tap do |freq| return arr.reverse_each.detect { |x| freq[x] > 1 } end
end
Given:
> a
=> [8, 5, 6, 6, 5, 8, 6, 1, 9, 7, 2, 10, 7, 7, 3, 4]
You can group the dups together:
> a.uniq.each_with_object(Hash.new(0)) {|e, h| c=a.count(e); h[e]=c if c>1}
=> {8=>2, 5=>2, 6=>3, 7=>3}
Or,
> a.group_by{ |e| e}.select{|k,v| v if v.length>1}
=> {8=>[8, 8], 5=>[5, 5], 6=>[6, 6, 6], 7=>[7, 7, 7]}
In each case, the order of the result is based on the order of the elements in a that have dups. If you just want the first:
> a.group_by{ |e| e}.select{|k,v| v if v.length>1}.first
=> [8, [8, 8]]
Or last:
> a.group_by{ |e| e}.select{|k,v| v if v.length>1}.to_a.last
=> [7, [7, 7, 7]]
If you want to 'fast forward' to the first value that has a dup, you can use drop_while:
> b=[1,2,3,4,5,4,5,6]
> b.drop_while {|e| b.count(e)==1 }[0]
=> 4
Or the last:
> b.reverse.drop_while {|e| b.count(e)==1 }[0]
=> 5
def find_duplicates(array)
array.dup.uniq.each { |element| array.delete_at(array.index(element)) }.uniq
end
The above method find_duplicates duplicated the input array and deletes the first occurrence of all the elements, leaving the array with only remaining occurrences of the duplicate elements.
Example:
array = [1, 2, 3, 4, 3, 4, 3]
=> [1, 2, 3, 4, 3, 4, 3]
find_duplicates(array)
=> [3, 4]
This is my array and custom method to reverse an array output without using the reverse method. not sure where it broke, tried running it in console, no dice.
numbers = [1, 2, 3, 4, 5, 6]
def reversal(array)
do |item1, item2| item2 <=> item1
end
p reversal(numbers)
Here's one way to handle this. This is not very efficient but works.
def reversal(array)
reversed = []
loop do
reversed << array.pop
break if array.empty?
end
reversed
end
Here is another implementation that does the same thing:
def reversal(array)
array.each_with_index.map do |value, index|
array[array.count-index-1]
end
end
So many ways... Here are three (#1 being my preference).
numbers6 = [1, 2, 3, 4, 5, 6]
numbers5 = [1, 2, 3, 4, 5]
For all methods my_rev below,
my_rev(numbers6)
#=> [6, 5, 4, 3, 2, 1]
my_rev(numbers5)
#=> [5, 4, 3, 2, 1]
#1
def my_rev(numbers)
numbers.reverse_each.to_a
end
#2
def my_rev(numbers)
numbers.each_index.map { |i| numbers[-1-i] }
end
#3
def my_rev(numbers)
(numbers.size/2).times.with_object(numbers.dup) do |i,a|
a[i], a[-1-i] = a[-1-i] , a[i]
end
end
there are so many ways to do this
1 Conventional way
a=[1,2,3,4,5,6,7,8]
i=1
while i <= a.length/2 do
temp = a[i-1]
a[i-1] = a[a.length-i]
a[a.length-i] = temp
i+=1
end
2 Using pop
a=[1,2,3,4,5,6]
i=0
b=[]
t=a.length
while i< t do
b << a.pop
i+=1
end
3 Using pop and loop
a=[1,2,3,4,5,6]
b=[]
loop do
b << a.pop
break if a.empty?
end
a = [1,2,3,4,5]
b = []
a.length.times { |i| b << a[(i+1)*-1] }
b
=> [5,4,3,2,1]
I'm trying to take out a number from each string and add 4 to each of them, but the compiler keeps telling me that:
undefined method `captures' for nil:NilClass (NoMethodError)
There is output 8 with the error message if I don't add match2 and int2 codes.
expecting output:
8
23
9
14
How can I fix this?
[
"I have 4 cucumbers",
"I've been given 19 radishes",
"I have 5 carrots in my hand",
"I gots 10 zucchini!"
].each do |string|
match = /^I have (\d) ([a-z]*)$/.match(string)
match2 = /I've been given (\d+) ([a-z]*)$/.match(string)
int = match.captures[0].to_i
int += 4
int2 = match2.captures[0].to_i
int2 += 4
puts int
puts int2
end
You can try this
array = []
[
"I have 4 cucumbers",
"I've been given 19 radishes",
"I have 5 carrots in my hand",
"I gots 10 zucchini!"
].each do |string|
array.push(string.scan(/\d+/))
end
new_array = array.flatten.map {|i| i.to_i}
#=> [4, 19, 5, 10]
new_array.map {|i| i.to_i + 4} #if you want to add 4 to each element
=> [8, 23, 9, 14]
It's not entirely clear what your expected output should be.
Meditate on this:
ary = ["a 4 b", "a 19 b"]
ary.each do |string|
string.gsub!(/\b\d+\b/) { |num| (num.to_i + 4).to_s }
end
ary # => ["a 8 b", "a 23 b"]
gsub! changes a string in place, whereas gsub returns the changed string. The difference would be:
ary = ["a 4 b", "a 19 b"]
new_ary = ary.map do |string|
string.gsub(/\b\d+\b/) { |num| (num.to_i + 4).to_s }
end
ary # => ["a 4 b", "a 19 b"]
new_ary # => ["a 8 b", "a 23 b"]
Notice that each became map, because we want to return an array of changed values, and gsub! because gsub.
It's important to use \b when searching for numbers in strings, otherwise you can run into problems with false-positive hits affecting digits inside "words" like "foo1".
If you want to return only the values after they've been incremented:
ary = ["a 0 b", "a 0 b 1"]
ary.map{ |a| a.scan(/\b\d+/).map{ |i| i.to_i + 4 }} # => [[4], [4, 5]]
Which, broken down, is doing this:
ary
.map{ |a|
a # => "a 0 b", "a 0 b 1"
.scan(/\b\d+/) # => ["0"], ["0", "1"]
.map{ |i| i.to_i + 4 } # => [4], [4, 5]
} # => [[4], [4, 5]]
In your code you're doing:
match = /^I have (\d) ([a-z]*)$/.match(string)
match2 = /I've been given (\d+) ([a-z]*)$/.match(string)
If you're processing freeform text you can't create a match for every possible incoming string; There are infinite possibilities. Even if you're in charge of the string creation, you shouldn't need to match entire strings, only specific parts. The more you try to match, the more likely it is the code will fail.
I've been going at this problem for a few hours, and I can't see why I can't get it to run properly. The end game to this method is having 2 numbers in an array equaling zero when added together. Here is my code:
def two_sums(nums)
i = 0
j = -1
while i < nums.count
num_1 = nums[i]
while j < nums.count
num_2 = nums[j]
if num_1 + num_2 == 0
return "There are 2 numbers that sum to zero & they are #{num_1} and #{num_2}."
else
return "Nothing adds to zero."
end
end
i += 1
j -= 1
end
end
The problem I'm having is unless the first and last number in the array are the positive and negative of the same number, this will always return false.
For example, if I had an array that was [1, 4, 6, -1, 10], it should come back true. I'm sure my 2 while statement is the cause of this, but I can't think of a way to fix it. If someone could point me in the right direction, that would be helpful.
You can find the first pair that adds up to 0 like this:
nums.combination(2).find { |x, y| x + y == 0 }
#=> returns the first matching pair or nil
Or if you want to select all pairs that add up to 0:
nums.combination(2).select { |x, y| x + y == 0 }
#=> returns all matching pairs or an empty array
Therefore you can implement your method like this:
def two_sums(nums)
pair = nums.combination(2).find { |x, y| x + y == 0 }
if pair
"There are 2 numbers that sum to zero & they are #{pair.first} and #{pair.last}."
else
"Nothing adds to zero."
end
end
Or if you want to find all pairs:
def two_sums(nums)
pairs = nums.combination(2).select { |x, y| x + y == 0 }
if pairs.empty?
"Nothing adds to zero."
else
"The following pairs sum to zero: #{pairs}..."
end
end
Here's another way:
Code
def sum_to_zero(arr)
arr.group_by { |e| e.abs }
.values
.select { |a| (a.size > 1 && a.first == 0) || a.uniq.size > 1 }
end
Examples
sum_to_zero [1, 4, 6, -1, 10] #=> [[1, -1]]
sum_to_zero [1, 4, 1, -2, 10] #=> []
sum_to_zero [1, 0, 4, 1, 0, -1] #=> [[1, 1, -1], [0, 0]]
This method is relatively fast. Let's try it with an array of 200,000 elements, each a random number between -500,000 and 500,000.
require 'time'
t = Time.now
arr = Array.new(200_000) { rand(1_000_001) - 500_000 }
arr.size #=> 200000
sum_to_zero(arr).size #=> 16439
Time.now - t
#=> 0.23 (seconds)
sum_to_zero(arr).first(6)
#=> [[-98747, 98747],
# [157848, -157848],
# [-459650, 459650],
# [176655, 176655, -176655],
# [282101, -282101],
# [100886, 100886, -100886]]
If you wish to group the non-negative and negative values that sum to zero:
sum_to_zero(arr).map { |a| a.partition { |e| e >= 0 } }.first(6)
#=> [[[98747], [-98747]],
# [[157848], [-157848]],
# [[459650], [-459650]],
# [[176655, 176655], [-176655]],
# [[282101], [-282101]],
# [[100886, 100886], [-100886]]]
If you only want a single value for each group (a non-negative value, say):
sum_to_zero(arr).map { |a| a.first.abs }.first(6)
#=> [98747, 157848, 459650, 176655, 282101, 100886]
I think the most Ruby way would be:
nums.combination(2).any? { |x,y| (x+y).zero? }
Here's a way that should work well for large arrays. The methods above which go through every possible combination of two numbers are perfectly fine for small cases but will be very slow and memory hungry for arrays with lots of elements.
def two_sums nums
h = Hash.new
nums.each do |n|
return true if h[-n]
h[n] = true
end
false
end
Well, given it's tagged as #ruby, here's the most "ruby way" I could think of tackling this problem:
def two_sums(arr)
numbers = arr.combination(2).select { |a| a.reduce(:+) == 0 }.flatten
if numbers.empty?
"Nothing adds to zero."
else
"There are 2 numbers that sum to zero & they are #{numbers.first} and #{numbers.last}."
end
end
array.combination(2).select{|x|x[0] + x[1] == 0}