Menu
Coddy logo textTech

Math - Set Difference

Coddy'nin C# Journey'sinin Mantık & Akış bölümünün bir parçası — ders 64 / 66.

Küme Farkı işlemi, ilk kümede bulunan ancak ikinci kümede bulunmayan öğelerle yeni bir küme oluşturur.

İki HashSet oluşturun:

HashSet<string> set1 = new HashSet<string>();
HashSet<string> set2 = new HashSet<string>();

Her iki kümeye de eleman ekleyin:

set1.Add("Apple");
set1.Add("Banana");
set1.Add("Cherry");

set2.Add("Banana");
set2.Add("Kiwi");

Farkı hesapla (set1'de olup set2'de olmayan elemanlar):

HashSet<string> difference = new HashSet<string>(set1);
difference.ExceptWith(set2);

Yukarıdaki kodu çalıştırdıktan sonra, fark kümesi şunları içerir:

["Apple", "Cherry"]
challenge icon

Görev

Kolay

GetSetDifference adında bir metot oluşturun ki bu metot iki tamsayı HashSet'i alsın (set1 ve set2) ve küme farkını (set1'de olup set2'de olmayan elemanlar) içeren yeni bir HashSet döndürsün.

Kopya kağıdı

Küme Farkı, birinci kümede bulunan ancak ikinci kümede bulunmayan öğelerle yeni bir küme oluşturur.

HashSet'ler oluşturun:

HashSet<string> set1 = new HashSet<string>();
HashSet<string> set2 = new HashSet<string>();

ExceptWith() kullanarak farkı hesaplayın:

HashSet<string> difference = new HashSet<string>(set1);
difference.ExceptWith(set2);

Kendin dene

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

class Program {
    public static HashSet<int> GetSetDifference(HashSet<int> set1, HashSet<int> set2) {
        // Kodu buraya yazın
        return null;
    }
    
    static void Main(string[] args) {
        // İlk küme için ilk satırı oku
        string line1 = Console.ReadLine();
        HashSet<int> set1 = new HashSet<int>();
        
        // İlk kümenin JSON dizi formatında olup olmadığını kontrol et
        if (line1 != null && line1.StartsWith("[") && line1.EndsWith("]")) {
            try {
                // Köşeli parantezler arasındaki içeriği çıkar
                string arrayContent = line1.Substring(1, line1.Length - 2);
                
                // JSON dizi içeriğini ayrıştırmayı dene
                string[] values = Regex.Split(arrayContent, @",\s*");
                foreach (string value in values) {
                    // Varsa tırnak işaretlerini kaldır
                    string cleanValue = value.Trim();
                    if (cleanValue.StartsWith("\"") && cleanValue.EndsWith("\"")) {
                        cleanValue = cleanValue.Substring(1, cleanValue.Length - 2);
                    }
                    
                    // Tamsayı olarak ayrıştır ve kümeye ekle
                    if (int.TryParse(cleanValue, out int num)) {
                        set1.Add(num);
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine($"Error parsing first set: {ex.Message}");
                return;
            }
        }
        else {
            // İlk küme için geleneksel boşlukla ayrılmış girdiyi işle
            string[] set1Input = line1.Split(' ');
            foreach (string item in set1Input) {
                if (int.TryParse(item, out int num)) {
                    set1.Add(num);
                }
            }
        }
        
        // İkinci küme için ikinci satırı oku
        string line2 = Console.ReadLine();
        HashSet<int> set2 = new HashSet<int>();
        
        // İkinci kümenin JSON dizi formatında olup olmadığını kontrol et
        if (line2 != null && line2.StartsWith("[") && line2.EndsWith("]")) {
            try {
                // Köşeli parantezler arasındaki içeriği çıkar
                string arrayContent = line2.Substring(1, line2.Length - 2);
                
                // JSON dizi içeriğini ayrıştırmayı dene
                string[] values = Regex.Split(arrayContent, @",\s*");
                foreach (string value in values) {
                    // Varsa tırnak işaretlerini kaldır
                    string cleanValue = value.Trim();
                    if (cleanValue.StartsWith("\"") && cleanValue.EndsWith("\"")) {
                        cleanValue = cleanValue.Substring(1, cleanValue.Length - 2);
                    }
                    
                    // Tamsayı olarak ayrıştır ve kümeye ekle
                    if (int.TryParse(cleanValue, out int num)) {
                        set2.Add(num);
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine($"Error parsing second set: {ex.Message}");
                return;
            }
        }
        else {
            // İkinci küme için geleneksel boşlukla ayrılmış girdiyi işle
            string[] set2Input = line2.Split(' ');
            foreach (string item in set2Input) {
                if (int.TryParse(item, out int num)) {
                    set2.Add(num);
                }
            }
        }
        
        HashSet<int> difference = GetSetDifference(set1, set2);
        
        // Sonucu yazdır
        List<int> sortedResult = new List<int>(difference);
        sortedResult.Sort();
        foreach (int item in sortedResult) {
            Console.WriteLine(item);
        }
    }
}
quiz iconKendini test et

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