Engineering Karthik
Python DSA — Course — Part 1
Course Link: [Link]
Website Link: [Link]
Hello everyone, welcome!
This course covers DSA (Data Structures and Algorithms) — the kind of problems that come up in
interviews.
Online Python Compiler
[Link]
First Code — Print in Python
print("Vignesh")
Story Begins Here
Meet Karthik, our guide for this course, standing at a crossroads — just like every beginner does when
they start learning to code.
Variables
Simply put, a variable is a name given to a value.
heroOne = "Chris"
heroTwo = "Alex"
luckyNumber = 1
Here, heroOne, heroTwo, and luckyNumber are all variables.
If-Else Statements
luckyNumber = 1
if(luckyNumber == 1):
print("Vignesh")
else:
print("Dinesh")
Let's Help Karthik — Exercise 1
karthik = "boy"
if(karthik == "boy"):
print("left")
else:
print("right")
Today marks a turning point in your life — you've made time for your own growth. All the best!
— Engineering Karthik
Engineering Karthik
Python DSA — Course — Part 2
Course Link: [Link]
Website Link: [Link]
Hello everyone, welcome!
Karthik's Problem
Karthik has a huge collection of boxes — lakhs of them — and each box has a number written on it. A
merchant offers to pay money for every box that has the number "1" on it.
Topics covered: Arrays, For Loops, Why Arrays, and an important edge case ("incase").
Without Arrays — Repetitive Variables
number1 = 1
number2 = 4
number3 = 8
number4 = 7
number5 = 0
print(number1)
print(number2)
print(number3)
print(number4)
print(number5)
This works, but it doesn't scale — imagine doing this for a million numbers.
With Arrays — A Better Way
numbers = [1, 4, 8, 7, 0]
print(numbers[0])
print(numbers[1])
print(numbers[2])
print(numbers[3])
print(numbers[4])
Each value in the array has a position, called its index, starting from 0:
Values: 1 4 8 7 0
Index: 0 1 2 3 4
String Arrays
names = ["Ben", "Anvi", "Nikhil", "Ram"]
print(names[0])
print(names[1])
print(names[2])
print(names[3])
Why For Loops?
Instead of writing a print statement for every single element, we can loop through the array:
names = ["Ben", "Anvi", "Nikhil", "Ram"]
for i in range(4):
print(names[i])
Or, more directly:
for val in names:
print(val)
Engineering Karthik
Python DSA — Course — Part 3
Course Link: [Link]
Website Link: [Link]
Hello everyone, welcome!
Karthik's Problem
Karthik again has lakhs of boxes, each with a number. This time, a merchant offers to pay for every box
with the number "1" on it. How many such boxes are there?
arr = [3, 2, 1, 5, 7, 8, 1, 4, 1, 2, 1, 4, 6]
ans = 0
for i in range(len(arr)):
if(arr[i] == 1):
ans = ans + 1
print(ans)
A Follow-up Problem — Divisible by 3
Now the merchant wants to know how many boxes have numbers divisible by 3.
Modulus
The modulus operator (%) gives the remainder of a division:
6%3=0
5%3=2
4%3=1
3%3=0
arr = [3, 2, 1, 5, 7, 8, 1, 4, 1, 2, 1, 4, 6]
ans = 0
for i in range(len(arr)):
if(arr[i] % 3 == 0):
ans = ans + 1
print(ans)
Divisible by 3 OR 2
or — true if either condition is true. and — true only if both are true.
arr = [3, 2, 1, 5, 7, 8, 1, 4, 1, 2, 1, 4, 6]
ans = 0
for i in range(len(arr)):
if(arr[i] % 3 == 0 or arr[i] % 2 == 0):
ans = ans + 1
print(ans)
Divisible by 3 AND 2
arr = [3, 2, 1, 5, 7, 8, 1, 4, 1, 2, 1, 4, 6]
ans = 0
for i in range(len(arr)):
if(arr[i] % 3 == 0 and arr[i] % 2 == 0):
ans = ans + 1
print(ans)
Engineering Karthik
Python DSA — Course — Part 4
Course Link: [Link]
Website Link: [Link]
Hello everyone, welcome!
Karthik's Problem
On a signboard someone writes a word. If you reverse it, that reversed word is worth 1 lakh rupees. Let's
help figure out the reversed word.
Sample Reversals
DSA -> ASD
bangaram -> maragnab
chapri -> irpahc
s = "bangaram"
ans = ""
for i in range(len(s) - 1, -1, -1):
ans = ans + s[i]
print(ans)
A New Problem — Palindromes
Now someone else writes words on the board — we need to check whether each one is a palindrome. A
palindrome reads the same forwards and backwards.
Examples of Palindromes
aba => (reversing it gives back "aba")
abba => (reversing it also gives back "abba")
paap => Yes, it is a palindrome
tuck => No, it is not a palindrome
Bot => No, it is not a palindrome
Syntax you should know: break — this can be used to exit a for loop immediately.
Way 1: Reverse the string and compare both
s = "bangaram"
ans = ""
for i in range(len(s) - 1, -1, -1):
ans = ans + s[i]
if ans == s:
print("yes, it is a palindrome")
else:
print("No, it is not a palindrome")
Way 2: Two-pointer comparison
isPalindrome = True
s = "aba"
n = len(s)
mid = n // 2
for i in range(mid):
if(s[i] != s[n - i - 1]):
isPalindrome = False
break
if(isPalindrome):
print("yes, it is a palindrome")
else:
print("No, it is not a palindrome")
Engineering Karthik
Python DSA — Course — Part 5
Course Link: [Link]
Website Link: [Link]
Hello everyone, welcome!
Ready to start solving problems on the LeetCode platform.
Karthik's Problem
Given a name and a number, print the name that many times.
Example:
Vignesh 3
-> Print "Vignesh" 3 times:
Vignesh
Vignesh
Vignesh
Now imagine the same for different names and numbers:
Ben 4
Bahubali 2
Functions
def fun(s, n):
for i in range(n):
print(s)
fun("Vignesh", 5)
fun("Ben", 4)
def fun():
print("Hello")
fun()
fun()
fun()
More Function Examples
def fun(n):
print(n + 5)
fun(0)
def fun(s):
print(s + "Hello")
fun("Hai")
def fun():
return "Macha"
a = fun()
print(a)
Printing a Multiplication Table Using a Function
def table(n):
for i in range(1, 11):
print(str(n) + " * " + str(i) + " = " + str(n * i))
print(f"{n} * {i} = {n*i}")
table(5)
Engineering Karthik
Python DSA — Course — Part 6
Course Link: [Link]
Website Link: [Link]
Hello everyone, welcome!
Ready to start solving problems on the LeetCode platform.
[Link]
New Basic Concepts
• ASCII values
• Absolute difference
• Adjacent characters
n = -1
if(n < 0):
n = n * -1
print(n)
print(abs(-1))
ASCII Value
print(ord("a"))
LeetCode Problem Solution
class Solution:
def scoreOfString(self, s: str) -> int:
ans = 0
for i in range(len(s) - 1):
a = ord(s[i])
b = ord(s[i + 1])
temp = abs(b - a)
ans = ans + temp
return ans
A shorter, equivalent version:
class Solution:
def scoreOfString(self, s: str) -> int:
ans = 0
for i in range(len(s) - 1):
ans = ans + abs(ord(s[i + 1]) - ord(s[i]))
return ans
Engineering Karthik
Python DSA — Course — Part 7
Course Link: [Link]
Website Link: [Link]
Hello everyone, welcome!
More practice problems from LeetCode.
Final Value of Variable After Performing Operations
[Link]
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
ans = 0
for i in operations:
if i == "--X" or i == "X--":
ans -= 1
if i == "X++" or i == "++X":
ans += 1
return ans
A shorter, equivalent version:
class Solution:
def finalValueAfterOperations(self, operations: List[str]) -> int:
ans = 0
for i in operations:
if i == "--X" or i == "X--":
ans -= 1
else:
ans += 1
return ans
Defanging an IP Address
[Link]
class Solution:
def defangIPaddr(self, address: str) -> str:
ans = ""
for i in address:
if i != ".":
ans += i
else:
ans += "[.]"
return ans
Jewels and Stones
[Link]
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
ans = 0
for i in stones:
for j in jewels:
if j == i:
ans += 1
break
return ans
A shorter, equivalent version:
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
ans = 0
for i in stones:
if i in jewels:
ans += 1
return ans