Skip to content

Instantly share code, notes, and snippets.

View cloudbank's full-sized avatar
🎯
Focusing

S.G. Vogel cloudbank

🎯
Focusing
View GitHub Profile
public class TestRepository {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Inject String name;
public class TestRepository {
@Inject
public TestRepository() {}
}
public class MainActivity extends DaggerActivity {
@Inject
@Component(modules = {
AndroidInjectionModule.class,
// don't worry about these other modules just yet
AndroidBindingModule.class,
RepoModule.class})
interface AppComponent extends AndroidInjector<TestApplication> {
@Component.Builder
abstract class Builder extends AndroidInjector.Builder<TestApplication> {
}
@cloudbank
cloudbank / ViewModel.kt
Created January 9, 2018 09:03 — forked from pbochenski/ViewModel.kt
LiveData meets RxJava
class MainViewModel(app: Application) : AndroidViewModel(app) {
private val postRepo = getApplication<App>().postRepo
private val LOAD_ITEM_COUNT = 15
fun getPosts(): LiveData<List<Post>> {
return postRepo.posts.map {
it.map { Post(it.id, it.title, it.url) }
}
}
//Arbitrage is the simultaneous purchase and sale of an asset to profit from a difference in the price. This translates to
//a cycle of one unit translated to other units, ending with that unit's increase. A graph can be used to represent arbitrage
//if there is a cycle. Since negative weights could be involved in such conversions, Bellman-Ford is a natural choice for
//this solution. In general 1 unit of X returns > 1 unit of X. 1X--2G--7Y--1.2X
//a*b > 1
//In the mathematical field of graph theory, a complete graph is a simple undirected graph in which every pair of distinct vertices
//is connected by a unique edge. Since the arbitrage problem involves a complete graph, we can run BF from any vertex.
//There is a mathematics hack that aids us in finding cycles that makes use of the fact that BF can detect negative cycles.
//Fact: log(a*b) == log(A) + log(b)
//then, if we allow each weight in the graph to be rewrittten as -lg(w), and the fact that log(1) == 0
@cloudbank
cloudbank / Find Cycle Start in SLL
Last active October 3, 2017 01:19
Finding cycle start in SLL is often presented wrong it its implementation, in fact even published. It is a simple mistake, but the correct version is a rare find as a result.
//see https://stackoverflow.com/questions/2936213/explain-how-finding-cycle-start-node-in-cycle-linked-list-work/42615373#42615373
public Node whereLoopBegins(Node head) {
Node slow = head;
Node fast = head;
// collide them at loopsize - K , K = k % loopsize
while (fast != null && fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) { // there is a cycle, but slow and fast may have
package algorithms;
import java.util.Arrays;
import java.util.Comparator;
public class FractionalKnapsack {
static class Item {
int value, weight;
/*
*Given a large list, insert into a balanced BST, then find the distance between 2 nodes
*/
//the BST tree is required by the question, n is unknown but could be large
//step 1: sort the data the best way we know for large data
//given a "list" and number N of items.
//Is this data in one list or not? We don't know.
@cloudbank
cloudbank / ParallelStreamsPuzzler.java
Created April 3, 2017 23:53 — forked from raoulDoc/ParallelStreamsPuzzler.java
Solving a Parallel Streams Puzzler
import java.util.List;
import java.util.stream.LongStream;
import static java.util.stream.Collectors.toList;
public class ParallelStreamsPuzzler {
public static void main(String[] args) {
List<Transaction> transactions
= LongStream.rangeClosed(0, 1_000)
import java.util.ArrayDeque;
import java.util.Queue;
// a Java version for http://www.geeksforgeeks.org/shortest-path-in-a-binary-maze/
public class SearchMazeBFS {
// BFS to find the shortest path between
// a given source cell to a destination cell.
public static final int ROW = 9;