I know that the following breaks up a list and put it into two lists depending on whether an index is even or odd. But not sure how [n::2] for n in [1,0] works.
[[1,2,3,4,5][n::2] for n in [1, 0] ]
[[2, 4], [1, 3, 5]]
Can you point me to post that explain and walk me through the logic behind it? Thank you.
The left part happens with each value of n. n indicates the starting position to take the 2nd element including the n'th one.
print([[1,2,3,4,5][n::2] for n in [1, 0] ] )
print([[1,2,3,4,5][1::2]]) # outputs [[2, 4]]
print([[1,2,3,4,5][0::2]]) # outputs [[1, 3, 5]]
print([[1,2,3,4,5][4::2]]) # outputs [[5]]
Related
I have a list of lists which I want to convert to a tensor and use that in my program. The tricky part being, the inner lists can have variable size. For instance consider the following list-
[[[1, 2, 3], [4, 5, 6]], [ [2, 3], [4, 6] ], [[0], [1] ]] .
So is there a way I can represent this as a tensor and use it in my program(without padding zeros and making lists of same size)? Also I want to pass this as a placeholder.
Why does prolog answer false to: member([5], [2, 5]).?
Is there a way to work around it?
Any help will be greatly appreciated.
Why does Prolog answer false to: member([5], [2, 5]).?
Let's ask Prolog why! Download library(diadem) into your working directory and:
?- use_module(diadem).
true.
?- member([5], [2,5]).? Expl.
Expl = member([_|_], [2, 5])
; ... .
Not only does it fail but also a generalization fails:instead of [5] that is a list with a single element 5, we have now simply at least one element - no matter which. So we can take the value for Expl as a query which still fails. Something in the remaining goal must thus be the culprit.
?- member([_|_], [2, 5]).
false.
Note also what was not generalized away: The two elements are still here! If they would be variables, the query would succeed! Generalize the query a bit:
?- member([5], [2, Any]).
Any = [5].
Now it succeeds!
As described in the SWI Prolog documentation for member/2:
member(?Elem, ?List )
True if Elem is a member of List.
The predicate is member. It is not subset or sublist or subsequence. It succeeds if the first argument is a member (that is, an element) of the list given in the second argument. The element [5] is not a member of the list [2, 5] since the element [5] isn't 2 and it isn't 5. However, [5] would be a member of the list [2, [5]], so member([5], [2, [5]]) would succeed.
It could be that you are looking for another predicate?
From the SWI Prolog documentation for subset/2:
subset( +SubSet, +Set )
True if all elements of SubSet belong to Set as well.
It works as expected:
Welcome to SWI-Prolog (threaded, 64 bits, version 7.5.5)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
?- subset([5], [2, 5]).
true.
?- subset([5, 3], [2, 3, 5, 7]).
true.
?- subset([5, 3], [2, 5]).
false.
Is there any standard algorithm that finds all possible paths in a directed a-cyclic graph.
If not, how can i make changes in BFS/Dijkstra/any other algorithm to enumerate all paths in a DAG
Finding all the possible paths in any graph in Exponential. It can be solved by using Backtracking.
For DAG's we can do it using Depth first search(DFS).
In DFS code, Start at any node, Go to the extreme dead end path and note down all the nodes visited in that path using some array or list. As soon as you find a dead end print the array containing the visited nodes and pop the last stored node and start in the other path of the (n-1)th node. If all the paths of the (n-1)th node are exhausted pop that node from list and start at (n-2)node. Do this untill you reach all the dead ends and reach the first node.
All the Printed paths are the Paths in the given DAG.
You can check the code http://pastebin.com/p6ciRJCU
Here is a short python example of a modified DFS to achieve this:
data = {1 : [2,3], # Directed acyclic graph adjacency list
2 : [3],
3 : [4,5],
4 : [5],
6 : [7,8]} # These nodes are disconnected from the rest of the graph
def dfs(data, path, paths):
datum = path[-1]
if datum in data:
for val in data[datum]:
new_path = path + [val]
paths = dfs(data, new_path, paths)
else:
paths += [path]
return paths
def enumerate_paths(graph):
nodes = list(graph.keys())
all_paths = []
for node in nodes:
node_paths = dfs(graph, [node], [])
all_paths += node_paths
return all_paths
Input:
enumerate_paths(data)
Output:
[[1, 2, 3, 4, 5], [1, 2, 3, 5], [1, 3, 4, 5], [1, 3, 5], [2, 3, 4, 5], [2, 3, 5], [3, 4, 5], [3, 5], [4, 5], [6, 7], [6, 8]]
My idea is to extends all path starting from inserting the first edge when there are no path candidates, then proceeding by extending each edge in the path sets at the head, at the tail, or splitting a path when the edge considered create a divergence (conflicting path).
It is an iterative method base on the idea of stability: each time all edges are considered, and if in a turn there were no action to do, then the turn is stable, and there is no more left to do.
One thing this method take care is to not fork path too soon: the first turn is a preparation turn, so the fork-phase is active only on the next turns.
I am evaluating if it is better (I mean: more correct) to alternate forks and extends phases, and considering stable_turn as stable couple of turns
Here the code:
https://gist.github.com/danielecr/6abd8ad48461347238ad1caf3714fe6a
(sorry, it is javascript, not really easy to read, but I need this exactly in this language)
In Prolog, I'm struggling to understand how to bind a list of lists to a variable. For instance, say I have the predicate makeList (which I don't know how to write), then I should be able to type:
makeList([[0, 0], [1, 0]]).
Now I want to refer to [[0, 0], [1, 0]] in another predicate with a variable, ListList, like:
predicateThatDoesSomething(ListList) :- write(ListList).
Expected Output:
[[0, 0], [1, 0]]
Obviously predicateThatDoesSomething() knows about the variable named ListList already. But how do I make the predicate makeList()? I want to be able to type makeList([[ANY, LIST, IN, HERE], [ANOTHER, LIST]]) and have that be ListList for example.
You have to write a predicate that merge the two list, thus this predicate must unify with something like:
makeList(List1, List2, [List1, List2]).
After defined this predicate in you knowled base, you can interrogate the prolog engine asking:
makeList([1,2],[2,4],D).
And you'll get: D = [[1, 2], [2, 4]]
To make the other predicate (defined in your KB)
predicateThatDoesSomething(ListList) :- write(ListList).
know the result (output parameter, the results of unification), you have to logically and the statements. So you have to ask:
makeList([1,2],[2,4],D) , predicateThatDoesSomething(D).
I have the following question for homework
Define a function append lists that
takes a list of lists and returns a
new list containing the sublist
values. For example, append lists([[1,
2], [3, 4], [5]]) should return the
list [1, 2, 3, 4, 5] and append
lists([[1, 2], [3], [[4, 5]]]) should
return the list [1, 2, 3, [4, 5]].
I've tried various ways of creating this function in order to append the list so it gives the desired output to no avail so I came here looking for some help. I've found a few other ways of going about this online, but they use extensive methods that we haven't even dabbled in as of yet in my CPSC 121 class. We're limited to the basics in what we've learned.
Any help would be much appreciated!
By now, it is likely that the assignment is gone, but here is a solution:
def append_lists(lists):
output = []
for l in lists:
for e in l:
output.append(e)
return output
This appends each element of each list to the output of the function, which eliminates exactly one level of nesting in the elements.