#๐Ÿ”’ refactoring help

57 messages ยท Page 1 of 1 (latest)

velvet lotus
#

can someone help refactor this method, perhaps by using helper methods and such.

def assembleRecur(board, numBlocks):
if board != "":
newboard = validify([*board])
if not newboard:
return ""
presBlock = newboard.count("#")
if presBlock > numBlocks:
return ""
while newboard != board:
board = newboard
newboard = validify([*newboard])
if not newboard:
return ""
presBlock = newboard.count("#")
if presBlock > numBlocks:
return ""
if checkConnect(board):
return ""
if presBlock == numBlocks: return board
for ind in findSpace(board):
if ind > area - 1 - ind:
ab = board[:area - 1 - ind] + "#" + board[area - ind:ind] + "#" + board[ind + 1:]
elif ind < area - 1 - ind:
ab = board[:ind] + "#" + board[ind + 1:area - 1 - ind] + "#" + board[area - ind:]
else:
ab = board[:ind] + "#" + board[ind + 1:]
nB = assembleRecur(ab, numBlocks)
if nB:
return nB
return ""

pale gobletBOT
#

@velvet lotus

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

stable yacht
#

!code

pale gobletBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

velvet lotus
#
def assembleRecur(board, numBlocks):
    if board != "":
        newboard = validify([board])
        if not newboard:
            return ""
        presBlock = newboard.count("#")
        if presBlock > numBlocks:
            return ""
        while newboard != board:
            board = newboard
            newboard = validify([newboard])
            if not newboard:
                return ""
            presBlock = newboard.count("#")
            if presBlock > numBlocks:
                return ""
        if checkConnect(board):
            return ""
        if presBlock == numBlocks: return board
    for ind in findSpace(board):
        if ind > area - 1 - ind:
            ab = board[:area - 1 - ind] + "#" + board[area - ind:ind] + "#" + board[ind + 1:]
        elif ind < area - 1 - ind:
            ab = board[:ind] + "#" + board[ind + 1:area - 1 - ind] + "#" + board[area - ind:]
        else:
            ab = board[:ind] + "#" + board[ind + 1:]
        nB = assembleRecur(ab, numBlocks)
        if nB:
            return nB
    return ""
stable yacht
#

so much red

gray jasper
#

is board a str

#

i would add types to the parameter variables

#

but I come from other languages that are strongly typed, so it may be a bias

#

also, add comments

stable yacht
#

no that's valid thoughts. All functions should have typehints because you cannot infer the type really

gray jasper
#

yeah

stable yacht
#

can OP provide the relevant functions that are used inside this function? such as validify

#

a large chunk of the code looks to be in the form

f()
while cond:
    f()
#

which can be

while True:
    f()
    if not cond:
        break
#

adds one line, but can remove many duplicated lines

#

I don't think I can work on this anymore without the proper amount of information tho

velvet lotus
#

def validify(board):
    for lim in puzzIndexes:
        inds = set()
        counterer = 0
        for conts in lim:
            char = board[conts]
            if char == "#":
                if counterer == 2 or counterer == 1:
                    for ind in inds:
                        if board[ind] != "-" or board[area - 1 - ind] != "-":
                            if board[ind] != "#" or board[area - 1 - ind] != "#": return ""
                        board[ind] = "#"
                        board[area - 1 - ind] = "#"
                counterer = 0
                inds = set()
            elif lim[-1] == conts:
                if (counterer == 0 or counterer == 1):
                    for ind in inds:
                        if board[ind] != "-" or board[area - 1 - ind] != "-": return ""
                        board[ind] = "#"
                        board[area - 1 - ind] = "#"
            else:
                inds.add(conts)
                counterer += 1
    return "".join(board)

#

validify function^^

#

@stable yacht @gray jasper

stable yacht
#

basically need the whole code man

velvet lotus
#

is there anyway i can send that

#

@stable yacht

stable yacht
#

yerp

gray jasper
#

that's a lot

stable yacht
#

now gimme a couple hours to understand it

#
alphabet = 'abcdefghijklmnopqrstuvwyz#'

๐Ÿธ

#

# is my favourite letter tbh

gray jasper
#

yeah, is that an ending character? why not just use the list's length?

velvet lotus
#

is the symbol for a blocking square

#

in my crosswords

#

program

stable yacht
#

maybe don't name it alphabet then

gray jasper
#

Would separate that

stable yacht
#

still working on this, I'll try to ignore most issues

velvet lotus
#

here is some help

gray jasper
#

have a delimiter variable specifically for that

#

don't have 50+ things in the global space

stable yacht
#

I'm sorry but I can't even understand what's going on.

#

there is lots of mixing of style that makes it hard to follow

velvet lotus
#

wdym by that

stable yacht
#

for example this

while i < len(args):
    if '.txt' in args[i].lower():
        ...
    elif args[i][0].lower() == 'h' or args[i][0].lower() == 'v':
        ...
        (hWords.append((vVals, hVals, word)) if args[i][0].lower(
        ) == 'h' else vWords.append((vVals, hVals, word)))
        i += 1
    ...

the usage of the ternary a if cond else b is not in the style of the rest of that area of code

velvet lotus
#

ok let me send u more consistent code

stable yacht
#

I don't think you have time to fix up everything that I would comment on

velvet lotus
velvet lotus
#

*method

stable yacht
#

I need to undestand like 5 parts of your code to even understand that function

velvet lotus
#

oh ok

stable yacht
#

I would personally make changes to nearly every part of this code, which makes it overwhelming

velvet lotus
#

ok

gray jasper
#

the god of ok

stable yacht
#

ok sorry I can't continue with the code. When I paste it in vscode, pylance raises 295 issues. It's too much for me to handle

pale gobletBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.