Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
{'frontends': {'frontend__2F': {'backend': 'backend__2F', 'passHostHeader': True, 'routes': {'test': {'rule': 'PathPrefix:/', 'data': '{"hub": true}'}}}, 'frontend__2Fuser_2Fyuvipanda': {'backend': 'backend__2Fuser_2Fyuvipanda', 'passHostHeader': True, 'routes': {'test': {'rule': 'PathPrefix:/user/yuvipanda', 'data': '{"user": "yuvipanda", "server_name": ""}'}}}, 'frontend__2Fuser_2Fusername': {'backend': 'backend__2Fuser_2Fusername', 'passHostHeader': True, 'routes': {'test': {'rule': 'PathPrefix:/user/username', 'data': '{"user": "username", "server_name": ""}'}}}}, 'backends': {'backend__2F': {'servers': {'server1': {'url': 'http://127.0.0.1:8081', 'weight': 1}}}, 'backend__2Fuser_2Fyuvipanda': {'servers': {'server1': {'url': 'http://127.0.0.1:41224', 'weight': 1}}}, 'backend__2Fuser_2Fusername': {'servers': {'server1': {'url': 'http://127.0.0.1:38844', 'weight': 1}}}}}
Traceback (most recent call last):
File "tbjh/test.py", line 9, in <module>
toml.dumps(d)
File "/Users/yuvipanda/code/the-batchies
apiVersion: v1
kind: Pod
metadata:
labels:
hub.jupyter.org/network-access-hub: "true"
name: efs-access
namespace: wfirst-sit
spec:
affinity:
nodeAffinity:
@yuvipanda
yuvipanda / Hint.md
Created October 13, 2011 19:07
Polygon Diagonals Solution (InterviewStreet CodeSprint Fall 2011)

`The required answer is : 1 / (n + k) * n-choose-r(n - 3,k) * n-choose-r(n + k, n - 1).

While I am not aware of a simple way to prove this which I can describe here, here's some practical advice on how such problems can be approached. It helps to write down a simpler solution and searching Google or OEIS (Online Encyclopedia of Integer Sequences) for small inputs like k = 3 and N = 1,2,...10. This usually gives you a formula describing the sequence, and possibly generalizations for the same.

Computing the answer using the above formula requires calculating the power of MOD (1e6 + 3) in the numerator and the demonimator. If the power of MOD is bigger in the numerator, the answer is 0. Otherwise, we can calculate all the factorials with the powers of MOD cast out. For example, if MOD = 3 and N = 11, we would calculate the product 1 * 2 * 3 / 3 * 4 * 5 * (6 / 3) * 7 * 8 * (9 / 9) * 10 * 11. This can be done easily in O(MOD). Once we have that, we can multiply the values in the numerator and mltiply with the mo

@yuvipanda
yuvipanda / Hint.md
Created October 13, 2011 18:53
Card Shuffling Solution (InterviewStreet CodeSprint Fall 2011)

The main observation is that the card at index i (1 <= i <= N) ends up at index K * i % (N + 1) after 1 shuffle. So after x shuffles, the card will be at position K^x * i % (N + 1). We want to find the smallest x such that K^x * i = i for all i. In other words, we want K^x = 1. This is known as the multiplicative order of K mod (N + 1). Lagrange's theorem implies that this will be a factor of phi(N + 1) where phi is the Euler Totient function. So we can check all factors of phi(N + 1) and find the smallest one which works. See: http://en.wikipedia.org/wiki/Euler's_totient_function http://en.wikipedia.org/wiki/Lagrange's_theorem_(group_theory)

@yuvipanda
yuvipanda / Hint.md
Created October 13, 2011 18:51
2's Compliment Solution (InterviewStreet CodeSprint Fall 2011)

The number of 1's in the range 0..X (X is positive) is easy to calculate (Can you get a simple recurrence which does this in O(log X) ?) Another observation is that the number of 1's in -X is equal to the number of 0's in ~(-X) = X - 1. Using this, it is easy to calculate the answer for negative ranges as well.

@yuvipanda
yuvipanda / Hint.md
Created October 13, 2011 18:52
Insertion Sort Solutions (InterviewStreet CodeSprint Fall 2011)

The answer is the number of inversions in the array, which is the number of pairs i,j such that i < j and a[i] > a[j]. Counting this is a fairly classical problem with many solutions such as using data structures such as Balanced Trees or Binary Indexed Trees. Another particularly elegant solution involves modifying merge sort to count the number of inversions when merging the two sorted halves in the algorithm.

@yuvipanda
yuvipanda / Hint.md
Created October 13, 2011 19:01
Bytelandian Tours Solution (InterviewStreet CodeSprint Fall 2011)

Such a tour exists only when the tree is a caterpiller tree which is a tree consisting of a straight path with some leafs attached to nodes on that path. Using this observation, getting a recurrence and solving it using Dynamic Programming is greatly simplified. Also, working out some examples on paper, it is easy to see that the answer is 2 * n1! * ... * nk! where n1,..,nk are the number of leafs attached to each node on the path defining the caterpiller tree. A special case is when the tree is a star, when the answer is simply (n - 1)!. See: http://en.wikipedia.org/wiki/Caterpillar_tree

@yuvipanda
yuvipanda / Hint.md
Created October 13, 2011 19:06
Cliques (InterviewStreet CodeSprint Fall 2011)

Turan's theorem gives us an upper bound on the number of edges a graph can have if we wish that it should not have a clique of size x. Though the bound is not exact, it is easy to extend the statement of the theorem to get an exact bound in terms of n and x. Once this is done, we can binary search for the largest x such that f(n,x) <= m. See: http://en.wikipedia.org/wiki/Tur%C3%A1n's_theorem

@yuvipanda
yuvipanda / Hint.md
Created October 13, 2011 19:02
Queens on a Board Solution (InterviewStreet CodeSprint Fall 2011)

Queens on a Board Solution (InterviewStreet CodeSprint Fall 2011)