# Converting a set into a string

The input is a set of alphabets.

The output is a string of 32 in length.

For any letter in the set if it is preceded by a '¬' we replace the n-th character in our output string with '0', such that n is the position of the letter in the alphabets. If it is not preceded by a '¬' and was there in the set then we replace it with '1', And finally if it wasn't there in the set then we replace it with 'b'.

So for an input like: {'¬g10','d13','ae6','f3','¬aa5','¬bg28','a2','¬af3'}

The output is: "1bb1b10bbbbbbbbbbbbbbbbbbb0bbb10"

Note that 'd' is the fourth letter and do not have a '¬' before it so we wrote '1' in the fourth character.

Note also that we count 'aa' as the 27th letter and 'ae' as the 31th letter.

For the numbers do not mind them. Treat them like they're not exist!

I came up with this solution to this problem:

set = {'¬g10','d13','ae6','f3','¬aa5','¬bg28','a2','¬af3'}
delemiter = 'false'
result = 'b'*32
result = list(result)
set = str(set)
i = 0
while set[i] != '}':
if set[i] == '¬':
delemiter = 'true'
elif set[i] == ',':
delemiter = 'false'
elif set[i+1] >= 'a' and set[i+1] <= 'z' and set[i] >= 'a' and set[i] <= 'z':
i = i+1
if delemiter == 'true':
if set[i] == 'a':
result[26] = '0'
elif set[i] == 'b':
result[27] = '0'
elif set[i] == 'c':
result[28] = '0'
elif set[i] == 'd':
result[29] = '0'
elif set[i] == 'e':
result[30] = '0'
elif set[i] == 'f':
result[31] = '0'
elif delemiter == 'false':
if set[i] == 'a':
result[26] = '1'
elif set[i] == 'b':
result[27] = '1'
elif set[i] == 'c':
result[28] = '1'
elif set[i] == 'd':
result[29] = '1'
elif set[i] == 'e':
result[30] = '1'
elif set[i] == 'f':
result[31] = '1'

elif set[i] >= 'a' and set[i] <= 'z':
if delemiter == 'true':
if set[i] == 'a':
result[0] = '0'
elif set[i] == 'b':
result[1] = '0'
elif set[i] == 'c':
result[2] = '0'
elif set[i] == 'd':
result[3] = '0'
elif set[i] == 'e':
result[4] = '0'
elif set[i] == 'f':
result[5] = '0'
elif set[i] == 'g':
result[6] = '0'
elif set[i] == 'h':
result[7] = '0'
elif set[i] == 'i':
result[8] = '0'
elif set[i] == 'j':
result[9] = '0'
elif set[i] == 'k':
result[10] = '0'
elif set[i] == 'l':
result[11] = '0'
elif set[i] == 'm':
result[12] = '0'
elif set[i] == 'n':
result[13] = '0'
elif set[i] == 'o':
result[14] = '0'
elif set[i] == 'p':
result[15] = '0'
elif set[i] == 'q':
result[16] = '0'
elif set[i] == 'r':
result[17] = '0'
elif set[i] == 's':
result[18] = '0'
elif set[i] == 't':
result[19] = '0'
elif set[i] == 'u':
result[20] = '0'
elif set[i] == 'v':
result[21] = '0'
elif set[i] == 'w':
result[22] = '0'
elif set[i] == 'x':
result[23] = '0'
elif set[i] == 'y':
result[24] = '0'
elif set[i] == 'z':
result[25] = '0'
elif delemiter == 'false':
if set[i] == 'a':
result[0] = '1'
elif set[i] == 'b':
result[1] = '1'
elif set[i] == 'c':
result[2] = '1'
elif set[i] == 'd':
result[3] = '1'
elif set[i] == 'e':
result[4] = '1'
elif set[i] == 'f':
result[5] = '1'
elif set[i] == 'g':
result[6] = '1'
elif set[i] == 'h':
result[7] = '1'
elif set[i] == 'i':
result[8] = '1'
elif set[i] == 'j':
result[9] = '1'
elif set[i] == 'k':
result[10] = '1'
elif set[i] == 'l':
result[11] = '1'
elif set[i] == 'm':
result[12] = '1'
elif set[i] == 'n':
result[13] = '1'
elif set[i] == 'o':
result[14] = '1'
elif set[i] == 'p':
result[15] = '1'
elif set[i] == 'q':
result[16] = '1'
elif set[i] == 'r':
result[17] = '1'
elif set[i] == 's':
result[18] = '1'
elif set[i] == 't':
result[19] = '1'
elif set[i] == 'u':
result[20] = '1'
elif set[i] == 'v':
result[21] = '1'
elif set[i] == 'w':
result[22] = '1'
elif set[i] == 'x':
result[23] = '1'
elif set[i] == 'y':
result[24] = '1'
elif set[i] == 'z':
result[25] = '1'
i += 1
result = ''.join(result)
print(result)


I am asking for a review to this code. Thank you

• This question has been mentioned on Meta. Sep 4, 2019 at 1:09
• Downvoted, because several comments on your deleted older question asked about the context and motivation for this code, and those requests remain unsatisfied. Sep 4, 2019 at 1:11

I've factored out the logic for transforming the cell-like string to a number, for example a7 -> 1, z8 -> 26, aa12 > 27 and so on. I've also factored out the logic to replace a character if it falls within the length of the target string. Finally I've parametrised the length of the input string. This approach makes your code more readable and easier to test and to maintain.

import string

def str_to_num(col):
"""
Converts the character part of a cell-like string to number.
For example, for col='aa3', it returns 27.
"""
num = 0
for c in col:
if c in string.ascii_letters:
num = num * 26 + ord(c.upper()) - ord('A') + 1
return num

def replace_char(txt, pos, char):
"""Replaces character in txt at position pos with char"""
result = list(txt)
if pos < len(txt):
result[pos] = char
return ''.join(result)

def func(elems, length=32):
"""
Takes an iterable elems and a length and returns a string of that length
with all b's, except of characters at indices that are including in elems
in the form of cell-like notation. For elements in elems that start with
a '¬' it replaces a 'b' with a '0', for the rest it replaces a 'b' with
an '1'.
"""
output = 'b' * length
for elem in elems:
pos = str_to_num(elem)
if elem.startswith('¬'):
elem = elem.split('¬')[1]
output = replace_char(output, pos-1, '0')
else:
output = replace_char(output, pos-1, '1')
return output


Example usage:

>> input_set = {'¬g10', 'd13', 'ae6', 'f3', '¬aa5', '¬bg28', 'a2', '¬af3'}
>> result = func(input_set, length=32)
>> print(result)
1bb1b10bbbbbbbbbbbbbbbbbbb0bbb10


DRY Code
The code is very long when it doesn't need to be, and it is very repetitive.

There is a programming principle called the Don't Repeat Yourself Principle sometimes referred to as DRY code. If you find yourself repeating the same code multiple times it is better to encapsulate it in a function. If it is possible to loop through the code that can reduce repetition as well.

You can use a data structure as an array to contain the alphabet. If the current letter in the array then if delimiter is false, otherwise return true. If the current letter is not in the alphabet move on to the next letter.