נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
25
חלקPart 5: Java מודרנית וכליםModern Java & tools כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Collections לעומקCollections in depth

List, Set, Map ו-Queue, מימושים שונים, סיבוכיות זמן, ומתי לבחור מה.

List, Set, Map and Queue, different implementations, time complexity, and when to choose which.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. צריך לבדוק הרבה פעמים "האם המילה כבר נראתה?". מה הכי מהיר?You need to check "was this word seen already?" many times. What's fastest?

2. stack.push(1); stack.push(2); stack.pop() מחזיר?stack.push(1); stack.push(2); stack.pop() returns?

3. איזה Set ידפיס את האיברים ממוינים?Which Set prints its items sorted?

תרגיליםExercises

תרגילExercise 1

דפים ייחודייםUnique pages

ברשימת ביקורים home, about, home, shop, about, cart. הדפיסו את הדפים בלי כפולים לפי סדר הביקור הראשון ([home, about, shop, cart]), ואז 2 repeats.

A list of visits home, about, home, shop, about, cart. Print the pages without duplicates in order of first visit ([home, about, shop, cart]), then 2 repeats.

איך הפלט אמור להיראות?What should the output look like?
[home, about, shop, cart]
2 repeats
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: new LinkedHashSet<>(visits)new LinkedHashSet<>(visits)
לפתרוןSolution
תרגילExercise 2

שכיחות ממוינתSorted frequencies

ספרו את המילים של the cat and the hat and the bat ב-TreeMap (עם merge), הדפיסו את המפה, ואז את המפתח הראשון והאחרון: and the.

Count the words of the cat and the hat and the bat in a TreeMap (with merge), print the map, then the first and last keys: and the.

איך הפלט אמור להיראות?What should the output look like?
{and=2, bat=1, cat=1, hat=1, the=3}
and the
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: freq.merge(w, 1, Integer::sum): אם אין, שם 1; אם יש, מחבר.freq.merge(w, 1, Integer::sum): if absent puts 1; if present adds.
לפתרוןSolution
תרגילExercise 3

סוגריים מאוזניםBalanced brackets

static boolean balanced(String s) בודקת שכל סוגר נסגר בסוג הנכון ובסדר הנכון, בעזרת מחסנית (ArrayDeque). הדפיסו true false false עבור {[()]}, ([)], ((.

static boolean balanced(String s) checks that every bracket closes with the right kind in the right order, using a stack (ArrayDeque). Print true false false for {[()]}, ([)], ((.

איך הפלט אמור להיראות?What should the output look like?
true false false
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: פותח: push. סוגר: pop ובודקים שזה הזוג שלו.An opener: push. A closer: pop and check it's its partner.
רמזHint 2: בסוף המחסנית חייבת להיות ריקה.At the end the stack must be empty.
לפתרוןSolution
תרגילExercise 4

משימות לפי עדיפותTasks by priority

record Task(String name, int priority) implements Comparable<Task> (1 הכי דחוף). הכניסו ל-PriorityQueue שלוש משימות והוציאו עד שריק: fix server, write report, lunch.

record Task(String name, int priority) implements Comparable<Task> (1 is most urgent). Put three tasks in a PriorityQueue and take them out until empty: fix server, write report, lunch.

איך הפלט אמור להיראות?What should the output look like?
fix server
write report
lunch
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: הדפסת PriorityQueue עצמו לא ממוינת; רק poll מוציא בסדר.Printing the PriorityQueue itself is not sorted; only poll comes out in order.
לפתרוןSolution
תרגילExercise 5

צייד באגים: מחיקה תוך כדי מעברBug hunter: removing while looping

התוכנית מוחקת מילים קצרות (פחות מ-3 אותיות) וקורסת. תקנו עם Iterator ו-it.remove() כך שיודפס [tree, green].

The program removes short words (under 3 letters) and crashes. Fix it with an Iterator and it.remove() so it prints [tree, green].

איך הפלט אמור להיראות?What should the output look like?
[tree, green]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: Iterator<String> it = words.iterator(); while (it.hasNext()) { if (...) it.remove(); }Iterator<String> it = words.iterator(); while (it.hasNext()) { if (...) it.remove(); }
לפתרוןSolution
תרגילExercise 6

מדף, צעד 25: אינדקס מהירShelf, step 25: a fast index

פרויקט מלווהCompanion project

Index מחזיק שתי מפות: סופר → רשימת כותרים (TreeMap<String, List<String>>), וז'אנר → כותרים ממוינים (TreeMap<String, Set<String>>). add, byAuthor ו-byGenre (רשימה ריקה כשאין). הדפיסו [Emma, Persuasion], [Children of Dune, Dune], [].

Index holds two maps: author → list of titles (TreeMap<String, List<String>>), and genre → sorted titles (TreeMap<String, Set<String>>). add, byAuthor and byGenre (empty when missing). Print [Emma, Persuasion], [Children of Dune, Dune], [].

איך הפלט אמור להיראות?What should the output look like?
[Emma, Persuasion]
[Children of Dune, Dune]
[]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: authors.computeIfAbsent(author, k -> new ArrayList<>()).add(title) (החץ הוא למבדה: במפגש הבא).authors.computeIfAbsent(author, k -> new ArrayList<>()).add(title) (the arrow is a lambda: next session).
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

מטמון LRUAn LRU cache

class Lru<K, V> extends LinkedHashMap<K, V> עם קיבולת. בנאי super(16, 0.75f, true) (סדר לפי גישה) ודריסה של removeEldestEntry. עם קיבולת 2: שימו a, b, קראו a, שימו c. הדפיסו את המפתחות: [a, c] (b, שהכי פחות היה בשימוש, נזרק).

class Lru<K, V> extends LinkedHashMap<K, V> with a capacity. Constructor super(16, 0.75f, true) (access order) and an override of removeEldestEntry. With capacity 2: put a, b, get a, put c. Print the keys: [a, c] (b, the least recently used, was dropped).

איך הפלט אמור להיראות?What should the output look like?
[a, c]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: return size() > capacity;return size() > capacity;
לפתרוןSolution
אתגרChallenge 2

השנה הקרובהThe nearest year

TreeSet של שנים 1815, 1922, 1949, 1965, 2004. הדפיסו את הקרובה מלמטה ומלמעלה ל-1950 (1949 1965), את השנים בין 1900 ל-2000 ([1922, 1949, 1965]), ואז null [2004, 1965, 1949, 1922, 1815] (higher(2004) והסדר ההפוך).

A TreeSet of the years 1815, 1922, 1949, 1965, 2004. Print the nearest below and above 1950 (1949 1965), the years between 1900 and 2000 ([1922, 1949, 1965]), then null [2004, 1965, 1949, 1922, 1815] (higher(2004) and the reversed order).

איך הפלט אמור להיראות?What should the output look like?
1949 1965
[1922, 1949, 1965]
null [2004, 1965, 1949, 1922, 1815]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: floor, ceiling, subSet, higher, descendingSet.floor, ceiling, subSet, higher, descendingSet.
לפתרוןSolution

נבנה על ידיBuilt by Ariel Yohimovich