This file contains hidden or 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 aiohttp | |
import itertools | |
from collections import namedtuple | |
import asyncio | |
async def get_top_stories(session): | |
url = "https://hacker-news.firebaseio.com/v0/topstories.json" | |
async with session.get(url) as response: | |
data = await response.json() | |
return data |
This file contains hidden or 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
from functools import reduce | |
def eratosthenes(n): | |
primes = [] | |
sieve = [True for _ in range(n+1)] | |
p = 2 | |
while p**2 <= n: | |
m = p**2 | |
while m <= n: | |
sieve[m] = False |
This file contains hidden or 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
thousand_digit_prime = """ | |
73167176531330624919225119674426574742355349194934 | |
96983520312774506326239578318016984801869478851843 | |
85861560789112949495459501737958331952853208805511 | |
12540698747158523863050715693290963295227443043557 | |
66896648950445244523161731856403098711121722383113 | |
62229893423380308135336276614282806444486645238749 | |
30358907296290491560440772390713810515859307960866 | |
70172427121883998797908792274921901699720888093776 | |
65727333001053367881220235421809751254540594752243 |
This file contains hidden or 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
""" | |
This problem was asked by Amazon. | |
Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A". | |
Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid. | |
""" | |
def encode(string): | |
i = 0 | |
encoded = "" |
This file contains hidden or 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
""" | |
This problem was asked by Palantir. | |
Write an algorithm to justify text. Given a sequence of words and an integer line length k, return a list of strings which represents each line, fully justified. | |
More specifically, you should have as many words as possible in each line. There should be at least one space between each word. Pad extra spaces when necessary so that each line has exactly length k. Spaces should be distributed as equally as possible, with the extra spaces, if any, distributed starting from the left. | |
If you can only fit one word on a line, then you should pad the right-hand side with spaces. | |
Each word is guaranteed not to be longer than k. |
This file contains hidden or 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
""" | |
This problem was asked by Google. | |
You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on. | |
Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board. | |
For example, given the following board: | |
[[f, f, f, f], |
This file contains hidden or 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
""" | |
This problem was asked by Facebook. | |
Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed). | |
For example, given the string "([])[]({})", you should return true. | |
Given the string "([)]" or "((()", you should return false. | |
""" | |
# Solution: |
This file contains hidden or 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
""" | |
This problem was asked by Google. | |
Given a singly linked list and an integer k, remove the kth last element from the list. k is guaranteed to be smaller than the length of the list. | |
The list is very long, so making more than one pass is prohibitively expensive. | |
Do this in constant space and in one pass. | |
""" | |
class Node: |
This file contains hidden or 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
""" | |
This problem was asked by Twitter. | |
You run an e-commerce website and want to record the last N order ids in a log. | |
Implement a data structure to accomplish this, with the following API: | |
record(order_id): adds the order_id to the log | |
get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N. | |
You should be as efficient with time and space as possible. |
This file contains hidden or 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
""" | |
This problem was asked by Google. | |
The area of a circle is defined as πr^2. | |
Estimate π to 3 decimal places using a Monte Carlo method. | |
Hint: The basic equation of a circle is x2 + y2 = r2. | |
""" | |
import random |
NewerOlder