C#: Sort lists of bits by which end they congregate at.

Say, you’ve got several lists which contain two potential values for each item, such as:

10010
11000
00101

and you’d like to sort them like a seesaw, from most weighed down on the right to most weighed down on the left.

 


and sort them like so 0010110010, 11000

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Polarity
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("indeed.");
            Random random = new Random();
            LinkedList[] arrayLL = new LinkedList[10];
 
            //Make 10 lists: each with 10 items
            //for each set of items there must be three 1's, in random locations
            //then sort the ten lists by their closeness towards the end 
 
            for (int i = 0; i < 10; i++)
            {
                LinkedList distinctOrder = new LinkedList();
                while (distinctOrder.Count < 10)
                {
                    int randomNumber = random.Next(0, 10);
                    if (!distinctOrder.Contains(randomNumber))
                        distinctOrder.AddLast(randomNumber);
                }
                Console.WriteLine();
 
                //now that i have my distinct list random 1-10 order
                //make 0,1,2 into 1's and the rest into 0's
                LinkedList polList = new LinkedList();
                foreach (var num in distinctOrder)
                {
                    if (num == 0 || num == 1 || num == 2)
                        polList.AddLast(1);
                    else
                        polList.AddLast(0);
                }
                foreach (var num in polList)
                {
                    Console.Write(num + " ");
                }
                arrayLL[i] = polList;
            }
 
            Console.WriteLine("\r\n-------------------");
            Dictionary dictionary = new Dictionary();
 
            //figure out the score of each line and add it to dictionary
            //the position of the 1's indicidate their score towards the end
            for (int i = 0; i < arrayLL.Length; i++)
            {
                int[] thisLine = arrayLL[i].ToArray();
                int sum = 0;
                string line = "";
                for (int k = 0; k < thisLine.Length; k++)                 
                {                    
                         line += " " + thisLine[k];                    
                         if (thisLine[k] == 1)                         
                                  sum += k;                 
                 }                 
                 Console.WriteLine(line+"    "+sum);                
                 //add string of 10 to dictionary object                
                 //Warning! this will crash if the randomized string repeats                
                dictionary.Add(line, sum);                                 
          }             
           Console.WriteLine("-------------------");             
            //sort lines from highest score to lowest             
            var sortedDict = (from entry in dictionary orderby entry.Value descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
            foreach (var strKey in sortedDict.Keys)
                Console.WriteLine(strKey);
 
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *