[Learning log] 2023.01.18 C# container comprehensive practice

C# Container Comprehensive Exercise

Question 1: Basic Dictionary Exercises

 1. Create a dictionary dictA, where Key is a string and Value is an integer.

 2. Add a total of 26 keys from "a" to "z" to this dictionary, and the values ​​are random numbers from 1 to 10.

 3. For the dictionary dictA, delete all key-value pairs whose value is even.

 4. Create the same dictionary dictB again, and also delete the keys with even numbers.

 5. The two dictionaries are then merged into a new dictionary. For the part with the same Key, the value of dictA shall prevail

using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApp9
{
     
    class Program
    {
        static void InitDic(Dictionary<char, int> dic)
        {           
            Random r = new Random();
            string a = $"a";
            int asc = Asc(a);
            for (int i = 0; i < 26; i++)
            {
                dic.Add((char)asc, r.Next(1, 10));
                asc++;
            }
        }
 
        public static int Asc(string s)
        {
            if(s.Length == 1)
            {
                ASCIIEncoding a = new ASCIIEncoding();
                int b = (int)a.GetBytes(s)[0];
                return b;
            }
            else
            {
                throw new Exception("String is not vaild");
            }
        }
 
        public static char Cha(string s)
        {
            int a = int.Parse(s);
            if(a >=0 && a <= 255)
            {
                char c = (char)a;
                return c;
            }
            else
            {
                throw new Exception("String is not vaild");
            }
        }
        static void PrintDic(Dictionary<char,int> dict)
        {
            foreach(var pair in dict)
            {
                Console.WriteLine(pair.Key + " " + pair.Value);
            }
        }
        static void Deleven(Dictionary<char,int> dict)
        {
            List<char> L = new List<char>();
 
            foreach(var pair in dict)
            {              
                if (pair.Value % 2 == 0)
                {
                    L.Add(pair.Key);
                }
            }
            foreach(var key in L)
            {
                dict.Remove(key);
            }
        }
        static void CombineAB(Dictionary<char, int> DA, Dictionary<char, int> DB, Dictionary<char, int> DC)
        {
            foreach (var pair in DA)
            {
                DC[pair.Key] = pair.Value;
            }
 
            foreach (var pair in DB)
            {
                if (!DA.ContainsKey(pair.Key))
                {
                    DC[pair.Key] = pair.Value;
                }
            }          
        }
 
        static void CombineAB2(Dictionary<char, int> DA, Dictionary<char, int> DB, Dictionary<char, int> DC)
        {
            foreach (var pair in DB)
            {
                DC[pair.Key] = pair.Value;
            }
            foreach (var pair in DA)
            {
                DC[pair.Key] = pair.Value;
            }
 
        }
 
 
        static void Main(string[] args)
        {
            Dictionary<char, int> dicA = new Dictionary<char, int>();
            InitDic(dicA);
            PrintDic(dicA);
            Console.WriteLine();
 
            Deleven(dicA);
            PrintDic(dicA);
            Console.WriteLine();
 
            Dictionary<char, int> dicB = new Dictionary<char, int>();
            InitDic(dicB);
            PrintDic(dicB);
            Console.WriteLine();
 
            Deleven(dicB);
            PrintDic(dicB);
            Console.WriteLine();
 
            Dictionary<char, int> dicC = new Dictionary<char, int>();
            CombineAB(dicA,dicB,dicC);
            //或者 CombineAB2(dicA,dicB,dicC);
            PrintDic(dicC);
        }
    }
}

Question 2: Dictionary and List Conversion

 1. Randomly generate a list of strings

 2. Use the first item in this list as key, the second item as value; the third item as key, the fourth item as value...and so on. generate a new dictionary

 3. Then convert this new dictionary into a new list

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp10
{
    class Program
    {
        static string Randomxiang()
        {
            Random R = new Random();
            string str = string.Empty;
            string Rch = "ABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789abcdefghijklmnopqrstuvwxyz";
            //string[] Rch = new string[R.Next(0, 10000)];
            for (int i = 0; i < 2; i++)
            {
                str += Rch[R.Next(Rch.Length)];
            }
            return str;
        }
        static string[] Randomstr(int length)
        {
            string[] strs = new string[length];
            for (int i = 0; i < length; i++)
            {
                strs[i] = Randomxiang();
            }
            return strs;
        }

        static void PrintString(string[] str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                Console.Write(str[i]);
                Console.Write(" ");
            }
            Console.WriteLine();
        }

        static void PrintDic(Dictionary<string, string> dict)
        {
            foreach (var pair in dict)
            {
                Console.WriteLine("(" + pair.Key + " " + pair.Value + ")");
            }
        }
        static List<string> CrList(Dictionary<string,string> dict)
        {
            List<string> L = new List<string>();
            foreach(var pair in dict)
            {
                L.Add(pair.Key);
                L.Add(pair.Value);
            }
            return L;
        }
        static void PrintList(List<string> list)
        {
            for(int i = 0; i < list.Count; i++)
            {
                Console.Write(list[i] + " ");
            }
        }
        
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string[] str = Randomstr(n);
            PrintString(str);

            Dictionary<string, string> D1 = new Dictionary<string, string>();
            for (int i = 0; i < n; i += 2)
            {
                D1.Add(str[i], str[i + 1]);
            }
            PrintDic(D1);
            PrintList(CrList(D1));
        }
    }
}

Question 3: C# Advanced Dictionary Nesting

1. Create a dictionary dictA, where Key is a string and Value is an integer.

2. Add a total of 26 keys from "a" to "z" to this dictionary, and the values ​​are random numbers from 1 to 10.

3. Create dictionary dictB again in the same way.

Build a complex new dictionary Dictionary<string, List<int>> dictC;

Merge dictA and dictB, the part with the same Key, keep all the values

(A and B may have duplicate Keys, but since the value of dictC is a list, multiple values ​​can be reserved).

(101 messages) ⭐️C# from zero basics to advanced ⭐️| The ultimate summary of the nested use of dictionaries and lists! _Little Y's blog who just knocked on the code-CSDN blog_Addition and deletion of nested lists in c#

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void InitDic(Dictionary<char,int> dict)
        {
            Random r = new Random();
            string a = $"a";
            int asc = Asc(a);
            for(int i = 0; i < 26; i++)
            {
                dict.Add((char)asc, r.Next(1, 10));
                asc++;
            }
        }

        public static int Asc(string s)
        {
            if (s.Length == 1)
            {
                ASCIIEncoding a = new ASCIIEncoding();
                int b = (int)a.GetBytes(s)[0];
                return b;
            }
            else
            {
                throw new Exception("String is not vaild");
            }
        }
        public static char Cha(string s)
        {
            int a = int.Parse(s);
            if(a>0 && a < 255)
            {
                char c = (char)a;
                return c;
            }
            else
            {
                throw new Exception("String is not vaild");
            }
        }
        static void PrintDic(Dictionary<char,int> dict)
        {
            foreach(var Pair in dict)
            {
                Console.Write("("+Pair.Key + " " + Pair.Value+")");
            }
            Console.WriteLine();
        }
        static void PrintDicC(Dictionary<string,List<int>> dict)
        {
            foreach (var pair in dict)
            {
                foreach(var val in pair.Value)
                {
                    Console.Write("(" + pair.Key + " " + val + ")");
                }
                
            }
            Console.WriteLine();
        }

        static void Main()
        {
            Dictionary<char, int> dictA = new Dictionary<char, int>();
            InitDic(dictA);
            PrintDic(dictA);
            
            Dictionary<char, int> dictB = new Dictionary<char, int>();
            InitDic(dictB);
            PrintDic(dictB);

            Dictionary<string, List<int>> dictC = new Dictionary<string, List<int>>();
            
            foreach(var paira in dictA)
            {
                List<int> list = new List<int>();
                string str = paira.Key.ToString();
                foreach(var pairb in dictB)
                {
                    if(pairb.Key == paira.Key)
                    {
                        list.Add(paira.Value);
                        list.Add(pairb.Value);
                    }
                }             
                dictC.Add(str,list);
            }
            PrintDicC(dictC);
        }
    }
}

Guess you like

Origin blog.csdn.net/Angelloveyatou/article/details/128729778