# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/LakshmiPrasannan/Data-Structures-in-Code-Optimization/blob/master/Data_Structures_in_code_optimization_.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="PLDyUYxdXVIO" colab_type="text"
# # Understanding how the use of righ data structure helps in Code Optimization
#
#

# + [markdown] id="f68faBD_Xg0p" colab_type="text"
# The task we are about to perform is to find commond elements between two datapoints. 
# > Datasets are:
# >> 1. all_coding_books.txt: contains the id of all the books written about coding in last two years
# >> 2. books_published_last_two_years.txt: contains the id of all books written in the last two years
#

# + [markdown] id="6LKIPArMYTKw" colab_type="text"
# > The first easily understood method that comes to our mind will be to store the data in two lists, compare the lists using for loop

# + id="JSncdsIYYvyx" colab_type="code" colab={}
#Import libraries
import time
import pandas as pd
import numpy as np

# + id="jivZjLmtU8DO" colab_type="code" colab={}
#Store both the file data to a csv as a list
with open('books_published_last_two_years.txt') as f:
    recent_books = f.read().split('\n')
    
with open('all_coding_books.txt') as f:
    coding_books = f.read().split('\n')

# + id="9rJglCbrY-mx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 52} outputId="b79dfeb4-7f02-4581-f789-6f7a2b19c18b"
start = time.time()
recent_coding_books = []

for book in recent_books:
    if book in coding_books:
      recent_coding_books.append(book)
print("There are " + str(len(recent_coding_books)) + " coding books released recently")
print('Duration: {} seconds'.format(time.time() - start))

# + [markdown] id="PFWAVezKec6X" colab_type="text"
# Let's try using the data structure of sets instead of lists to improve the speed of search
# > Note: While using sets we can also use the vector function intersection to check for common elements.

# + id="KKj0hS4qeMm8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 52} outputId="a34f7121-c98f-4901-ead4-f5833a7483ce"
start = time.time()
recent_coding_books = set(recent_books).intersection(coding_books)
print("There are " + str(len(recent_coding_books)) + " coding books released recently")
print('Duration: {} seconds'.format(time.time() - start))



# + [markdown] id="j6WMDI_yfQvH" colab_type="text"
# **Observation:** Using of sets is more efficient in terms of time in this case. 

StarcoderdataJupyterScriptsDedupFiltered