Re-arranging a string in python
Question:
Given a string to create 2 parallel arrays, process the letters in the string from left to right, inserting each letter into the first array in alphabetical order, one letter at a time. During this process, each letter is assigned an integer value in the second array as follows:
The first letter has a value of 0.
If the new letter is the first or last in the array, its value is one more than the value of the adjacent letter.
Otherwise, it is one more than the larger value of the two adjacent values. If the letter is already in the array, the new letter is placed before the existing letter. Once the two arrays are created, output the letters in order of their value, from left to right, starting with the 0, then all of the 1s, then all of the 2s, etc.
For example, if someone input "PYTHONN" into the function, it would output "PHYOTNN."
I tried to use if-else statements to try to solve this problem, but my code got messy and I kept recieving errors. This is my code:
def ltnb(i):
return i.split()
def listByValue(input):
process_str=input
converted_list=[]
test_v=[]
arr_1=[]
arr_2=[]
full_list=[]
x=0
for letter in process_str:
test_v.clear()
for i in converted_list:
test_v.append(ltnb(i)[0])
test_v.append(letter)
test_v.sort()
if len(converted_list)==0:
number=0
elif x==0:
x=test_v.index(letter)
number=int(ltnb(converted_list[x-1])[1])+1
elif x==len(converted_list):
x=test_v.index(letter)
number=int(ltnb(converted_list[x])[1])+1
else:
x=test_v.index(letter)
number=max(int(ltnb(converted_list[x-1])[1]), int(ltnb(converted_list[x])[1]))
converted_list.insert(x, f"{letter} {number}")
for i in range(0,len(converted_list)):
ltnb(converted_list)[1]
I also couldn’t figure out what and how to return the correct code. Is there anything wrong with my code and how can I solve this problem?
Answers:
Just for the "how can I solve this problem?" part, a working implementation:
s = 'PYTHONN'
L, V = [], [-1]
for c in s:
L += c
L.sort()
i = L.index(c)
V.insert(i+1, max(V[i:i+2])+1)
print(''.join(sorted(L, key=lambda _: V.pop(1))))
The extra -1
just makes the code easier.
Given a string to create 2 parallel arrays, process the letters in the string from left to right, inserting each letter into the first array in alphabetical order, one letter at a time. During this process, each letter is assigned an integer value in the second array as follows:
The first letter has a value of 0.
If the new letter is the first or last in the array, its value is one more than the value of the adjacent letter.
Otherwise, it is one more than the larger value of the two adjacent values. If the letter is already in the array, the new letter is placed before the existing letter. Once the two arrays are created, output the letters in order of their value, from left to right, starting with the 0, then all of the 1s, then all of the 2s, etc.
For example, if someone input "PYTHONN" into the function, it would output "PHYOTNN."
I tried to use if-else statements to try to solve this problem, but my code got messy and I kept recieving errors. This is my code:
def ltnb(i):
return i.split()
def listByValue(input):
process_str=input
converted_list=[]
test_v=[]
arr_1=[]
arr_2=[]
full_list=[]
x=0
for letter in process_str:
test_v.clear()
for i in converted_list:
test_v.append(ltnb(i)[0])
test_v.append(letter)
test_v.sort()
if len(converted_list)==0:
number=0
elif x==0:
x=test_v.index(letter)
number=int(ltnb(converted_list[x-1])[1])+1
elif x==len(converted_list):
x=test_v.index(letter)
number=int(ltnb(converted_list[x])[1])+1
else:
x=test_v.index(letter)
number=max(int(ltnb(converted_list[x-1])[1]), int(ltnb(converted_list[x])[1]))
converted_list.insert(x, f"{letter} {number}")
for i in range(0,len(converted_list)):
ltnb(converted_list)[1]
I also couldn’t figure out what and how to return the correct code. Is there anything wrong with my code and how can I solve this problem?
Just for the "how can I solve this problem?" part, a working implementation:
s = 'PYTHONN'
L, V = [], [-1]
for c in s:
L += c
L.sort()
i = L.index(c)
V.insert(i+1, max(V[i:i+2])+1)
print(''.join(sorted(L, key=lambda _: V.pop(1))))
The extra -1
just makes the code easier.