Related
I want to write a predicate in Prolog, which creates a all possible segments of a given Size of a given List and returns the un-selected elements as a List.
My Code so far:
select_seg(List, Segment, Rest, Size ):-
select_seg(List, Segment, Rest, Size, Size).
select_seg(_,_,_,_, 0):- !.
select_seg(List, [Head_Segs|Tail_Segs],[Head_Rest|Tail_Rest], Size,Acc ):-
select(Head_Segs, List, Head_Rest),
Acc >= 0,
New_Acc is Acc - 1,
select_seg(Head_Rest, Tail_Segs, Tail_Rest, Size, New_Acc).
When I call this predicate with:
select_seg([1,2,3,4,5,6,7,8,9], Seg, R ,3 ).
It returns:
Seg = [1, 2, 3|_],
R = [[2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9]|_] ;
Seg = [1, 2, 4|_],
R = [[2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9], [3, 5, 6, 7, 8, 9]|_] ;
Seg = [1, 2, 5|_],
R = [[2, 3, 4, 5, 6, 7, 8, 9], [3, 4, 5, 6, 7, 8, 9], [3, 4, 6, 7, 8, 9]|_] ;
This is desired output, except that the list of remaining elements contain three lists for each element in the Segment the List of remaining elements, but should only contain the last one as following:
Seg = [1, 2, 3|_],
R = [4, 5, 6, 7, 8, 9]|_] ;
Seg = [1, 2, 4|_],
R = [3, 5, 6, 7, 8, 9]|_] ;
Seg = [1, 2, 5|_],
R = [3, 4, 6, 7, 8, 9]|_] ;
I tried everything, but I am not able to come up with the right solution.
It's combining select with a variant of select:
select_len_seg(Len, L, Seg, Rem) :-
length(Seg, Len),
select_len_seg_(Seg, L, L, Rem).
select_len_seg_([], _, R, R).
select_len_seg_([H|T], F, R, Rem) :-
% Preventing "duplicates" such as [3,2,1]
select_forward(H, F, F0),
select(H, R, R0),
select_len_seg_(T, F0, R0, Rem).
select_forward(E, [H|T], F) :-
select_forward_(T, H, E, F).
select_forward_(T, H, H, T).
select_forward_([H|T], _, E, F) :-
select_forward_(T, H, E, F).
Results in swi-prolog:
?- select_len_seg(3, [1,2,3,4,5,6,7,8,9], S, R).
S = [1, 2, 3],
R = [4, 5, 6, 7, 8, 9] ;
S = [1, 2, 4],
R = [3, 5, 6, 7, 8, 9] ;
...
S = [6, 7, 9],
R = [1, 2, 3, 4, 5, 8] ;
S = [6, 8, 9],
R = [1, 2, 3, 4, 5, 7] ;
S = [7, 8, 9],
R = [1, 2, 3, 4, 5, 6] ;
false.
I have a nested list called a:
a = [[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]
My desired output is a new list (b) containing the first list in a: [0, 1, 2, 3, 4]. Then I want to append to this list all of the values in [10, 11, 12, 13, 14] added to the last number in [0, 1, 2, 3, 4]:
b = [0, 1, 2, 3, 4, 14, 15, 16, 17, 18]
# Your First List
a =[[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]
# Your Second List which is initalized as empty.
b = []
# Adding first list from (a) which is a[0] into second list
# a[0] = [0, 1, 2, 3, 4]
# a[1] = [10, 11, 12, 13, 14]
b.extend(a[0])
# b = [0, 1, 2, 3, 4]
# I then want to append to this list all of the values in [10, 11, 12, 13, 14] added
# the last number in [0, 1, 2, 3, 4].
# a[0] = [0, 1, 2, 3, 4]
# a[0][-1] = 4
last_val = a[0][-1]
second_list = a[1] # [10, 11, 12, 13, 14]
for item in second_list:
b.append(item+last_val)
print(b)
# b = [0, 1, 2, 3, 4, 14, 15, 16, 17, 18]
I have a 2D square list of size n. My purpose is to run through it with a shape of a snake. For example:
array = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
For this case result should be [1, 2, 3, 6, 9, 8, 7, 4, 5].
def spiral_array(arr):
if arr == []: return []
up = 0; left = 0
down = len(arr)-1
right = len(arr[0])-1
direct = 0 # 0: go right 1: go down 2: go left 3: go up
res = []
while True:
if direct == 0:
for i in range(left, right+1):
res.append(arr[up][i])
up += 1
if direct == 1:
for i in range(up, down+1):
res.append(arr[i][right])
right -= 1
if direct == 2:
for i in range(right, left-1, -1):
res.append(arr[down][i])
down -= 1
if direct == 3:
for i in range(down, up-1, -1):
res.append(arr[i][left])
left += 1
if up > down or left > right: return res
direct = (direct+1) % 4
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(spiral_array(array)) # [1, 2, 3, 6, 9, 8, 7, 4, 5]
source: https://leetcode.com/problems/spiral-matrix/
import random
s = []
for i in range(14):
s.append(random.randint(0,5))
print(s)
how i just print last line in a list? Because Python print me s like this:
[3]
[3, 0]
[3, 0, 3]
[3, 0, 3, 0]
[3, 0, 3, 0, 2]
[3, 0, 3, 0, 2, 3]
[3, 0, 3, 0, 2, 3, 3]
[3, 0, 3, 0, 2, 3, 3, 0]
[3, 0, 3, 0, 2, 3, 3, 0, 5]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5, 5]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5, 5, 1]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5, 5, 1, 4] <- this is the only line I want Python print me..
Please help me.
And another question...
How i change this program:
for i in s:
if i < 1:
print ("It's all good.")
else:
print("Not good")
that he will print me "Not good" when at least one of numbers in list is 0?
You are almost certainly doing this:
import random
s = []
for i in range(14):
s.append(random.randint(0,5))
print(s)
Instead, change it to this:
import random
s = []
for i in range(14):
s.append(random.randint(0,5))
print(s)
In Python, indentation is significant, so if you have your print statement indented inside your for loop, it will be called each time through the loop. If you have it indented at the same level as the for ... it will only execute once after the loop has finished completely.
As to the second question regarding:
for i in s:
if i < 1:
print ("It's all good.")
else:
print("Not good")
I am not sure where you want to place the logic you are looking for, but you can test if "at least one of numbers in list is 0" with:
if 0 in s:
print("Not good")
You should have this check outside of your for loop though, because this check need only be performed once per list, rather than once per value in the list.
Take list foo
foo = [5, 6, 7, 8, 9, 10, 11, 12]
How can I reverse only the elements of indices x to y within the list?
For example:
x = 1
y = 5
# reverse foo[x:y]
foo = [5, 9, 8, 7, 6, 10, 11, 12]
Python allows you to assign to slices
Slicings may be used as expressions or as targets in assignment or del
statements
thus it's possible to do everything on one line:
foo[x:y] = foo[y-1:x-1:-1]
Note thatfoo[y-1:x-1:-1] has the same meaning as foo[x:y][::-1].
It's as simple as:
foo[x:y] = foo[y - 1:x - 1:-1]
For example:
>>> foo = [5, 6, 7, 8, 9, 10, 11, 12]
>>> foo[1:5] = foo[4:0:-1]
>>> foo
[5, 9, 8, 7, 6, 10, 11, 12]
def reverse(l, x, y):
l[x:y+1] = l[y:x-1:-1]
foo = [5, 6, 7, 8, 9, 10, 11, 12]
x = 1
y = 4
reverse(foo, x, y)
print(foo) # [5, 9, 8, 7, 6, 10, 11, 12]