# Introduction to Data Structures and Algorithms | 000

Aug 17, 2022·

I am going to call Data Structures and Algorithms as DSA Code written is in Python

## Why DSA?

The main moto to learn DSA is to know how to write optimized code. In this age of Cloud Functions, the optimized code requires less money as less computation and memory is used in the servers. On the other hand if you don't write optimized code, you will have to pay more money and also the results from the cloud servers would be very slow.

## Judging code

Now you know that writing optimized code would save your time. But how will you judge if a code is optimized or not? Many people say that the less number of lines means the more optimized code. But this is a true lie. Optimized code can't be judged by the number of lines but the parameters involved in the code. Judge this case:

Problem Statement: Generate a series of number from 1 to n and store it in a variable or function so that it can be accessed later

Code 1:

def gen_series():
a = []
n = int(input("What's n? "))
for i in range(1, n+1):
a.append(i)
return a

def print_series():
a = gen_series()
for i in a:
print(i)

if __name__ == "__main__":
print_series()

Code 2:

def gen_series():
n = int(input("What's n? "))
a = series(n)
print(a)
return a

def print_series():
a = gen_series()
for i in a:
print(i)

def series(n):
d = 11
for i in range(1, n+1):
yield i + d
d += 11

if __name__ == "__main__":
print_series()

If you count the number of lines, you would say Code 1 is more optimized but is it?

In both the cases the series is generated and stored inside the function named gen_series(). If you know about the sys.getsizeof() method in python, you will know that it gives out the memory used inside a variable, function. If you write this in your code editor, run the program, and enter 200 as the input of What's n?

import sys

def gen_series():
a = []
n = int(input("What's n? "))
for i in range(1, n+1):
a.append(i)
return a

def print_series():
a = gen_series()
for i in a:
print(i)

if __name__ == "__main__":
print(sys.getsizeof(gen_series()))

You will get the memory used in bytes. And surprisingly, you will notice that the 2nd code consumes less memory.

So, we can come to a conclusion that less code always doesn't mean optimized code. For a code to be judged, the parameters are important.

## Programming Language

DSA is mostly independent of programming language the main things which matter are the concepts. I'll be using python in the next articles regarding DSA