Solved

How do I mirror text in Python?

Posted on 2011-02-22
7
1,763 Views
Last Modified: 2012-05-11
Mirroring the string 'COMPUTER' should result in the string 'COMPPMOC'.
Mirroring the string 'PYTHON' should result in 'PYTTYP'.

How do you do this in python??
0
Comment
Question by:mille109
7 Comments
 
LVL 6

Expert Comment

by:xRalf
ID: 34956040
Hello mille109,

you can try this.
from math import floor
import sys
s = sys.argv[1]

middle = int(floor(len(s)/2))
for i in range(middle):
        print s[i],

if len(s) % 2 == 1:
        print s[i+1],
for i in range(middle):
        print s[-i + middle - 1],

Open in new window

0
 

Author Comment

by:mille109
ID: 34956303
how would you do this and be able to take text as input parameter??
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 34958400
Here is your function

def mirror(s):
    s = s[:len(s)//2]                # take the first half of the string
    return s + ''.join(reversed(s))  # concatenate with the reversed string
    
    
print mirror('COMPUTER')
print mirror('PYTHON')

Open in new window


The len(s) returns the length of the content (here of the string). The // operators is the division that returns integer. The s[:n] is the slice syntax for geting the substring with characters from index 0 to n (n excluded).

The reversed(s) takes any sequence (here the string of characters) and returns reversed iterator. The .join() joins the iterated sequence by its given string -- here the empty one.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:zaghaghi
ID: 35038302
Hi,
I recommend pepr solution, it's clear and correct.
0
 
LVL 6

Expert Comment

by:xRalf
ID: 35038630
>> how would you do this and be able to take text as input parameter??

If you write the code into the file mirror.py, you can use the program as follows:
python mirror.py computer
python mirror.py python

I agree that pepr's solution is clear and nice.
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 36515794
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
passing lists 9 42
regex to extract ip:john 17 58
linux crontab output 3 41
Path to Python 9 48
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now