Advertisement
aadi_pastebin

Final RiTangle Working Q20

Dec 4th, 2023 (edited)
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. /*
  2. IN C#
  3. fraudseter guessing 6 digit password
  4. in some order the password features the digits 123 twice each
  5. "additionally, he knows that no consecutive pair of digits are the same"
  6. what to 3sf is his chance of a successful random guess
  7. */
  8.  
  9.  
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14.  
  15. internal class Program
  16. {
  17.     private static void Main(string[] args)
  18.     {
  19.         // Create a list to store generated numbers
  20.         List<int> master_list = new List<int>();
  21.         Console.WriteLine("Working");
  22.  
  23.         // Generate 620 unique 6-digit numbers and add them to the master list
  24.         for (int i = 0; i < 620; i++)
  25.         {
  26.             // Generate a new 6-digit number
  27.             int new_number = CreateNewNumber();
  28.  
  29.             // Ensure the new number is not a duplicate
  30.             if (!master_list.Contains(new_number))
  31.             {
  32.                 master_list.Add(new_number);
  33.             }
  34.         }
  35.  
  36.         // Display information about the master list
  37.         Console.WriteLine($"Created the master list\n{master_list.Count} items in it");
  38.         Console.WriteLine($"{string.Join(", ", master_list)}");
  39.  
  40.         // Filter the master list based on conditions and create a final list
  41.         List<int> final_list = MakeFinalList(master_list);
  42.  
  43.         // Display information about the final list
  44.         Console.WriteLine($"Created the final list\n{final_list.Count} items in it");
  45.         Console.WriteLine($"{string.Join(", ", final_list)}");
  46.     }
  47.  
  48.     // Function to generate a random 6-digit number with digits 1, 2, and 3 appearing twice each
  49.     static int CreateNewNumber()
  50.     {
  51.         // String to store the generated number
  52.         string big_number = "";
  53.  
  54.         // List of available digits (1, 1, 2, 2, 3, 3)
  55.         List<int> digits_possible = new List<int> {1, 1, 2, 2, 3, 3};
  56.  
  57.         // Loop until all digits are used
  58.         while (digits_possible.Count() != 0)
  59.         {
  60.             // Create a random number generator
  61.             Random random = new Random();
  62.  
  63.             // Randomly select a digit
  64.             int random_index = random.Next(0, digits_possible.Count);
  65.             int random_digit = digits_possible[random_index];
  66.  
  67.             // Remove the selected digit from the list
  68.             digits_possible.RemoveAt(random_index);
  69.  
  70.             // Append the digit to the number
  71.             big_number += random_digit;
  72.         }
  73.  
  74.         // Convert the string to an integer and return the generated number
  75.         return int.Parse(big_number);
  76.     }
  77.  
  78.     // Function to filter a list based on conditions
  79.     static List<int> MakeFinalList(List<int> old_list)
  80.     {
  81.         // List to store the final filtered numbers
  82.         List<int> new_list = new List<int>();
  83.  
  84.         // Iterate over each number in the old list
  85.         foreach (int number in old_list)
  86.         {
  87.             // Convert the number to a string for digit-level analysis
  88.             string number_as_string = number.ToString();
  89.  
  90.             // Ensure that the digits 1, 2, and 3 appear exactly twice each
  91.             if (number_as_string.Count(c => c == '1') == 2 &&
  92.                 number_as_string.Count(c => c == '2') == 2 &&
  93.                 number_as_string.Count(c => c == '3') == 2)
  94.             {
  95.                 // Check for no consecutive pair of digits being the same
  96.                 bool add_to_new_list = true;
  97.                 for (int i = 0; i < 5; i++)
  98.                 {
  99.                     if (number_as_string[i] == number_as_string[i + 1])
  100.                     {
  101.                         // If consecutive digits are the same, set flag to false and break the loop
  102.                         add_to_new_list = false;
  103.                         break;
  104.                     }
  105.                 }
  106.  
  107.                 // If all conditions are met, add the number to the new list
  108.                 if (add_to_new_list)
  109.                 {
  110.                     new_list.Add(number);
  111.                 }
  112.             }
  113.         }
  114.  
  115.         // Return the final filtered list
  116.         return new_list;
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement