This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MistakesActivity extends ActionBarActivity | |
{ | |
public static final String TABLE_NAME = "names"; | |
ExecutorService executorService = Executors.newFixedThreadPool(4); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_mistakes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MistakesActivity extends ActionBarActivity | |
{ | |
public static final String TABLE_NAME = "names"; | |
ExecutorService executorService = Executors.newFixedThreadPool(4); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_mistakes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*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. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package algorithms; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
public class FractionalKnapsack { | |
static class Item { | |
int value, weight; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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> { | |
} |
OlderNewer