Code Monkey home page Code Monkey logo

supr's Introduction

supr

A programme to find a minimal-sum path in a valued triangle.

Usage

In project path, execute sbt run. Of course, you could also use cat << EOF | sbt run instead.

Algorithmic complexity

The programme runs in O(n) for n being the total number of nodes. This is **O() for h the height (number of rows) of the triangle. It is the theoretical limit of complexity, for the simple reason that all nodes must be visited.

Shortcomings

I am not happy with the data structure I used to represent the triangle. A simplified version of my data type is the following:

sealed trait Triangle
case object Empty extends Triangle
case class Cons(base: NonEmptyVector[Int], peak: Triangle) extends Triangle

The problem with this representation is that it does not reflect the fact that the length of base array is equal to the height of the triangle in a Cons.

Of course, this requirement can be enforced by the likes of Eithers or exceptions (in the actual version, I used Eithers). However, these are runtime solutions, in the sense that if you append NonEmptyVector.of(1, 2, 3) to Empty, your code will still compile, albeit at runtime you have a more or less nice error message.

It would be much nicer to enforce the constraint of base size on compile time. Some functional languages, like Idris, support dependent types, types that are specified by a value. If Scala supported dependant types, there could have been a solution like this:

sealed trait Triangle
case object Triangle[0] extends Triangle
case class Triangle[n + 1](base: FixedLengthVector[n + 1][Int], peak: Triangle[n]) extends Triangle

But of course, Scala is not that advanced!

I did try to do implement some sort of fixed-length list using Shapeless HLists, but then I noticed that I do not have much time and abandoned this. I am still confident that this must be possible, with enough time and more proficiency in Shapeless than I have.

I welcome any suggestions!

supr's People

Contributors

shn-amn avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.