Skip to main content
Commonmark migration
Source Link

#Mathematica 126

Mathematica 126

With x holding the input string,

t = ToExpression; 
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /. 
{a_, "-", b_} :>   UndirectedEdge[t@a, t@b]]

If x is input instead as a list of undirected edges, in standard Mathematica format, the solution can be reduced to 34 chars:

Length@ConnectedComponents@Graph@i

Example:

enter image description here

#Mathematica 126

With x holding the input string,

t = ToExpression; 
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /. 
{a_, "-", b_} :>   UndirectedEdge[t@a, t@b]]

If x is input instead as a list of undirected edges, in standard Mathematica format, the solution can be reduced to 34 chars:

Length@ConnectedComponents@Graph@i

Example:

enter image description here

Mathematica 126

With x holding the input string,

t = ToExpression; 
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /. 
{a_, "-", b_} :>   UndirectedEdge[t@a, t@b]]

If x is input instead as a list of undirected edges, in standard Mathematica format, the solution can be reduced to 34 chars:

Length@ConnectedComponents@Graph@i

Example:

enter image description here

example
Source Link
DavidC
  • 25.4k
  • 2
  • 52
  • 105

#Mathematica 126

With x holding the input string,

t = ToExpression; 
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /. 
{a_, "-", b_} :>   UndirectedEdge[t@a, t@b]]

If x is input instead as a list of undirected edges, in standard Mathematica format, the solution can be reduced to 34 chars:

Length@ConnectedComponents@Graph@i

Example:

enter image description here

#Mathematica 126

With x holding the input string,

t = ToExpression; 
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /. 
{a_, "-", b_} :>   UndirectedEdge[t@a, t@b]]

#Mathematica 126

With x holding the input string,

t = ToExpression; 
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /. 
{a_, "-", b_} :>   UndirectedEdge[t@a, t@b]]

If x is input instead as a list of undirected edges, in standard Mathematica format, the solution can be reduced to 34 chars:

Length@ConnectedComponents@Graph@i

Example:

enter image description here

Source Link
DavidC
  • 25.4k
  • 2
  • 52
  • 105

#Mathematica 126

With x holding the input string,

t = ToExpression; 
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /. 
{a_, "-", b_} :>   UndirectedEdge[t@a, t@b]]