DSA Partner Challenge | Week 1: Language Basics + Maths for DSA
Practice Day — Functions
##notes : https://drive.google.com/file/d/1xReOUrtqExV73TYS46sKs8UnJU2VJ5_B/view?usp=sharing
No new syntax today. Pure problem solving. Every question must be solved as a proper function — don't dump everything in main().
Write once. Call anywhere. That's the whole point.
Same as Day 5 — Functions & Methods Tutorial
Before writing code, answer these three things:
- What goes IN? (parameters / inputs)
- What comes OUT? (return type / output)
- What's the step-by-step logic? (pseudocode first)
Define a method that returns true if a number is prime, false otherwise.
Key logic:
- A prime has exactly 2 factors: 1 and itself
- 1 is NOT prime. 2 is prime (only even prime).
- Check divisors from 2 to √n — if any divide evenly, not prime
// Java
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0) return false;
}
return true;
}# Python
import math
def is_prime(n):
if n <= 1: return False
if n == 2: return True
if n % 2 == 0: return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0: return False
return True// C++
bool isPrime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i <= sqrt(n); i += 2)
if (n % i == 0) return false;
return true;
}💡 Why √n? If n has a factor > √n, it must also have one < √n. So checking to √n is enough.
Ask the user for marks (0–100). Define a method that returns the grade.
| Marks | Grade |
|---|---|
| 91–100 | AA |
| 81–90 | AB |
| 71–80 | BB |
| 61–70 | BC |
| 51–60 | CD |
| 41–50 | DD |
| ≤ 40 | Fail |
// Java
public static String getGrade(int marks) {
if (marks >= 91) return "AA";
else if (marks >= 81) return "AB";
else if (marks >= 71) return "BB";
else if (marks >= 61) return "BC";
else if (marks >= 51) return "CD";
else if (marks >= 41) return "DD";
else return "Fail";
}# Python
def get_grade(marks):
if marks >= 91: return 'AA'
elif marks >= 81: return 'AB'
elif marks >= 71: return 'BB'
elif marks >= 61: return 'BC'
elif marks >= 51: return 'CD'
elif marks >= 41: return 'DD'
else: return 'Fail'Define a method named Factorial that returns n! for a given n.
4! = 1 × 2 × 3 × 4 = 24
0! = 1 (by definition)
1! = 1
// Java — use long for big numbers
public static long factorial(int n) {
if (n == 0 || n == 1) return 1;
long result = 1;
for (int i = 2; i <= n; i++) result *= i;
return result;
}# Python
def factorial(n):
if n == 0 or n == 1: return 1
result = 1
for i in range(2, n + 1):
result *= i
return result// C++
long long factorial(int n) {
if (n == 0 || n == 1) return 1;
long long result = 1;
for (int i = 2; i <= n; i++) result *= i;
return result;
}Write a function that takes a number and returns true if it's a palindrome.
Mathematical approach — reverse the digits:
// Java
public static boolean isPalindrome(int n) {
if (n < 0) return false;
int original = n, reversed = 0;
while (n > 0) {
int digit = n % 10;
reversed = reversed * 10 + digit;
n = n / 10;
}
return original == reversed;
}# Python — string trick
def is_palindrome(n):
if n < 0: return False
return str(n) == str(n)[::-1]💡 Trace n=121: digits → 1, 2, 1 → reversed = 121 → matches original ✓
Go back to your Day 1–3 programs. Wrap each one in a proper named function.
Minimum 3 to refactor:
isEven(int n)— from Day 2isLeapYear(int year)— from Day 2areaOfCircle(double r)— from Day 3simpleInterest(double p, double t, double r)— from Day 2fibonacci(int n)— from Day 3
Refactoring = improving structure without changing behaviour. Real skill, real codebase.
Write a function that returns true if three numbers form a Pythagorean triplet (a² + b² = c²).
// Java
public static boolean isPythagorean(int a, int b, int c) {
int[] nums = {a, b, c};
java.util.Arrays.sort(nums);
return nums[0]*nums[0] + nums[1]*nums[1] == nums[2]*nums[2];
}# Python
def is_pythagorean(a, b, c):
nums = sorted([a, b, c])
return nums[0]**2 + nums[1]**2 == nums[2]**2// C++
bool isPythagorean(int a, int b, int c) {
int nums[] = {a, b, c};
sort(nums, nums + 3);
return nums[0]*nums[0] + nums[1]*nums[1] == nums[2]*nums[2];
}Sort first — you don't know which is the hypotenuse (c). Classic triplets: (3,4,5), (5,12,13), (8,15,17)
Write a function that prints/returns all primes between two given numbers.
// Java — reuse isPrime() from Q1
public static void printPrimes(int start, int end) {
for (int i = start; i <= end; i++)
if (isPrime(i)) System.out.print(i + " ");
}# Python
def primes_between(start, end):
return [n for n in range(start, end + 1) if is_prime(n)]This question is 3 lines because isPrime() already exists. That's the payoff of reusable functions.
Write a function that returns the sum of first n natural numbers.
// Java — formula O(1)
public static long sumNaturals(int n) {
return (long) n * (n + 1) / 2;
}# Python
def sum_naturals(n):
return n * (n + 1) // 2🧠 Formula: n×(n+1)/2 — Gauss's formula. Appears constantly in DSA. Know it cold.
- isPrime() — understand why √n is the limit
- gradeCalculator() — proper if-else chain, highest to lowest
- factorial() — handled 0! = 1 edge case, used
long - isPalindrome() — digit reversal logic understood
- Refactored at least 3 programs from Day 1–3 into functions
- isPythagorean() — sorted first, then checked
- primesInRange() — reused your own isPrime()
- sumNaturals() — know both loop AND formula approach
Drop your solutions or doubts in the community.
Solved all 8? Drop a 🔥 in the community.
Next up → Day 7: Maths for DSA — Part 1