Python Forum
Try to solve GTG multiplication table problem.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try to solve GTG multiplication table problem.
#1
Hello,
I try to solve a problem from GTG.

Multiplication Table Print the multiplication table of a given number N.
Input:
N = 9

Output:
9 18 27 36 45 54 63 72 81 90

Explanation:
The table of 9 is the output whose 1st
term is 9 and the 10th term is 90.

There's my understanding with an attemp of a solution:

class Solution:
    def getTable(self,N):
       
       self.N = N

    def myfunc(self):
  
     s = range(11)
     for x in s:
      return x * self.N
      
      print(self.N)
or

class Solution:
    def getTable(self,N):
       
       self.N = N

    def myfunc(self):
  
     s = range(11)
     for x in s:
      print(x * self.N)

     p1 = Solution(9)  
     
     p1.myfunc()  
Any insights would be appreciated
Thank you
Reply
#2
Hi @Frankduc ,
Why do you make a class for this? And does it work? Do you get error messages? If so, show them. Is the result not as expected?
Please think about these questions and let us know.
I am not giving a solution because I think you want to learn. So I help you best by asking these questions.
Reply
#3
Hello breeden,

You can find the problem on https://practice.geeksforgeeks.org/probl...able0303/1

It starts with
#User function Template for python3
class Solution:
    def getTable(self,N):
Than you got to fill the rest by adding your code. It start with a Class Solution and use def and self.

Runtime Error
Traceback (most recent call last):
File "/home/02c18d795a7f598ff92099653af722fc.py", line 31, in <module>
for i in ans:
TypeError: 'NoneType' object is not iterable

Dont know what it means.

The first solution i provided is my answer in GTG compiler. The second solution is a test i did with an online compiler. The test return no errors and no output.

In C# it be simple but Python is still out of my grasp.
Reply
#4
Your classes are missing the__init__()method.
Reply
#5
ok problem solve

class Solution:
    def getTable(self,N):
 
     return (x*N for x in range(1, 11))
       
i didn't think doing that was possible.
Reply
#6
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))
Reply
#7
(Jan-05-2022, 10:13 PM)deanhystad Wrote: 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))

Sorry for late reply i dont get notice in email box if someone reply to the thread. There must be a way to change that.

Very interesting stuff.

2 questions!

1) To print you need the class first and then the def? Like print(*Solution().getTable(9)) otherwise nothing comes up.

2) Do you have to return a list with Return or can you return something else? Lets say you have for i in range[10]. Can you return (i)?
In the exercise in GFG you dont have to print by using return you can see the output in their compiler. But return never print? Its just returning a function called. Thats all. You can connect it (not the right choice of words) to another def function to return a output.

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can someone help me solve this programming problem? SuchUmami 6 882 Nov-20-2023, 10:01 AM
Last Post: EdwardMatthew
  A simple problem, how best to solve it? SuchUmami 2 714 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,219 Jun-14-2022, 08:35 PM
Last Post: thesquid
  Multiplication Table code alexsendlegames100 3 1,359 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  How do I solve the second problem? Cranberry 1 1,120 May-16-2022, 11:56 AM
Last Post: Larz60+
  Sudoku Solver, please help to solve a problem. AdithyaR 5 2,092 Oct-28-2021, 03:15 PM
Last Post: deanhystad
  Need help with my Python code (Multiplication) NeedHelpPython 2 1,676 Oct-04-2021, 12:09 PM
Last Post: Pedroski55
  General list size question to solve problem Milfredo 3 2,346 Sep-27-2020, 08:42 AM
Last Post: Milfredo
  I want to solve the following problem srisrinu 4 5,927 May-09-2020, 01:07 PM
Last Post: Larz60+
  Solve Pynput Problem when changing language? ppel123 0 2,261 Feb-19-2020, 03:38 PM
Last Post: ppel123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020