Skip to content

Instantly share code, notes, and snippets.

View gudnm's full-sized avatar

Ivan Vashchenko gudnm

View GitHub Profile
@gudnm
gudnm / gist:892951
Created March 29, 2011 18:33
Simple procedural program that finds a smallest rectangle to accommodate a given set of rectangles
#include <iostream>
#include <cmath>
using namespace std;
struct placedRect
{
int x,
y,
width,
height;
@gudnm
gudnm / worstMove.py
Last active June 15, 2016 16:53
A program that plays Tic-tac-toe with you and tries to lose. Uses Minimax rule to evaluate possible moves.
"""
Write a program that plays Tic-tac-toe with you and tries its best to lose
"""
def printBoard(b):
for i in range(3):
print(b[3*i], b[3*i+1], b[3*i+2])
def getNext(b):
move = int(input("Your move, sir (must be a number from 1 to 9): ")) - 1
@gudnm
gudnm / Gomoku.py
Last active June 22, 2016 21:31
Gomoku game (also called Five in a Row)
class Gomoku:
def __init__(self, size):
self.size = size
self.board = ['-'] * size * size
self.inarow = 5
self.players = ['x', 'o']
self.curplayer = 1
def printboard(self):
s = self.size
@gudnm
gudnm / Twitter.py
Last active June 30, 2016 19:11
Problem #355 on LeetCode: Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed.
class Twitter(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.followers = {}
self.tweets = {}
def postTweet(self, userId, tweetId):
@gudnm
gudnm / andproduct.py
Last active August 19, 2016 15:26
RC Code Dojo Aug 16 2016
n = int(input())
for _ in range(n):
a, b = map(int, input().split())
while b > a:
b &= (b-1)
print(b)
@gudnm
gudnm / prime_rotations.py
Created August 23, 2016 22:25
Project Euler problem #35
n = 1000000
primes = [False, False] + [True]*(n-2)
for i in range(2, int(n**0.5)+1):
if primes[i]:
primes[i*i::i] = [False] * len(primes[i*i::i])
def check_rotations(num):
s = str(num)
return all(primes[int((s+s)[i:i+len(s)])] for i in range(len(s)))
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param A : root node of tree
# @return an integer
import collections
class TrieNode(object):
def __init__(self, letter=None):
self.letter = letter
self.freq = 1
self.suffixes = collections.OrderedDict()
def add(self, word):
node = self
for letter in word:
@gudnm
gudnm / sherlock.lc
Created September 5, 2016 22:19
Sherlock and the Beast problem from Hackerrank, in LOLCODE
HAI 1.1
I HAS A t ITZ A YARN
GIMMEH t
MAEK t A NUMBR
VISIBLE t
BTW I HAS A count ITZ 0
IM IN YR mainloop UPPIN YR count TIL BOTH SAEM count AN t
I HAS A n ITZ A NUMBR
GIMMEH n
VISIBLE n
@gudnm
gudnm / reverse_mn.ipynb
Created October 12, 2016 16:39
Reverse part of linked list
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.