Skip to content

Instantly share code, notes, and snippets.

View rishi93's full-sized avatar

Rajarishi Devarajan rishi93

  • SRH University of Applied Sciences
  • Heidelberg, Germany
View GitHub Profile
@rishi93
rishi93 / hacker_news_filter.py
Last active November 21, 2022 15:57
A simple script to filter hacker news articles
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
@rishi93
rishi93 / prog10.py
Created August 16, 2022 22:33
Solution to Project Euler problem #10
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
@rishi93
rishi93 / project_euler_8.py
Created August 16, 2022 22:12
Solution to Project Euler problem #8
thousand_digit_prime = """
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
@rishi93
rishi93 / day-29.py
Created December 20, 2019 19:08
Daily Coding Problem - 29
"""
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 = ""
@rishi93
rishi93 / day-28.py
Created December 20, 2019 16:12
Daily Coding Problem - 28
"""
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.
@rishi93
rishi93 / day-23.py
Created December 20, 2019 13:10
Daily Coding Problem - 23
"""
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],
@rishi93
rishi93 / day-27.py
Created December 19, 2019 12:24
Daily Coding Problem - 27
"""
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:
@rishi93
rishi93 / day-26.py
Created December 18, 2019 16:20
Daily Coding Problem - 26
"""
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:
@rishi93
rishi93 / day-16.py
Created December 12, 2019 16:49
Daily Coding Problem - 16
"""
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.
@rishi93
rishi93 / day-14.py
Created December 12, 2019 15:08
Daily Coding Problem - 14
"""
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