LC-TEST-22 — Recap Basic Logic

🧠 Why Learn Logic First?

Before computers understand code,
they understand logic.

Every if, every loop, every algorithm
is built from simple logical rules.

If your logic is strong:

  • Code becomes obvious
  • Bugs become visible
  • Interviews become easier

This chapter builds mental foundations.


🔌 Logic Gates — Explained for Humans

Think in TRUE / FALSE, not code.


✅ AND (&&)

TRUE only if everything is TRUE

A B A AND B
T T T
T F F
F T F
F F F

🧠 Real life:

“I will go outside if it is sunny AND I finish work.”

sunny = True
finished_work = False
print(sunny and finished_work)  # False

🔵 OR (||)

TRUE if at least one is TRUE

A B A OR B
T T T
T F T
F T T
F F F

🧠 Real life:

“I am happy if I have coffee OR tea.”

coffee = False
tea = True
print(coffee or tea)  # True

❌ NOT (!)

Flips the value

A NOT A
T F
F T
is_raining = True
print(not is_raining)  # False

⚡ XOR (Exclusive OR)

TRUE only if values are different

A B A XOR B
T T F
T F T
F T T
F F F

🧠 Memory trick:

Same = false, Different = true

print(True ^ True)    # False
print(True ^ False)   # True

🔥 NAND (NOT AND)

AND, then flip it Used heavily in real hardware

A = True
B = True
print(not (A and B))  # False

💎 XNOR (NOT XOR)

TRUE if values are the same

A = True
B = True
print(not (A ^ B))  # True

🧠 Meaning:

“Are these two states equal?”


🧩 Wizard & Cartoon Logic (20 Puzzles)

These puzzles train thinking, not syntax.


🟢 Problem 1 — Harry’s Spell Permission

Harry can cast a spell only if:

  • wand works
  • spell learned
wand = True
learned = False
✅ Solution — AND Logic
print(wand and learned)

🟢 Problem 2 — Elsa Can Use Ice?

Elsa can use ice if:

  • not tired
  • OR emotion controlled
tired = True
control = True
✅ Solution — OR + NOT
print((not tired) or control)

🟢 Problem 3 — Door Access Card

Door opens only if exactly one card is valid.

cardA = True
cardB = False
✅ Solution — XOR
print(cardA ^ cardB)

🟢 Problem 4 — Mickey’s Safety Rule

Alarm rings unless both conditions are safe.

fire = False
door_closed = True
✅ Solution — NAND
print(not (not fire and door_closed))

🟢 Problem 5 — Clone Detection

Are two magic clones identical?

clone1 = True
clone2 = True
✅ Solution — XNOR
print(not (clone1 ^ clone2))

🟡 Problem 6 — Any Villain Nearby?

villains = [False, False, True, False]
✅ Solution — OR Scan
danger = False
for v in villains:
    danger = danger or v
print(danger)

🟡 Problem 7 — All Students Passed?

scores = [True, True, True, False]
✅ Solution — AND Scan
ok = True
for s in scores:
    ok = ok and s
print(ok)

🟡 Problem 8 — Light Toggle Switch

Switch toggles state each press.

presses = [True, True, False, True]
✅ Solution — XOR Accumulate
state = False
for p in presses:
    state ^= p
print(state)

🟡 Problem 9 — Password Check

Correct only if length ≥ 8 AND has number.

length_ok = True
has_number = False
✅ Solution
print(length_ok and has_number)

🟡 Problem 10 — Emergency Shutdown

Shutdown if NOT safe OR power lost.

safe = True
power = False
✅ Solution
print((not safe) or (not power))

🧙 Problem 11 — The Broken Spell Lock

A spell door opens only if:

  • the spell is activated
  • AND the wand is stable

But today:

  • the spell is activated ❌
  • the wand is unstable ❌

The system inverts the final result.

spell = True
wand = False
print(not (spell and wand))

🧠 Think:

  • Does the spell activate?
  • What does NOT do at the end?
✅ Solution Explanation
  • spell and wandFalse
  • not FalseTrue

Door opens


🧙 Problem 12 — Potion Choice Confusion

Hermione drinks a potion if:

  • it is sweet
  • OR (it is bitter AND effective)

Today:

  • sweet ❌
  • bitter ❌
  • effective ❌
sweet = True
bitter = False
effective = False
print(sweet or bitter and effective)

🧠 Rule reminder:

AND runs before OR

✅ Solution Explanation
  • bitter and effectiveFalse
  • sweet or FalseTrue

Potion is consumed


🧙 Problem 13 — The Mirror of Truth

The mirror lights up if:

  • exactly one spell is real
  • AND the wizard is focused
spell_real = True
illusion = False
focused = True
print((spell_real ^ illusion) and focused)

🧠 XOR question:

Are the spells different?

✅ Solution Explanation
  • spell_real ^ illusionTrue
  • True and TrueTrue

Mirror lights up


🧙 Problem 14 — Moody’s Paradox Curse

Mad-Eye Moody casts a curse that:

  • flips the spell result
  • then checks if it differs from danger
spell = True
danger = False
print(not spell ^ danger)

🧠 Careful:

NOT runs before XOR

✅ Solution Explanation
  • not spellFalse
  • False ^ FalseFalse

Curse fails


🧙 Problem 15 — Patronus Confidence Test

A Patronus appears if:

  • wizard believes
  • AND the wizard is not afraid
believe = True
afraid = False
print(believe and not afraid)
✅ Solution Explanation
  • not afraidTrue
  • True and TrueTrue

Patronus summoned successfully


🧙 Problem 16 — The Room of Requirement

The room appears if:

  • any hidden desire exists
desire1 = False
desire2 = False
desire3 = True
print(desire1 or desire2 or desire3)
✅ Solution Explanation
  • At least one TrueTrue

Room appears


🧙 Problem 17 — Forbidden Knowledge Scroll

The scroll activates only if:

  • NOT even one guardian is awake
guardian1 = False
guardian2 = True
print(not (guardian1 or guardian2))

🧠 Think:

If ANY guardian is awake → blocked

✅ Solution Explanation
  • False or TrueTrue
  • not TrueFalse

Scroll remains sealed


🧙 Problem 18 — Twin Wand Resonance

Two twin wands resonate if:

  • their magic differs
  • OR the backup charm activates
wandA = True
wandB = True
backup = False
print((wandA ^ wandB) or backup)
✅ Solution Explanation
  • True ^ TrueFalse
  • False or FalseFalse

No resonance


🧙 Problem 19 — Identity Verification Charm

The charm passes if:

  • the identity is NOT different
real = True
fake = False
print(not (real ^ fake))

🧠 Meaning:

Are they the same?

✅ Solution Explanation
  • True ^ FalseTrue
  • not TrueFalse

Verification failed


🧙 Problem 20 — Final Hogwarts Gate

The final gate opens if:

  • both keys are active
  • OR the override rune is active
  • BUT the result is flipped
key1 = True
key2 = False
override = True
print((key1 and key2) ^ override)

🧠 Slow down:

Evaluate AND → then XOR

✅ Solution Explanation
  • key1 and key2False
  • False ^ TrueTrue

Gate opens


🧠 Final Mental Model

Computers don’t think in English They think in logic switches

If you master:

  • AND
  • OR
  • NOT
  • XOR

You master:

  • if
  • while
  • algorithms
  • interviews

You’re not learning Python. You’re learning how machines think 🧠⚡


Previous