So in a program I am creating I have a list that contains tuples, and each tuple contains 3 numbers. For example...
my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1),...]
Now I want to delete any tuple whose last two numbers are less than any other tuple's last two numbers are.
The first number has to be the same to delete the tuple. *
So with the list of tuples above this would happen...
my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1),...]
# some code...
result = [(1, 2, 4), (2, 4, 1), (1, 5, 2)]
The first tuple is not deleted because (2 and 4) are not less than (4 and 1 -> 2 < 4 but 4 > 1), (1 and 5 -> 2 > 1), or (4 and 1 -> 2 < 4 but 4 > 1)
The second tuple is not deleted because its first number (2) is different than every other tuples first number.
The third tuple is not deleted for the same reason the first tuple is not deleted.
The fourth tuple is deleted because (4 and 1) is less than (5 and 2 -> 4 < 5 and 1 < 2)
I really need help because I am stuck in my program and I have no idea what to do. I'm not asking for a solution, but just some guidance as to how to even begin solving this. Thank you so much!
I think this might actually work. I just figured it out. Is this the best solution?
results = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1)]
for position in results:
for check in results:
if position[0] == check[0] and position[1] < check[1] and position[2] < check[2]:
results.remove(position)
Simple list comprehension to do this:
[i for i in l if not any([i[0]==j[0] and i[1]<j[1] and i[2]<j[2] for j in my_list])]
Your loop would work too, but be sure not to modify the list as you are iterating over it.
my_list = [(1, 2, 4), (2, 4, 1), (1, 5, 2), (1, 4, 1)]
results = []
for position in my_list:
for check in my_list:
if not (position[0] == check[0] and position[1] < check[1] and position[2] < check[2]):
results.append(position)
results
>[(1, 2, 4), (2, 4, 1), (1, 5, 2)]
Related
from given list of numbers
nums=[4,3,2,3,5,2,1]
from itertools import combinations
nums=[4,3,2,3,5,2,1]
li=[]
for i in range(1,len(nums)):
comb=combinations(nums,i)
for j in comb:
if sum(j)==5:
li.append(j)
print(li)
and output is
[(5,), (4, 1), (3, 2), (3, 2), (2, 3), (3, 2), (2, 2, 1)]
I am able to find the subsets but the elements seem to be repeated
so interested in non-repeating elements
I want the list of subsets that gives sum equal to 5
(without repetition)
example: [(5), (1, 4), (2,3), (2,3)]
If you change the loop slightly so that used numbers are removed from the list, they aren't reused in another sum, e. g.
i = 1
while i <= len(nums):
comb = combinations(nums, i)
for j in comb:
if sum(j) == 5:
li.append(j)
for n in j: nums.remove(n)
break
else: i += 1 # increment only if nothing found
Like the title said, my code is reading a 2D array entirely wrong.
const int WINNING_ROWS[8][3] = { (0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6) };
Above is my 2D array of numbers.
My program doesn't seem to be able to read it properly.
For example, if I were to ask for row 2, item 1, I would expect 7, it, however, instead gives me 6.
Here is a list of rows and item requests I have done to try and figure out what has gone wrong here.
row 0, item 0, expected outcome 0, actual outcome 2
row 3, item 2, expected outcome 6, actual outcome 0
row 1, item 0, expected outcome 3, actual outcome 6
row 5, item 1, expected outcome 5, actual outcome 0
row 8, item 2, expected outcome 6, actual outcome 13629648
row 7, item 2, expected outcome 6, actual outcome 0
for reference, here is the code I have been using to call the items from the array
cout << WINNING_ROWS[7][2] << endl;
Edit 1: ignore the item in bold, that was a mistake on my part when testing my code.
Edit 2: my question has been answered.
const int WINNING_ROWS[8][3] = { (0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6) };
That doesn't mean what you think it does. The (0,1,2) is not a row of three elements, but a single integer computed using the comma operator. 0,1,2 evaluates to 2.
You need to use the proper {...} braces instead of parenthesis, or leave them out completely.
Suggest also you change const to constexpr.
WINNING_ROWS[8][2] is out of the array bounds, which means it will cause undefined behavior. If you want to get the last element, you should try cout << WINNING_ROWS[7][2] << endl; since they are 0-indexed.
Suppose i have a list of tuples representing positions on a grid by their x and y values.
The tuples are defined as type Pos, a pair of Integer values.
The Board is further defined as a list of Pos.
type Pos = (Int, Int)
type Board = [Pos]
exampleBoard :: Board
exampleBoard = [(1, 1), (1, 2), (2, 2), (2, 3), (1, 3),
(5, 1), (5, 2), (4, 2), (4, 1), (5, 3),
(9, 10), (9, 11), (9, 12), (9, 13),
(10, 10), (10, 11), (10, 12), (10, 13),
(10, 20), (11, 20), (12, 20), (13, 20)]
The Board is a x*x grid, where you can always consider the known variables height and width having the same value. height = width = size
If the x or y value is not in a certain range (0<x<size || 0<y<size) I want the tuple removed.
Is there a simple way to filter the list as I describe? From searching for similar questions I have tried using library functions such as "break" and "span" to no success as of yet.
To test if a tuple (Int,Int) is in a given range, you can use the inRange function:
import Data.Ix (inRange)
inRange ((1,1),(10,10)) (5,5) -- True
inRange ((1,1),(10,10)) (11,6) -- False
filter (inRange ((1,1),(10,10))) [(5,5),(11,6)] -- [(5,5)]
with an input of n lists which may contain n elements each. i want to find all combinations which have at least one element of each input list.
example with 2 input lists of length 2
input 1: {1,2}
input 2: {3,4}
expected result:
{{1,3}, {1,4}, {2,3}, {2,4}, {1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}, {1,2,3,4}}
expandable to n input lists with n elements
Basically you can count binary from 1 to 2^n - 1. This for each set.
Then you select one number for each set, giving (2^n - 1)^n combinations.
The binary number tells you which elements to include (1) and which not(0).
In the above example (assuming that {1, 2, 3, 4} is indeed part of the expected result), you'll have 9 = 3^2 = (2^2 - 1)^2 combinations.
Because the answer didn't explain enough, a further explanation.
Let me stick to your example. We have
1 = 01b -> {2} for set1, {4} for set2
2 = 10b -> {1} for set1, {3} for set2
3 = 11b -> {1, 2} for set1, {3, 4} for set2
Because the 0 = 00b is omitted, it is guaranteed that at least one element from each set is chosen.
Now we pick a number for each set. That gives
(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2) and (3, 3).
If you substitute the first number with the corresponding set of elements from the first set and the second number with the corresponding elements from the second set, you'll have the desired result:
(1, 1) -> {2, 4}
(1, 2) -> {2, 3}
(1, 3) -> {2, 3, 4}
(2, 1) -> {1, 4}
(2, 2) -> {1, 3}
(2, 3) -> {1, 3, 4}
(3, 1) -> {1, 2, 4}
(3, 2) -> {1, 2, 3}
(3, 3) -> {1, 2, 3, 4}
Basically you just count within a 2^n - 1 number system. That has digits that run from 0 to 2^n - 2. So we have 00, 01, 02, 10, 11, 12, 20, 21, 22 which exactly corresponds to the list above, only that the digits are 1 smaller than the number they correspond to.
For example I need the list = [(0, 0), (0, 1), (0, 2), (0, 4), (1, 0), (1, 1), (1, 2), (1, 4)] to come out sorted by the sum of each tuple eg [(0, 0),(1, 0), (0, 1), (1, 1),(0, 2), (1, 2), (0, 4), (1, 4)].
The order of (1,1) vs (0,2) does not matter, however the tuples will be of varying but equal length.
And then chop the list down to only tuples which add up to 4 or less. eg
[(0, 0),(1, 0), (0, 1), (1, 1),(0, 2), (1, 2), (0, 4)]
The order of sorting and then chopping is not necessary but the outcome should be the same if done by chopping and then sorting.
Something like this should do the job:
list.sort(key=sum)
while sum(list[-1])>4:
list.pop()
Little late but for everybody reading this later.