+ 4
How can you sort a string using another string?
How can you create a function that takes two strings as arguments and sorts the first string by the second Ex Foos,of👉oops Banana, ab👉aaabnn
29 Réponses
+ 7
HonFu, I am still a bit confused. If i take input as:
letters = 'banana'
pattern = 'ab'
result is : nnaaab
+ 7
HonFu, ok thanks! I got it.
+ 6
Saja Ali there are many ways of course, but this is how I did it (not the best one tho):
def sort(string, pattern):
rest = [i for i in string if not i in pattern]
string = [i for i in string if i in pattern]
result = ''
for i in pattern:
result += i*string.count(i)
return result + rest
+ 6
HonFu if I understood right, his program takes two strings. The first one will be sorted, and the second one is how the sorting is defined.
For example, when you usually sort strings, it follows this pattern: 'abcdefghijklmnopqrstuvwxyz'
He wants to change that, and sort them using a custom string: 'fkzlvpzx...'
+ 6
Anyway, in Python we have a nice function sort with a nice parameter key.
I'll make a guess now and suppose that you want to get the string ordered by the order in which the letters appear in the second string.
For easy checking, let's just take the alphabet inverted, so that the letters will be sorted by inverted alphabetic order.
letters = 'shzkgsdaahjk'
pattern = 'zyxwvutsrqponmlkjihgfedcba'
print(
sorted(
letters,
key=lambda l: pattern.find(l)
)
)
+ 5
Anuran Pal it's not python
+ 5
Anuran Pal no, it's only O(n) for list comprehension
+ 4
Where is your attempt ?
Also, how did Foos become oops ?
+ 4
Lothar, if 'find' doesn't find the letter, it returns -1, so letters not included in pattern will be assembled at the beginning.
+ 4
This is how I would do it (probably there are better ways to do it):
string = input()
pattern = input()
res = sorted(string, key = lambda x : pattern.index(x) if x in pattern else len(pattern))
print(''.join(res))
# I used len(pattern) because it comes after the last char in pattern so those chars
#not found in pattern are placed in the end.
+ 4
Anuran Pal I don't think so..
+ 4
Ok I got it now. Thanks.
+ 3
Saja Ali what if the second string doesn't contain more than 2 chars of the first one ? How will you sort the rest ?
+ 3
Saja Ali, if you don't know what your program is supposed to do, how can we help you?
+ 3
How do you think you can define a patter using only two letters 🧐
+ 3
Anuran Pal can you explane your solution plz, I didn't understand one word.
+ 3
Algo -
1. create a char to index mapping for the second string.
2. now write a compare/sort function based on the indexposition of a character of the first string inside the second string.
it takes O(n) time.
using javascript-
function sortUsingCustomString(stringToBeSorted,customString){
var charToIndexMap={};
for (var i = 0; i < customString.length; i++) { charToIndexMap[customString.charAt(i)] = i + 1; }
var sortFunc = function(a,b){
var ia = charToIndexMap[a];
var ib = charToIndexMap[b];
if(ia){
if(ib){
return ia > ib? 1:ia == ib? 0:-1;
}else
return -1;
}else if(ib){
if(ia)
return ia > ib? 1:ia == ib? 0:-1;
else
return 1;
}else
return 0;
}
return [].slice.call(stringToBeSorted.toLowerCase()).sort(sortFunc).join("");
}
console.log(sortUsingCustomString("banana","ab"));
+ 3
You are looping through the second string each time you compare two char in the first string. I am using hashmap for faster lookup. Is not that better?
+ 3
Aymane Boukrouh [INACTIVE]
rest = [i for i in string if not i in pattern]
string = [i for i in string if i in pattern]
I am a bit confused. Does not each this lines take O(n^2) time in python?
+ 3
Anuran Pal in each line he is iterating through the string once so it would take O(n) to complete not O(n^2)