2

How could I create a Python function that takes

  • a string
  • an array like this [0, 0, ... 0] (one zero per letter of the alphabet)

and returns

  • a new array with with the zeros transformed to ones if their respective character appears in the word.

So if a word has an 'A' or 'a' and the first spot in the array corresponds to 'a', then the output array would have a 1 in its first spot:

[1, ...]

If a word has a 'B' or 'b', then the output array would have a 1 for its second spot. If a word has an 'a' and a 'b', then the output array would have a 1 in the first and second spots:

[1, 1, ...]

And so on. So the string "abba" would result in something like this:

[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Ideally, I would also be able to search for characters not in the alphabet, like ! and ?, too, and just add other bits to the array to represent those characters.

Any help would be welcome! Thanks a ton.

1
  • Are you asking for optimisation or just for any brute force way to do this. If optimisation, please show your brute force solution. Otherwise please help us estimate on which level you need help. Can you do a hello-world? Can you get the input? Can you do the output? Can you set up an array like the one to be filled? If any of that is yes, please show your code for it. Commented Dec 10, 2018 at 21:28

2 Answers 2

2

A very simple way to create such a list is:

def string_to_bit_array(text):
    # We don't care if upper or lower case
    text = text.lower()
    # Remove duplicate alphabet characters
    text = set(text)
    # Define alphabet characters
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    # Create list with zeros
    matches = [0] * len(alphabet)
    # Loop over every character of the text
    for character in text:
        # Skip this character if not in alphabet
        if not character in alphabet:
            continue
        # Find index of character in alphabet
        index = alphabet.find(character)
        # Set match index to one instead of zero
        matches[index] = 1
    # Return result
    return matches

print(string_to_bit_array("abba"))

This prints:

[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

You can just add further characters to alphabet if you need them:

alphabet = "abcdefghijklmnopqrstuvwxyz!?"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Much appreciated. And thank you for leaving comments. I'm just learning to code, so that made it much easier to follow.
2

Why not create a simple mapping dictionary

import string
alphabet=string.ascii_lowercase
d=dict(zip(alphabet,range(0,26)))
a=[0]*26

The dictionary will look like this

{'a': 0,
 'b': 1,
 'c': 2,
 'd': 3,
 'e': 4,
 'f': 5,
 'g': 6,
 'h': 7,
 'i': 8,
 'j': 9,
 'k': 10,
 'l': 11,
 'm': 12,
 'n': 13,
 'o': 14,
 'p': 15,
 'q': 16,
 'r': 17,
 's': 18,
 't': 19,
 'u': 20,
 'v': 21,
 'w': 22,
 'x': 23,
 'y': 24,
 'z': 25}

Logic for lookup and updating the list

for i in set('aabbc?'):
    index_to_update=d.get(i,None)
    if index_to_update is not None:
        a[index_to_update]=1
print(a)#[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

2 Comments

Thank you! I ultimately did not go with this option since I didn't want to import a new module.
@XaviPi It's okay. Python is full of modules to make your life easier and to make your code more readable. Enjoy coding:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.