+ 1
what is happenning in the below code
code vizulation is not giving any better answer,here where is the input given and i understood back processes but i couldn't understand how the input is giving.Hope for the best and fast reply Thanking you all class Solution(object): def mergeAlternately(self, word1, word2): m = len(word1) n = len(word2) i = 0 j = 0 result = [] while i < m or j < n: if i < m: result += word1[i] i += 1 if j < n: result += word2[j] j += 1 return "".join(result)
5 Respuestas
+ 9
CHENNAMSETTY SUMANTH ,
to use the code as it is, we can give input values like this:
sol = Solution() # create an instance of the class `Solution`
print(sol.mergeAlternately('abc', 'xyz')) # now we can call the method `mergeAlternately()` and pass 2 strings to it. str1: `abc`, str2: `xyz`.
# since the method returns the merged string, print will output it. => `axbycz`.
+ 6
CHENNAMSETTY SUMANTH ,
the code is oop, so we create a class first. this is like a blueprint to create an instance of the class where we are working with.
btw: we don't need `object` as argument, this was required in python 2, but for python 3 it is not.
0
What is the purpose of the first line that
class Solution(Object):
0
Normally we do like class Solution:
That's it what is (object) means.
It is just I'm asking because on leetcode the inputs are taken without any other code than i mentioned above
Thanking you
0
CHENNAMSETTY SUMANTH ,
class Solution(object):
That first line says that the class you are creating inherits from the class called object.
object (not Object) is the built-in base class for all other classes.
I think that it was best practice in Python 2 to always explicitly (manually and visibly) inherit from object, but it is not needed in Python 3 because Python 3 made the behavior implicit (automatic and invisible), or something.
https://docs.python.org/3/library/functions.html#object
In my opinion, naming a class "object" was a horrible design choice, because it clashes with the existing industry term "object" that already means "instance of a class". It also makes it nearly impossible to find the search results you want, because the industry term is so much more common.
https://stackoverflow.com/questions/4015417/why-do-python-classes-inherit-object