DSA Partner Challenge | Week 1: Language Basics + Maths for DSA
##notes: https://drive.google.com/file/d/1zICbYGLxtGIG28DUc229gbpiOFoua2bO/view?usp=sharing
switch / match case — keeping it light
When you have one variable and many possible values to check, switch/match is cleaner than a chain of if-else. Today we learn all three flavours.
| Language | Resource |
|---|---|
| ☕ Java | switch case Tutorial |
| 🐍 Python | Same video + match-case docs (Python 3.10+) |
| ⚙️ C++ | Same video — C++ switch is identical to Java's traditional syntax |
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// fallback
break;
}Enhanced switch (Java 14+) — no break needed:
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
default -> System.out.println("Other");
}Key rules:
breakis required in traditional switch — without it, execution falls into the next casedefaultis likeelse— runs when nothing matches- Supported types:
int,char,String(Java 7+),byte,short,enum - NOT supported:
long,double,float,boolean
match expression:
case value1:
# code
case value2:
# code
case _: # _ is the wildcard / default
# fallbackGroup cases with | (OR):
match month:
case 4 | 6 | 9 | 11:
print("30 days")
case 2:
print("28 or 29 days")
case _:
print("31 days")Key rules:
- NO
breakneeded — Python match-case does not fall through _is the wildcard — acts as default- Works with Python 3.10+ only
- Supports structural pattern matching (more powerful than Java's switch)
Before Python 3.10 — use a dict:
days = {1: "Monday", 2: "Tuesday", 3: "Wednesday"}
print(days.get(3, "Invalid")) # WednesdaySame as Java's traditional switch:
switch (x) {
case 1:
cout << "One";
break;
case 2:
cout << "Two";
break;
default:
cout << "Other";
break;
}Key differences from Java:
- No
Stringsupport in C++ switch — useif-elsefor strings - Only integer types:
int,char,short,long,long long,enum - C++17 added
[[fallthrough]]attribute for intentional fall-through
| Feature | Java switch | Python match | C++ switch |
|---|---|---|---|
| Default | default: |
case _: |
default: |
| Fall-through | Yes — needs break |
No (auto-isolated) | Yes — needs break |
| String support | Yes (Java 7+) | Yes | No |
| OR in cases | case 1: case 2: |
case 1 | 2: |
case 1: case 2: |
| Pattern matching | Limited | Full structural | No |
Solve in your language. Push solutions to your fork.
1. Perimeter of Rectangle
Formula: 2 × (length + breadth)
2. Perimeter of Square
Formula: 4 × side
3. Perimeter of Rhombus
Formula: 4 × side
4. Volume of Prism
Formula: base area × height
5. Volume of Cylinder
Formula: π × r² × h
6. Volume of Sphere
Formula: (4/3) × π × r³
7. Volume of Pyramid
Formula: (1/3) × base area × height
8. Curved Surface Area of Cylinder
Formula: 2 × π × r × h
9. Total Surface Area of Cube
Formula: 6 × side²
10. Fibonacci Series up to n numbers
0, 1, 1, 2, 3, 5, 8, 13, 21 ... Each number = sum of previous two
11. Subtract the Product and Sum of Digits of an Integer
Given: 234 Product of digits: 2 × 3 × 4 = 24 Sum of digits: 2 + 3 + 4 = 9 Answer: 24 - 9 = 15
- I know the basic switch syntax in Java and C++
- I understand fall-through and why
breakis needed - I know which types are supported in switch (Java/C++)
- I know how Python
match-caseis different fromswitch - I know
_is the wildcard/default in Python - I can use
|to group cases in Python match-case - Solved at least 7 of the 11 practice questions
Stuck on any question? Drop the number in the community.
Solved all 11? Drop a 🔥 in the community.
Next up → Day 5: Functions & Methods — writing reusable code