This repository contains Python code for some assignments and LeetCode problems. The code is written for educational purposes and may not be optimal or efficient.
To use this repository, you need to have Python installed on your system. You can run the code in any Python IDE or terminal. You can also modify the code or add new files as per your requirements.
The repository is organized into folders, each containing a specific assignment or a set of LeetCode problems. The folders are:
This folder contains Python code for the first assignment, which involves generating random numbers, creating and manipulating linked lists, and implementing a general tree data structure.
-
Q1.py: This code simply uses a for loop to generate 1 billion random integers between 0 and 999 using the
random.randint()
function. It then writes each number to a file calledrandom_numbers.txt
. Since this code can still take a long time to run and may use a lot of memory, we are generating the numbers and writing them to a file. -
Q2.py: This code defines a
Node class
to represent a node in the linked list, and aLinkedList class
to represent the linked list itself. The LinkedList class has methods toappend
new nodes to the end of the list,view
data at a given index,reverse
the list, andsort
the list. To create the linked list from data read from a file, we first read the data from the file using theopen()
andread()
functions, then loop through the data and append each element to the linked list using theappend()
method. We can view the data at a given index in the linked list using theview()
method, which traverses the list until it reaches the desired index and returns the data at that index. To reverse the linked list, we traverse the list using three pointers:prev_node
,current_node
, andnext_node
. We setcurrent_node
to the head of the list andprev_node
to None. We then loop through the list, settingnext_node
to the node followingcurrent_node
, then setting the next attribute ofcurrent_node
toprev_node
, and finally settingprev_node
tocurrent_node
andcurrent_node
tonext_node
.
This folder contains Python code for the second assignment, which involves generating random characters and finding the probability of getting a substring match within the generated data.
- charGen.py: This code is a Python script that generates a file with 100,000 random lowercase letters. It uses the random module to select a character from the ASCII range 97 to 122, which corresponds to the lowercase letters ‘a’ to ‘z’. It writes each character to the file named ‘random_chars.txt’, and displays a progress bar on the standard output stream using the sys module. The code uses a with a statement to ensure that the file is closed properly after writing.
- Q2.py: This Python code searches for a given string and its substrings in a file with random characters. It generates all possible substrings of the given string using a function called genSubString. It then reads the file and compares each substring with the file contents. It counts the number of matches and partial matches for each substring length and calculates the probability of occurrence. It prints the results on the standard output stream.
This folder contains Python implementations of some common data structures and algorithms.
-
binary_tree: This folder contains a module that defines a binary tree class and some methods to manipulate it, such as traversing, inserting, deleting, searching, and finding the height of the tree.
-
dna_sequence_search: This folder contains a module that implements a DNA sequence search algorithm using a trie data structure. The algorithm can find all occurrences of a given pattern in a DNA sequence in linear time.
-
graph/basic_terminology: This folder contains a module that introduces some basic graph terminology and concepts, such as vertices, edges, adjacency lists, adjacency matrices, degrees, paths, cycles, connected components, etc.
-
stack_and_queue: This folder contains two modules that define stack and queue classes using Python lists. The modules also demonstrate some applications of stacks and queues, such as reversing a string, checking balanced parentheses, and implementing a breadth-first search algorithm.
This folder contains Python solutions to some LeetCode problems, categorized by difficulty level and topic. The folder also contains some test cases and explanations for each solution.