Skip to content

Instantly share code, notes, and snippets.

Avatar

Giulio Piancastelli gpiancastelli

View GitHub Profile
@gpiancastelli
gpiancastelli / cycle.ts
Created Aug 12, 2019
A cyclic generator for values in an iterable
View cycle.ts
// 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 Aug 12, 2019
A more general FizzBuzz solution that also works for Raindrops
View factor_converter.ts
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 Aug 12, 2019
A more general FizzBuzz solution that also works for Raindrops
View factor_converter.py
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 Aug 17, 2018
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.
View goog-a-1.py
#
# 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 Dec 3, 2017
Advent of Code 2017 day 3 problem solved in Python
View aoc_2017_day_3.py
n = 361527
##########
# Part I #
##########
FRONTIER_SIZE_MULTIPLIER = 8
def distance(n):
frame = 1
@gpiancastelli
gpiancastelli / README.md
Last active Jul 1, 2016
How to use Poster (Firefox plug-in) to upload a Hearthstone match result to Track-o-bot profile
View README.md

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 Feb 22, 2013
Classify a triangle given the length of its three sides as input.
View TriangleDemo.java
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 Jul 2, 2011
Print the letters of an input string in alphabetic order alongside their frequency
View Letters.java
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 Jun 26, 2011
Print the letters of an input string in decreasing order of frequency.
View Letters.java
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 Apr 14, 2011
A simplified keyword counter for Python scripts, written in Java
View KeywordCounter.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;