Menu
Coddy logo textTech

מתמטיקה - הפרש קבוצות

חלק מהיחידה לוגיקה וזרימת תוכנית במסלול ה-C# של Coddy. שיעור 64 מתוך 66.

פעולת הפרש קבוצות יוצרת קבוצה חדשה עם איברים שקיימים בקבוצה הראשונה אך לא בקבוצה השנייה.

צרו שתי קבוצות HashSet:

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

הוסיפו איברים לשתי הקבוצות:

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

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

חשבו את ההפרש (האיברים שנמצאים ב־set1 אך לא ב־set2):

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

לאחר הרצת הקוד שלמעלה, קבוצת ההפרש מכילה:

["Apple", "Cherry"]
challenge icon

אתגר

קל

צרו מתודה בשם GetSetDifference שמקבלת שתי קבוצות HashSet של מספרים שלמים (set1 ו-set2) ומחזירה HashSet חדש שמכיל את הפרש הקבוצות (הפריטים שנמצאים ב-set1 ואינם נמצאים ב-set2).

נסו בעצמכם

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) {
        // כתבו את הקוד שלכם כאן
        return null;
    }
    
    static void Main(string[] args) {
        // קראו את השורה הראשונה עבור הקבוצה הראשונה
        string line1 = Console.ReadLine();
        HashSet<int> set1 = new HashSet<int>();
        
        // בדקו אם הקבוצה הראשונה היא בפורמט של מערך JSON
        if (line1 != null && line1.StartsWith("[") && line1.EndsWith("]")) {
            try {
                // חלצו את התוכן שבין הסוגריים המרובעים
                string arrayContent = line1.Substring(1, line1.Length - 2);
                
                // נסו לפענח את תוכן מערך ה-JSON
                string[] values = Regex.Split(arrayContent, @",\s*");
                foreach (string value in values) {
                    // הסירו מירכאות, אם ישנן
                    string cleanValue = value.Trim();
                    if (cleanValue.StartsWith("\"") && cleanValue.EndsWith("\"")) {
                        cleanValue = cleanValue.Substring(1, cleanValue.Length - 2);
                    }
                    
                    // המירו למספר שלם והוסיפו לקבוצה
                    if (int.TryParse(cleanValue, out int num)) {
                        set1.Add(num);
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine($"Error parsing first set: {ex.Message}");
                return;
            }
        }
        else {
            // עבדו את הקלט המסורתי המופרד ברווחים עבור הקבוצה הראשונה
            string[] set1Input = line1.Split(' ');
            foreach (string item in set1Input) {
                if (int.TryParse(item, out int num)) {
                    set1.Add(num);
                }
            }
        }
        
        // קראו את השורה השנייה עבור הקבוצה השנייה
        string line2 = Console.ReadLine();
        HashSet<int> set2 = new HashSet<int>();
        
        // בדקו אם הקבוצה השנייה היא בפורמט של מערך JSON
        if (line2 != null && line2.StartsWith("[") && line2.EndsWith("]")) {
            try {
                // חלצו את התוכן שבין הסוגריים המרובעים
                string arrayContent = line2.Substring(1, line2.Length - 2);
                
                // נסו לפענח את תוכן מערך ה-JSON
                string[] values = Regex.Split(arrayContent, @",\s*");
                foreach (string value in values) {
                    // הסירו מירכאות, אם ישנן
                    string cleanValue = value.Trim();
                    if (cleanValue.StartsWith("\"") && cleanValue.EndsWith("\"")) {
                        cleanValue = cleanValue.Substring(1, cleanValue.Length - 2);
                    }
                    
                    // המירו למספר שלם והוסיפו לקבוצה
                    if (int.TryParse(cleanValue, out int num)) {
                        set2.Add(num);
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine($"Error parsing second set: {ex.Message}");
                return;
            }
        }
        else {
            // עבדו את הקלט המסורתי המופרד ברווחים עבור הקבוצה השנייה
            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);
        
        // הדפיסו את התוצאה
        List<int> sortedResult = new List<int>(difference);
        sortedResult.Sort();
        foreach (int item in sortedResult) {
            Console.WriteLine(item);
        }
    }
}
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה לוגיקה וזרימת תוכנית

תרגלו בעצמכם: קומפיילר C# אונליין