Skip to content

Instantly share code, notes, and snippets.

View sweko's full-sized avatar

Wekoslav Stefanovski sweko

View GitHub Profile
@sweko
sweko / 0.ListDifference.cs
Last active December 29, 2015 10:09
I always need to determine which objects from a collection that was round tripped to the UI were added, removed and changed. The code is usually straight-forward, but repetitive. The ListDifference class is a helper with a simple usage that makes it easy to do that.
/// <summary>
/// Given two collections, determines added, removed and changed items.
/// </summary>
public class ListDifference<T>
{
/// <summary>
/// The items that were added to the collection
/// </summary>
public IEnumerable<T> AddedItems { get; private set; }
@sweko
sweko / FizzBuzzDictionary.cs
Last active January 1, 2016 04:49
C# FizzBuzz (alternative approaches)
void Main()
{
//no comparisons are used, only dictionary lookup
// the key Tuple<bool, bool> determines if the value is divisible by 3 (first bool) and by 5 (second bool)
//the value is a function taking an integer and returning a string
Dictionary<Tuple<bool, bool>, Func<int, string>> dict= new Dictionary<Tuple<bool, bool>, Func<int, string>>();
//if the number is not divisible by anything, return the value of the number
dict.Add(Tuple.Create(false, false), i => i.ToString());
@sweko
sweko / EnumerableHierarchyExtensions.cs
Last active January 1, 2016 05:09
Hierarchy flattening and extraction for same-type nested items.
public static class EnumerableHierarchyExtensions
{
/// <summary>
/// A wrapper for an item that includes the level the item has in the hierarchy
/// </summary>
/// <typeparam name="T"></typeparam>
public class Leveled<T>
{
/// <summary>
/// The wrapped item
@sweko
sweko / UpToHundred.cs
Last active August 29, 2015 14:20
My solution to problem 5 (https://goo.gl/vHqksc) in under 1 hour
void Main()
{
int[] numbers = {1,2,3,4,5,6,7,8,9};
var combinations = GetCombinations(8, 3);
foreach(var comb in combinations)
{
StringBuilder display = new StringBuilder("1");
var running = 0;
var current = 1;
var op = 1;
void Main()
{
var chances = Enumerable.Range(0, 10000000).Select(i => new Chance{Value = i}).ToList();
Stopwatch c = Stopwatch.StartNew();
var fresult = CountChances(chances);
c.Stop();
//c.Dump();
c = Stopwatch.StartNew();
long GetMaxCutout(long number, int skipDigits){
for (int i = 0; i<skipDigits; i++){
number = SkipOneDigit(number);
}
return number;
}
long SkipOneDigit(long number){
var max = long.MinValue;
var strNumber = number.ToString();
@sweko
sweko / 01. Primes.cs
Last active October 14, 2016 12:46
Helper objects for Final Exam of SEDC CodeCademy 2015/2016
int GetPrimeByIndex(int index){
//Terrible, Horrible, No Good, Very Inneficient implementation
List<int> primes = new List<int>{2};
int value = 3;
while (primes.Count != index){
bool isPrime = true;
for (int i=3; i<value; i+=2){
if (value % i == 0){
isPrime = false;
string Solution(int[] source){
var sum = source.Sum();
if (sum % 3 != 0)
return "Impossible";
sum /= 3;
var opts = Enumerable.Range(0, 1 << source.Length)
.Select(i => source.Where((s, x) => ((1 << x) & i)!=0))
.Where(o => o.Sum()==sum)
.ToList();
@sweko
sweko / syncForEach.ts
Created October 24, 2017 18:26
Synchronous version of for each for use with async / await
interface Array<T> {
syncForEach(action: (value: T, index: number, array: T[]) => Promise<void>): void;
}
if (!Array.prototype.syncForEach) {
Array.prototype.syncForEach = async function<T> (action: (value: T, index: number, array: T[]) => Promise<void>): Promise<void> {
if (this == null) {
throw new TypeError('Array.prototype.syncForEach called on null or undefined');
}
render() {
return <FancyInputBox></FancyInputBox>;
}