I was wondering how to get the list of values of diagonals that do not pass through the center.
Let's say I have a nested List:
L = [[1,2,3],[4,5,6][7,8,9]]
How would I, say, get the diagonal [2,6]?
I'm not quite sure what you want, but this code gives you all the complete diagonals in each direction:
L = [[1,2,3],[4,5,6], [7,8,9]]
# number of rows, number of columns: ie L is m x n
m, n = len(L), len(L[0])
# Retreive the NE-SW (diag1) and NW-SE (diag2) diagonals
diag1 = []
diag2 = []
for p in range(m+n-1):
diag1.append([])
diag2.append([])
q1 = 0
if p >= n:
q1 = p - n + 1
q2 = m
if p < m-1:
q2 = p+1
for q in range(q1, q2):
x, y = p - q, q
diag1[-1].append(L[y][x])
# To get the other diagonal, read each row "backwards"
x = n - x - 1
diag2[-1].append(L[y][x])
print 'diag1:', diag1
print 'diag2:', diag2
That is:
diag1: [[1], [2, 4], [3, 5, 7], [6, 8], [9]]
diag2: [[3], [2, 6], [1, 5, 9], [4, 8], [7]]
How would you go about amending the code to accept non- m x n nested lists with the characteristic: len(L[i]) > len(L[i+1]).
For example, the following nested list:
[[1,2,3,4,5,6,7,8,9,10],
[11,12,13,14,15,16,17],
[18,19,20,21,22],
[23,24,25,26],
[27,28],
[29]]
should produce:
[[1],
[2,11],
[3,12,18],
[4,13,19,23],
[5,14,20,24,27],
[6,15,21,25,28,29],
[7,16,22,26],
[8,17],
[9],
[10]
Related
How may I convert a Tuple{Array{Float64,1},Array{Float64,1}} to Array{Tuple{Float64,Float64},1} ?
Code
#Sampling
function sam()
x = range(0, 10.0, length = 9) |> collect
y = range(0, 10.0, length = 9) |> collect
return (x,y)
end
xy = sam()
typeof(xy)
The code above returns this output:
Tuple{Array{Float64,1},Array{Float64,1}}
The easiest thing to do in your situation is to assign the output of your function to two separate variables, like this:
function foo()
x = [1, 2, 3]
y = [4, 5, 6]
return x, y
end
x, y = foo()
See the docs on multiple return values.
Then you can use zip to turn the vectors into an iterator of tuples:
julia> x, y = foo()
([1, 2, 3], [4, 5, 6])
julia> x
3-element Array{Int64,1}:
1
2
3
julia> y
3-element Array{Int64,1}:
4
5
6
julia> z = zip(x, y)
zip([1, 2, 3], [4, 5, 6])
Note that the output of zip is an iterator, rather than an array of tuples. You can either iterate through the elements of the iterator to get the individual tuples,
julia> foreach(println, z)
(1, 4)
(2, 5)
(3, 6)
or you can collect the iterator if you actually need an array of tuples:
julia> collect(z)
3-element Array{Tuple{Int64,Int64},1}:
(1, 4)
(2, 5)
(3, 6)
I have list of lists -
list_1 = [[2,2],[3,4],[4,5],[2,1]
i want to multiply the list with
x = 2
output will be:
list_1 =[[4,4],[6,8],[8,10],[4,2]]
thanks!
Try this one:
list_1 = [[2,2],[3,4],[4,5],[2,1]]
x = 2
[[elem*x for elem in l] for l in list_1]
>>>
[[4, 4], [6, 8], [8, 10], [4, 2]]
Okay. I write an algorithm for show me all the permutations of a list of integers. But during the algorithm I got a problem to append a permuted list to my result list.
The code is the heap's algorithm. I got my finished permutation when size == 1. So a can append the permutated list V to my final list res. Here's the code:
The function for permutate the list
def permutations(V, size):
global res
if size == 1:
print(V)
res.append(V)
for i in range(0, size):
permutations(V, size-1)
if size % 2 == 1:
V[size-1], V[0] = V[0], V[size-1]
else:
V[i], V[size-1] = V[size-1], V[i]
A = [1,2,3]
res = []
permutation(A, len(A))
print(res)
And this is the output:
[1, 2, 3]
[2, 1, 3]
[3, 1, 2]
[1, 3, 2]
[2, 3, 1]
[3, 2, 1]
res: [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
The printed permutations of V are all correct. But the list V append to my global res are not change. They are being append right after the print and the list append is different.
If you change the lines like this:
res.append(V)
|
|
v
D = [V[i] for i in range(len(V))]
res.append(D)
The results is correct on the final. Anyone can explain how can a printed list can be different from a appended list using the same variable.
Replace res.append(V) with res.append(list(V)) simply fixes your issue.
All V you appended to the res are references to the same object. This can be observed by printing the id of each element in the list:
for i in res:
print(id(i))
I'm new to Prolog and I'm having a bit of a hard time understanding how some of the mechanics actually works. Right now I'm trying to work on a particular problem.
I need to find all possible pairs from a single list and so I'm trying to define rules select_pairs(X,Y,_,Z).
Below is what I expect to see when I run the given queries.
The query: select_pairs(X,Y,[1,2,3],Z). returns the following:
X = 1, Y = 2, Zs = [3] ;
X = 1, Y = 3, Zs = [2] ;
X = 2, Y = 1, Zs = [3] ;
X = 2, Y = 3, Zs = [1] ;
X = 3, Y = 1, Zs = [2] ;
X = 3, Y = 2, Zs = [3]
AND the query select_pairs(1,2,Xs,[3]). returns the following:
Xs = [1, 2, 3] ;
Xs = [2, 1, 3] ;
Xs = [1, 3, 2] ;
Xs = [2, 3, 1] ;
Xs = [3, 1, 2] ;
Xs = [3, 2, 1] ;
As of right now, I can only get the first result from the first query to show up and nothing more. What's the best way from me to approach this? Thank you!
Your Prolog should come with select/3, a builtin that's doing exactly what's suggested by its name:
?- select(X,[1,2,3],R).
X = 1,
R = [2, 3] ;
X = 2,
R = [1, 3] ;
X = 3,
R = [1, 2] ;
false.
it also works 'backwards'
?- select(1,R,[2,3]).
R = [1, 2, 3] ;
R = [2, 1, 3] ;
R = [2, 3, 1] ;
false.
Then, to get a working select_pairs/4, you could just combine 2 select/3.
I'd like to get some help in the following exam problem, I have no idea how to do this:
Input: a list of numbers, eg.: [1,2,3,4]
Output: every possible correct bracketing. Eg.: (in case of input [1,2,3,4]):
((1 2) (3 4))
((1 (2 3)) 4)
(1 ((2 3) 4))
(1 (2 (3 4)))
(((1 2) 3) 4)
Bracketing here is like a method with two arguments, for example multiplication - then the output is the possible multiplication orders.
Your assignment could be seen as the inverse of 'computing the yield of a binary tree'.
You can code yield with 2 recursive calls and append/3:
yield((L, R), Y) :-
yield(L, Ly),
yield(R, Ry),
append(Ly, Ry, Y).
yield(T, [T]).
test:
?- yield(((1,2),(3,4)),Y).
Y = [1, 2, 3, 4] ;
Y = [1, 2, (3, 4)] ;
Y = [ (1, 2), 3, 4] ;
Y = [ (1, 2), (3, 4)] ;
Y = [ ((1, 2), 3, 4)].
Thus abstractly, yield/2 should solve your assignment, when called in this way:
?- yield(BinTree, [1,2,3,4]).
but, of course, that do not terminate. Clearly, the SLD resolution (Prolog computing algorithm) can't solve this problem without some help.
But if you recall that append/3 can generate all the alternatives left & right lists that compose the appended:
?- append(L,R,[1,2,3,4]).
L = [],
R = [1, 2, 3, 4] ;
L = [1],
R = [2, 3, 4] ;
L = [1, 2],
R = [3, 4] ;
L = [1, 2, 3],
R = [4] ;
L = [1, 2, 3, 4],
R = [] ;
false.
you can attempt to change the order of calls to get your solution.
Beware that you need sufficiently instantiated arguments before recursing, thus check the 'output' of append. You can test with
...
Yr = [_|_],
...
I also suggest to rename the predicate and change the order of arguments for clarity:
?- brackets([1,2,3,4],B).
B = 1* (2* (3*4)) ;
B = 1* (2*3*4) ;
B = 1*2* (3*4) ;
B = 1* (2*3)*4 ;
B = 1*2*3*4 ;
false.
This code works with SWI-Prolog (nextto/3).
You can explain to Prolog what you want, then ask him all the solutions :
bracket([A,B], [A,B]).
bracket(In, Out) :-
member(X, In),
nextto(X,Y, In),
append_3(A, [X,Y], B, In),
append_3(A, [[X,Y]], B, Temp),
bracket(Temp, Out).
append_3(A,B,C, Out) :-
append(A, B, Temp),
append(Temp, C, Out), !.
all_brackets(R) :-
setof(L, bracket([1,2,3,4], L), R).
you get
?- all_brackets(R), maplist(writeln, R).
[[[1,2],3],4]
[[1,2],[3,4]]
[[1,[2,3]],4]
[1,[[2,3],4]]
[1,[2,[3,4]]]
R = [[[[1,2],3],4],[[1,2],[3,4]],[[1,[2,3]],4],[1,[[2,3],4]],[[1,2],[3,4]],[1,[2,[3,4]]]].