Dispersing Whole Pennies Proportionally to Hours Worked

This test will disperse pennies across a set of workdays proportionally to the hours worked. Pay must not be in fractions of cents and no day can have 0 pay.

The basic strategy is to use the ratio of hours worked to total hours to determine the approximate pay for the day. Round the pay down to an integer by using Math.Floor. Calculate the total pay after doing this. If there are any remaining pennies to disperse, simply sort the list by hours worked and traverse the list, adding any remaining pennies to the remaining days.

Here are the results for dispersing 32 pennies across 8 work days: (date / hours worked / pennies earned)

12/26/09 12 7
12/29/09 10 6
12/27/09 10 6
12/31/09 10 5
12/28/09 7 4
12/30/09 5 2
01/01/10 1 1
12/25/09 1 1

                                
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.Linq;

namespace Test
{
    [TestFixture]
    class DisperseMoney
    {
        public class WorkDay
        {
            public WorkDay(string date, int hours)
            {
                Date = date;
                Hours = hours;
            }
            public string Date { get; set; }
            public int Hours { get; set; }
            public int Pay { get; set; }
        }

        class HoursComparer : IComparer<WorkDay>
        {
            #region IComparer<WorkDay> Members

            public int Compare(WorkDay x, WorkDay y)
            {
                if (x.Hours < y.Hours) return 1;
                if (x.Hours > y.Hours) return -1;
                return 0;
            }

            #endregion
        }
        
        [Test]
        public void Test()
        {
            // Pennies to disperse.
            int pennies = 32;

            // Build a list of workdays.
            List<WorkDay> workDays = new List<WorkDay>();
            workDays.Add(new WorkDay("12/25/09", 1));
            workDays.Add(new WorkDay("12/26/09", 12));
            workDays.Add(new WorkDay("12/27/09", 10));
            workDays.Add(new WorkDay("12/28/09", 7));
            workDays.Add(new WorkDay("12/29/09", 10));
            workDays.Add(new WorkDay("12/30/09", 5));
            workDays.Add(new WorkDay("12/31/09", 10));
            workDays.Add(new WorkDay("01/01/10", 1));

            // Calculate base pay for each day by using ration and rounding down.
            foreach (WorkDay workDay in workDays)
            {
                workDay.Pay = (int)Math.Floor(
                    ((double)workDay.Hours / (double)(workDays.Sum(w => w.Hours))) * pennies);

                // Make sure no days have 0 pay.
                if (workDay.Pay == 0)
                    workDay.Pay = 1;
            }

            // Determine leftover pay.
            int leftover = pennies - workDays.Sum(w => w.Pay);

            // Sort by hours.
            workDays.Sort(new HoursComparer());

            // Disperse remaining pennies via sorted list.
            foreach (WorkDay workDay in workDays)
            {
                if (leftover == 0)
                    break;
                workDay.Pay = workDay.Pay + 1;
                leftover = leftover - 1;
            }

            // Report pay.
            foreach (WorkDay workDay in workDays)
                Console.WriteLine(workDay.Date + " " + workDay.Hours + " " + workDay.Pay);
        }
    }
}

Posted in C#, Software Programming | Leave a comment

bSplineCurve by xFunction, yFunction and zFunction

    

using System;
using Bentley.Geometry;
using Bentley.GenerativeComponents;
using Bentley.GenerativeComponents.MicroStation;
using Bentley.Interop.MicroStationDGN;
using Wolfram.NETLink;

// Must be in this namespace.
namespace Bentley.GenerativeComponents.Features
{

    public class BSplineCurveMath : Features.BSplineCurve
    {

        /// <summary>
        /// Takes an xFunction, yFunction, and zFunction and
        /// generates a curve.
        /// </summary>
        /// TODO: Error handling, ReFactor Generation of points.


        [Update]
        public bool ByXYZFunction
        (
            FeatureUpdateContext updateContext,
            [ParentModel] CoordinateSystem coord,
            [Replicatable] string xFunction,
            [Replicatable] string yFunction,
            [Replicatable] string zFunction
        )
        {
            // This launches the Mathematica kernel:
            IKernelLink ml = MathLinkFactory.CreateKernelLink();

            // Discard the initial InputNamePacket the kernel will send when launched.
            ml.WaitAndDiscardAnswer();

            // Evaluate the definitions for xFunction, yFunction, and zFunction.
            // These are coming from Generative Components Feature Definition.
            // For example, Make the following assignments in GC:
            // xFunction = "X[t_] = Sin[t];"
            // yFunction = "Y[t_] = Cos[t];"
            // zFunction = "t;"
            // This defines a helix in the Mathematica Environment.
            ml.EvaluateToOutputForm(xFunction, 0);
            ml.EvaluateToOutputForm(yFunction, 0);
            ml.EvaluateToOutputForm(zFunction, 0);

            DPoint3d[] points = new DPoint3d[10];


            // This is where the points are assigned.  Refactoring neccecary here.
            points[0] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[0.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[0.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[0.]", 0)));
            points[1] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[1.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[1.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[1.]", 0)));
            points[2] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[2.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[2.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[2.]", 0)));
            points[3] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[3.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[3.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[3.]", 0)));
            points[4] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[4.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[4.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[4.]", 0)));
            points[5] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[5.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[5.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[5.]", 0)));
            points[6] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[6.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[6.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[6.]", 0)));
            points[7] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[7.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[7.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[7.]", 0)));
            points[8] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[8.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[8.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[8.]", 0)));
            points[9] = new DPoint3d(Convert.ToDouble(ml.EvaluateToOutputForm("X[9.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Y[9.]", 0)), Convert.ToDouble(ml.EvaluateToOutputForm("Z[9.]", 0)));

            // Always close the math link.
            ml.Close();

            // Since we are inheriting from base bSplineCurve class,
            // we can call the base to generate the curve based on a collection of DPoint3ds.
            return base.ByPointsAsDPoint3ds(coord, points, false);
        }
    }
}

Posted in Generative Components, Mathematics, Software Programming | Leave a comment

Sol LeWitt – Wall Drawing 38

Drawing 38

Wall Drawing 38 is a wall sculpture designed by Sol LeWitt and first created in 1970 for the Tokyo Biennale.  The piece was reproduced in 2008 for the exhibition “Sol LeWitt: A Wall Drawing Retrospective” at the Massachusetts Museum of Contemporary Art in North Adams, MA and will be on display for 25 years.  The instructions for the piece call for the systematic insertion of tightly rolled and colored origami paper sheets into four peg-board walls.  Wall Drawing 38 is an early work of LeWitt that may have played a role in a transition towards the integration of variable steps and random events into the plans for later wall drawing projects.  Wall Drawing 38 highlights the reproducibility of LeWitt wall drawings, but also questions the validity of the reproduction and LeWitt’s assertion that “the same work can exist in two or more places at the same time” (Chan 14).   Read More »

Posted in Sculpture | Leave a comment