Changed from a static variable to a dynamic function.
module Solution export greet function greet( whom::String ) "Hello " * whom * "!" end end
println("Hello Julia!")const hello = "world"- module Solution
- export greet
- function greet( whom::String )
- "Hello " * whom * "!"
- end
- end
using FactCheck facts("greetings") do @fact Solution.greet("world") --> "Hello world!" @fact Solution.greet("Julia") --> "Hello Julia!" @fact Solution.greet("Fred") --> "Hello Fred!" @fact Solution.greet("Kmactavish") --> "Hello Kmactavish!" end
facts("Is hello world?") do@fact hello => "world"- using FactCheck
- facts("greetings") do
- @fact Solution.greet("world") --> "Hello world!"
- @fact Solution.greet("Julia") --> "Hello Julia!"
- @fact Solution.greet("Fred") --> "Hello Fred!"
- @fact Solution.greet("Kmactavish") --> "Hello Kmactavish!"
- end
a function that takes a list of connections on the following format:
connections[n] is the list of connections from node n.
connections={
[1] ={ 1, 2, 6, 7 },
[2] ={ 1, 2, 4, 5, 6 },
[3] ={ 3, 4, 5, 6, 7 },
[4] ={ 2, 3, 4, 5, 6 },
[5] ={ 2, 3, 4, 5, 7 },
[6] ={ 1, 2, 3, 4, 6 },
[7] ={ 1, 3, 5, 7 },
[8] ={ 8, 9, 11, 12, 13, 15 },
[9] ={ 8, 9, 10, 12, 13, 15, 16 },
[10]={ 9, 10, 11, 12, 16 },
[11]={ 8, 10, 11, 12 },
[12]={ 8, 9, 10, 11, 12, 13, 15 },
[13]={ 8, 9, 12, 13, 14, 15 },
[14]={ 13, 14, 15, 16 },
[15]={ 8, 9, 12, 13, 14, 15, 16 },
[16]={ 9, 10, 14, 15, 16 },
[17]={ 17, 18, 19, 20, 21 },
[18]={ 17, 18, 19, 20 },
[19]={ 17, 18, 19, 21 },
[20]={ 17, 18, 20, 21 },
[21]={ 17, 19, 20, 21 },
}
The output should be a table of tables with nodes in a given net.
nets={
{1,2,6,7,4,5,3},
{8,9,11,12,13,15,10,16,14},
{17,18,19,20,21}
}
This solution assumes symmetrical connections between nodes.
solution={}
local isIn=function(list,item)
--simple function to find a vale in list
if type(list)~="table" or item==nil then
return false
end
local FoundAtIndex=""
local found=false
for index,value in pairs(list) do
if value==item then
found=true
FoundAtIndex=index
break
end
end
return found,FoundAtIndex
end
solution.findConnectedNets=function(connections)
local nets={}
for i=1,#connections do --traverses nodes to build the nets
local new=true
for k=1,#nets do --if it already is in a net, skip it
if isIn(nets[k],i) then
new=false
break
end
end
if new then
local net={}
local queue={i}
while #queue>0 do
local vertex=queue[1]
table.insert(net,vertex)
table.remove(queue,1)
for _,node in pairs(connections[vertex]) do
if not isIn(net,node) and not isIn(queue,node) then
table.insert(queue,node)
end
end
end
table.insert(nets,net)
end
end
return nets
end
return solution
local solution = require 'solution'
describe("solution", function()
it("test precalculated solutions", function()
assert.are.same(
{{1,2,6,7,4,5,3},{8,9,11,12,13,15,10,16,14},{17,18,19,20,21}},
solution.findConnectedNets(
{{1,2,6,7},{1,2,4,5,6},{3,4,5,6,7},{2,3,4,5,6},{2,3,4,5,7},{1,2,3,4,6},{1,3,5,7},{8,9,11,12,13,15},{8,9,10,12,13,15,16},{9,10,11,12,16},{8,10,11,12},{8,9,10,11,12,13,15},{8,9,12,13,14,15},{13,14,15,16},{8,9,12,13,14,15,16},{9,10,14,15,16},{17,18,19,20,21},{17,18,19,20},{17,18,19,21},{17,18,20,21},{17,19,20,21}}
)
)
end)
end)