Menu
Coddy logo textTech

בדיקה אם איבר קיים

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

השיטה Contains(element) בודקת אם קיים איבר מסוים ב־HashSet. היא מחזירה true אם האיבר נמצא, ו־false אחרת.

צרו HashSet ריק:

HashSet<string> fruits = new HashSet<string>();

הוסף כמה איברים ל-HashSet:

fruits.Add("Apple");
fruits.Add("Banana");

בדקו אם איבר קיים:

bool hasApple = fruits.Contains("Apple");  // מחזיר true
bool hasOrange = fruits.Contains("Orange"); // מחזיר false

אפשר להשתמש בתוצאה במשפטי תנאי:

if (fruits.Contains("Apple"))
{
    Console.WriteLine("Apple is in the set");
}
challenge icon

אתגר

קל

צרו מתודה בשם ElementExists שמקבלת שני ארגומנטים:

  • HashSet של מחרוזות (set)
  • מחרוזת (element) לבדיקה

המתודה צריכה לבדוק אם האיבר קיים בקבוצה ולהחזיר הודעת מחרוזת:

  • אם האיבר קיים, החזירו "The element 'X' exists in the set"
  • אם האיבר לא קיים, החזירו "The element 'X' does not exist in the set"

כאשר 'X' הוא הערך בפועל של האיבר שנבדק.

נסו בעצמכם

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

public class Program
{
    public static string ElementExists(HashSet<string> set, string element)
    {
        // כתבו כאן את הקוד שלכם
        return "";
    }
    
    public static void Main()
    {
        // קראו את השורה הראשונה כדי לבדוק אם היא בפורמט JSON
        string firstLine = Console.ReadLine();
        string elementToCheck = Console.ReadLine();
        HashSet<string> set = new HashSet<string>();
        
        // בדקו אם הקלט הוא בפורמט של מערך JSON
        if (firstLine != null && firstLine.StartsWith("[") && firstLine.EndsWith("]"))
        {
            try
            {
                // חלצו את התוכן שבין הסוגריים המרובעים
                string arrayContent = firstLine.Substring(1, firstLine.Length - 2);
                
                // השתמשו בביטוי רגולרי כדי למצוא את כל המחרוזות שבמירכאות
                MatchCollection matches = Regex.Matches(arrayContent, @"""([^""]*)""");
                
                foreach (Match match in matches)
                {
                    // הוסיפו את הקבוצה שנלכדה (ללא המירכאות)
                    if (match.Groups.Count > 1)
                    {
                        set.Add(match.Groups[1].Value.Trim());
                    }
                }
                
                // בדקו אם האלמנט הוא בפורמט של מחרוזת JSON
                Match elementMatch = Regex.Match(elementToCheck, @"""([^""]*)""");
                if (elementMatch.Success && elementMatch.Groups.Count > 1)
                {
                    elementToCheck = elementMatch.Groups[1].Value;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error parsing JSON input: {ex.Message}");
                return;
            }
        }
        else
        {
            // עבדו את הקלט המסורתי המופרד בפסיקים
            string[] elements = firstLine.Split(',');
            foreach (string element in elements)
            {
                set.Add(element.Trim());
            }
        }
        
        string result = ElementExists(set, elementToCheck);
        Console.WriteLine(result);
    }
}
quiz iconבחנו את עצמכם

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

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

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