Skip to content

Instantly share code, notes, and snippets.

View ericntd's full-sized avatar

Eric Nguyen ericntd

View GitHub Profile
// prepare variable employees which is a list of Employee object
// Same as Task 0
employees.groupingBy { it.department }
.fold(initialValue = 0) { total, employee -> total + employee.salary }
.map {
println("Total salary for department ${it.key} is ${it.value}")
}
// prepare variable employees which is a list of Employee object
// Same as Task 0
employees.groupBy { it.department }
.map { group ->
val departmentName = group.key
val departmentEmployees = group.value
val departmentTotalSalary = departmentEmployees.sumBy { it.salary }
println("Total salary for department $departmentName is $departmentTotalSalary")
}
// prepare variable employees which is a list of Employee object
// Same as Task 0
employees.groupingBy { it.department }
.eachCount()
.forEach { (s, i) ->
println("there are $i employees for department $s")
}
// prepare variable employees which is a list of Employee object
val csvStr = """
Name,Department,Salary,Title
Victoria,Engineering,88429,Other
Ana,Other,58611,Manager
Amelia,UX,55086,Analyst
Victoria,Marketing,72769,Analyst
Rosa,UX,83077,Other
Troy,Engineering,87487,Analyst
Fern,Finance,76190,Engineer

I have an interface

interface Logger {
    void log(message: String);
}

and a specific implementation of that interface:

class TimberLogger implements Logger {
    @Override
// prepare variable employees which is a list of Employee object
// Same as Task 0
var groupCount = 0
employees.groupingBy { it.department }
.aggregate { key, kAverage: Double?, element, isFirstElement ->
// Reset the group count
if (isFirstElement) {
groupCount = 1
} else {
groupCount++
employees.groupingBy { it.department }
.aggregate { key, acc: Int?, element, isFirstElement ->
return@aggregate if (isFirstElement) {
0 + element.salary
} else {
acc!! + element.salary
}
}
.map {
println("Total salary for department ${it.key} is ${it.value}")
fun compareMapInits(repeat: Int, size: Int) {
println(">>> compareMapInits for $size elements (repeated $repeat times)")
var totalMutableMapRuntime: Long = 0
var totalHashMapRuntime : Long = 0
var countHashMapSlower = 0
repeat(repeat) {
// mutableMapOf()
val startTime = System.currentTimeMillis()
val map = mutableMapOf<Int, Int>()
(0..size).forEach {
/**
* @deprecated This method is deprecated because its meaning is ambiguous due to the async
* handling of adapter updates. You should use {@link #getLayoutPosition()} or
* {@link #getAdapterPosition()} depending on your use case.
*
* @see #getLayoutPosition()
* @see #getAdapterPosition()
*/
@Deprecated
public final int getPosition() {
public static final ListNode<Integer> mergeTwoSortedLists(ListNode<Integer> L1, ListNode<Integer> L2) {
System.out.printf(">>> Merging %s and %s", L1, L2);
System.out.println();
ListNode<Integer> dummyHead = new ListNode<>(new Random().nextInt(), (ListNode<Integer>) null);
ListNode<Integer> current = dummyHead;
ListNode<Integer> p1 = L1;
ListNode<Integer> p2 = L2;
while (p1.getNext() != null && p2.getNext() != null) {
if (p1.getData() <= p2.getData()) {