-5

Possible Duplicate:
Pyramid of asterisks program in Python

I've written a program in C++ that displays a pyramid of asterisk (see below) and now I'd like to see how it's done in Python but it's not as easy as I'd thought it would be. :) Has anyone tried this and if so could you show me code that would help out? 2) Within those lines the number of "*" will appear as an ODD number (1 ,3 ,5 ,7 ,9 )

this is the output

   *  1
 ***  3
***** 5
Community
  • 1
  • 1
mohd izat
  • 1
  • 1
  • 1

3 Answers3

8

Disclaimer: This may not work in Python 3.x:

Python 2.7.1 (r271:86832, May 27 2011, 21:41:45) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print '''   *  1
...  ***  3
... ***** 5'''
   *  1
 ***  3
***** 5
navid
  • 566
  • 6
  • 15
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
2

It's not that difficult..

>>> lower = 1
>>> higher = 19
>>> for i in xrange(lower,higher,2):
...     print ' ' * [Calculation Here] + '*' * i
... 
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • 1
    Given that this is for a coding interview, asking others to pass it for you should be an indicator that you're not ready for this job. I originally posted the answer because I thought it was just a fun thing. – Josh Smeaton Jun 03 '11 at 11:09
  • Within those lines the number of "*" will appear as an ODD number (1 ,3 ,5 ,7 ,9 ). can we? – mohd izat Jun 03 '11 at 11:09
  • i know, but i m willing to learn python language..thanks bro.. – mohd izat Jun 03 '11 at 11:11
  • 1
    @Johnsy, I didn't realise this was for an interview, so I've modified the answer slightly. – Josh Smeaton Jun 03 '11 at 11:11
  • 2
    Willing to learn is one thing. But you must LEARN. Show an example of your working so far, and then ask for help, not the answer. If you DO get the job, and they find out your skill is much less than they thought given your interview, they may come to the conclusion you cheated and fire you anyway. That means you should attempt the interview as you would attempt a real project. – Josh Smeaton Jun 03 '11 at 11:12
  • @mohd izat: Have you tried counting the asterisks on each line, dividing by two and comparing the remainder with one? – johnsyweb Jun 03 '11 at 11:14
  • def pyramid(rows=8): pyramid_width = rows + 2 for asterisks in range(1, pyramid_width, 2): print("{0:^{1}}".format("*" * asterisks, pyramid_width)) – mohd izat Jun 03 '11 at 11:17
  • that the example work that i had try.. – mohd izat Jun 03 '11 at 11:18
  • That looks a lot like this answer: http://stackoverflow.com/questions/4911341/pyramid-of-asterisks-program-in-python/4913048#4913048 – johnsyweb Jun 03 '11 at 11:21
  • 1
    @john it's exactly the same. very unhappy with this person. – Josh Smeaton Jun 03 '11 at 11:24
1
import pprint

def get_vals(mVal):
   return map(lambda x: ' ' * (mVal - x - 1) + ('*' * x) + ' %i' % x, xrange(1, mVal, 2))

pprint.pprint(get_vals(12))
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52