Skip to content

Instantly share code, notes, and snippets.

View gpiancastelli's full-sized avatar

Giulio Piancastelli gpiancastelli

View GitHub Profile
@gpiancastelli
gpiancastelli / cycle.ts
Created August 12, 2019 07:58
A cyclic generator for values in an iterable
// implementation ported from
// https://docs.python.org/3/library/itertools.html#itertools.cycle
function* cycle<T>(iterable: Iterable<T>) {
const saved: T[] = []
for (let element of iterable) {
yield element
saved.push(element)
}
while (saved.length > 0) {
for (let element of saved) {
@gpiancastelli
gpiancastelli / factor_converter.ts
Last active August 12, 2019 08:05
A more general FizzBuzz solution that also works for Raindrops
type Factor = {
n: number,
word: string
};
function createConverter(factors: readonly Factor[]): (n: number) => string {
return (n: number): string => {
const converted = factors.map(f => n % f.n == 0 ? f.word : '').join('');
return converted || `${n}`;
};
@gpiancastelli
gpiancastelli / factor_converter.py
Last active August 12, 2019 08:12
A more general FizzBuzz solution that also works for Raindrops
def createConverter(factors):
def convert(n):
converted = ''.join([
factor[1] if n % factor[0] == 0 else ''
for factor in factors
])
return converted or f'{n}'
return convert
fizzbuzz = createConverter([(3, 'Fizz'), (5, 'Buzz')])
@gpiancastelli
gpiancastelli / goog-a-1.py
Last active August 17, 2018 08:45
Given a sequence of numbers and a value, find a couple of numbers in the sequence whose sum is equal to value. This is the problem Google showcases in their example coding/engineering interview at https://youtu.be/XKu_SEDAykw. This Gist contains possible solutions written in Python.
#
# The first (intuitive but inefficient) solution is to nest two loops
# on the sequence, with the second loop shifted by one w.r.t the first.
#
# Since Python does not offer the same kind of for instruction that
# C-based languages find so suitable to this kind of looping, the
# implementation of this solution uses while loops.
#
def find_pair_with_sum(numbers, s):
@gpiancastelli
gpiancastelli / aoc_2017_day_3.py
Last active December 3, 2017 15:45
Advent of Code 2017 day 3 problem solved in Python
n = 361527
##########
# Part I #
##########
FRONTIER_SIZE_MULTIPLIER = 8
def distance(n):
frame = 1
@gpiancastelli
gpiancastelli / README.md
Last active July 1, 2016 20:42
How to use Poster (Firefox plug-in) to upload a Hearthstone match result to Track-o-bot profile

The popular Track-o-bot Hearthstone tracker offers an API to upload match results to a player's online profile. The simplest way to do it is to download a Firefox plug-in called Poster to send a proper request to the API. After installing it and opening its window, a minimal request can be compiled as follows:

  • URL must be compiled to https://trackobot.com/profile/results.json?username=YOUR_USERNAME&token=YOUR_SECRET_TOKEN where username and token can be obtained on the API page of your online profile Settings section.
  • Content Type must be compiled to application/json.
  • The payload for the request must be a JSON object of the following form:
{
  "result": {
 "hero": "Warlock",
@gpiancastelli
gpiancastelli / TriangleDemo.java
Created February 22, 2013 17:52
Classify a triangle given the length of its three sides as input.
package com.example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class TriangleDemo {
@gpiancastelli
gpiancastelli / Letters.java
Created July 2, 2011 12:55
Print the letters of an input string in alphabetic order alongside their frequency
package com.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Letters {
@gpiancastelli
gpiancastelli / Letters.java
Created June 26, 2011 14:49
Print the letters of an input string in decreasing order of frequency.
package com.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@gpiancastelli
gpiancastelli / KeywordCounter.java
Created April 14, 2011 09:11
A simplified keyword counter for Python scripts, written in Java
package com.example;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;