






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







if x > 0: elif x == 0: else:
result = "yes" if cond else "no"
match value: case 1: ... case _: ...
python # For loop โ iterate over sequence for item in items: if item == target: break # exit loop entirely if item < 0 : continue # skip to next iteration else: # runs only if loop completed WITHOUT break print("not found")
# While loop โ repeat until condition fails while not done: done = check() # Enumerate โ index + value for i, val in enumerate(items, start= 1 ): print(i, val) # Zip โ parallel iteration for a, b in zip(list1, list2): print(a, b) # Comprehension โ build list in one line squares = [x** 2 for x in range( 10 ) if x % 2 == 0 ] lookup = {k: v for k, v in pairs} unique = {x for x in items}
3. Data Structure Selection Logic
list
tuple
set
risky() except (ValueError, KeyError) as e: # catch specific, not bare log(e) except Exception as e: # last resort raise RuntimeError("context") from e else: # runs if NO exception celebrate() finally: cleanup() # always runs
6. Common Algorithm Patterns python # Accumulator pattern total = 0 for x in nums: total += x # Filter-map-reduce result = sum(x** 2 for x in nums if x > 0 ) # Two-pointer (sorted list) l, r = 0 , len(arr)- 1 while l < r: if arr[l] + arr[r] == target: break elif arr[l] + arr[r] < target: l += 1 else: r - = 1 # Frequency count from collections import Counter freq = Counter(items).most_common( 3 ) # Guard clauses โ flatten nested ifs def process(x): if x is None: return None # exit early if x < 0 : raise ValueError
return x * 2 # main path unindented
java // If / else-if / else โ same logic as Python if (x > 0 ) { ... } else if (x == 0 ) { ... } else { ... } // Switch statement (classic) switch (day) { case MONDAY: case TUESDAY: doWork(); break; // fall-through: both cases default: rest(); break; }
// Streams โ functional iteration (Java 8+) List upper = names.stream() .filter(n - > !n.isBlank()) .map(String::toUpperCase) .sorted() .collect(Collectors.toList());
3. Data Structure Selection Logic
4. Class & Method Logic java // Immutable class โ best practice for data carriers public final class Point { private final int x, y; public Point(int x, int y) { this.x = x; this.y = y; }
public int x() { return x; } public int y() { return y; } } // Interface with default method โ shared logic public interface Shape { double area(); // abstract โ must implement default String describe() { // default โ inherited return "A shape with area " + area(); } } // Record (Java 16+) โ auto-generated constructor/equals/toString public record Point(int x, int y) {} // Varargs + generics public static List listOf(T... items) { return new ArrayList<>(Arrays.asList(items)); }
5. Exception Handling Logic java try { risky(); } catch (IllegalArgumentException | NullPointerException e) { // multi-catch log(e); } catch (Exception e) { throw new RuntimeException("context", e); // preserve cause } finally { cleanup(); // always runs } // Try-with-resources โ auto-closes try (var reader = Files.newBufferedReader(path)) {
7. Collections Quick Reference