This file contains 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
def f(word): | |
open = 0 | |
for x in word: | |
if x == "(": | |
open += 1 | |
elif x == ")": | |
open += -1 | |
if open < 0: | |
return False | |
return open==0 |
This file contains 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
TimeDelta(hours:1,minutes:30) |
This file contains 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
public static class ObservableExtensions | |
{ | |
/// <summary> | |
/// Group observable sequence into buffers separated by periods of calm | |
/// </summary> | |
/// <param name="source">Observable to buffer</param> | |
/// <param name="calmDuration">Duration of calm after which to close buffer</param> | |
/// <param name="maxCount">Max size to buffer before returning</param> | |
/// <param name="maxDuration">Max duration to buffer before returning</param> | |
public static IObservable<IList<T>> BufferUntilCalm<T>(this IObservable<T> source, TimeSpan calmDuration, Int32? maxCount=null, TimeSpan? maxDuration = null) |
This file contains 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
public static class LINQExtensions | |
{ | |
public static IEnumerable<IGrouping<TKey, TElement>> GroupAdjacentBy<TElement, TKey>(this IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer=null) | |
{ | |
comparer = comparer ?? EqualityComparer<TKey>.Default; | |
List<TElement> elements = null; | |
TKey key = default(TKey); | |
TKey lastKey = default(TKey); | |
foreach (var x in source) | |
{ |
This file contains 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
#!/usr/bin/env coffee | |
process.stdin.resume() | |
require('tty').setRawMode(true) | |
history = {} | |
plans = {} | |
spew = () -> console.log(new Date) | |
setInterval spew, 1000 |
This file contains 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
public static class EnumerableExtensions | |
{ | |
/// <summary> | |
/// Perform an action on each element of an IEnumerable<T>, optionally specifying a different action for the final item. | |
/// </summary> | |
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action, Action<T> finalAction=null) | |
{ | |
if (source == null) | |
{ | |
throw new ArgumentNullException("source"); |
This file contains 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
public static class ObservableExtensions | |
{ | |
public static IObservable<TSource> Where<TSource> (this IObservable<TSource> source, Func<TSource,bool> predicate) | |
{ | |
if (source == null) | |
{ | |
throw new ArgumentNullException("source"); | |
} | |
if (predicate == null) | |
{ |
This file contains 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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics.Contracts; | |
using System.Linq; | |
/// <summary> | |
/// A dictionary that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end. | |
/// </summary> | |
/// <typeparam name="TKey">The type of keys</typeparam> |
This file contains 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
#!python | |
# https://code.google.com/codejam/contest/2434486/dashboard | |
# Armin is playing Osmos, a physics-based puzzle game developed by Hemisphere Games. In this game, he plays a "mote", moving around and absorbing smaller motes. | |
# lemma if we delete a mote size x, we should delete all motes greater than size x | |
import fileinput | |
f = fileinput.input() | |
T = int(f.readline()) |
This file contains 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
#!python | |
# -*- coding: utf-8 -*- | |
# http://blog.jgc.org/2013/04/the-minimum-coin-problem-with-prize.html | |
# I have in my pocket the following British coins: one £2, two £1, two 50p, three 20p, one 10p, two 5p, two 2p and two 1p. I wish to buy a copy of today's paper at a cost of £1.70. | |
# What is the maximum number of coins I can use to pay for the paper? With the restriction that I pay in a reasonable manner (e.g. I could give exactly £1.70 or an amount greater than that but without giving extraneous coins: e.g. giving the seller all the coins would not be acceptable, but giving him one £1 an two 50ps and expecting 30p in change is OK). The reasonable manner is simply: if I give the seller a set of coins he should not be able to return any to me without the sum of the remaining coins dropping below £1.70. | |
from collections import Counter, OrderedDict | |
def solve_exact(coins, P, f=len): |
OlderNewer