2

質問は、金融商品、金利、および特定の商品がどの利息を持っているかを決定する2つのプロパティに関連しています。しかし、果物やバスケットはこれを視覚化する方が簡単だと思います。

まず、実があります。果物は特定のサイズ(小、中、大)と色(赤、緑、青)を持つことができます。これらは2つの異なる列挙になります。バスケットにはあらゆる種類の果物が入っていますが、色や形が同じものは2つありません。しかし、すべての可能な組み合わせがバスケットに含まれます。3つのサイズと3つの色があるので、すべてのバスケットに9個の果物が入っています。4色だと12個になります。

バスケットごとにバスケット情報を保存します。すべてのバスケットには、バスケット内のすべての果物を定義する辞書<{Size、Color}、Fruit>があります。ただし、この辞書は完全にいっぱいではない可能性があります。その場合、他のすべての組み合わせはリンゴです。辞書には、異なる種類の果物のみが含まれています。(リンゴを入れることもできますが。)リンゴの隣には、梨やバナナもあります。ええ、それらは赤かもしれませんが、私はそれらを赤にするためにどのような塗料が使われたのだろうかと思います。これは問題を視覚化するためだけのものであることを忘れないでください。:-)

とにかく、私は今、バスケットごとに果物のリストを含むバスケットのリストを持っています。デフォルトではリンゴ、辞書にある場合は梨またはバナナ。しかし、私は別の角度から情報を見る必要があります。

この構造を果物のリストに変換する必要があります。果物ごとに、それらが見つかるバスケットに加えて、特定の果物のサイズと色を変換します。したがって、バナナはバスケット1({small、yellow}、{small、red}、{medium、red})、バスケット3(...)、バスケット4、8、および10にあります。梨と同じですが、私はできます。バスケット1には黄色の洋ナシが入っています。これは、バナナ用にすでに定義されているためです。

私には大きな利点があります。これらの構造はまだ明確に定義されていません。ただし、変換プロセスに情報を提供する方法として、バスケットビューが必要です。さらに計算するには、フルーツビューが必要です。サイズや色、またはフルーツの元のバスケットではなく、フルーツ自体だけに基づいて追加の計算を行う必要があるためです。

では、変換前後の構造と、C#でLinqを使用して変換自体を実行する方法についての良い提案はありますか?


実際には、バスケットは製品です。サイズと色は、製品の小さな差異です。実は金利であり、私は数学を行うためにこの金利だけが必要です。金利で商品をグループ化することで、計算回数を数回に減らすことができます。1つの商品あたり約(10x10)100の異なるバリエーションを持つ、1600以上の商品を扱っているため、合計で160.000の金利になります。率自体は一般に3〜7.5%で、1/20パーセントに丸められます。したがって、約90の異なるレートにより、160.000の計算ではなく90の計算が行われます。

そして、私は経営陣にこのステップを踏むように説得する必要があります。なぜなら、彼らはそれが多くの仕事であり、読めなくなったり、維持するのが難しくなることを恐れているからです。


金利に基づいて、特定の条件の製品に1か月に費やす意思がある金額に基づいて、人が融資できる金額を計算できます。この最適化により、異なる製品間ではるかに高速な比較を行うことができます。残念ながら、私はこの興味深い最適化に気付いた会社の最初の人です!:-)

4

1 に答える 1

2

さて、私が始めたら、私はコード全体を書かなければなりませんでした。多分それはあなたの根本的な問題を解決しないかもしれませんが、私はそれがフルーツバスケットのことを釘付けにしていると思います。

public enum FruitSize{Small, Medium, Large}
public enum FruitColor {Red, Green, Blue}

  // immutable struct is important for use as dictionary keys
public struct FruitDescription
{
    readonly FruitSize _size;
    public FruitSize Size {get{return _size;}}
    readonly FruitColor _color;
    public FruitColor Color { get { return _color; } }
    public FruitDescription(FruitSize size, FruitColor color)
    {
        _size = size;
        _color = color;
    }
}
    //abstract parent class ...
public abstract class Fruit
{ public FruitDescription Description {get;set;} }
    //... and children
public class Apple : Fruit{}
public class Banana : Fruit{}
public class Pear : Fruit{}

public class Basket
{
    private Dictionary<FruitDescription, Fruit> internalFruits =
        new Dictionary<FruitDescription, Fruit>();

    public void AddFruit(Fruit addme)
    {
        internalFruits[addme.Description] = addme;
    }

    public IEnumerable<FruitDescription> AllDescriptions()
    {
        foreach (FruitSize size in Enum.GetValues(typeof(FruitSize)))
        {
            foreach (FruitColor color in Enum.GetValues(typeof(FruitColor)))
            {
                FruitDescription desc = new FruitDescription(size, color);
                yield return desc;
            }
        }
    }

    public Apple GetDefaultApple(FruitDescription desc)
    {
        return new Apple() { Description = desc };
    }

    public IEnumerable<Fruit> GetFruits()
    {
        IEnumerable<Fruit> result = AllDescriptions()
            .Select(desc =>
              internalFruits.ContainsKey(desc) ?
              internalFruits[desc] :
              GetDefaultApple(desc));
        return result;
    }
}

public class Pair<T, U>
{
    public T First { get; set; }
    public U Second { get; set; }
}

public class TestClass
{
    public static void Test()
    {
        Basket b1 = new Basket();
        b1.AddFruit(new Banana() { Description =
            new FruitDescription(FruitSize.Medium, FruitColor.Blue) });
        b1.AddFruit(new Banana() { Description =
            new FruitDescription(FruitSize.Medium, FruitColor.Green) });

        Basket b2 = new Basket();
        b2.AddFruit(new Pear() { Description =
            new FruitDescription(FruitSize.Medium, FruitColor.Green) });

        List<Basket> source = new List<Basket>();
        source.Add(b1);
        source.Add(b2);

        //the main event - a query.
        List<Pair<Fruit, Basket>> result =
        (
          from basket in source
          from fruit in basket.GetFruits()
          select new Pair<Fruit, Basket>()
          { First = fruit, Second = basket }
        ).ToList();

        //a second results structure for fun
        ILookup<Type, Basket> resultByType = result.ToLookup
        (
            p => p.First.GetType(),
            p => p.Second
        );

        Console.WriteLine("Number of fruit: {0}",
            result.Count);
        Console.WriteLine("Number of apples: {0}",
            resultByType[typeof(Apple)].Count());
        Console.WriteLine("Number of baskets with apples: {0}",
            resultByType[typeof(Apple)].Distinct().Count());
        Console.WriteLine("Number of bananas: {0}",
            resultByType[typeof(Banana)].Count());
        Console.WriteLine("Number of baskets with bananas: {0}",
            resultByType[typeof(Banana)].Distinct().Count());
    }

}

これらの結果で:

Number of fruit: 18
Number of apples: 15
Number of baskets with apples: 2
Number of bananas: 2
Number of baskets with bananas: 1
于 2009-09-01T01:52:11.307 に答える