Math - Symmetric Difference
Coddy'nin C# Journey'sinin Mantık & Akış bölümünün bir parçası — ders 65 / 66.
İki kümenin simetrik farkı, kümelerin herhangi birinde ama kesişimlerinde olmayan öğeleri içerir (tam olarak bir kümede yer alan öğeler).
İki HashSet nesnesi oluşturun:
HashSet<int> set1 = new HashSet<int>() { 1, 2, 3, 4 };
HashSet<int> set2 = new HashSet<int>() { 3, 4, 5, 6 };Sonuç için yeni bir HashSet oluşturun:
HashSet<int> symmetricDifference = new HashSet<int>(set1);Simetrik farkı hesaplamak için SymmetricExceptWith metodunu kullanın:
symmetricDifference.SymmetricExceptWith(set2);Yukarıdaki kodu çalıştırdıktan sonra, symmetricDifference kümesi şunları içerir:
{ 1, 2, 5, 6 }3 ve 4 elemanları her iki kümede de bulunduğu için simetrik farkta görünmezler.
Görev
KolayFindSymmetricDifference adında iki parametre alan bir metot oluşturun:
- Tamsayılar içeren bir HashSet (
set1) - Tamsayılar içeren bir HashSet (
set2)
Metot, iki kümenin simetrik farkını içeren yeni bir HashSet<int> döndürmelidir. Programın geri kalanı (giriş okuma, sıralama ve sonuçları yazdırma) sizin için zaten sağlanmıştır.
Kendin dene
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static HashSet<int> FindSymmetricDifference(HashSet<int> set1, HashSet<int> set2)
{
// Kodunuzu buraya yazın
}
static void Main(string[] args)
{
string[] input1 = Console.ReadLine().Split(',');
string[] input2 = Console.ReadLine().Split(',');
HashSet<int> set1 = new HashSet<int>();
HashSet<int> set2 = new HashSet<int>();
foreach (string s in input1)
{
if (int.TryParse(s.Trim(), out int num))
{
set1.Add(num);
}
}
foreach (string s in input2)
{
if (int.TryParse(s.Trim(), out int num))
{
set2.Add(num);
}
}
HashSet<int> result = FindSymmetricDifference(set1, set2);
// Sonucu artan sırada yazdır
int[] orderedResult = result.OrderBy(x => x).ToArray();
foreach (int num in orderedResult)
{
Console.WriteLine(num);
}
}
}Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.
Mantık & Akış bölümündeki tüm dersler
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety9HashMap Part 1
What is a HashMap?Declare a HashMapCheck If Key ExistsAccessing ValuesModifying DictionariesRecap - HashMap12HashSet Part 2
Math - Union of HashSetsMath - Intersection of HashSetMath - Set DifferenceMath - Symmetric DifferenceIterating Over Sets