Classes don't need to have an __init__() method.
This code is a generator comprehension. It creates a generator.
(x*N for x in range(1, 11))
Your solution does not return a list, it returns a generator. It is similar to doing this:
class Solution:
def getTable(self, N, count=10):
for i in range(1, count+1):
yield N * i
range() is likely the most commonly used Python generator, but since their introduction way back in Python 2.4, more and more standard libraries have moved away from returning lists and return generators instead. A generator is usually faster and more efficient because it only generates the values your program actually uses when they are requested. By comparison, a function that returns a list must create the entire list when it is first called. For a large list this may cause noticeable delay in your program.
But the instructions say that getTable() should return a list, and a generator is not a list. To print your output you must be doing something like this:
Output:
print(*Solution.getTable(9))
This would print:
Output:
9 18 27 36 45 54 63 72 81 90
The "*" unpacks the generator into 10 distinct integers, essentially calling the generator over an over until it runs out of values to return. It is very much like doing this:
for number in Solution().getTable(9):
print(number, end=" ")
I think you should modify your code to return a list, as requested in the instructions. This code uses a for loop to build the list.
class Solution:
def getTable(self, N, count=10):
table = []
for i in range(1, count+1):
table.append(i * N)
return table
print(Solution().getTable(9))
Output:
[9, 18, 27, 36, 45, 54, 63, 72, 81, 90]
You can see from the brackets in the output that getTable() now returns a list. If you want to get rid of the brackets you can use "*" to unpack the list.
print(*Solution().getTable(9))
Output:
9 18 27 36 45 54 63 72 81 90
Or you could convert the list to a string. This prints the list values separated by commas.
print(", ".join(map(str, Solution().getTable(9))))
Output:
9, 18, 27, 36, 45, 54, 63, 72, 81, 90
str_separator.join(str_list) is a method of class str, and joins together the strings in str_list separated by str_separator. The list returned by getTable() contains integers, not strings, so the map() function is used to call str() for each item in the list. Together the join() and map() functions are doing this:
table = Solution().getTable(9)
result_str = str(table[0])
for number in table[1:]:
result_str += (", " + str(number))
print(result_str)
When you learn more about the standard python libraries and how they can be used together you'll be amazed with what you can do with a few short lines of code.
Speaking of a few lines of code you can easily modify your code to return a list using a list comprehension in place of your generator comprehension. A list comprehension is a shorthand way of writing for loops that build lists. In addition to list comprehensions there are generator comprehensions like you use in your solution, dictionary comprehensions and set comprehensions. This code replaces the for loop and list.append() from my example with a comprehension
class Solution:
def getTable(self, N, count=10):
return [N * i for i in range(1, count+1)] # Only difference from your solution are the brackets
print(*Solution().getTable(9))