Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I came to know that graph colouring algorithms are NP-Complete problems. Still, I want to know whether any implementation is possible using heuristic approach or not, especially the distinguishing graph colouring? If possible then is there any suitable resource to learn about that ?
As discussed in a somewhat related post:
Constraint solvers like MiniZinc are able to solve a broad range of graph colouring problems.
This MiniZinc example demonstrates colouring of the Petersen graph:
% Petersen Graph
set of int: Colors = 1..3;
set of int: Nodes = 1..10;
set of int: Edges = 1..15;
array[Edges] of Nodes: aFrom = [ 1, 2, 3, 4, 1, 1, 2, 3, 4, 5, 6, 7, 7, 8, 6 ];
array[Edges] of Nodes: aTo = [ 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 8, 10, 9, 10, 9 ];
array[int] of string: colorName = [ "red", "green", "blue", "purple", "yellow", "brown", "black" ];
array[Nodes] of var Colors: nodeColor;
constraint
forall(e in Edges) (
nodeColor[aFrom[e]] != nodeColor[aTo[e]]
);
solve satisfy;
output [ show(colorName[nodeColor[n]]) ++ "\n" | n in Nodes ];
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I have the following array:
[1,1,3,3,3,3,1023,1023,1,3,3,3, ...]
Is there a way to sort the array in a way where the items can appear more mixed up?
Like:
[1, 3, 1023, 3, 1, 3, 1023, ...]
You could shuffle
a=[1,1,3,3,3,3,1023,1023,1,3,3,3]
p a.shuffle
Output
[1023, 1, 1, 1, 3, 3, 1023, 3, 3, 3, 3, 3]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I remove elements from a given array of integers that are evenly divisible by 3? A new array should be returned and the original array should not be altered.
For example, if
array = [1,2,3,4,1,5,2,6,7,8,9,10,7,11,12,13,14,15]
the array
[1, 2, 4, 1, 5, 2, 7, 8, 10, 7, 11, 13, 14]
should be returned.
Just found it:
array.reject{|a| a % 3 == 0}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
A,B,C are the arrays :
A = {1,2,3,4}
B = {8,1,2,3}
C = {1,2,9,3}
Result is the uncommon values from three arrays
Result = {4,8,9}
Asking for the logic what I can implement ?
There might be some other better answers. But here is the most simple one.
On line 4 I took the Intersection of 3 sets
On line 7 I took the Union of three sets
On line 10 the difference or XOR operation, of Union and intersection
I hope it helps:-
>>> a = {1,2,3,4}
>>> b = {8,1,2,3}
>>> c = {1,2,9,3}
>>> d = a & b & c
>>> print(d)
{1, 2, 3}
>>> e = a | b | c
>>> print(e)
{1, 2, 3, 4, 8, 9}
>>> f = d^e
>>> print(f)
{4, 8, 9}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I can't seem to find a ruby-way to transform this
[0, 1, 2, 3]
into
[0, 0.5, 1, 1.5, 2, 2.5, 3]
e.g, insert new elements based on existing ones applying some rule.
EDIT: I'm asking about a general case, not necessarily a 0.5 increment. Say, when elements are not successive:
[1, 3, 12] => [1, 2, 3, 7.5, 12]
So the accepted answer is just perfect here, thanks.
arr = [0, 1, 2, 3]
def rule(x, y)
(x + y) / 2.0
end
arr.each_cons(2).flat_map{|x, y| [x, rule(x, y), y]} # => [0, 0.5, 1, 1, 1.5, 2, 2, 2.5, 3]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to ruby on rails .
I was programming ruby and want to try checking if 2 numbers of array add up to Input number in ruby.
eg,
array A[]= {3, 1, 8, 11, 5, 7}
given integer say N = 6
answer will be 1,5.
I know how to program it in java,C++ but i am stuck in ruby coding,
Can anyone please help me.Thanks in advance
You can use Array#combination:
ary = [3, 1, 8, 11, 5, 7]
n = 6
ary.combination(2).detect { |a, b| a + b == n }
#=> [1, 5]
combination(2) creates an array of all combinations of length 2, i.e. [3,1], [3,8], [3,11] etc.
detect { |a, b| a + b == n } returns the first pair with sum n
You can use find_all instead of detect to return all pairs with sum n.
a = [3, 1, 8, 11, 4, 5, 7, 2]
> a.combination(2).select {|i| i.inject(:+) == 6 }
#=> [[1, 5], [4, 2]]
a = [3, 1, 8, 11, 5, 7]
p a.combination(2).find{|i| i.inject(:+) == 6}
# >> [1, 5]