Skip to content

Instantly share code, notes, and snippets.

@cgrushko
cgrushko / gist:1aea22fca9cbae703489
Last active August 29, 2015 13:57
Given two OS X keychains, create two 2-column files. First column is last modification date of an entry, the second column is the name of the entry.
if [ "$#" -ne 2 ]; then
echo "Usage: compare-keychains.sh first.keychain second.keychain"
exit 1
fi
# Takes a keychain ($1), and generates a file that has a column of last-modifed dates,
# and a column of the names of items in the keychain.
# Result is written into a temp file whose name is stored in "$2".
extract() {
local BASE=`basename "$1"`
@cgrushko
cgrushko / gist:7419423
Created November 11, 2013 20:04
On OS X 10.9
KeyPress event, serial 34, synthetic NO, window 0x600001,
root 0xe2, subw 0x0, time 1220704819, (1332,-13), root:(1332,9),
state 0x10, keycode 64 (keysym 0xffe1, Shift_L), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False
KeyPress event, serial 34, synthetic NO, window 0x600001,
root 0xe2, subw 0x0, time 1220705015, (1332,-12), root:(1332,10),
state 0x11, keycode 38 (keysym 0x7d, braceright), same_screen YES,
@cgrushko
cgrushko / gist:4703462
Last active December 12, 2015 02:59
Facebook Hacker Cup 2012 Round 1 (Question 3, Dead Pixels)

John's friend Peter purchases a new high resolution monitor with dimension W * H where W is the number of pixels in each row (i.e. width) and H is the number of pixels in each column (i.e. height).

However, there are N dead pixels on the monitor. The i-th dead pixel is located at (x[i], y[i]). (0, 0) is the top-left pixel and (W - 1, H - 1) is the bottom-right pixel. The locations of the dead pixels could be generated by 6 given integers X, Y, a, b, c and d by the following rules. If 2 pixels are at the same location, they are considered the same. It is possible that there are less than N distinct dead pixels.

  • x[0] = X
  • y[0] = Y
  • x[i] = (x[i - 1] * a + y[i - 1] * b + 1) % W (for 0 < i < N)
  • y[i] = (x[i - 1] * c + y[i - 1] * d + 1) % H (for 0 < i < N)

Peter connects his monitor to his computer and opens an image with dimension P (width) * Q (height). How many unique positions can the image be placed so that it can be displayed perfectly (i.e. all pixels of the picture are shown on the monitor)? The ima

@cgrushko
cgrushko / gist:4703439
Last active December 12, 2015 02:59
Facebook Hacker Cup 2012 Round 1 (Question 2, Security)

You are designing a new encryption system that works in the following way:

For server-client communication you need a key k, composed of m sections, each of length l, and the key consists only of lowercase characters in the set {a, b, c, d, e, f}. The server has a key k1 and the client has a key k2 where:

k1 = f(k). f is a function that receives a key and replace some random letters by ? indicating that those characters can be any lowercase letter of the set described before.
k2 = f(g(k)). g is a function that takes a key and produces a random permutation of its m sections. And f is the function defined above.
For example: let m = 3, l = 2

f('abacbc') = '?ba??c'

@cgrushko
cgrushko / gist:4703412
Last active December 12, 2015 02:59
Facebook Hacker Cup 2012 Round 1 (Question 1, Card Game)

John is playing a game with his friends. The game's rules are as follows: There is deck of N cards from which each person is dealt a hand of K cards. Each card has an integer value representing its strength. A hand's strength is determined by the value of the highest card in the hand. The person with the strongest hand wins the round. Bets are placed before each player reveals the strength of their hand.

John needs your help to decide when to bet. He decides he wants to bet when the strength of his hand is higher than the average hand strength. Hence John wants to calculate the average strength of ALL possible sets of hands. John is very good at division, but he needs your help in calculating the sum of the strengths of all possible hands.

#Problem#

You are given an array a with N ≤ 10 000 different integer numbers and a number, K, where 1 ≤ K ≤ N. For all possible subsets of a of size K find the sum of their maximal elements modulo 1 000 000 007.

#Input#

@cgrushko
cgrushko / gist:3379919
Created August 17, 2012 15:30
Crash report for Ingredients issue
Process: Ingredients [38360]
Path: /Users/USER/Library/Developer/Xcode/DerivedData/Ingredients-hayiunjctfhyapgafiutvgjqsawr/Build/Products/Release/Ingredients.app/Contents/MacOS/Ingredients
Identifier: net.fileability.ingredients
Version: 1.1b1 (1.0)
Code Type: X86-64 (Native)
Parent Process: Instruments [38355]
Date/Time: 2012-08-17 18:23:34.159 +0300
OS Version: Mac OS X 10.7.4 (11E53)
Report Version: 9