Converting a set into a stringWhat is the better solution to this?Converting an object into a JSON stringCoderbyte SimpleSymbols challenge in JavascriptFinding the first non-repeating character in a stringCheck a string for any occurrences of certain character classesFind the first unique character in a stringMethod to replace all spaces in a String with '%20'Reverse an array without affecting special charactersTransform String a into bDetect whether a string has a letter 'b' 3 characters after 'a'
Why do gliders have bungee cords in the control systems and what do they do? Are they on all control surfaces? What about ultralights?
Anatomically Correct Whomping Willow
You have 3 cakes. Everytime you eat one, there's 17% chance the number of cakes is reset to 3. Find average number of cakes eaten?
Is gzip atomic?
Is there any music source code for sound chips?
Do they have Supervillain(s)?
If all stars rotate, why was there a theory developed that requires non-rotating stars?
How do you harvest carrots in creative mode?
Was it ever possible to target a zone?
Is “I am getting married with my sister” ambiguous?
Why would an IIS hosted site prompt for AD account credential if accessed through a hostname or IP, but not through servername?
Converting a set into a string
What would be the challenges to taking off and landing a typical passenger jet at FL300?
How to make Ubuntu support single display 5120x1440 resolution?
Justifying the use of directed energy weapons
Why did this happen to Thanos's ships at the end of "Avengers: Endgame"?
Is there any method of inflicting the incapacitated condition and no other condition?
What to say to a student who has failed?
Prove your innocence
I can see my two means are different. What information can a t test add?
Nothing like a good ol' game of ModTen
What is a CirKle Word™?
French abbreviation for comparing two items ("vs")
Are there any elected officials in the U.S. who are not legislators, judges, or constitutional officers?
Converting a set into a string
What is the better solution to this?Converting an object into a JSON stringCoderbyte SimpleSymbols challenge in JavascriptFinding the first non-repeating character in a stringCheck a string for any occurrences of certain character classesFind the first unique character in a stringMethod to replace all spaces in a String with '%20'Reverse an array without affecting special charactersTransform String a into bDetect whether a string has a letter 'b' 3 characters after 'a'
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
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
python python-3.x strings converting
$endgroup$
add a comment |
$begingroup$
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
python python-3.x strings converting
$endgroup$
1
$begingroup$
Possible duplicate of What is the better solution to this?
$endgroup$
– pacmaninbw
7 hours ago
add a comment |
$begingroup$
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
python python-3.x strings converting
$endgroup$
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
python python-3.x strings converting
python python-3.x strings converting
edited 8 hours ago
dfhwze
9,3051 gold badge19 silver badges57 bronze badges
9,3051 gold badge19 silver badges57 bronze badges
asked 8 hours ago
BubbalooBubbaloo
214 bronze badges
214 bronze badges
1
$begingroup$
Possible duplicate of What is the better solution to this?
$endgroup$
– pacmaninbw
7 hours ago
add a comment |
1
$begingroup$
Possible duplicate of What is the better solution to this?
$endgroup$
– pacmaninbw
7 hours ago
1
1
$begingroup$
Possible duplicate of What is the better solution to this?
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
Possible duplicate of What is the better solution to this?
$endgroup$
– pacmaninbw
7 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
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
$endgroup$
1
$begingroup$
This is an alternate solution without any observations about the users code. This answer may be removed if no observations about the posters code are made. Please see the code review guidelines for answers codereview.stackexchange.com/help/how-to-answer.
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
Good point @pacmaninbw. I added some comments and docstrings to the functions.
$endgroup$
– Nikos Oikou
6 hours ago
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226699%2fconverting-a-set-into-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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
$endgroup$
1
$begingroup$
This is an alternate solution without any observations about the users code. This answer may be removed if no observations about the posters code are made. Please see the code review guidelines for answers codereview.stackexchange.com/help/how-to-answer.
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
Good point @pacmaninbw. I added some comments and docstrings to the functions.
$endgroup$
– Nikos Oikou
6 hours ago
add a comment |
$begingroup$
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
$endgroup$
1
$begingroup$
This is an alternate solution without any observations about the users code. This answer may be removed if no observations about the posters code are made. Please see the code review guidelines for answers codereview.stackexchange.com/help/how-to-answer.
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
Good point @pacmaninbw. I added some comments and docstrings to the functions.
$endgroup$
– Nikos Oikou
6 hours ago
add a comment |
$begingroup$
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
$endgroup$
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
edited 6 hours ago
answered 7 hours ago
Nikos OikouNikos Oikou
3414 bronze badges
3414 bronze badges
1
$begingroup$
This is an alternate solution without any observations about the users code. This answer may be removed if no observations about the posters code are made. Please see the code review guidelines for answers codereview.stackexchange.com/help/how-to-answer.
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
Good point @pacmaninbw. I added some comments and docstrings to the functions.
$endgroup$
– Nikos Oikou
6 hours ago
add a comment |
1
$begingroup$
This is an alternate solution without any observations about the users code. This answer may be removed if no observations about the posters code are made. Please see the code review guidelines for answers codereview.stackexchange.com/help/how-to-answer.
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
Good point @pacmaninbw. I added some comments and docstrings to the functions.
$endgroup$
– Nikos Oikou
6 hours ago
1
1
$begingroup$
This is an alternate solution without any observations about the users code. This answer may be removed if no observations about the posters code are made. Please see the code review guidelines for answers codereview.stackexchange.com/help/how-to-answer.
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
This is an alternate solution without any observations about the users code. This answer may be removed if no observations about the posters code are made. Please see the code review guidelines for answers codereview.stackexchange.com/help/how-to-answer.
$endgroup$
– pacmaninbw
7 hours ago
$begingroup$
Good point @pacmaninbw. I added some comments and docstrings to the functions.
$endgroup$
– Nikos Oikou
6 hours ago
$begingroup$
Good point @pacmaninbw. I added some comments and docstrings to the functions.
$endgroup$
– Nikos Oikou
6 hours ago
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
add a comment |
$begingroup$
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.
$endgroup$
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.
answered 8 hours ago
pacmaninbwpacmaninbw
7,4172 gold badges20 silver badges45 bronze badges
7,4172 gold badges20 silver badges45 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226699%2fconverting-a-set-into-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Possible duplicate of What is the better solution to this?
$endgroup$
– pacmaninbw
7 hours ago