Skip to content

Commit

Permalink
ADD data structures and algorithms projects
Browse files Browse the repository at this point in the history
  • Loading branch information
vnk8071 committed Feb 13, 2024
1 parent e396c05 commit 0281a64
Show file tree
Hide file tree
Showing 61 changed files with 19,486 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/data_structures_and_algorithms/nanodegree_outline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
sidebar_label: 'Nanodegree Outline'
sidebar_position: 1
---

# Data Structures and Algorithms Nanodegree

[Syllabus](https://d20vrrgs8k4bvw.cloudfront.net/documents/en-US/Enterprise+Syllabi/Generic/Udacity+Enterprise+Syllabus+Data+Structures+%26+Algorithms+nd256.pdf)

[Noted course in Google Drive](https://docs.google.com/document/d/199eoKlppeCCv1WBt3c3A9RntoR3O2xtduDcYYN6mVYk/edit?usp=sharing)

## Introduction
Start with a warm welcome to the program by refreshing your Python skills and learning about problem solving and efficiency!

### Project: [Unscramble Computer Science Problems](https://learn.udacity.com/nanodegrees/nd256/parts/41f9d398-7fa4-48e7-96de-88c163e8fba6/lessons/4f281aec-fbff-45f5-9044-ab5caf4cc5b6/concepts/0fd3dae5-41ca-4597-a4a4-d3842129fd65)

### Source code: [vnk8071/unscramble_computer_science_problems](https://github.com/vnk8071/machine-learning-in-production/tree/main/projects/unscramble_computer_science_problems)

## Data Structures
Learn about the core data structures used in programming.

### Project: [Show Me the Data Structures](https://learn.udacity.com/nanodegrees/nd256/parts/cd0328/lessons/2d9a942c-bcc4-4954-9d65-954b5ab3066f/concepts/07869251-2f07-4202-a00a-0d53922e7f26)

### Source code: [vnk8071/show_me_the_data_structures](https://github.com/vnk8071/machine-learning-in-production/tree/main/projects/show_me_the_data_structures)

## Basic Algorithms
Learn about the basic algorithms used in programming.

### Project: [Problems vs. Algorithms](https://learn.udacity.com/nanodegrees/nd256/parts/cd1887/lessons/032713d7-07e0-468f-8393-7b34bf2899e7/concepts/93275178-63ff-4666-88a3-e66e6e29eb2f)

### Source code: [vnk8071/problems_vs_algorithms](https://github.com/vnk8071/machine-learning-in-production/tree/main/projects/problems_vs_algorithms)

## Advanced Algorithms
Learn about the advanced algorithms used in programming.

### Project: [Route Planner](https://learn.udacity.com/nanodegrees/nd256/parts/cd1888/lessons/dc2487e4-aa4a-4301-a0eb-71a99728d78b/concepts/90577506-dfd3-40b7-9696-e1a3369db256)

### Source code: [vnk8071/route_planner](https://github.com/vnk8071/machine-learning-in-production/tree/main/projects/route_planner)
76 changes: 76 additions & 0 deletions docs/data_structures_and_algorithms/problems_vs_algorithms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
sidebar_label: 'Problems vs. Algorithms'
sidebar_position: 4
---

# Problems vs. Algorithms

## Data Structure Questions

For this project, you will answer the seven questions laid out in the next sections. The questions cover a variety of topics related to the basic algorithms you've learned in this course. You will write up a clean and efficient answer in Python, as well as a text explanation of the efficiency of your code and your design choices.

## Problem 1: Square Root of an Integer

Find the square root of the integer without using any Python library. You have to find the floor value of the square root.

For example if the given number is 16, then the answer would be 4.

If the given number is 27, the answer would be 5 because sqrt(5) = 5.196 whose floor value is 5.

The expected time complexity is O(log(n))

Code in `problem_1.py` and explanation in `explanation_problem_1.md`

## Problem 2: Search in a Rotated Sorted Array

You are given a sorted array which is rotated at some random pivot point.

Example: [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]

You are given a target value to search. If found in the array return its index, otherwise return -1.

You can assume there are no duplicates in the array and your algorithm's runtime complexity must be in the order of O(log n).

Example:

Input: nums = [4, 5, 6, 7, 0, 1, 2], target = 0, Output: 4

Code in `problem_2.py` and explanation in `explanation_problem_2.md`

## Problem 3: Rearrange Array Digits

Rearrange Array Elements so as to form two number such that their sum is maximum. Return these two numbers. You can assume that all array elements are in the range [0, 9]. The number of digits in both the numbers cannot differ by more than 1. You're not allowed to use any sorting function that Python provides and the expected time complexity is O(nlog(n)).

For example, the input [1, 2, 3, 4, 5] will return 531, 42.

Code in `problem_3.py` and explanation in `explanation_problem_3.md`

## Problem 4: Sort 0, 1, 2

Given an input array consisting on only 0, 1, and 2, sort the array in a single traversal. You're not allowed to use any sorting function that Python provides.

Note: O(n) does not necessarily mean single-traversal. For e.g. if you traverse the array twice, that would still be an O(n) solution but it will not count as single traversal.

Code in `problem_4.py` and explanation in `explanation_problem_4.md`

## Problem 5: Autocomplete with Tries

We've learned about Tries earlier in this course, its time to implement one. Implement a trie with insert, search, and startsWith methods.

Code in `problem_5.py` and explanation in `explanation_problem_5.md`

## Problem 6: Unsorted Integer Array

In this problem, we will look for smallest and largest integer from a list of unsorted integers. The code should run in O(n) time. Do not use Python's inbuilt functions to find min and max.

Code in `problem_6.py` and explanation in `explanation_problem_6.md`

## Problem 7: Request Routing in a Web Server with a Trie

For this exercise we are going to implement an HTTPRouter like you would find in a typical web server using the Trie data structure we learned previously.

There are many different implementations of HTTP Routers such as regular expressions or simple string matching, but the Trie is an excellent and very efficient data structure for this purpose.

The purpose of an HTTP Router is to take a URL path like "/", "/about", or "/blog/2019-01-15/my-awesome-post" and figure out what content to return. In a dynamic web server, the content will often come from a block of code called a handler.

Code in `problem_7.py` and explanation in `explanation_problem_7.md`
50 changes: 50 additions & 0 deletions docs/data_structures_and_algorithms/route_planner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
sidebar_label: 'Route Planner'
sidebar_position: 5
---

# Route Planner

## Introduction

This is a simple route planner that uses the A* algorithm to find the shortest path between two points on a grid. The grid is represented as a 2D array of 0s and 1s, where 0s represent open cells and 1s represent obstacles.

## Path Finder

This Python program implements a path finder that finds the shortest path between two nodes in a graph space. The graph space is represented by nodes and lines, where each node has an id, x and y coordinates, and each line connects two nodes.

## Classes

The program consists of several classes:

1. `GraphNode`: Represents a node in the graph. It has an id, x and y coordinates, and a list of nodes it is connected to.

2. `PriorityQueue`: A priority queue that stores nodes and their total distances. It allows adding or updating a node, and removing the node with the smallest total distance.

3. `GraphSpace`: Represents a graph space that contains nodes and lines. It allows finding linked nodes for a given node.

4. `PathFinder`: Finds the shortest path between two nodes in a graph space. It uses the A* search algorithm to find the shortest path.

## Methods

The main methods in the `PathFinder` class are:

1. `calculate_distance(from_xy, to_xy)`: Calculates the Euclidean distance between two points.

2. `initialize_goal(goal_id)` and `initialize_origin(origin_id)`: Initialize the goal and origin nodes respectively.

3. `add_to_frontier(from_node, to_node)`: Adds a node to the frontier, or updates its total distance if it is already in the frontier.

4. `get_route_ids()`: Returns the ids of the nodes in the shortest path.

5. `find_shortest_path(origin_id, goal_id)`: Finds the shortest path between two nodes.

## Usage

The main function `shortest_path(map_object, start, goal)` takes a map object and the ids of the start and goal nodes, and returns the shortest path between the start and goal nodes. The map object should have `intersections` and `roads` attributes, where `intersections` is a dictionary of node ids to (x, y) coordinates, and `roads` is a list of lists, where each sublist contains the ids of nodes that a node is connected to.

## Time and Space Complexity

The time complexity of the `find_shortest_path` method is O(n log n), where n is the number of nodes in the graph. This is because it uses a priority queue to keep track of the nodes to visit next, and each insertion operation in a priority queue takes log n time.

The space complexity is O(n), where n is the number of nodes in the graph. This is because it needs to store all nodes in the graph space, and in the worst case, all nodes could be added to the priority queue.
51 changes: 51 additions & 0 deletions docs/data_structures_and_algorithms/show_me_the_data_structures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
sidebar_label: 'Show Me the Data Structures'
sidebar_position: 3
---

# Show me the data structures

## Data Structure Questions

For this project, you will answer the six questions laid out in the next sections. The questions cover a variety of topics related to the data structures you've learned in this course. You will write up a clean and efficient answer in Python, as well as a text explanation of the efficiency of your code and your design choices.

## Problem 1: Least Recently Used Cache

A Least Recently Used (LRU) Cache is a type of cache in which we remove the least recently used entry when the cache memory reaches its limit. For the current problem, consider both get and set operations as an use operation.

Your job is to use an appropriate data structure to implement a LRU cache which has the following methods:

- `set(key, value)`: set the value if the key is not present in the cache. If the cache is at capacity, remove the oldest entry.
- `get(key)`: return the value if the key is present in the cache, otherwise return -1.

Code in `problem_1.py` and explanation in `explanation_problem_1.md`

## Problem 2: File Recursion

For this problem, the goal is to write code for finding all files under a directory (and all directories beneath it) that end with ".c"

Code in `problem_2.py` and explanation in `explanation_problem_2.md`

## Problem 3: Huffman Coding

Huffman coding is a compression algorithm that can be used to compress lists of characters. The algorithm uses a binary tree to assign variable-length codes to each character. The more frequent the character, the shorter the code used to represent it. The binary tree is constructed in such a way that the code assigned to each character is the prefix of any other code assigned to any other character.

Code in `problem_3.py` and explanation in `explanation_problem_3.md`

## Problem 4: Active Directory

In Windows Active Directory, a group can consist of user(s) and group(s) themselves. We can construct this hierarchy as such. Where User is represented by str representing their ids.

Code in `problem_4.py` and explanation in `explanation_problem_4.md`

## Problem 5: Blockchain

A Blockchain is a sequential chain of records, similar to a linked list. Each block contains some information and how it is connected related to the other blocks in the chain. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data. For our blockchain, we will be using a SHA-256 hash, the Greenwich Mean Time when the block was created, and text strings as the data.

Code in `problem_5.py` and explanation in `explanation_problem_5.md`

## Problem 6: Union and Intersection of Two Linked Lists

Your task for this problem is to fill out the union and intersection functions. The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. The intersection of two sets A and B, denoted by A ∩ B, is the set of all objects that are members of both the sets A and B.

Code in `problem_6.py` and explanation in `explanation_problem_6.md`
Loading

0 comments on commit 0281a64

Please sign in to comment.