Skip to content

Instantly share code, notes, and snippets.

@Nagelfar
Created April 1, 2020 18:24
Show Gist options
  • Save Nagelfar/6796337c79fb329458749f28fbf56b16 to your computer and use it in GitHub Desktop.
Save Nagelfar/6796337c79fb329458749f28fbf56b16 to your computer and use it in GitHub Desktop.
SSC Coding Dojo - Manhattan Distance
using System;
using Xunit;
namespace DojoApr
{
public delegate int DistanceCalculation (Point p1, Point p2);
public static class Metrics {
public static readonly Metric MANHATTAN = new Manhattan();
public static readonly DistanceCalculation MANHATTAN1 = new Manhattan().Distance;
}
public abstract class Metric {
public int Distance(Point a, Point b) {
return a.Distance(this, b);
}
public abstract int Distance(int deltaX, int deltaY);
}
public class Manhattan : Metric {
public override int Distance(int deltaX, int deltaY) {
return Math.Abs(deltaX) + Math.Abs(deltaY);
}
}
public class Point
{
private readonly int x;
private readonly int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public int Distance(Metric metric, Point other)
{
return metric.Distance(other.x - x, other.y - y);
}
}
public class ManhattenDistanceTest
{
[Fact]
public void equal_point_should_have_zero_distance()
{
var distance = new Manhattan().Distance(new Point(1, 1),new Point(1, 1));
Assert.Equal(0, distance);
}
[Theory]
// [InlineData(0, 0)] // redundant
[InlineData(1, 1)]
[InlineData(-1, 1)]
[InlineData(int.MaxValue - 2, int.MaxValue - 2)]
// [InlineData(int.MinValue, int.MaxValue)] // int cannot represent this
public void point_should_have_x_distance(int deltaX, int expectedDistance)
{
var distance = Metrics.MANHATTAN.Distance(new Point(2, 5), new Point(2 + deltaX, 5));
Assert.Equal(expectedDistance, distance);
}
[Theory]
[InlineData(1, 1)]
[InlineData(-1, 1)]
public void point_should_have_y_distance(int deltaY, int expectedDistance)
{
var distance = Metrics.MANHATTAN1(new Point(2, 5), new Point(2, 5 + deltaY));
Assert.Equal(expectedDistance, distance);
}
[Theory]
[InlineData(5, -8, 13)]
public void point_should_have_arbitrary_distance(int deltaX, int deltaY, int expectedDistance)
{
var distance = Metrics.MANHATTAN.Distance(new Point(2, 5), new Point(2 + deltaX, 5 + deltaY));
Assert.Equal(expectedDistance, distance);
}
}
/*
* Retrospective #1
*
** Remote
* small bandwith is sufficient
* 3 situations with more people talking - unsure if heard/proceed
* video would have been nice, maybe avoid misunderstanding
*
** Tooling
* tooling worked kind of well, surprised
* tooling problem: code got duplicated when opened, code out of sync
* run test in life share is missing!
* there is a shared terminal, could be used for tests
* switch of control works well with live share, fast switch
*
** Mob
* 7' timer, then 5', no problem in the end, could be shorter even
* there were moments of silence, maybe because we don't know each other that well
* navigator needs to know exactly what needs to be done (shortcuts, etc.)
*
** General
* guest bring new perspectives
* good to explicit focus on learning goal
* C# was no problem
* good to get started with something than discuss for ever
* C# and Java feel like home
*
** Kata
* simple problem, interesting challenges inside?
* 3 examples in slides help to understand requirement
*/
public class EuclideanDistanceTests {
// euclideanDistance( [1, 1], [1, 1] ) // => returns 0
// euclideanDistance( [5, 4], [3, 2] ) // => returns 2,828
// euclideanDistance( [1, 1], [0, 3] ) // => returns 2,236
}
// https://mathepedia.de/Euklidischer_Raum.html
/*
* Retrospective #2
*
** Design Extension
* Tell Don't Ask -> Callback
* design is more discussion, 2nd part more interesting
* static fields vs. dynamic strategies is inconsistent
*
** Mob
* Prototyping together very fast
* less disciplined, too much going on
* more people typing at same time
*
** General
* too much C# syntax
* bring in constructs of my language
*
** Time
* total time 2h15'
* wait for ppl 15'
* Intro 15'
* Setup 5' (C#, Liveshare)
* Code 45'
* Retro 15'
* Code 30'
* Retro 15'
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment