From 92571f619700a05019853aff0230015fe4b85a2d Mon Sep 17 00:00:00 2001 From: saiumasankar Date: Wed, 5 Jun 2024 11:29:23 +0530 Subject: [PATCH 1/4] update dynamic programming Added LIS problem and Tabulation code of LCS and 0-1 Knapsack --- contrib/ds-algorithms/dynamic-programming.md | 124 ++++++++++++++++++- 1 file changed, 121 insertions(+), 3 deletions(-) diff --git a/contrib/ds-algorithms/dynamic-programming.md b/contrib/ds-algorithms/dynamic-programming.md index 43149f86..54c6fcbf 100644 --- a/contrib/ds-algorithms/dynamic-programming.md +++ b/contrib/ds-algorithms/dynamic-programming.md @@ -84,8 +84,32 @@ Y = "GXTXAYB" print("Length of Longest Common Subsequence:", longest_common_subsequence(X, Y, len(X), len(Y))) ``` +## Longest Common Subsequence Code in Python (Bottom-Up Approach) + +```python + +def longestCommonSubsequence(X, Y, m, n): + L = [[None]*(n+1) for i in range(m+1)] + for i in range(m+1): + for j in range(n+1): + if i == 0 or j == 0: + L[i][j] = 0 + elif X[i-1] == Y[j-1]: + L[i][j] = L[i-1][j-1]+1 + else: + L[i][j] = max(L[i-1][j], L[i][j-1]) + return L[m][n] + + +S1 = "AGGTAB" +S2 = "GXTXAYB" +m = len(S1) +n = len(S2) +print("Length of LCS is", longestCommonSubsequence(S1, S2, m, n)) +``` + ## Complexity Analysis -- **Time Complexity**: O(m * n) for the top-down approach, where m and n are the lengths of the input sequences +- **Time Complexity**: O(m * n) for both approaches, where m and n are the lengths of the input sequences - **Space Complexity**: O(m * n) for the memoization table
@@ -122,11 +146,105 @@ capacity = 50 n = len(weights) print("Maximum value that can be obtained:", knapsack(weights, values, capacity, n)) ``` +## 0-1 Knapsack Problem Code in Python (Bottom-up Approach) + +```python +def knapSack(capacity, weights, values, n): + K = [[0 for x in range(capacity + 1)] for x in range(n + 1)] + for i in range(n + 1): + for w in range(capacity + 1): + if i == 0 or w == 0: + K[i][w] = 0 + elif weights[i-1] <= w: + K[i][w] = max(values[i-1] + + K[i-1][w-weights[i-1]], + K[i-1][w]) + else: + K[i][w] = K[i-1][w] + + return K[n][capacity] +values = [60, 100, 120] +weights = [10, 20, 30] +capacity = 50 +n = len(weights) +print(knapSack(capacity, weights, values, n)) +``` ## Complexity Analysis -- **Time Complexity**: O(n * W) for the top-down approach, where n is the number of items and W is the capacity of the knapsack +- **Time Complexity**: O(n * W) for both approaches, where n is the number of items and W is the capacity of the knapsack - **Space Complexity**: O(n * W) for the memoization table

-
\ No newline at end of file +
+ +# 4. Longest Increasing Subsequence + +The Longest Increasing Subsequence (LIS) is a task is to find the longest subsequence that is strictly increasing, meaning each element in the subsequence is greater than the one before it. This subsequence must maintain the order of elements as they appear in the original sequence but does not need to be contiguous. The goal is to identify the subsequence with the maximum possible length. + +**Algorithm Overview:** +- **Base cases:** If the sequence is empty, the LIS length is 0. +- **Memoization:** Store the results of previously computed subproblems to avoid redundant computations. +- **Recurrence relation:** Compute the LIS length by comparing characters of the sequences and making decisions based on their values. + +## Longest Increasing Subsequence Code in Python (Top-Down Approach using Memoization) + +```python + +import sys + +def f(idx, prev_idx, n, a, dp): + if (idx == n): + return 0 + + if (dp[idx][prev_idx + 1] != -1): + return dp[idx][prev_idx + 1] + + notTake = 0 + f(idx + 1, prev_idx, n, a, dp) + take = -sys.maxsize - 1 + if (prev_idx == -1 or a[idx] > a[prev_idx]): + take = 1 + f(idx + 1, idx, n, a, dp) + + dp[idx][prev_idx + 1] = max(take, notTake) + return dp[idx][prev_idx + 1] + +def longestSubsequence(n, a): + + dp = [[-1 for i in range(n + 1)]for j in range(n + 1)] + return f(0, -1, n, a, dp) + +a = [3, 10, 2, 1, 20] +n = len(a) + +print("Length of lis is", longestSubsequence(n, a)) + +``` + +## Longest Increasing Subsequence Code in Python (Bottom-Up Approach) + +```python +def lis(arr): + n = len(arr) + lis = [1]*n + + for i in range(1, n): + for j in range(0, i): + if arr[i] > arr[j] and lis[i] < lis[j] + 1: + lis[i] = lis[j]+1 + + maximum = 0 + for i in range(n): + maximum = max(maximum, lis[i]) + + return maximum + +arr = [10, 22, 9, 33, 21, 50, 41, 60] +print("Length of lis is", lis(arr)) +``` +## Complexity Analysis +- **Time Complexity**: O(n * n) for both approaches, where n is the length of the array. +- **Space Complexity**: O(n * n) for the memoization table in Top-Down Approach, O(n) in Bottom-Up Approach. + +
+
+
From f742fddff366a0eff24520c8ee292b81d2188d45 Mon Sep 17 00:00:00 2001 From: saiumasankar Date: Wed, 5 Jun 2024 17:28:33 +0530 Subject: [PATCH 2/4] Adding the BeautifulSoup web scrapping --- contrib/web-scrapping/beautifulsoup.md | 206 +++++++++++++++++++++++++ contrib/web-scrapping/index.md | 1 + 2 files changed, 207 insertions(+) create mode 100644 contrib/web-scrapping/beautifulsoup.md diff --git a/contrib/web-scrapping/beautifulsoup.md b/contrib/web-scrapping/beautifulsoup.md new file mode 100644 index 00000000..372b8f29 --- /dev/null +++ b/contrib/web-scrapping/beautifulsoup.md @@ -0,0 +1,206 @@ +# Beautiful Soup Library in Python + +## Table of Contents +1. Introduction +2. Prerequisites +3. Setting Up Environment +4. Beautiful Soup Objects +5. Tag object +6. Children, Parents and Siblings +7. Filter: Findall method +8. Web Scraping the Contents of a Web Page + +## 1. Introduction +Beautiful Soup is a Python library used for web scraping purposes to extract data from HTML and XML files. It creates a parse tree for parsed pages that can be used to extract data easily. + +## 2. Prerequisites +- Understanding of HTTP Requests and Responses. +- Understanding of HTTP methods (GET method). +- Basic Knowledge on HTML. +- Python installed on your machine (version 3.6 or higher). +- pip (Python package installer) installed. + +## 3. Setting Up Environment +1.**Install latest version of Python:** Go to Python.org and Download the latest version of Python. + +2.**Install a IDE:** Install any IDE of your choice to code. VS code is preferred. +>Note: You can use Google Colab without installing Python and IDE. + +3.**Install Beautiful Soup:** +```python +!pip install bs4 +``` + +## 4. Beautiful Soup Objects +Beautiful Soup is a Python library for pulling data out of HTML and XML files. This can be done by presenting the HTML as a set of objects. + +Take the below HTML page as input +``` + + + +Page Title + + +

Title

+

This is the main context of the page

+ + +``` + +lets store the HTML code in a variable +``` +html= " + + + Page Title + + +

Title

+

This is the main context of the page

+ + " +``` + +To parse the HTML document, pass the variable to the `BeautifulSoup` Constructor. +``` +soup = BeautifulSoup(html,'html.parser') +``` +Beautiful Soup transforms a complex HTML document into a complex tree of Python objects. + +## 5. Tags + +The Tag object corresponds to an HTML tag in the original document. + +``` +tag_obj = soup.title +print("tag object:", tag_obj) +print("tag object type:",type(tag_obj)) +``` +``` +Result: +tag object: Page Title + +tag object type: +``` + +## 6. Children, Parent and Siblings + +We can access the child of the tag or navigate down. + +``` +tag_obj = soup.h3 +child = tag_obj.b +print(child) +parent = child.parent +print(parent) +sib = tag_obj.next_sibling +print(sib); +``` +``` +Result: + Title +

Title

+

This is the main context of the page

+``` + +> We need to mention the child to which we want to navigate. It is because there can be more than one child to a tag but only one parent and next sibling. + +**Navigable String:** A string corresponds to a bit of text or content within a tag. Beautiful Soup uses the NavigableString class to contain this text. + +``` +tagstr = child.string +print(tagstr) +``` +``` +Result: +Title +``` + +## 7. Filter + +Filters allow you to find complex patterns, the simplest filter is a string. + +Consider the following HTML code: + +``` + + + + + + Sample Table + + +

Sample Table

+ + + + + + + + + + + + + + + + + + + + + + + + + +
NameAgeCity
John Doe28New York
Jane Smith34Los Angeles
Emily Jones23Chicago
+ + + +``` +Store the above code in a variable. +``` +table = " + + + + + Sample Table +.............. + +table_bs = BeautifulSoup(table, 'html.parser') +" +``` +**Find All:** The find_all() method looks through a tag’s descendants and retrieves all descendants that match your filters. It places all the objects in a list. We can access by indexing. + +## 8. Web Scraping the Contents of a Web Page + +1. Get the URL of the webpage we need to scrape. + +2. Get the HTML content by using `get` method in HTTP request. + +3. Pass the HTML content to the BeautifulSoup Constructor to prepare an object. + +4. Access the HTML elements using Tag name. + +Below is the code to extract the img tags in a wikipedia page. + +``` +import requests +!pip install bs4 +from bs4 import BeautifulSoup + +url = "https://en.wikipedia.org/wiki/Encyclopedia" +data = requests.get(url).text +soup = BeautifulSoup(data,"html.parser") + +for link in soup.find_all('a'): + print(link) + print(link.get('src')) +``` +We can retrieve any data in the webpage by access through the tags. diff --git a/contrib/web-scrapping/index.md b/contrib/web-scrapping/index.md index 276014ea..29f558aa 100644 --- a/contrib/web-scrapping/index.md +++ b/contrib/web-scrapping/index.md @@ -2,3 +2,4 @@ - [Section title](filename.md) - [Introduction to Flask](flask.md) +- [Web Scrapping Using Beautiful Soup](beautifulsoup.md) From b2643ad26d31b8e70e3bb41842745bb9f3129716 Mon Sep 17 00:00:00 2001 From: saiumasankar Date: Sat, 8 Jun 2024 15:03:43 +0530 Subject: [PATCH 3/4] deleting unnecessary --- contrib/web-scrapping/beautifulsoup.md | 206 ------------------------- contrib/web-scrapping/index.md | 1 - 2 files changed, 207 deletions(-) delete mode 100644 contrib/web-scrapping/beautifulsoup.md diff --git a/contrib/web-scrapping/beautifulsoup.md b/contrib/web-scrapping/beautifulsoup.md deleted file mode 100644 index 372b8f29..00000000 --- a/contrib/web-scrapping/beautifulsoup.md +++ /dev/null @@ -1,206 +0,0 @@ -# Beautiful Soup Library in Python - -## Table of Contents -1. Introduction -2. Prerequisites -3. Setting Up Environment -4. Beautiful Soup Objects -5. Tag object -6. Children, Parents and Siblings -7. Filter: Findall method -8. Web Scraping the Contents of a Web Page - -## 1. Introduction -Beautiful Soup is a Python library used for web scraping purposes to extract data from HTML and XML files. It creates a parse tree for parsed pages that can be used to extract data easily. - -## 2. Prerequisites -- Understanding of HTTP Requests and Responses. -- Understanding of HTTP methods (GET method). -- Basic Knowledge on HTML. -- Python installed on your machine (version 3.6 or higher). -- pip (Python package installer) installed. - -## 3. Setting Up Environment -1.**Install latest version of Python:** Go to Python.org and Download the latest version of Python. - -2.**Install a IDE:** Install any IDE of your choice to code. VS code is preferred. ->Note: You can use Google Colab without installing Python and IDE. - -3.**Install Beautiful Soup:** -```python -!pip install bs4 -``` - -## 4. Beautiful Soup Objects -Beautiful Soup is a Python library for pulling data out of HTML and XML files. This can be done by presenting the HTML as a set of objects. - -Take the below HTML page as input -``` - - - -Page Title - - -

Title

-

This is the main context of the page

- - -``` - -lets store the HTML code in a variable -``` -html= " - - - Page Title - - -

Title

-

This is the main context of the page

- - " -``` - -To parse the HTML document, pass the variable to the `BeautifulSoup` Constructor. -``` -soup = BeautifulSoup(html,'html.parser') -``` -Beautiful Soup transforms a complex HTML document into a complex tree of Python objects. - -## 5. Tags - -The Tag object corresponds to an HTML tag in the original document. - -``` -tag_obj = soup.title -print("tag object:", tag_obj) -print("tag object type:",type(tag_obj)) -``` -``` -Result: -tag object: Page Title - -tag object type: -``` - -## 6. Children, Parent and Siblings - -We can access the child of the tag or navigate down. - -``` -tag_obj = soup.h3 -child = tag_obj.b -print(child) -parent = child.parent -print(parent) -sib = tag_obj.next_sibling -print(sib); -``` -``` -Result: - Title -

Title

-

This is the main context of the page

-``` - -> We need to mention the child to which we want to navigate. It is because there can be more than one child to a tag but only one parent and next sibling. - -**Navigable String:** A string corresponds to a bit of text or content within a tag. Beautiful Soup uses the NavigableString class to contain this text. - -``` -tagstr = child.string -print(tagstr) -``` -``` -Result: -Title -``` - -## 7. Filter - -Filters allow you to find complex patterns, the simplest filter is a string. - -Consider the following HTML code: - -``` - - - - - - Sample Table - - -

Sample Table

- - - - - - - - - - - - - - - - - - - - - - - - - -
NameAgeCity
John Doe28New York
Jane Smith34Los Angeles
Emily Jones23Chicago
- - - -``` -Store the above code in a variable. -``` -table = " - - - - - Sample Table -.............. - -table_bs = BeautifulSoup(table, 'html.parser') -" -``` -**Find All:** The find_all() method looks through a tag’s descendants and retrieves all descendants that match your filters. It places all the objects in a list. We can access by indexing. - -## 8. Web Scraping the Contents of a Web Page - -1. Get the URL of the webpage we need to scrape. - -2. Get the HTML content by using `get` method in HTTP request. - -3. Pass the HTML content to the BeautifulSoup Constructor to prepare an object. - -4. Access the HTML elements using Tag name. - -Below is the code to extract the img tags in a wikipedia page. - -``` -import requests -!pip install bs4 -from bs4 import BeautifulSoup - -url = "https://en.wikipedia.org/wiki/Encyclopedia" -data = requests.get(url).text -soup = BeautifulSoup(data,"html.parser") - -for link in soup.find_all('a'): - print(link) - print(link.get('src')) -``` -We can retrieve any data in the webpage by access through the tags. diff --git a/contrib/web-scrapping/index.md b/contrib/web-scrapping/index.md index 29f558aa..276014ea 100644 --- a/contrib/web-scrapping/index.md +++ b/contrib/web-scrapping/index.md @@ -2,4 +2,3 @@ - [Section title](filename.md) - [Introduction to Flask](flask.md) -- [Web Scrapping Using Beautiful Soup](beautifulsoup.md) From 41e41d804d105358a512e7a6aaa0feb1e3db4522 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Thu, 13 Jun 2024 01:33:30 +0530 Subject: [PATCH 4/4] Update dynamic-programming.md --- contrib/ds-algorithms/dynamic-programming.md | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/contrib/ds-algorithms/dynamic-programming.md b/contrib/ds-algorithms/dynamic-programming.md index 54c6fcbf..0f16d5c9 100644 --- a/contrib/ds-algorithms/dynamic-programming.md +++ b/contrib/ds-algorithms/dynamic-programming.md @@ -51,10 +51,6 @@ print(f"The {n}th Fibonacci number is: {fibonacci(n)}.") - **Time Complexity**: O(n) for both approaches - **Space Complexity**: O(n) for the top-down approach (due to memoization), O(1) for the bottom-up approach -
-
-
- # 2. Longest Common Subsequence The longest common subsequence (LCS) problem is to find the longest subsequence common to two sequences. A subsequence is a sequence that appears in the same relative order but not necessarily contiguous. @@ -112,10 +108,6 @@ print("Length of LCS is", longestCommonSubsequence(S1, S2, m, n)) - **Time Complexity**: O(m * n) for both approaches, where m and n are the lengths of the input sequences - **Space Complexity**: O(m * n) for the memoization table -
-
-
- # 3. 0-1 Knapsack Problem The 0-1 knapsack problem is a classic optimization problem where the goal is to maximize the total value of items selected while keeping the total weight within a specified limit. @@ -146,6 +138,7 @@ capacity = 50 n = len(weights) print("Maximum value that can be obtained:", knapsack(weights, values, capacity, n)) ``` + ## 0-1 Knapsack Problem Code in Python (Bottom-up Approach) ```python @@ -170,14 +163,11 @@ capacity = 50 n = len(weights) print(knapSack(capacity, weights, values, n)) ``` + ## Complexity Analysis - **Time Complexity**: O(n * W) for both approaches, where n is the number of items and W is the capacity of the knapsack - **Space Complexity**: O(n * W) for the memoization table -
-
-
- # 4. Longest Increasing Subsequence The Longest Increasing Subsequence (LIS) is a task is to find the longest subsequence that is strictly increasing, meaning each element in the subsequence is greater than the one before it. This subsequence must maintain the order of elements as they appear in the original sequence but does not need to be contiguous. The goal is to identify the subsequence with the maximum possible length. @@ -190,7 +180,6 @@ The Longest Increasing Subsequence (LIS) is a task is to find the longest subseq ## Longest Increasing Subsequence Code in Python (Top-Down Approach using Memoization) ```python - import sys def f(idx, prev_idx, n, a, dp): @@ -241,10 +230,7 @@ def lis(arr): arr = [10, 22, 9, 33, 21, 50, 41, 60] print("Length of lis is", lis(arr)) ``` + ## Complexity Analysis - **Time Complexity**: O(n * n) for both approaches, where n is the length of the array. - **Space Complexity**: O(n * n) for the memoization table in Top-Down Approach, O(n) in Bottom-Up Approach. - -
-
-