How do I make a function read a row and a column with only zeros without using libraries [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
This post was edited and submitted for review 12 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm new to programming in general... I have to check all the rows and columns of an matrix and in case any of them are complete with zeros, return a True value. I made this code in a silly attempt but it doesn't work for the purpose of the question itself. The matrix will always be a square and a list of lists.
def determinanteEhNulo(matriz):
contador = 0
for i in matriz:
for j in range(len(matriz)):
if i[j] == 0:
contador += 1
if contador >= int(len(matriz)):
return True
return False

This seems to do what you need.
def determinanteEhNulo(matriz):
for i in matriz:
if all(x==0 for x in i):
return True
for i in zip(*matriz):
if all(x==0 for x in i):
return True
return False

count = 0
def determinanteEhNulo(matrix):
global count
for i in matrix:
if all([j ==0 for j in i]):
count += 1
if count == len(matrix):
return True
else:
return False

Related

How to get list of palindrome in text? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I have the following interview question that may require traversing through the entire string.
Problem I searched about find Problem and most people do it like this def palindrome(s): return s==s[::-1] But this task is diffrent?
palindrome is a word the can read from both directions like 'mam', 'dad'. Your task is to get a list of palindrome in a given string.
def palindrome(s):
stack = []
return ['aaa']
exampls
palindrome('aaa') #['aaa']
palindrome('abcbaaaa') #['abcba','aaaa']
palindrome('xrlgabccbaxrlg') #['abccba']
palindrome('abcde') #['']
Let's try to avoid checking all the possible combinations :). My idea is, start from the extremities and converge:
def palindrome(s):
out = [''] #we need the list to not be empty for the following check
for main_start in range(len(s) - 1):
for main_end in range(len(s) - 1, main_start, -1):
start = main_start
end = main_end
while (end - start) > 0:
if s[start] == s[end]: ##may be palindrome
start += 1
end -= 1
else:
break
else:
if s[main_start:main_end + 1] not in out[-1]: #ignore shorter ("inner") findings
out.append(s[main_start:main_end + 1])
return out[1:] #skip the dummy item

Function that returns even numbers from a list and halves them [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
So I am having trouble concatenating this, I am not allowed to use .append(), and right now Im getting the error 'int' object not iterable.
def halveEvens(l):
num = []
for n in l:
if n % 2 == 0:
num += (n // 2)
return num
print(halveEvens([10,21,32,42,55]))```
sum(x//2 for x in numbers if x%2 == 0)
is probably how I would do it
if you just want to collect them (without summing them)
generator (x//2 for x in numbers if x%2 == 0) would be evaluated as you iterate it
or list comprehension that is evaluated immediatly
[x//2 for x in numbers if x%2 == 0]

Where is the infite loop and how to fix it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a practice quiz question for a course I'm taking. Need to fix the code.
def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
while n % 2 == 0:
n = n / 2
# If after dividing by two the number is 1, it's a power of two
if n == 1:
return True
return False
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
for n = 0, each cycle n is 0, and it will run into next cycle
while n % 2 == 0:
n = n / 2

Is there a better pythonic way to rewrite the while loop? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I wrote this function to do the strStr function:
def strStr(haystack: str, needle: str) -> int:
if not needle:
return 0
len_h = len(haystack)
len_n = len(needle)
if len_n > len_h:
return -1
index_h = 0
index_n = 0
matched_count = 0
while index_h < len_h and index_n < len_n:
v_h = haystack[index_h]
v_n = needle[index_n]
if v_h != v_n:
index_h = index_h - matched_count + 1
index_n = 0
matched_count = 0
else:
index_h += 1
index_n += 1
matched_count += 1
if index_n == len_n:
return (index_h - len_n)
return -1
print(strStr('hello', 'll'))
print(strStr('mississippi', 'issip'))
The while loop is the C or Java way. I tried to use for ... in range in Python. But failed to do it, because I need to update the indexes based on conditions. The for loop in Python doesn't seem to work. Still, is there a python way to replace the while loop?
Python has a string method .find() that's implemented using the Boyer-Moore method (https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm).
This contains a couple of while loops. So I would say it's a reasonably pythonic way to do it.

recursive function that goes through a built in list of numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
need help with this question. Much appreciated.
Write a recursive function big_numbers that takes a Python built-in list of numbers and returns True if each number in the list is greater than 100 and False otherwise.
def isGreater(myList, i=0):
if(i == len(myList)): # Termination True case
return True
if myList[i] > 100:
return isGreater(myList, i+1)
else:
return False # Termination False case
Demonstration:
test = [101, 129, 130]
print(isGreater(test))
True

Categories