-5

The word is "racecar" and this is the triangle I'm supposed to get:-

   e
  cec 
 aceca
racecar

I'm using python 2.7 I'm trying to make it using for loops. Thank you. this is the output I am getting

e
e c c
e c a a c
e c a r r a c
e c a r r r r a c

ive found the mid value and tried printing it. But the output I am getting always has e at the beginning. I am not able to get the alphabets before e and after e to print.

m="racecar"
mid=len(m)/2

for i in range(0,5):
    for k in range(0,i):
        print m[mid-k],
    for j in range(i,0,-1):
        print m[mid-j],

    print

I have tried more but somehow I'm not getting the output in the proper order.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Pras.V
  • 11
  • 1
  • 4
    SO is not a code service! – ᴀʀᴍᴀɴ Nov 17 '16 at 12:25
  • 3
    I'm voting to close this question as off-topic because SO is not a coding service or homework completion service. – TigerhawkT3 Nov 17 '16 at 12:26
  • 4
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to show this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/help/how-to-ask). – Rory Daulton Nov 17 '16 at 12:27
  • what? do you mean? – Pras.V Nov 17 '16 at 12:29
  • I have tried a lot – Pras.V Nov 17 '16 at 12:29
  • 3
    Show us your code, describe the problems you're having completing the task. Show your result. If it's not what you expect, explain why you thought it should be different. We're a friendly place but we'll work with you, not for you. – Peter Wood Nov 17 '16 at 12:30
  • 2
    @Pras.V You may have tried a lot, but we don't see any proof of that. Give us some code that doesn't work and we can start from there.. – Ian Nov 17 '16 at 12:30
  • Just as a head start. For content as `my_word = "racecar"`, `my_word[2:5]` will return `'cec'` – Moinuddin Quadri Nov 17 '16 at 12:32
  • 2
    @MoinuddinQuadri tahnks for the hint I got it to work. – Pras.V Nov 17 '16 at 12:58

1 Answers1

0

I recommend to KISS it. With some Jedi Tricks you can got it there:

def isItAPalindrome( wordPalindrome ):

    wordLengh = len( wordPalindrome )

    if wordLengh % 2 != 0 and wordLengh:

        return True

    return False

import sys
wordPalindrome = "racecar"

if not isItAPalindrome( wordPalindrome ):

    sys.exit()

middlePoint = len( wordPalindrome )/2

for currentRange in range( 0, middlePoint + 1 ):

    # print empty spaces
    for k in range( 0, middlePoint - currentRange ):

        # print without new line
        sys.stdout.write( " " )

    # print the Palindrome "racecar"
    for k in range( middlePoint - currentRange, middlePoint + currentRange + 1 ):

        sys.stdout.write( wordPalindrome[ k ] )

    # print new line
    sys.stdout.write( "\n" )

Output example:

   e
  cec
 aceca
racecar

     f
    efe
   defed
  cdefedc
 bcdefedcb
abcdefedcba

References:

  1. How do I abort the execution of a Python script?
  2. Python: avoid new line with print command
Community
  • 1
  • 1
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144