Why isnt my output returning as expected?

2024/9/20 7:22:44

So I wrote this code

def diagsDownRight(M):n = len(M)m = [[''] * (n - i - 1) + row + [''] * i for i, row in enumerate(M)]return ([''.join(col) for col in zip(*m)]), [''.join(col[::-1]) for col in zip(*m)]
def diagsUpRight(M):n = len(M)m = [['']*i + row + ['']*(n-i-1) for i, row in enumerate(M)]return [''.join(col) for col in zip(*m)], [''.join(col[::-1]) for col in zip(*m)]def rows(M):return ["".join(row) for row in M], ["".join(reversed(row)) for row in M]
def cols(M):return ["".join(col) for col in zip(*M)], [''.join(col[::-1]) for col in zip(*M)]
def contains_word(grid: list[list[str]], w: str):if w in diagsUpRight(grid):return wif w in diagsDownRight(grid):return wif w in rows(grid):return wif w in cols(grid):return wprint(contains_word(grid=[
["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
, w='raw'))

For this code, I want to let contains_word return the word w if it is found in either rows(M), cols(M), diagsDownRight(M), diagsUpRight(grid)but when I put in the code as shown above, the output doesn't show up. What am I doing wrong here?

Edit I tried doing this and still the output returns None

def contains_word(grid: list[list[str]], w: str):for col in diagsUpRight(grid):if w in col:return wfor col in diagsDownRight(grid):if w in col:return wfor row in rows(grid):if w in row:return wfor col in cols(grid):if w in col:return wprint(contains_word(grid=[["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
, w='raw'))
Answer

Error:-

  • In your code every function returned tuples with 2 lists.
  • You iterated within the lists in your second code but the returned string is 'rawbit' so you need to iterate through 'rawbit' to get 'raw'.

Code:-

def diagsDownRight(M):n = len(M)m = [[''] * (n - i - 1) + row + [''] * i for i, row in enumerate(M)]return [''.join(col) for col in zip(*m)]+[''.join(col[::-1]) for col in zip(*m)]def diagsUpRight(M):n = len(M)m = [['']*i + row + ['']*(n-i-1) for i, row in enumerate(M)]return [''.join(col) for col in zip(*m)]+[''.join(col[::-1]) for col in zip(*m)]def rows(M):return ["".join(row) for row in M]+["".join(reversed(row)) for row in M]def cols(M):return ["".join(col) for col in zip(*M)]+ [''.join(col[::-1]) for col in zip(*M)]def contains_word(grid: list[list[str]], w: str):for words in diagsUpRight(grid):if w in words:return wfor words in diagsDownRight(grid):if w in words:return wfor words in rows(grid):if w in words:return wfor words in cols(grid):if w in words:return wprint(contains_word(grid=[["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
, w='raw'))
https://en.xdnf.cn/q/119355.html

Related Q&A

Django Stripe payment does not respond after clicking the Submit Payment button

I have an e-commerce application that Im working on. The app is currently hosted on Heroku free account. At the moment I can select a product, add it on the cart and can get up to the stripe form and t…

get file path using backslash (\) in windows in python [duplicate]

This question already has answers here:How can I put an actual backslash in a string literal (not use it for an escape sequence)?(4 answers)Closed 2 years ago.How to get result exactly the same format…

Printing progress bar on a console without the use of for -loop

I have a script written in python, where I have a statement:Process.open() //some parametersWhich executes a command and puts the output on the console ,where I do not know the time taken to execute t…

ModuleNotFoundError: No module named verovio

Hi there I would like to run my flask app in a container but I got stucked caused of a third party module. (I am using PyCharm)This is my docker file:FROM python:3-alpineMAINTAINER fooCOPY app /appWORK…

Python: TypeError: list object is not callable on global variable

I am currently in the process of programming a text-based adventure in Python as a learning exercise. I want "help" to be a global command, stored as values in a list, that can be called at (…

Python beautifulsoup how to get the line after href

I have this piece of html:<a href="http://francetv.fr/videos/alcaline_l_instant_,12163184.html" class="ss-titre">"Paris Combo" </a> <…

Scrapy empty output

I am trying to use Scrapy to extract data from page. But I get an empty output. What is the problem? spider: class Ratemds(scrapy.Spider):name = ratemdsallowed_domains = [ratemds.com]custom_settings =…

nested classes - how to use function from parent class?

If I have this situation:class Foo(object):def __init__(self):self.bar = Bar()def do_something(self):print doing somethingclass Bar(object):def __init(self):self.a = adef some_function(self):I want to …

CUDA Function Wont Execute For Loop on Python with Numba

Im trying to run a simple update loop of a simulation on the GPU. Basically there are a bunch of "creatures" represented by circles that in each update loop will move and then there will be a…

Implementing the Ceaser Cipher function through input in Python

Im trying to create a Ceaser Cipher function in Python that shifts letters based off the input you put in.plainText = input("Secret message: ") shift = int(input("Shift: "))def caes…