Katas in Python (II)

Continuing with the kata series started here, let’s solve the following problem in Python:

Problem

You have to write 3 functions to reverse a string in 3 different forms (one function to each one):

  • Reversing the string characters: "this is text" -> "txet si siht"
  • Reversing words (not characters) in the string: "this is text" -> "text is this"
  • Reversing characters in every word of the string: "this is text" -> "siht si txet"

Each function must receive the original string and return a new reversed string. But you can’t use reversing predefined functions of your favourite language. Use only pure code, without facilities.

Solution

from sys import argv

script, text = argv

def printArray(array):
    output = ""
    for item in array:
        output = output + item
    return output

def function_1(text):
    size = len(text)
    result = [None] * size
    i = size - 1
    for letter in text:
        result[i] = letter
        i = i - 1
    return result

def function_2(text):
    words = text.rsplit(" ")
    size = len(words)
    result = [None] * size
    i = size - 1
    for word in words:
        result[i] = word
        result.insert(i," ")
        i = i - 1
    return result

def function_3(text):
    words = text.rsplit(" ")
    size = len(words)
    result = []
    i = size - 1
    for word in words:
        result.append(printArray(function_1(word)))
        result.append(" ")
        i = i - 1
    return result

print "First specification: reverse string -> %r " % (printArray(function_1(text)))
print "Second specification: reverse string -> %r " % (printArray(function_2(text)))
print "Third specification: reverse string -> %r " % (printArray(function_3(text)))

function_1 just reverses the string manually, character by character; function_2 does the same but firstly reading the words of the sentence and reverting word by word. Finally function_3 reads the sentence, and reverses the letters in every word using function_1. Quite simple. I know, I know, the function names are not good ones but it’s an exercise with three parts so I identified part 1, part 2 and part3; so please take it easy😉

Other interesting solutions

Almost all the solutions I found follow the same approach. The idea is to reverse the string iterating over letters or words depending on the case. A simpler code but still readable is the following:

def reverseStringCharacters(inputString):
final = ""
for letter in inputString:
    final = letter + final
return final.strip()

def reverseWords(inputString):
final = ""
for word in inputString.split():
    final = word + " " + final
return final.strip()

def reverseWordsCharacters(inputString):
final = ""
for word in inputString.split():
    final = final + " " + reverseStringCharacters(word)
return final.strip()

I’d rather this last solution because pre-concatenates the string instead of iterating on a list from the last item backwards to the beginning, which I think it’s less understandable for humans.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s