eko.harmawan.susilo
BAN USERIn scala:
def getOccurance(start: Int, end: Int): List[Int] = {
if (start > end)
getOccurance(end, start)
else {
var result = for (i <- start to end; if (i.toString().contains(start.toString()))) yield i
result.toList
}
}
println(getOccurance(2, 25)) //> List(2, 12, 20, 21, 22, 23, 24, 25)
println(getOccurance(25, 2)) //> List(2, 12, 20, 21, 22, 23, 24, 25)
In scala i would use plain class list and filter function. Complexity is linear O(n)
type Exam = (String, Double)
val subjectToSearch = "Math"
val minimumPassing = 60.0
case class Student(val id: String, val name: String, val age: Int, val exams: List[Exam]) {
}
val students = List[Student](
Student("1", "1", 18, List(("English",20.34),("Math",84.5))),
Student("2", "1", 18, List(("English",30.34),("History",74.5))),
Student("3", "1", 18, List(("English",40.34),("History",64.5))),
Student("4", "1", 18, List(("English",50.34),("History",54.5))),
Student("5", "1", 18, List(("English",60.34),("Math",64.5))),
Student("6", "1", 18, List(("English",70.34),("Math",34.5))),
Student("7", "1", 18, List(("English",80.34),("History",24.5)))
)
students.filter(curStudent => curStudent.exams.filter(exam => exam._2>minimumPassing && exam._1.equalsIgnoreCase(subjectToSearch)).size > 0)
public static void main(String args[]) {
List<String> data = Arrays.asList("H", "HW", "Ho", "HeWorM");
List<String> details = Arrays.asList("HelloMars", "HelloWorld", "HelloWorldMars", "HiHo");
details.forEach(str -> {
data.forEach(curCamCase -> {
System.out.println(" cam case " + curCamCase + " compared to string " + str + " is "
+ isMatchCamelCase(curCamCase, str));
});
});
System.out.println("match?" + isMatchCamelCase("HelWorM", "HelloWorld"));
}
public static boolean isMatchCamelCase(String camelCase, String word) {
if (camelCase.length() > 0 && word.length() == 0) {
return false;
}
if (camelCase.length() == 0) {
return true;
}
if (Character.isUpperCase(camelCase.charAt(0))) {
if (Character.isUpperCase(word.charAt(0))) {
return camelCase.charAt(0) == word.charAt(0)
&& isMatchCamelCase(camelCase.substring(1), word.substring(1));
} else {
return isMatchCamelCase(camelCase, word.substring(1));
}
} else if (!Character.isUpperCase(camelCase.charAt(0))) {
return camelCase.charAt(0) == word.charAt(0) && isMatchCamelCase(camelCase.substring(1), word.substring(1));
}
return true;
}
public static void main(String args[]) {
String pattern = "acc";
String dictionary[] = { "cdf", "too", "hgfdt", "paa" };
findMatchingPattern(pattern, dictionary).forEach(System.out::println);
}
public static List<String> findMatchingPattern(String pattern, String dictionary[]) {
int patternLength = pattern.length();
long uniqueLength = getUniqueChars(pattern);
return Arrays.stream(dictionary)
.filter(string -> string.length() == patternLength)
.filter(string -> getUniqueChars(string) == uniqueLength)
.collect(Collectors.toList());
}
private static long getUniqueChars(String string) {
return string.chars().distinct().count();
}
complexity = O(n). getUniqueChars = O(n), findMatchingPattern = O(n)
- eko.harmawan.susilo August 05, 2016Complexity = O (n log n) due to the sorting of the stream
memory = n
public static void main(String args[]) {
// pair = id, weight
Pair itemOne[] = { new Pair<Integer, Integer>(2, 5), new Pair<Integer, Integer>(5, 10), new Pair<Integer, Integer>(1, 10), new Pair<Integer, Integer>(4, 12) };
Pair itemTwo[] = { new Pair<Integer, Integer>(3, 7), new Pair<Integer, Integer>(2, 10), new Pair<Integer, Integer>(1, 10) };
Pair<Integer,Integer> merged[]=mergeById(itemOne,itemTwo);
Arrays.stream(merged).forEach(System.out::println);
}
@SuppressWarnings("unchecked")
public static Pair<Integer,Integer>[] mergeById(Pair<Integer, Integer>[] first, Pair<Integer, Integer>[] second) {
Map<Integer, Integer> reduced = Stream.concat(Arrays.stream(first), Arrays.stream(second))
.collect(Collectors.groupingBy(Pair::getKey,
Collectors.reducing(0, Pair::getValue, Integer::sum)));
Pair<Integer,Integer>[] pairs=(Pair<Integer, Integer>[])reduced.entrySet()
.stream()
.map(entry -> new Pair<Integer,Integer>(entry.getKey(),entry.getValue()))
.sorted((p1,p2)-> p1.getValue().compareTo(p2.getValue()))
.toArray(Pair[]::new);
return pairs;
}
Non repeating characters mean the first occurance of that character is the same as the last occurance.
Space o(1),memory o(n)
In scala
println(nonRepeatingChar("abcdeabc")) //> d
- eko.harmawan.susilo June 08, 2017