Hey I am writing a function that takes a matrix input such as the one below and returns its inverse, where all the 1s are changed to 0s and all the 0s changed to 1s, while keeping the diagonal from top left to bottom right 0s.
An example input:
g1 = [[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 1, 0]]
the function should output this:
g1 = [[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 0, 0]]
When I run the program, it throws a "list index out of range" error. I'm sure this is because the loops I have set up are trying to access values that do not exist, but how do I allow an input of unknown row and column size? I only know how to do this with a single list, but a list of lists? Here is the function, not including the test function that calls it:
def inverse_graph(graph):
# take in graph
# change all zeros to ones and ones to zeros
r, c = 0, 0 # row, column equal zero
while (graph[r][c] == 0 or graph[r][c] == 1): # while the current row has a value.
while (graph[r][c] == 0 or graph[r][c] == 1): # while the current column has a value
if (graph[r][c] == 0):
graph[r][c] = 1
elif (graph[r][c] == 1):
graph[r][c] = 0
c+=1
c=0
r+=1
c=0
r=0
# sets diagonal to zeros
while (g1[r][c] == 0 or g1[r][c] == 1):
g1[r][c]=0
c+=1
r+=1
return graph
This doesn't directly answer your question, but I want to point out that in Python you can often reduce and sometimes eliminate the need to use indexing by using a
for <element> in <container>:
statement. By use it along with the built-in enumerate() function, it's possible to get both the index and the corresponding element
for <index>,<element> in enumerate(<container>):
Applying them to your problem would allow something like this:
g1 = [[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 1, 0]]
def inverse_graph(graph):
""" invert zeroes and ones in a square graph
but force diagonal elements to be zero
"""
for i,row in enumerate(graph):
for j,cell in enumerate(row):
row[j] = 0 if cell or i == j else 1
return graph
print(g1)
print(inverse_graph(g1))
Output:
[[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]]
[[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]
Which is simpler and clearly works. Another point is that since you're applying the function to a mutable (changeable) container, a list-of-lists, there's really no need to return the container because it is being changed in-place. It's not wrong to do so because it can make using the function easier, but it's something you may not have realized.
You could shorten the function a little bit more and eliminate indexing altogether by using something called a list comprehension:
def inverse_graph(graph):
return [[0 if cell or i == j else 1
for j,cell in enumerate(row)]
for i,row in enumerate(graph)]
Because of the way they work, this version doesn't change the graph in-place, but instead creates and returns a new one.
while (graph[r][c] == 0 or graph[r][c] == 1): # while the current row has a value.
You have to make sure first, that both indices exist, prior to comparing its -possible- value to 0 or 1. This causes your exceptions. To invert your matrix you would want to do something like
for row in graph:
for idx, v in enumerate (row):
row [idx] = 0 if v else 1
The mistake is in your "while the current row has a value". This will be always true while you will iterate through the elements in the row, and when you'll reach past them, you'll get the exception.
Instead, use:
for r in range(len(graph):
for c in range(len(graph[0]):
# do something with graph[r][c]
It is fairly simple.
Basically you need to find the number of elements in the array
mylist = [1,2,3,4,5]
len(mylist) # returns 5
#this gives the number of elements.
rows=len(g1) # get the number of rows
columns=len(g1[0]) #get the number of columns
#Now iterate over the number of rows and columns
for r in range(0, rows):
for c in range (0,columns):
if (r==c):
g1[r][c]=0
else:
g1[r][c]=1-g1[r][c]
Hope that helps
Not an answer to your question but here is a 'easy' way to do it
return [[0 if i2==i else 1 if item == 0 else 0 for i2,item in enumerate(row)] for i,row in graph]
Related
I need to consider a swimming pool "legitimate". For the given list, the function should return "illegitimate". However, my code returns "legitimate", even though I haven't done anything to the data.
This is the code that I tried and I was expecting/should return "illegitimate" before trying to modify the list.
pool = [[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 0, 0],
[0, 1, 0, 0, 0],
[0, 1, 0, 0, 0]]
def is_legitimate_pool(pool):
for r in range(len(pool)):
for l in range(len(pool[r])):
if pool[r][0] == 1 or pool[4][l] == 1:
return str("illegitimate")
elif pool[r][0] == 0 or pool[4][l] == 0:
return str("legitimate")
print(is_legitimate_pool(pool))
Solution
You could start off by checking if any element in the first and last sub-list is non-zero. Any non-zero integer i when passed to bool(i) will evaluate to True and only zero is "falsy" (see Truth Value Testing). This allows us to simply use the built-in any function for checking those two lists. If it returns True, at least one element is not zero.
Then we just iterate through the other sub-lists and check if their first or last element is falsy (i.e. zero). If at least one is not, we can immediately return. If we get to the end of the loop, that means the "pool is legitimate".
Code
LEGIT = "legitimate"
NOT_LEGIT = "illegitimate"
def is_legitimate_pool(pool: list[list[int]]) -> str:
if any(pool[0]) or any(pool[-1]):
return NOT_LEGIT
for row in pool[1:-1]:
if row[0] or row[-1]:
return NOT_LEGIT
return LEGIT
Test
test_pool1 = [
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 0, 0],
[0, 1, 0, 0, 0],
[0, 1, 0, 0, 0],
]
test_pool2 = [
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 1, 0, 0, 0],
[0, 1, 0, 0, 0],
]
test_pool3 = [
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
]
print(is_legitimate_pool(test_pool1)) # illegitimate
print(is_legitimate_pool(test_pool2)) # illegitimate
print(is_legitimate_pool(test_pool3)) # legitimate
Caveat
The assumption is of course, that we are only interested in the "borders of the pool" being 0 and that an element can only ever be 0 or 1. If you actually need to explicitly check for border elements being 1s, we'd have to be a little more strict:
def is_legitimate_pool(pool: list[list[int]]) -> str:
if any(element == 1 for element in pool[0] + pool[-1]):
return NOT_LEGIT
for row in pool[1:-1]:
if row[0] == 1 or row[-1] == 1:
return NOT_LEGIT
return LEGIT
Errors in your code
There are a number of problems with your original function. One of them is that you must not return, before you have checked each sub-list. You need to check each of them, but you have a statement returning "legitimate", if your elif-condition holds, which would interrupt the loop as soon as just one row satisfies that condition.
The second problem is that you have your indices all messed up. The expression if pool[r][0] == 1 or pool[4][l] == 1 is the equivalent of saying "if the zero-th element in row r or the l-th element in row 4 equals 1". So you the second part is only ever checking row 4. You should check row r in both cases, but for the 0-th and 4-th element in that row being 1, so something like this: if pool[r][0] == 1 or pool[r][4] == 1.
Finally, you are not accounting for the fact that the very first row and the very last row must not contain any 1 at all. You'd have to check that at some point (preferably before starting to loop).
Optimizations
Fixing those problems would make the function work correctly, but it would still be sub-optimal because you would only be able to work on 5x5-lists of lists since you hard-coded the index 4 in a row to mean the "last" element. If you instead use index -1 it will refer to the last element no matter how long the list is.
For the sake of readability, you should avoid "index-juggling" as much as possible and instead leverage the fact that lists are iterable and can therefore be used in for-loops that yield each element one after the other. That way we can explicitly name and work on each sub-list/row in pool, making the code much clearer to the reader/yourself.
str("legitimate") is a no-op on the string literal "legitimate". You don't need the str function.
You should avoid shadowing global names in a local namespace. That means, if you have a global variable named pool, you should not also have a locally scoped variable pool in your function. Change one or the other so they have distinct names.
Suppose that I have three binary numbers, we must Or the first number(x) and the second number(y) together and make the third number(z).
I have the third number, I also have some bits of the first and second number, now I need to get the number of possible states I can make for the first and second numbers with the lowest order.
for example
x = 0 _ (We only know the first bit and the first bit is unknown)
y = _ _ (We do not know any of its bits and we have to fill them in such a way that the condition of the problem is fulfilled)
z = 11
if we bitwise OR operation x and y we find z. The possible states for x , y are [(00,11),(01,10),(01,11)]
Note that the first bit of all states obtained for the number x is zero
What you have is essentially a product where at each position there are between 1 and 3 possibilities for the x and y bits in that position. You can exploit this by using itertools.product. First construct, for each position, a list of the possibilities for that position, and then form their product.
In the following code I am representing numbers as lists of bits, with the blanks being represented by -1:
from itertools import product
#states will be represented by int lists consisting of
#0,1, or -1. -1 will be the flag that the bit is undetermined
def find_states(x,y,z):
bit_pairs = []
for i,j,k in zip(x,y,z):
if k == 0:
if max(i,j) == 1: return [] #no valid states
bit_pairs.append([(0,0)])
else:
if 0 <= min(i,j):
if i == j == 0:
return [] #no valid states
else:
bit_pairs.append([(i,j)])
else: #a -1 is present
match (i,j):
case (-1,-1):
bit_pairs.append([(0,1),(1,0),(1,1)])
case (-1,0):
bit_pairs.append([(1,0)])
case (-1,1):
bit_pairs.append([(0,1),(1,1)])
case (0,-1):
bit_pairs.append([(0,1)])
case (1,-1):
bit_pairs.append([(1,0),(1,1)])
states = []
for p in product(*bit_pairs):
#first elements of the pairs form x, the second ones form y
states.append(([i for i,_ in p],[j for _,j in p]))
return states
For example:
for p in find_states([1, -1, -1, 0], [-1, 1, 0, 0], [1, 1, 0, 0]): print(p)
Output (each row is an x,y pair):
([1, 0, 0, 0], [0, 1, 0, 0])
([1, 1, 0, 0], [0, 1, 0, 0])
([1, 0, 0, 0], [1, 1, 0, 0])
([1, 1, 0, 0], [1, 1, 0, 0])
I have tried many things but still stuck. This is mostly because I'm failing to understand how python loops through an array or what options I have to loop through it.
I want to build an array: 4 columns, 18 rows.
I need the first column of each row to be a persons name in the person_list. Every 3 rows of my array I would then increment to the next person.
It would seem that my loop is not looping through the array the way I intended. It seems that it sets all values in the first column equal to the person_list value.
person_list = ["person1", "person2","person3","person4","person5","person6"]
my_array = [[0]*4]*len(person_list)*3
i=0
j=1
for row in my_array:
while j <= 3:
row[0] = person_list[i]
j+=1
j=1
i+=1
So the goal would be the resulting array
[
["person1", 0, 0, 0],
["person1", 0, 0, 0],
["person1", 0, 0, 0],
["person2", 0, 0, 0],
["person2", 0, 0, 0],
["person2", 0, 0, 0],
["person3", 0, 0, 0],
["person3", 0, 0, 0],
...
]
The problem seems to stem from the way my_array was initialized. Each row is 'nested' with the other rows, by that I mean if you try to modify my_array[0], you will end up modify all the remaining rows. Here is an example to illustrate that:
my_array[0].insert(0, 'hello_world')
# this prints:
# [['hello_world', 0, 0, 0],
['hello_world', 0, 0, 0],
....
['hello_world', 0, 0, 0]]
To get around, this you can use numpy to initialize your array of zeros or do something like this:
my_array = [[0, 0, 0, 0] for i in range(18)]
To make the code slight more concise and print the expected output, you can then do the following:
for i in range(len(person_list)):
# person1 : idx 0, 1, 2
# person2 : idx 3, 4, 5
# ...
end_idx = (i + 1) * 3
start_idx = end_idx - 3
for sub_arr in arr[start_idx:end_idx]:
sub_arr.insert(0, person_list[i])
Hope this helped!
You could use the modulo % to increment at certain intervals. For example, row%3==0 will return True at multiples of 3. FYI in your above code you were not resetting the row variable every interval of 3.
person_list = ["person1", "person2","person3","person4","person5","person6"]
my_array = [[0]*4]*len(person_list)*3
j=0
i=0
for row in my_array:
if j%3==0:
row[0] = person_list[i]
i+=1
j+=1 # increment the row here
I think everything is good in your code, you are just multiplying it by 3 :
person_list = ["person1", "person2","person3","person4","person5","person6"]
my_array = [[0]*4]*len(person_list)
i=0
j=1
for row in my_array:
while j <= 3:
row[0] = person_list[i]
j+=1
j=1
i+=1
I think its this like which needs to change from my_array = [[0]*4]*len(person_list)*3 to my_array = [[0]*4]*len(person_list)
So I think NeuralX was helpful in pointing out the way I create the array is not going to work for what I intend to do to it. He is absolutely correct when I run a change it will update every entry in the final array.
In more searching with working with nested lists, I have found how I should create the array so that I can update nested lists individually by looping through it. The final results are going to be below.
person_list = ["person1", "person2","person3","person4","person5","person6"]
# The way I create the array appears the be the main root cause here. I need to create it like this
my_array = [[0 for x in range(4)] for y in range(sum(person_assessments))]
i=0
j=0
for person in person_list:
for assessment in range(1, 4):
my_array[i][0] = person
# print(person, ", ", assessment)
# print(i,", ", j,", ", person)
i+=1
j += 1
print(my_array)
I am working on moving all zeroes to end of list. .. is this approach bad and computationally expensive?
a = [1, 2, 0, 0, 0, 3, 6]
temp = []
zeros = []
for i in range(len(a)):
if a[i] !=0:
temp.append(a[i])
else:
zeros.append(a[i])
print(temp+zeros)
My Program works but not sure if this is a good approach?
A sorted solution that avoids changing the order of the other elements is:
from operator import not_
sorted(a, key=not_)
or without an import:
sorted(a, key=lambda x: not x) # Or x == 0 for specific numeric test
By making the key a simple boolean, sorted splits it into things that are truthy followed by things that are falsy, and since it's a stable sort, the order of things within each category is the same as the original input.
This looks like a list. Could you just use sort?
a = [1, 2, 0, 0, 0, 3, 6]
a.sort(reverse=True)
a
[6, 3, 2, 1, 0, 0, 0]
To move all the zeroes to the end of the list while preserving the order of all the elements in one traversal, we can keep the count of all the non-zero elements from the beginning and swap it with the next element when a non-zero element is encountered after zeroes.
This can be explained as:
arr = [18, 0, 4, 0, 0, 6]
count = 0
for i in range(len(arr):
if arr[i] != 0:
arr[i], arr[count] = arr[count], arr[i]
count += 1
How the loop works:
when i = 0, arr[i] will be 18, so according to the code it will swap with itself, which doesn't make a difference, and count will be incremented by one. When i=1, it will have no affect as till now the list we have traversed is what we want(zero in the end). When i=4, arr[i]= 4 and arr[count(1)]= 0, so we swap them leaving the list as[18, 4, 0, 0, 0, 6] and count becomes 2 signifying two non-zero elements in the beginning. And then the loop continues.
You can try my solution if you like
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
for num in nums:
if num == 0:
nums.remove(num)
nums.append(num)
I have tried this code in leetcode & my submission got accepted using above code.
Nothing wrong with your approach, really depends on how you want to store the resulting values. Here is a way to do it using list.extend() and list.count() that preserves order of the non-zero elements and results in a single list.
a = [1, 2, 0, 0, 0, 3, 6]
result = [n for n in a if n != 0]
result.extend([0] * a.count(0))
print(result)
# [1, 2, 3, 6, 0, 0, 0]
You can try this
a = [1, 2, 0, 0, 0, 3, 6]
x=[i for i in a if i!=0]
y=[i for i in a if i==0]
x.extend(y)
print(x)
There's nothing wrong with your solution, and you should always pick a solution you understand over a 'clever' one you don't if you have to look after it.
Here's an alternative which never makes a new list and only passes through the list once. It will also preserve the order of the items. If that's not necessary the reverse sort solution is miles better.
def zeros_to_the_back(values):
zeros = 0
for value in values:
if value == 0:
zeros += 1
else:
yield value
yield from (0 for _ in range(zeros))
print(list(
zeros_to_the_back([1, 2, 0, 0, 0, 3, 6])
))
# [1, 2, 3, 6, 0, 0, 0]
This works using a generator which spits out answers one at a time. If we spot a good value we return it immediately, otherwise we just count the zeros and then return a bunch of them at the end.
yield from is Python 3 specific, so if you are using 2, just can replace this with a loop yielding zero over and over.
Numpy solution that preserves the order
import numpy as np
a = np.asarray([1, 2, 0, 0, 0, 3, 6])
# mask is a boolean array that is True where a is equal to 0
mask = (a == 0)
# Take the subset of the array that are zeros
zeros = a[mask]
# Take the subset of the array that are NOT zeros
temp = a[~mask]
# Join the arrays
joint_array = np.concatenate([temp, zeros])
I tried using sorted, which is similar to sort().
a = [1, 2, 0, 0, 0, 3, 6]
sorted(a,reverse=True)
ans:
[6, 3, 2, 1, 0, 0, 0]
from typing import List
def move(A:List[int]):
j=0 # track of nonzero elements
k=-1 # track of zeroes
size=len(A)
for i in range(size):
if A[i]!=0:
A[j]=A[i]
j+=1
elif A[i]==0:
A[k]=0
k-=1
since we have to keep the relative order. when you see nonzero element, place that nonzero into the index of jth.
first_nonzero=A[0] # j=0
second_nonzero=A[1] # j=1
third_nonzero=A[2] # j=2
With k we keep track of 0 elements. In python A[-1] refers to the last element of the array.
first_zero=A[-1] # k=-1
second_zero=A[-2] # k=-2
third_zero= A[-3] # k=-3
a = [4,6,0,6,0,7,0]
a = filter (lambda x : x!= 0, a) + [0]*a.count(0)
[4, 6, 6, 7, 0, 0, 0]
Thank you for taking the time to read this. I'm running into an issue where calling a list outside of the function returns empty values. What I want to do is to look at the document and if the words in that document are also in a predefined list (or not), make a list of 1s (and 0s). Next, I want to iterate through multiple documents and make a list of lists. I think the example below, in the code, will give more context to what I am trying to achieve.
Input:
import nltk
company_list = ["This is a company that does excavation",
"Last financial quarter was bad ",
"This year we are going be exceed the projected returns."]
middle_list = []
vector = []
final_list = []
bag = ["year", "excavation", "quarter", "returns"]
def test_function():
counter = 0
for company in company_list:
tokenize = nltk.word_tokenize(company)
# eliminate the duplicates
tokenize = set(tokenize)
# make all the words lower case
for word in tokenize:
word = word.lower()
middle_list.append(word)
for word in bag:
if word in middle_list:
x = 1
else:
x = 0
vector.append(x)
# clear the middle list so that a new company's words can be put inside an empty list
middle_list.clear()
counter += 1
print("Vector values: At", counter, vector)
final_list.append(vector)
print("List values: At", counter, final_list)
# clear the vector so that for each company it starts with an empty list
vector.clear()
return final_list
test_function()
print("list outside function: ", final_list)
Output:
Vector values: At 1 [0, 1, 0, 0]
List values: At 1 [[0, 1, 0, 0]]
Vector values: At 2 [0, 0, 1, 0]
List values: At 2 [[0, 0, 1, 0], [0, 0, 1, 0]]
Vector values: At 3 [1, 0, 0, 1]
List values: At 3 [[1, 0, 0, 1], [1, 0, 0, 1], [1, 0, 0, 1]]
list outside function: [[], [], []]
Expected result: [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 1]
As you can see there are 2 problems:
1) When I print the list inside the function, it returns a list of vectors, but the vectors are duplicates (which I do not want)
2) When I want to print the list outside the function it returns a list of 3 lists, but each of these lists is empty.
Thank you for your time and help!
I looked at your code and if you add a print just after vector.clear() I think you will see what is going on.
Final_list just contain references to vector, so when you clear this it will also clear the contents in your final_list.
change
final_list.append(vector)
to
final_list.append(vector.copy())