1

I'm working on some of the old Google Code Jam problems as practice in python since we don't use this language at my school. Here is the current problem I am working on that is supposed to just reverse the order of a string by word.

Here is my code:

import sys


f = open("B-small-practice.in", 'r')


T = int(f.readline().strip())


array = []

for i in range(T):
    array.append(f.readline().strip().split(' '))


for ar in array:
    if len(ar) > 1:
        count = len(ar) - 1
        while count > -1:
            print ar[count]
            count -= 1

The problem is that instead of printing:

test a is this

My code prints:

test
a
is
this

Please let me know how to format my loop so that it prints all in one line. Also, I've had a bit of a learning curve when it comes to reading input from a .txt file and manipulating it, so if you have any advice on different methods to do so for such problems, it would be much appreciated!

Kyle H
  • 921
  • 1
  • 8
  • 20
  • related: [Efficiently reverse the order of the words (not characters) in an array of characters](http://stackoverflow.com/q/47402/4279) – jfs Mar 11 '14 at 02:29

3 Answers3

2

print by default appends a newline. If you don't want that behavior, tack a comma on the end.

    while count > -1:
        print ar[count],
        count -= 1

Note that a far easier method than yours to reverse a list is just to specify a step of -1 to a slice. (and join it into a string)

print ' '.join(array[::-1]) #this replaces your entire "for ar in array" loop 
roippi
  • 25,533
  • 4
  • 48
  • 73
1

There are multiple ways you can alter your output format. Here are a few:


One way is by adding a comma at the end of your print statement:

print ar[count],

This makes print output a space at the end instead of a newline.


Another way is by building up a string, then printing it after the loop:

reversed_words = ''
for ar in array:
    if len(ar) > 1:
        count = len(ar) - 1
        while count > -1:
            reversed_words += ar[count] + ' '
            count -= 1
print reversed_words

A third way is by rethinking your approach, and using some useful built-in Python functions and features to simplify your solution:

for ar in array:
    print ' '.join(ar[::-1])

(See str.join and slicing).


Finally, you asked about reading from a file. You're already doing that just fine, but you forgot to close the file descriptor after you were done reading, using f.close(). Even better, with Python 2.5+, you can use the with keyword:

array = list()
with open(filename) as f:
    T = int(f.readline().strip())
    for i in range(T):
        array.append(f.readline().strip().split(' '))

# The rest of your code here

This will close the file descriptor f automatically after the with block, even if something goes wrong while reading the file.

Community
  • 1
  • 1
George Bahij
  • 597
  • 2
  • 9
0
T = int(raw_input())

for t in range(T):
    print "Case #%d:" % (t+1),
    print ' '.join([w for w in raw_input().split(' ')][::-1])
Aziz Alfoudari
  • 5,193
  • 7
  • 37
  • 53