#๐Ÿ”’ Leetcode errors running code

110 messages ยท Page 1 of 1 (latest)

thick barn
#

Got an issue where I don't know how to fix it, the code works on another site.

s = "leeetcode"

def fun(s):
    result = ""
    i = 0
    while i < len(s):
        if i + 2 < len(s) and s[i] == s[i+1] == s[i+2]:
            result += s[i] * 2  # Only keep two of them
            i += 3  # Skip all three (we already took two)
        else:
            result += s[i]
            i += 1
    print(result)

fun(s)
NameError: name 'Solution' is not defined
          ^^^^^^^^
    ret = Solution().makeFancyString(param_1)
Line 41 in _driver (Solution.py)
    _driver()
Line 56 in <module> (Solution.py)
hollow jayBOT
#

@thick barn

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.

mossy bison
#

I think you've deleted the boilerplate code needed by leetcode

thick barn
#

I see, didn't know.

mossy bison
#
class Solution:
    def nameOfProblem(self, nums: List[int], target: int) -> List[int]:
    ... Your code here ...
        
#

For example two sum is

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        
thick barn
#
class Solution:
    def makeFancyString(self, s: str) -> str:
        s = "leeetcode"

def fun(s):
    result = ""
    i = 0
    while i < len(s):
        if i + 2 < len(s) and s[i] == s[i+1] == s[i+2]:
            result += s[i] * 2  # Only keep two of them
            i += 3  # Skip all three (we already took two)
        else:
            result += s[i]
            i += 1
    print(result)

fun(s)
#

i dont even know how to structure this to get my code to work

mossy bison
#

your def fun(s) and it's body needs to be indented so that it is inside of the solution class

thick barn
mossy bison
#

because right now you're saying:

here is the class named solution, within that solution there is makeFancyString.
here is the function fun
run the function fun

thick barn
#

im trying with chatGPT now

mossy bison
#

what you want it to say is

here is the class named solution, within that solution is makeFancy string and also fun

#

so just indent the def fun() and it's contents to be inside fo the class and make sure you're calling it

thick barn
#

its not working

#

i tried

#

various indentations

mossy bison
#

let's have a look

thick barn
mossy bison
#

You need to indent fun(s)

#

well actually, you need to put it instead of pass on line 5

#

so that you actually call the function

#

leetcode's backend will call the method makeFancyString and nothing else.

Since you're doing pass nothing happens. If you replace pass with fun(s) it will now call your fun method

thick barn
#

i never did that outside of leetcode

mossy bison
#

Yeah leetcode works a bit differently to what you might be used to

mossy bison
#

Just know that when you click run code, it calls makeFancyString so you need to make sure that any execution you want is in that function (instead of pass)

thick barn
#

im just super confused now and have no clue what to do

thick barn
mossy bison
#

Don't worry, you're almost there. Let's have another look to see where things might be going wrong

thick barn
mossy bison
#

perfect you've almost done it

#

you're currently printing the result. You need to return it instead (as you can tell by the -> str on line 4 (which means "this function returns a string"))

thick barn
#

case 1 and 3 work

#

thanks lol, that was such a struggle

mossy bison
#

If you're a beginner I would also really not recommend doing leetcode. It's quite tricky and only valuable once you truly understand the basics of a programming language

#

no problem at all

thick barn
#

im not sure where to find the right exercises for me as a complete beginner unfortunately

#

those on leetcode seem to be too difficult but at least they show me new functions classes methods

mossy bison
#

My suggestion would be to follow a youtube course and do your own mini projects (calculator app, quadratic equation solver, personal finance tracker app, fake ATM app) etc.

#

Also avoid getting AI to write the code as much as possible. There are studies that show clear evidence that people who just copy-paste from ChatGPT do not learn anything. Use it to help explain things but not to write the code

thick barn
#

i was thinking about doing something like that but I'd like to make it a program with an ui

mossy bison
#

That's my two pence :^)

thick barn
#

so like a real calculator executable app for windows

#

dont even know where to start with implementing ui elements

mossy bison
#

Hm, I think you'd be better off following a youtube tutorial to get a grasp of the basics otherwise you'll get engulfed by too much infromation

thick barn
#

im doing a lot of things in parallel

#

reading docs, reading a book, leetcode, and so on

mossy bison
#

IMO UI in Python is trash. I would personally suggest starting with web development if you're interested in visual programming.

#

That's a really good start tbh

#

Readings docs and books are good

#

leetcode I do not recommend

#

(for a beginner)

#

Youtube has some EXCELLENT courses

thick barn
#

hard to filter what I should actually watch

#

usually i find a video that goes through the absolute basics like "lets print a string" and it bores me to death right away

mossy bison
#

Valid. I would say if it has a lot of views and is highly rated, it'll be quite good. Also the longer ones are quite often better since they cover a lot

#

yeah real

thick barn
#

ill check it out

mossy bison
#

If you know the super basic stuff then maybe just doing mini projects is a good start

thick barn
#

im having trouble coming up with a proper project

mossy bison
#

my first ever script when I was like 13 was a quadratic equation solver. It was super easy conceptually but taught me a lot about data structures etc.

thick barn
#

i dont want to just do a function that calculates a number and then sorts the values or sth like that

#

but something useful instead

mossy bison
#

ahaha lol

thick barn
#

a mini-program

mossy bison
#

Well, I think you might be jumping the gun I'm afraid

#

You need to learn first before you can start doing super useful things. Otherwise you'll be overwhelmed

thick barn
#

no idea where i should be after a few days

mossy bison
#

days? My friend, it can take months

thick barn
#

no time for months, needs to be done in a few weeks

#

the basics and all that

mossy bison
#

Uh oh! That'll be tricky

thick barn
#

i wanna do leetcode problems by the end of next month

mossy bison
#

Ah, basics you can do in a few weeks

thick barn
#

and that'd be late

#

maybe code my own game by the end of the year or sth like that

#

(im kidding)

mossy bison
#

haha it's great to be ambitious but pls don't be disheartened when you get stuck

#

because it's something that can months and years so when you begin to struggle it's not because you're bad, it's because it is a difficult skill to learn

thick barn
#

or maybe it is because im bad

mossy bison
#

Not relative to other people with your experience

#

You don't compare with people who have years or months of experience, that would be silly

#

You compare with people who have the same amount of experience as you. You'll see, you're probably ahead of them :^)

thick barn
#

i wont quit that easy thats for sure

#

all good, thanks for the help! ๐Ÿ™‚

mossy bison
#

No problem at all. If you ever need anything this server is a pretty great place to ask questions

#

have a good one

thick barn
#

I'll ask in here because you seem pretty knowledgable

#

am I blind and oversee it in the description

#

For floating-point numbers, this truncates towards zero.

Or is it this statement right here that says it rounds down towards 0, instead of up towards 10

#

Which would be an odd way to describe it, a new one for me

#

(of course i could simply test it with .9 but i want to see it explained in the documentation)

mossy bison
#

I'm assuming you mean if you were to do with int(0.5) would it round up or down? If that's your question the answer is always down.

#

Those are not the official python docs btw

thick barn
#

no but they are easy to look up in

#

though it leaves me wanting for a better search still

thick barn
mossy bison
#

Well, there is a round() method

thick barn
#

wouldn't that be a logical thing to do because then you would not need to write another function or use a method to round up or down before doing the conversion

mossy bison
#

I gtg for dinner now but I'm sure there are others who can help out

thick barn
#

bon appetit

#

there's gonna be plenty and it isn't that important either

stone veldt
hollow jayBOT
#

math.ceil(x)```
Return the ceiling of *x*, the smallest integer greater than or equal to *x*. If *x* is not a float, delegates to [`x.__ceil__`](https://docs.python.org/3/reference/datamodel.html#object.__ceil__), which should return an [`Integral`](https://docs.python.org/3/library/numbers.html#numbers.Integral) value.
thick barn
#

just curiosity

#

so int(math.ceil(x)) basically

hollow jayBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.