-6

So I have a lot of strings in my "BIGLIST" which contain multiple conditions, like this: Colors, country, town, good/bad, day, morning/afternoon/evening/night

there are:

5 colors

5 countries

5 town

2 good/bad

7 days

4 morning/afternoon/evening/night

So, 5*5*5*2*7*2 = 3500 possibilities

Some examples of my data:

green england london good sunday evening

red thenetherlands amsterdam bad monday night

blue america newyork bad tuesday morning

So now I want to sort EVERY possibility into a list. So if you have 2x this possiblity in my BIGLIST : blue america newyork bad tuesday morning, the list: "blueamericanewyorkbadtuesdaymorningList".count will return 2.

Now, I dont feel like make 3500lists, either with another name. And ALSO if I want to sort the BIGLIST this was my idea to do it so far: Is this a good way to do it? are there easier ways?

List<string> colorlist = new List<string>();
colorlist[0] = "blue";
colorlist[1] = "red";
//etc
for (int i = 0;i<BIGLIST;i++)
{
   for (int j=0;j<colorlist.count;j++)
   {
       if(BIGLIST[i].contains(colorlist[j])) 
       {
          //etc
       }
   }
}
wouter
  • 359
  • 4
  • 15

2 Answers2

1

You could organize your data as follows:

class Program {
    static void Main(string[] args) {
        List<Criteria> list = new List<Criteria>() {
            new Criteria(Color.green, Country.england, Town.london, GoodBad.good, DayOfTheWeek.sunday, Daytime.evening),
            new Criteria(Color.red, Country.thenetherlands, Town.amsterdam, GoodBad.bad, DayOfTheWeek.monday, Daytime.night),
            new Criteria(Color.blue, Country.america, Town.newyork, GoodBad.bad, DayOfTheWeek.tuesday, Daytime.morning),
        };


        Console.WriteLine("- Native sorting:");
        list.Sort();
        foreach(var criteria in list) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Color:");
        IOrderedEnumerable<Criteria> byColor = list.OrderBy(c => c.Color);
        foreach(var criteria in byColor) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Country:");
        IOrderedEnumerable<Criteria> byCountry = list.OrderBy(c => c.Country);
        foreach(var criteria in byCountry) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Town:");
        IOrderedEnumerable<Criteria> byTown = list.OrderBy(c => c.Town);
        foreach(var criteria in byTown) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Good:");
        IOrderedEnumerable<Criteria> byGood = list.OrderBy(c => c.GoodBad);
        foreach(var criteria in byGood) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By DayOfTheWeek:");
        IOrderedEnumerable<Criteria> byDayOfTheWeek = list.OrderBy(c => c.DayOfTheWeek);
        foreach(var criteria in byDayOfTheWeek) {
            Console.WriteLine(criteria);
        }
        Console.WriteLine();
        Console.WriteLine("- By Daytime:");
        IOrderedEnumerable<Criteria> byDaytime = list.OrderBy(c => c.Daytime);
        foreach(var criteria in byDaytime) {
            Console.WriteLine(criteria);
        }

        Console.ReadKey();
    }
}

sealed class Criteria : IComparable<Criteria> {
    public readonly Color Color;
    public readonly Country Country;
    public readonly Town Town;
    public readonly GoodBad GoodBad;
    public readonly DayOfTheWeek DayOfTheWeek;
    public readonly Daytime Daytime;

    public Criteria(Color color, Country country, Town town, GoodBad goodBad, DayOfTheWeek dayOfTheWeek, Daytime daytime) {
        this.Color = color;
        this.Country = country;
        this.Town = town;
        this.GoodBad = goodBad;
        this.DayOfTheWeek = dayOfTheWeek;
        this.Daytime = daytime;
    }

    public override int GetHashCode() {
        int result = (int)Color | (int)Country << 2 | (int)Town << 5 | (int)GoodBad << 8 | (int)DayOfTheWeek << 9 | (int)Daytime << 12;
        return result;
    }

    public override string ToString() {
        return string.Join(" ", Color, Country, Town, GoodBad, DayOfTheWeek, Daytime);
    }

    public int CompareTo(Criteria that) {
        int result = this.Color.CompareTo(that.Color);
        if(result != 0) {
            return result;
        }
        result = this.Country.CompareTo(that.Country);
        if(result != 0) {
            return result;
        }
        result = this.Town.CompareTo(that.Town);
        if(result != 0) {
            return result;
        }
        result = this.GoodBad.CompareTo(that.GoodBad);
        if(result != 0) {
            return result;
        }
        result = this.DayOfTheWeek.CompareTo(that.DayOfTheWeek);
        if(result != 0) {
            return result;
        }
        result = this.Daytime.CompareTo(that.Daytime);
        return result;
    }
}

//2 bits
enum Color {
    green,
    red,
    blue,
}

//3 bits
enum Country {
    england,
    thenetherlands,
    america,
}

//3 bits
enum Town {
    london,
    amsterdam,
    newyork,
}

//1 bit
enum GoodBad {
    good,
    bad,
}

//3 bits
enum DayOfTheWeek {
    monday,
    tuesday,
    wednesday,
    thursday,
    friday,
    saturday,
    sunday,
}

//3 bits
enum Daytime {
    morning,
    afternoon,
    evening,
    night,
}
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
0

I would achieve this using a custom hashingalgorithm with simple linking and no probing if its the same value. That would be the fastes way.

You could start like using the indexnumbers of enums or lengths of the string result or whatever.

just return the List.Count() and youre good.

if you need more information about hashing you should watch this: https://www.youtube.com/watch?v=0M_kIqhwbFo

EDIT:

after reading your comment, if you just want to generate all permutations you should make enums or list with the possible values for a category. Then adapt the code to c# from this question How to generate all permutations of a list in Python

Community
  • 1
  • 1
Sebastian L
  • 838
  • 9
  • 29