user392500
user392500

Reputation: 37

Python program to returns how far away letters are in a string

Im having some problems with this code i made. Here are the parameters

Given a word, find the minimum width of a "mop" required to erase each of the letters.

Example

For word = "abacaba", the output should be theJanitor(word) = [7, 5, 1, 0, 0, ..., 0, 0] (26 elements altogether).

def theJanitor(word):

    left = [0] * 26
    right = [0] * 26
    was = [0] * 26
    for i in range(26):
        left.append(0)
        right.append(0)
        was.append(False)

    for i in range(len(word)):
        c = ord(word[i]) - ord('a')
        if was[c]:
            left[c] = i
            was[c] = True
        right[c] = i

    ans = []
    for i in range(26):
        ans.append(right[i] - left[i] + 1 if was[i] else 0)
    return ans

Upvotes: 0

Views: 102

Answers (4)

yash
yash

Reputation: 1357

you can do it by using both rfind and find

import string
def janitor(word):
    alphabets = string.ascii_lowercase
    result = [0]*26
    for i in word:
        index = alphabets.find(i)
        if word.rfind(i) < 0:
            result[index] = 0
        else:
            result[index]=word.rfind(i) - word.find(i) + 1
    print(result)

if __name__ == '__main__':
    janitor('abacaba')

Output:

[7, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Upvotes: 0

atru
atru

Reputation: 4744

I recommend a bit different and cleaner approach then your original code:

def theJanitor(word):
    let = sorted(set(word))
    mop_len = [word.rfind(c) - word.find(c) + 1 for c in let]
    mop_len += [0]*(26-len(mop_len))

    return mop_len

Using set(word) the program finds all unique letters in each word sorted in alphabetical order. For each letter it computes the mop width from the first and last index of occurrence. In the last line the array with mop widths is padded with zeros to the target length of 26.

In case you want your letters appearing in order of occurrence use this instead of the set approach,

let = [x for i, x in enumerate(word) if x not in word[0:i]]

Upvotes: 0

user8651755
user8651755

Reputation:

Using word.find() as suggested by @blue_note seems like the easiest way to do this, but here's a "lower level" version in the spirit of the original code:

def janitor(word):
    left = [None] * 26
    right = [None] * 26
    widths = []

    for i, c in enumerate(word):
        k = ord(c) - ord('a')
        if left[k] is None:
            left[k] = i
        right[k] = i

    for k in range(26):
        if left[k] is None:
            width = 0
        else:
            width = right[k] - left[k] + 1
        widths.append(width)

    return widths

Upvotes: 0

blue note
blue note

Reputation: 29081

Find the first and last occurence of each letter

for letter in 'abc...':
    left_index = text.find(letter)
    right_index = text.rfind(letter)
    mop_size = right_index - left_index + 1

Upvotes: 1

Related Questions