Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- IN C#
- fraudseter guessing 6 digit password
- in some order the password features the digits 123 twice each
- "additionally, he knows that no consecutive pair of digits are the same"
- what to 3sf is his chance of a successful random guess
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- internal class Program
- {
- private static void Main(string[] args)
- {
- // Create a list to store generated numbers
- List<int> master_list = new List<int>();
- Console.WriteLine("Working");
- // Generate 620 unique 6-digit numbers and add them to the master list
- for (int i = 0; i < 620; i++)
- {
- // Generate a new 6-digit number
- int new_number = CreateNewNumber();
- // Ensure the new number is not a duplicate
- if (!master_list.Contains(new_number))
- {
- master_list.Add(new_number);
- }
- }
- // Display information about the master list
- Console.WriteLine($"Created the master list\n{master_list.Count} items in it");
- Console.WriteLine($"{string.Join(", ", master_list)}");
- // Filter the master list based on conditions and create a final list
- List<int> final_list = MakeFinalList(master_list);
- // Display information about the final list
- Console.WriteLine($"Created the final list\n{final_list.Count} items in it");
- Console.WriteLine($"{string.Join(", ", final_list)}");
- }
- // Function to generate a random 6-digit number with digits 1, 2, and 3 appearing twice each
- static int CreateNewNumber()
- {
- // String to store the generated number
- string big_number = "";
- // List of available digits (1, 1, 2, 2, 3, 3)
- List<int> digits_possible = new List<int> {1, 1, 2, 2, 3, 3};
- // Loop until all digits are used
- while (digits_possible.Count() != 0)
- {
- // Create a random number generator
- Random random = new Random();
- // Randomly select a digit
- int random_index = random.Next(0, digits_possible.Count);
- int random_digit = digits_possible[random_index];
- // Remove the selected digit from the list
- digits_possible.RemoveAt(random_index);
- // Append the digit to the number
- big_number += random_digit;
- }
- // Convert the string to an integer and return the generated number
- return int.Parse(big_number);
- }
- // Function to filter a list based on conditions
- static List<int> MakeFinalList(List<int> old_list)
- {
- // List to store the final filtered numbers
- List<int> new_list = new List<int>();
- // Iterate over each number in the old list
- foreach (int number in old_list)
- {
- // Convert the number to a string for digit-level analysis
- string number_as_string = number.ToString();
- // Ensure that the digits 1, 2, and 3 appear exactly twice each
- if (number_as_string.Count(c => c == '1') == 2 &&
- number_as_string.Count(c => c == '2') == 2 &&
- number_as_string.Count(c => c == '3') == 2)
- {
- // Check for no consecutive pair of digits being the same
- bool add_to_new_list = true;
- for (int i = 0; i < 5; i++)
- {
- if (number_as_string[i] == number_as_string[i + 1])
- {
- // If consecutive digits are the same, set flag to false and break the loop
- add_to_new_list = false;
- break;
- }
- }
- // If all conditions are met, add the number to the new list
- if (add_to_new_list)
- {
- new_list.Add(number);
- }
- }
- }
- // Return the final filtered list
- return new_list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement