Generate Set from Array with the bounds - ruby

I want to know is some sort of "Array.to_range" method in order to create some Range from an Array of two elements.
I know that i could perform something like this:
a = [1,5]
Set.new(a[0]..a[1])
=> #<Set: {1, 2, 3, 4, 5}>
This a good solution, however i try to synthesize more, to archive something like this:
a = [1,5]
Set.new(a.to_range)
because if i do this, generate a Set with only 2 elements and i want the full range of elements.
a = [1,5]
Set.new(a)
=> #<Set: {1, 5}>
and obviously
{1, 5} != {1, 2, 3, 4, 5}
So, any idea how can i synthesize this expression more?
Set.new(a[0]..a[1])

I think this give you what you are looking for:
a = [1,5]
Set.new(Range.new(*a)) # => #<Set: {1, 2, 3, 4, 5}>

Related

I have to generate 10000 numbers which are the result of 12 random numbers subtracted by 0.5 and then added together

I'm stuck here, and can't generate a do loop that repeats this operation 10k times and result in a list or array
{((RandomVariate[TruncatedDistribution[{0, 1}, NormalDistribution[]],
12])) - 0.5}
Do does things but it doesn't generate output. E.g.
Do[{1, 2, 3}, 2]
- no output -
Have it add to a list though...
alist = {};
Do[alist = Join[alist, {1, 2, 3}], 2]
alist
{1, 2, 3, 1, 2, 3}

How can i print all r-permutation with repetition with a given rank?

Hello guys i have a question: i have to search through the r-pemutation with repetition of a given list let's say {1, 2, 3} those which have a certain rank.Well i know how to generate all permutations but i'm stuck at the part where i have to check if their rank is equal with the one i choose. I tried to use Select function but i don't know what argument should i give to RankPermutation so it would work.
Select[Tuples[{1, 2, 3} , 3], RankPermutation[] == 2]
PS: Sorry if the question is stupid or i am wrong with something but it's only my first week of Wolfram-Mathematica so don't judge me too hard.
Quiet#Needs["Combinatorica`"]
Select[Tuples[{1, 2, 3}, 3], RankPermutation[#] == 2 &]
{{2, 1, 3}}
or using a more longhand form
Select[Tuples[{1, 2, 3}, 3], Function[tuple, RankPermutation[tuple] == 2]]
{{2, 1, 3}}
See Slot and Pure Functions

Can I merge two Set objects in Ruby?

I understand the Set class has the merge method just as the Hash class does. However, the Set#merge documentation says:
Merges the elements of the given enumerable object to the set and returns self.
It seems that merging can only take place between a Set and another non-Set object. Is that the case, or can I merge two sets as below?
set1.merge(set2)
Why This Question is Useful
While the OP has been criticized for lack of research effort, it should be pointed out that the Ruby documentation for Set#merge is not friendly for new Rubyists. As of Ruby 2.3.0, it says:
Merges the elements of the given enumerable object to the set and returns self.
It offers merge(enum) as the signature, but no useful examples. If you need to know what classes mix in Enumerable, it can be difficult to grok from just this one piece of documentation what kind of duck-typed ducks can be merged in. For example, set.merge {foo: 'bar'}.to_enum will raise SyntaxError despite the fact that it is enumerable:
{foo: 'bar'}.to_enum.class
#=> Enumerator
{foo: 'bar'}.to_enum.class.include? Enumerable
#=> true
Merging Sets
If you're thinking of Set#merge as creating a set union, then yes: you can merge sets. Consider the following:
require 'set'
set1 = Set.new [1, 2, 3]
#=> #<Set: {1, 2, 3}>
set2 = Set.new [3, 4, 5]
#=> #<Set: {3, 4, 5}>
set1.merge set2
#=> #<Set: {1, 2, 3, 4, 5}>
Merging Other Enumerable Objects Like Arrays
However, you can also merge other Enumerable objects (like arrays) into a set. For example:
set = Set.new [1, 2, 3]
#=> #<Set: {1, 2, 3}>
set.merge [3, 4, 5]
#=> #<Set: {1, 2, 3, 4, 5}>
Using Array Union Instead
Of course, you may not need sets at all. Compare and contrast Set to array unions (Array#|). If you don't need the actual features of the Set class, you can often do similar things directly with arrays. For example:
([1, 2, 3, 4] | [3, 4, 5, 6]).uniq
#=> [1, 2, 3, 4, 5, 6]

Pop matching elements from array, then push them back

Say I have an array: [1, 2, 3, 4, 5].
Given another array ([2, 4], for example), I would like to have a new array (or the initial array modified, doesn't matter) that looks like: [1, 3, 5, 2, 4]. So selected elements are moved to the end of the array.
Pushing the elements back is quite straight-forward, but how do I pop specific elements from an array?
a = [1, 2, 3, 4, 5]
b = [2, 4]
(a - b) + (b & a)
#=> [1, 3, 5, 2, 4]
a - b is the elements in a but not in b, while b & a is the elements that are common in both arrays. There goes your expected result.
In case if elements in a are not uniq (as mentioned by eugen) and it's important to remove only one element from b you could do something like:
a = [1, 2, 3, 2, 4, 5, 4, 2]
b = [2, 4, 7]
p (b&a).unshift(a.map{|el|
b.include?(el) ? begin b = b -[el]; nil end : el
}.compact).flatten
#=> [1, 3, 2, 5, 4, 2, 2, 4]

Take and remove elements from a Ruby Set in one operation

I have a Set of elements from which I want to take and remove the first few elements a bunch of times. Is there a shorter way (so one operation instead of two) to do that than this:
require 'set'
s = Set[1, 2, 3, 4] # => #<Set: {1, 2, 3, 4}>
first_two = s.take(2) # => [1, 2]
s.subtract(first_two) # => #<Set: {3, 4}>
(So basically I'm wondering whether I'm overlooking a shift for Sets)
You could add a new method take! (or remove! or whatever name seems appropriate) to the Set class:
class Set
def take!(args)
taken = self.take(args)
self.subtract(taken)
return taken
end
end
a = Set[1, 2, 3, 4] # <Set: {1, 2, 3, 4}>
a.take!(2) # <Set: {1, 2}>
a # <Set: {3, 4}>
There is no shorter way using builtin methods.
There is an open feature request for a method to return and remove one element; you may want to help refine the API?
From http://www.ruby-doc.org/stdlib-1.9.3/libdoc/set/rdoc/Set.html:
Set implements a collection of unordered values with no duplicates.
This is a hybrid of Array's intuitive inter-operation facilities and
Hash's fast lookup.
It would be odd and probably illogical to implement methods like shift and pop on an object that knows nothing about index.
I'm late to the party, but here's my solution. Convert the set to an array first, and then all Enumerable methods are available. Take 2 from the array and then be sure to also remove the from the set. The two values from the set get removed and returned.
require 'set'
s = Set[1, 2, 3, 4] # => #<Set: {1, 2, 3, 4}>
first_two = s.to_a.take(2).tap {|a| s.subtract(a)} # => [1, 2]
s # => #<Set: {3, 4}>

Resources