Lexicographical sorting
By : user2608655
Date : March 29 2020, 07:55 AM
it helps some times It seems that what you're looking for is a better understanding of the question, so let me just make it clear. The usual sorting on strings is lexicographic sorting. If you sort the strings [jibw, ji, jp, bw, jibw] into lexicographic order, the sorted sequence is [bw, ji, jibw, jibw, jp], which is what you got. So your problem is not with understanding the word "lexicographic"; you already understand it correctly. Your problem is that you're misreading the question. The question doesn't ask you to sort the strings in lexicographic order. (If it did, the answer you got by sorting would be correct.) Instead, it asks you to produce one string, got by concatenating the input strings in some order (i.e., making one string without spaces), so that the resulting single string is lexicographically minimal. code :
bwjijibwjibwjp //Your answer
bwjibwjibwjijp //The correct answer
|
Sorting in lexicographical order
By : vmwong
Date : March 29 2020, 07:55 AM
I hope this helps you . First the spaceship operator is <=> not <==> Secondly you're not combining the 2 comparisons correctly: the result of the comparison will be -1,0,or 1. These are all truthy values and true && foo is just foo, so your code would just sort by the y values code :
x_ordering = a.x <=> b.x
x_ordering == 0 ? a.y <=> b.y : x_ordering
array.sort! { |a,b| [a.x, a.y] <=> [b.x, b.y]}
array.sort_by! { |a| [a.x, a.y] }
|
Sorting dictionary by value and lexicographical
By : Oscar
Date : March 29 2020, 07:55 AM
To fix the issue you can do To match the actual output you want you have to use two keys to sort negating the int value with -: code :
d = {'a':10,'b':20,'c':5,'d':5,'e':5}
for k,v in sorted(d.items(),key=lambda x:(-x[1],x[0])):
print("{} {}".format(k,v))
b 20
a 10
c 5
d 5
e 5
|
Sorting a String in lexicographical order
By : user1521924
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further The question provides an input String and an integer, it asks us to convert the string into all the possible combinations of sub-strings of length as specified by the input integer provided. Then we have to find the maximum and minimum from those sub-strings. , These two substring will be the start code :
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int k = sc.nextInt();
String maxString = s.substring(0, k);
String minString = s.substring(0, k);
for (int i = 1; i <= s.length() - k; ++i){
String curSubstr = s.substring(i, i + k);
System.out.println(curSubstr);
if (maxString.compareTo(curSubstr) < 0)
maxString = s.substring(i, i + k);
if (minString.compareTo(curSubstr) > 0)
minString = s.substring(i, i + k);
}
System.out.println(minString);
System.out.println(maxString);
}
Case #1
Input:
HelloWorld
2
Output:
el
ll
lo
oW
Wo
or
rl
ld
He // the min
rl // the max
|
Sorting in Map (Lexicographical Order)
By : user2905886
Date : March 29 2020, 07:55 AM
wish help you to fix your issue If you want the entries of a map sorted in a particular order (and the default order is operator <, which doesn't do what you're asking), then you need to instantiate your map with a custom comparator. code :
struct myComp {
bool operator()(const std::pair<std::string, int>& lhs,
const std::pair<std::string, int>& rhs) const
{ /* your code here */ }
};
std::map<std::pair<std::string, int>, int, myComp> m;
|