+ 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

9th Jan 2020, 11:07 AM
Pattern
Pattern - avatar
29 odpowiedzi
+ 7
HonFu, I am still a bit confused. If i take input as: letters = 'banana' pattern = 'ab' result is : nnaaab
9th Jan 2020, 2:53 PM
Lothar
Lothar - avatar
+ 7
HonFu, ok thanks! I got it.
9th Jan 2020, 4:47 PM
Lothar
Lothar - avatar
+ 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
9th Jan 2020, 12:05 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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...'
9th Jan 2020, 12:12 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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) ) )
9th Jan 2020, 12:14 PM
HonFu
HonFu - avatar
+ 5
10th Jan 2020, 9:06 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 5
Anuran Pal no, it's only O(n) for list comprehension
10th Jan 2020, 9:36 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Where is your attempt ? Also, how did Foos become oops ?
9th Jan 2020, 11:26 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Lothar, if 'find' doesn't find the letter, it returns -1, so letters not included in pattern will be assembled at the beginning.
9th Jan 2020, 3:11 PM
HonFu
HonFu - avatar
+ 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.
10th Jan 2020, 6:02 PM
MedG
MedG - avatar
+ 4
Anuran Pal I don't think so..
10th Jan 2020, 9:13 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Ok I got it now. Thanks.
10th Jan 2020, 9:40 PM
Anuran Pal
Anuran Pal - avatar
+ 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 ?
9th Jan 2020, 11:55 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Saja Ali, if you don't know what your program is supposed to do, how can we help you?
9th Jan 2020, 12:08 PM
HonFu
HonFu - avatar
+ 3
How do you think you can define a patter using only two letters 🧐
10th Jan 2020, 4:27 PM
Prapunj Tiwari
Prapunj Tiwari - avatar
+ 3
Anuran Pal can you explane your solution plz, I didn't understand one word.
10th Jan 2020, 8:02 PM
MedG
MedG - avatar
+ 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"));
10th Jan 2020, 8:57 PM
Anuran Pal
Anuran Pal - avatar
+ 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?
10th Jan 2020, 9:17 PM
Anuran Pal
Anuran Pal - avatar
+ 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?
10th Jan 2020, 9:33 PM
Anuran Pal
Anuran Pal - avatar
+ 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)
10th Jan 2020, 9:38 PM
MedG
MedG - avatar