Menu
CoddyTech

קבוצות ובדיקת חברות

שיעור 19 מתוך 48 בקורס C# למפתחי C של Coddy.

ל־C אין טיפוס של קבוצה. האחריות לייחודיות מוטלת על הקוד שלך: סורקים את המערך לפני כל הוספה, כמו בדוגמה שלהלן, פעולה שעולה מעבר אחד לכל הוספה, או ממיינים באמצעות qsort ומסירים איברים סמוכים שמשווים כשווים. ב־C#‏ HashSet<T> אוכף ייחודיות עבורך באמצעות בדיקות חברות בגיבוב, ו־Add מחזירה false עבור כפילות במקום לאחסן אותה. קבוצת גיבוב אינה שומרת את האיברים שלה ממוינים, ולכן כשדוח דורש סדר, יש למיין אותו במפורש או לבחור ב־SortedSet<T>.

HashSet<T> מייצגת ייחודיות ובדיקות חברות מהירות. היא אינה רצף ממוין, וסדר המעבר על איבריה אינו אמור לקבוע את סדר הדוח. משווה קובע שוויון בין מחרוזות. חברות בקבוצה ומיקום ברצף עונים על שאלות שונות.

דוגמה ב־C

#include <stdio.h>
#include <string.h>

static int contains(const char **items, size_t count, const char *item) {
    for (size_t i = 0; i < count; i++)
        if (strcmp(items[i], item) == 0) return 1;
    return 0;
}

int main(void) {
    const char *input[] = { "a", "a", "b" };
    const char *seen[3];
    size_t count = 0;
    for (size_t i = 0; i < 3; i++)
        if (!contains(seen, count, input[i])) seen[count++] = input[i];
    printf("%s\n", contains(seen, count, "b") ? "yes" : "no");
    return 0;
}

פקודות C# בתוך Main

var seen = new HashSet<string> { "a", "a", "b" };
Console.WriteLine(seen.Contains("b") ? "yes" : "no");

אלה קטעים ממוקדים לצורכי לימוד. קובצי האתגר כוללים מעטפות מלאות שאפשר להריץ; קטעי הקוד שלמעלה מציגים את ההצהרות והפקודות הרלוונטיות.

פלט לדוגמה:

yes

פלט C#:

yes
challenge icon

אתגר

קל

החזר את מספר המילים הייחודיות, תוך הבחנה בין אותיות גדולות לקטנות, ברשימה מופרדת בפסיקים של מילים שאינן ריקות. שורה ריקה פירושה שאין מילים.

ערוך את קובצי C# הלא נעולים. הקובץ הנעול הגלוי Main.cs קורא את השורות המתוארות, קורא ל־Exercise.Solve, מוודא שהקלט לא השתנה ומדפיס את התוצאה עם תו שורה חדשה אחד בסוף. השאר את ממשקי ה־API שהוגדרו. אל תוסיף Main שני ואל תדפיס פלט אבחון.

נסו בעצמכם

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = Words(input[0]); string before0=Snapshot(a0);

var result = Exercise.Solve(a0);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}
quiz iconבחנו את עצמכם

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

כל השיעורים ביחידה C# למפתחי C

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