Multiplication table

This is the place for queries that don't fit in any of the other categories.

Multiplication table

Postby guscer » Tue Dec 03, 2013 3:45 pm

Hey guys, i'm new to Python and i have a homework about print out a multiplication table according to this http://math2.org/math/general/multiplytable.htm, please help me do it in simplest way. Thank you
Last edited by stranac on Tue Dec 03, 2013 9:25 pm, edited 1 time in total.
Reason: Corrected title from "Multiplication cable"
guscer
 
Posts: 4
Joined: Sat Nov 16, 2013 10:03 am

Re: Multiplication table

Postby stranac » Tue Dec 03, 2013 4:36 pm

This is a pretty simple way:
Code: Select all
>>> import lxml.html
>>> print '\n'.join([''.join(['{:>4}'.format(val.strip()) for val in row.xpath('./td//text()') if val.strip()]) for row in lxml.html.parse('http://math2.org/math/general/multiplytable.htm').xpath('//table[2]/tr')])
   x   0   1   2   3   4   5   6   7   8   9  10  11  12
   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   1   0   1   2   3   4   5   6   7   8   9  10  11  12
   2   0   2   4   6   8  10  12  14  16  18  20  22  24
   3   0   3   6   9  12  15  18  21  24  27  30  33  36
   4   0   4   8  12  16  20  24  28  32  36  40  44  48
   5   0   5  10  15  20  25  30  35  40  45  50  55  60
   6   0   6  12  18  24  30  36  42  48  54  60  66  72
   7   0   7  14  21  28  35  42  49  56  63  70  77  84
   8   0   8  16  24  32  40  48  56  64  72  80  88  96
   9   0   9  18  27  36  45  54  63  72  81  90  99 108
  10   0  10  20  30  40  50  60  70  80  90 100 110 120
  11   0  11  22  33  44  55  66  77  88  99 110 121 132
  12   0  12  24  36  48  60  72  84  96 108 120 132 144


If you don't like this solution, and are looking for something different, maybe you should show us what you have tried?
Last edited by stranac on Tue Dec 03, 2013 9:25 pm, edited 3 times in total.
Reason: Updated with superior code
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Multiplication table

Postby Marbelous » Tue Dec 03, 2013 9:11 pm

I don't understand what that link is supposed to mean. It's just a picture of a multiplication table. What are the requirements for the Python program that's supposed to create it? Does it need to be in a nice table form? Do you need to be able to enter in how big a table to generate?

You were honest enough to indicate this is schoolwork so although we would like to help you we don't give out complete code. Tell us the assignment details and post any kind of first attempt you have made on it yourself. Don't be shy if you haven't gotten very far, just post it! Also, when you post code it is important to use CODE TAGS to preserve the indentation so important in Python. Simply select the code and press the little Code button above the text window.

@ stranac: A little :evil: but a lot :lol:

EDIT: I just saw your other post so I guess I didn't need to mention the code tags. I also thought of a small hint to pass on... Nested For Loops. ;)
Marbelous
 
Posts: 146
Joined: Fri May 31, 2013 8:12 pm

Re: Multiplication table

Postby micseydel » Tue Dec 03, 2013 9:40 pm

Just wanna point out that str.join() will happily take a generator comprehension...
(Thinking about this though, I now wonder if it does anything to guess the size of a generator.)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Multiplication table

Postby stranac » Tue Dec 03, 2013 10:29 pm

It creates a list from the generator expression.
It must loop it twice: once to determine how much memory to allocate, and then to actually copy the stuff.

That's why using str.join on an generator expression is slower than using it on a list comprehensions, even though using lazy generation usually results in better performance.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Multiplication table

Postby micseydel » Tue Dec 03, 2013 10:30 pm

Ah, that makes sense, thanks stranac!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Multiplication table

Postby guscer » Wed Dec 04, 2013 3:21 am

My programming program at school based on http://interactivepython.org/runestone/ ... Intro.html. And i just finished lecture: " String".
guscer
 
Posts: 4
Joined: Sat Nov 16, 2013 10:03 am

Re: Multiplication table

Postby ochichinyezaboombwa » Wed Dec 04, 2013 4:43 am

Let's have a "like" button here. I would "like" at least two posts on this page:-).
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Multiplication table

Postby Kebap » Wed Dec 04, 2013 11:52 am

ochichinyezaboombwa: While we can't easily update the PHP workings of the forum software, we have a thing close enough whenever you want to show you like something here: Post of the week!
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: mdovey and 5 guests