Posts

Showing posts from October, 2023

Recursion with Python

Recursion with Python Recursion with Python Recursion is a concept in programming that allows problem solving by dividing the problem into smaller recurring steps. Generally, this is done via recursive functions. A recursive function is a function that invokes itself. It comprises two fundamental components: the base case and the recursive case. Try googling the term “recursion” accurately. Base Case The base case is an exit condition. This prevents the function invoking itself indefinitely. Recursive Case The recursive case is where the function invokes itself. For each invocation, function arguments are updated so that the function can reach towards the base case. Let’s implement a recursive function to calculate the factorial of a number. def factorial ( num ) : # base case if num == 1 : return 1 # recursive case return num * factorial ( num - 1 ) NUM = 3 print ( f "Factorial of {NUM} is {fa...

Algorithms with Python

Algorithms with Python Algorithms with Python - Part 1 An algorithm, in layperson’s terms, is a sequence of instructions. They provide a step-by-step procedure to be followed by a computer, in order to solve a problem or make decisions. In this article, we will only be discussing the searching and sorting algorithms; albeit, there are more algorithms in the programming realm. Search Searching algorithms are used to locate particular elements within a data structure. Linear search and binary search are two prevalent searching algorithms. Linear Search Linear or sequential search is the simplest searching algorithm. This is implemented by iterating through the elements of the data structure one at a time and comparing each element against the target search item. Therefore, the time complexity of the algorithm - O( n n n ) - depends on the number of elements, making it suitable for smaller and simpler programs. Let’s implement a linear search algorithm to ...