- Home
- Skills
- Mobile Development
- Learning Mode: AI Tutor That Won't Do Your Homework
More screenshots
Works with the AI Agents you already use
Learning Mode: AI Tutor That Won't Do Your Homework
by wmeenw
Turns Claude into a tutor who teaches instead of doing your homework.
Free
Learning Mode: AI Tutor That Won't Do Your Homework
Example session with this skill installed
Write a Swift function that returns the second largest number in an array of Int.
- Read your context and instructions
- Compiled the learning mode
Second Largest Number in an Array (Swift)
What needs to be done
- Write a function that takes an array of
Int. - It returns the second largest number in that array.
- It has to cope with tricky inputs: an empty array, one element, duplicates, negative numbers.
The point of the exercise is to walk through an array once and
remember state as you go. You also have to be honest in the function's type when there is no answer.
Part 1. Theory, step by step
Step 1. Optional (Int?): "a box that might be empty"
What it is and why. What's the second largest number in [] or in [7]? There isn't one. A function declared -> Int must return a number, so you'd have to invent a fake one. An
Optional (Int?) is a box that holds either an Int or nothing (nil).
Formally: T? is shorthand for Optional<T>, an enum with two cases: .some(T) and .none (written nil).
func firstSongLongerThan(_ seconds: Int, in durations: [Int]) -> Int? {
for d in durations {
if d > seconds {
return d // the box has something in it
}
}
return nil // the box is empty
}
if let found = firstSongLongerThan(300, in: [180, 240, 320]) {
print("Found: \(found)") // Found: 320
} else {
print("No such song")
}
Breakdown:
-> Int?: the result is a box that may hold anInt.return d: Swift wrapsdinto the box for you.return nil: returns an empty box.if let found = …: opens the box. If something is inside,foundis a plainIntinside the braces. If not, theelsebranch runs.
⚠️
Trap: "magic" values. It's tempting to return -1 or Int.min for "no answer". But -1 can be a real answer ([-1, 5]). The caller can't tell "no answer" apart from a real -1.
👉 If an answer can be missing, put that in the type (Int?), not in a special number.
Step 2. One pass with state: guard, var, for … in
What it is. Walk through the array once. Keep the "best so far" in a variable and update it when you find something better. That variable is your
state.
Example on different material: the shortest word in an array of strings.
func shortestWord(in words: [String]) -> String? {
guard var shortest = words.first else { return nil }
for word in words.dropFirst() {
if word.count < shortest.count {
shortest = word
}
}
return shortest
}
print(shortestWord(in: ["melody", "sky", "harmony"]) ?? "none") // sky
print(shortestWord(in: []) ?? "none") // none
| Part | What it means |
|---|---|
guard … else { return nil } | "Make sure this holds; otherwise leave the function right away." Handles the edge case at the top. |
var shortest = words.first | first is itself an Optional (an empty array has no first element). guard var unwraps it into a mutable variable. |
words.dropFirst() | The array without its first element. We already used that one as the starting value. |
if word.count < shortest.count | Compare. If the new word is better, update the state. |
?? "none" | nil-coalescing: "use what's in the box, or this default if it's empty." |
Step 3. Two pieces of state at once
Sometimes one variable isn't enough. Example: the shortest and longest song in a single pass.
func durationRange(_ durations: [Int]) -> (shortest: Int, longest: Int)? {
guard let first = durations.first else { return nil }
var shortest = first
var longest = first
for d in durations {
if d < shortest { shortest = d }
if d > longest { longest = d }
}
return (shortest, longest)
}
if let range = durationRange([210, 185, 342, 199]) {
print(range.shortest, range.longest) // 185 342
}
Breakdown:
(shortest: Int, longest: Int)?: an optional tuple, meaning two named values returned together.- Two
ifs: the variables update independently. A new minimum has nothing to do with the maximum.
💡 Your task also needs two variables, but
they are not independent. Think it through: when a new "champion" arrives, what happens to the old champion? Where does it go?
Step 4. Sorting vs. one pass
There's an obvious shortcut: sort the array and pick an element from the end. It works, but it costs more:
| Approach | Time | Extra memory | Notes |
|---|---|---|---|
sorted() then index | O(n log n) | O(n) (a copy) | Short to write; duplicates still need handling |
| Single pass with 2 variables | O(n) | O(1) | Each element is looked at exactly once |
Both are acceptable answers. The one-pass version is what this exercise is really training, so do that one first. You can write the sorting version afterwards as a cross-check.
★ Insight ─────────────────────────────────────
- The hard part of this problem is the
specification, not the loop. You decide what "second largest" means when values repeat.
Int?turns the edge cases into something the compiler enforces. Callers can't forget about the empty-array case.- The step from Step 3 to your task is "independent updates" → "cascading updates". The same idea scales up to keeping a top-k list.
─────────────────────────────────────────────────
Part 2. Your task
Part A. The signature
- Create a function
secondLargestthat takes an array ofIntas its only parameter. Decide the return type yourself, using Step 1. Ask: can there be "no answer"? For which inputs? - Before writing any logic,
decide on duplicates. What should [5, 5, 3] return? Is "second largest" the second distinct value, or the second position after sorting? Both are defensible. Pick one and write it as a one-line comment above the function.
Part B. The edge cases
- At the very top of the body, handle arrays that are too small to have an answer. Use
guardas in Step 2. Think about whethercount < 2is enough, given your decision in step 2. (Hint: what about[4, 4]?)
Part C. The single pass
- Declare two variables for the state: the largest value seen so far and the second largest seen so far. Think about their
starting values. Step 1 warned you about magic numbers. Can one of these variables be an Optional itself while you're scanning?
5. Loop over the array with for … in. For each number, decide among three situations:
- it's bigger than the current largest → what happens to
both variables? (Remember the Step 3 hint about the old champion.)
- it's not the largest, but it beats the second largest → update only one variable;
- otherwise → do nothing.
Check that your conditions match your duplicates decision from step 2.
- After the loop, return the second-largest variable.
Part D. Check it
-
Put the function in an Xcode Playground, or in a file
main.swiftthat you run with:swift main.swift -
Print the result for each of these inputs and check that every line matches what you predicted on paper first:
[][7][1, 2, 3][3, 2, 1](the order shouldn't matter)[5, 5, 3](the answer depends on your rule from step 2)[4, 4][-8, -3, -10](all negative: this is where a magic starting value would break)
Use
?? "none"orif letwhen printing so the output doesn't showOptional(…).
🤔 Something to think about: if you needed the k-th largest (say, the 3rd or the 10th), how would your two variables grow? At what point does the "sort it" approach become the better choice?
Send me your code when you've got it and I'll review it. I didn't create any files or run any code; everything above is explanation only. Per your request, I didn't read the memory files either.
Connects securely to your tools. The creator never sees your data.
About this skill
A skill for students who want to actually learn, not just get it done.
- Theory first, with examples on different material, so nothing can be copied by renaming variables
- A detailed step-by-step task: what to write, where, and which edge cases to test
- Review of your code: a verdict, each mistake explained through a scenario where it breaks, and a hint instead of a fix
- The full solution only when you explicitly ask
Free and open source (MIT): https://github.com/wmeenw/learning-mode
Will not write the solution unless you explicitly ask for it. Never modifies your files. Answers are long and go one stage at a time by design. Best suited for students and junior developers.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free to use
No account required to browse
Trust & safety
Security scan
Security scan passed 16 days ago
- Free with an Agensi account
Nothing to install. It runs in your AI agent.
Answers in your language and use your memory. Reads your files only to review your code when you ask. Never edits or runs anything.
Permissions
What's inside
Frequently Asked Questions
New in Mobile Development
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Mobile Development.