Third Greatest Coderbyte Solution

0

Have the function ThirdGreatest(strArr) take the array of strings stored in strArr and return the third largest word within in. 


Example
: if strArr is ["hello", "world", "before", "all"] your output should be world because "before" is 6 letters long, and "hello" and "world" are both 5, but the output should be world because it appeared as the last 5 letter word in the array. If strArr was ["hello", "world", "after", "all"] the output should be after because the first three words are all 5 letters long, so return the last one. The array will have at least three strings and each string will only contain letters.

Third Greatest Coderbyte Java

import java.util.Scanner;



public class ThirdGreatest {
String ThirdGreatest(String[] strArr) {
Arrays.sort(strArr, new Comparator<String>() {
@Override
public int compare(String s, String s2) {
if (s.length() > s2.length()) {
return 1;
} else if (s.length() == s2.length()) {
return 0;
}
return -1;
}
});

String last = strArr[strArr.length - 1];
String secondLast = strArr[strArr.length - 2];
String thirdLast = strArr[strArr.length - 3];
if (last.length() == secondLast.length() && last.length()
== thirdLast.length()) {
return last;
} else if (secondLast.length() == thirdLast.length()) {
return secondLast;
}

return thirdLast;

}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
ThirdGreatest c = new ThirdGreatest();
System.out.print(c.ThirdGreatest(new String[]
{"hello", "world", "before", "all"}));
}
}


Third Greatest Coderbyte JavaScript


function ThirdGreatest(strArr) {
var charNumArr=[];
for (var i=0;i<strArr.length;i++){
charNumArr[i]=strArr[i].length;
}
charNumArr.sort(function(a, b) {
return b - a;
});
for (var i=strArr.length-1;i>=0;i--){
if (strArr[i].length==charNumArr[2])
return strArr[i];
}
}


Third Greatest Coderbyte Python


def ThirdGreatest(strArr):
return sorted([(word, -len(word)) for word in strArr], key=lambda x: x[1])[2][0]
# keep this function call here
# to see how to enter arguments in Python scroll down
print ThirdGreatest(raw_input())




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !