Is a Number a Prime Number using Trial Division

I am tutoring students in Java II this semester and one of the programming assignments is to determine if a number is prime number, and if it is not a prime number, to determine all the factors of the given number. A prime number is any number, N, that only has factors of 1 and N. For example, 11 is a prime number, because it only has factors of 1 and 11. The number 6 is not a prime number, because it has factors of 1, 2, 3, and 6.

Reverse a Linked List is a Popular Technical Interview Question

Reversing a linked list is a popular technical interview question and one I see a lot on popular programming challenge websites. The programming challenge typically has a custom LinkedList Class with a Reverse Method that the developer needs to complete to properly reverse a linked list. A Node Class is provided that represents each node in the linked list, and the LinkedList Class has a pointer to the head node. Let's solve this programming challenge in Python so we can solve it for our next technical job interview.

Detect Cycle in Linked List Using Floyd's Cycle-Finding Algorithm

We've looked at using linear search to find a node in a linked list, inserting nodes in linked list to maintain a sorted list of values, and even created Stack and Queue abstract data structures using linked list as the underlying data structure. Now let's turn our attention to a very popular programming challenge using linked lists - detecting a cycle in a linked list.

Coding a Queue Abstract Data Type using a Linked List in Python

In the previous tutorial I discussed how to code a Stack Abstract Data Type using a linked list as the underlying data structure. In this tutorial I will create a Queue Abstract Data Type using a linked list. The queue will implement the two most common operations of enqueue and dequeue to add and remove an item from the queue. Although a linked list will maintain the state of the queue, the software developer will not interact with the linked list directly. The details of the algorithm and data structures used to perform dequeue and enqueue are encapsulated by the custom Python Queue Class.

Coding a Stack Abstract Data Structure Using Linked List in Python

In recent tutorials I have been discussing linked lists. Linked lists are a fundamental data structure in computer science and a popular topic in technical interviews and programming challenges. In the first tutorial I discussed using linear search to find a value in a linked list, and in the second tutorial I discussed inserting a new node in a linked list.

Inserting New Node in a Linked List Using Python

Earlier I demonstrated how to search a linked list using linear search, which is a popular technical interview question and programming challenge to test one's knowledge of computer science data structures and algorithms. Another popular coding challenge is to insert a new value in a linked list, which is what I would like to demonstrate in this tutorial. However, let's add a twist. Let's insert integer values so that the linked list maintains the list of integers in ascending numerical value.

Linear Search of Linked List in Python

A linked list is a fundamental data structure in computer science and a common subject of technical job interview questions and programming challenges. One of the simplest challenges, and a great starting point for learning linked lists, is to search a linked list to see if it contains a given value.

Shell Sort

Shell sort is a sorting algorithm based on insertion sort. Insertion sort uses a fixed distance of 1 when it compares values during the sorting process. Insertion sort moves from left-to-right and picks the next value in the unsorted portion of the list and inserts it into its proper position in the sorted portion of the list. Shellsort uses a similar algorithm, but compares values over greater distances, called gaps. The benefit of comparing values over larger distances is that shell sort can quickly move small and large values to the front and end of the list, reducing the overall number of exchanges done by insertion sort. The final pass or gap used by shell sort is 1, which is insertion sort.

Cocktail Sort

Cocktail sort is a sorting algorithm, like comb sort, that attempts to improve the performance of bubble sort by eliminating turtles. Turtles are small numbers at the end of the unsorted list that slowly move to the front of the list one position at a time using bubble sort. Turtles greatly reduce the performance of bubble sort by increasing the number of passes bubble sort must make through the list. Although cocktail sort, comb sort, and bubble sort are considered impractical for use in real-world applications, there is a lot to gain from understanding them algorithmically in a computer science classroom.

Comb Sort Improves Bubble Sort by Eliminating Turtles

Bubble sort is one of the first sorting algorithms presented in Computer Science. It's not a very practical sorting algorithm, but it is useful for presenting the concepts of sorting and teaching computer science students to think algorithmically.

Depth-First Search in Python - Recursive and Non-Recursive Programming

In my graph algorithms course we have been discussing breadth-first search and depth-first search algorithms and are now transitioning to directed acyclic graphs (DAGs) and topological sorting. In class we discussed one method of topological sorting that uses depth-first search. Before writing an article on topological sorting in Python, I programmed 2 algorithms for doing depth-first search in Python that I want to share. One is a recursive Python function and the other is a non-recursive solution that introduces a Stack Data Structure to implement the stack behavior that is inherent to a recursive function. I already coded C# versions of depth-first search and breadth-first search, but I am learning Python along with learning algorithms, so I want to share examples of depth-first search in Python as well.

Breadth-First Search and Shortest Path in C# and .NET Core

Last time I talked about using Depth-First Search in C# for traversing graphs, such as networks, web pages, social networks, etc. In this article I will discuss Breadth-First Search, which is another graph search algorithm. Depth-first search is considered an aggressive algorithm, diving deep into the graph and backtracking only when it hits a dead end, only to dive deep once it finds the next available path. Breadth-first search, on the otherhand, is considered a more cautious algorithm. Rather than going deep, breadth-first search checks each path level-by-level, slowly reaching the depths of the graph.

Depth-First Search Algorithm in C# and .NET Core

I am currently learning and using Python in my computer science and algorithms courses, but C# is the programming language I have been using for years. In this article I will be coding the depth-first search algorithm using C#. In particular, this is C# 6 running on .NET Core 1.1 on macOS, and I am coding with VS Code. If interested, you can also learn about breadth-first search in C#.

Decorators in Python to Improve Naive Recursive Function for Fibonacci

Fibonacci numbers have been used a lot by my computer science and algorithms professors to demonstrate recursive functions and performance differences among algorithms. The naive recursive versions of calculating Fibonacci numbers is the one you see the most.

Verify Binary Search Tree is Valid in Python

This weekend I completed the data structures portion of the Cracking the Coding Interview Questions on HackerRank. There are a couple of data structurer programming challenges I want to mention, because they opened my eyes to new and interesting ways to solve the problem and/or helped me realize the error of my ways.

Binary Heap and heapq in Python

In my data structures class I have been learning about Binary Heaps. I have been coding examples of min and max Binary Heaps in both Python and C# the past few days, and have become quite fond of the simplicity and elegance of the data structure. Typically you just need the ability to push an element onto the Binary Heap and pop either the minimum or maximum element from the Binary Heap. However, if you write other methods, like change priority or remove if using it as a Priority Queue, the solutions are very elegant, because they re-use the siftup and siftdown methods already in place for push and pop. It's a pretty amazing data structure when you get to know its inner-workings. The fact that you can use it to sort items (Heapsort) makes it all that much cooler.

Queue Data Structure in Python and C#

Last time I looked at implementing the Stack Data Structure in Python and C#. In this article I'll be implementing similar solutions in Python and C# using the Queue Data Structure. The Stack is a last-in, first-out data structure and the Queue is a first-in, first-out data structure.

Local Functions and Memoization in Python to Solve for Fibonacci Numbers

As I continue to take computer science and algorithms courses, the naieve, recursive function to calculate Fibonacci numbers is used to illustrate a lot of programming techniques. The main issue with the recursive algorithm to calculate Fibonacci numbers is that as the numbers get big, the recursive algorithm calculates several Fibonacci numbers over and over again. Memoization, which I will talk about here, helps reduce the number of recursive calls by storing previous values.

Stack Data Structure in Python and C#

In my data structures class we are talking about stacks and ways to implement them using arrays and linked-lists. When I first started learning Python I learned that Python's built-in List Type provides stack functionality.

Inorder Traversal of Binary Search Tree in Python

In addition to taking courses on Python, computer science, data structures, and algorithms, I have joined a few communities offering programming challenges to help me improve my coding skills. I enjoy these programming challenges, and recently finished the 30 Days of Code Challenge at HackerRank using both C# and Python.

Bubble Sort in Python and HackerRank 30 Days of Code

I enjoy coding sorting algorithms (merge sort, insertion sort, selection sort, quicksort), so I was excited to see that Day 20 of HackerRank's 30 Days of Code was to write Bubble sort. I have written Bubble sort in Python numerous times and meant to write an article on it, but there are so many cool things I am learning now that it got pushed on the backburner. Today is the day, however, that I not only mention Bubble sort, but also mention HackerRank in case you haven't heard of it before.

Quickselect Algorithm in Python

Right after learning the Quicksort Algorithm in one of my algorithms classes we learned the Quickselect Algorithm. The Quickselect Algorithm is a selection algorithm, whereas the Quicksort Algorithm is a sorting algorithm. The really interesting part about the Quickselect Algorithm is that it is essentially the Quicksort Algorithm, except that one only needs to recursively call only one side of the partition to be re-partitioned. This changes the best case runtime from O(nlogn) to O(n).

Set Host Environment in ASP.NET Core Using Command Line Arguments

Most articles talk about setting the host environment in ASP.NET Core using Environment Variables, but I wanted to set the host environment in an ASP.NET Core Web Application using command line arguments. This is actually really easy to do.

User Secrets in ASP.NET Core MVC Web Application

ASP.NET Core supports user secrets as part of its configuration system, which is the ability to store and retrieve configuration information on a per application and per user basis. This information is stored as key value pairs in a secrets.json file in the user profile directory. This is a perfect location for storing information that is user specific, like the username and password for social media and other services like twitter, facebook, github, etc. Since the user secrets are not stored in the .NET Core application directory, there is no chance the user secrets will be pushed to your source code repository and shared with others.

Quicksort Algorithm in Python

I've covered a few sorting algorithms in Python that I have learned and analyzed in my computer science and algorithms classes: Insertion Sort Using Python, Selection Sort in Python, and Merge Sort in Python Using Pythonista 3 on iPad Pro. Today, in one of my algorithms design and analysis classes I learned about Quicksort. My instructor was particularly enthusiastic about Quicksort, because he thinks it is a very elegant and practical algorithm. Much like Merge Sort, it is a divide and conquer sorting algorithm that sorts the items in O(nlogn).

Python Fibonacci Number Generator

Earlier I had mentioned I was taking several courses on computer science and algorithms while I was learning Python. The Fibonacci Sequence seems to appear a lot in these classes and I mentioned several assignments where I had to generate a list of Fibonacci numbers using a recursive function and Dynamic Programming. I talked about this in the article: Fibonacci Numbers - Tale of Two Algorithms using Python.

Getting Started with ASP.NET Core MVC and EF Core

I have been writing about Entity Framework Core ( EF Core ) lately and how to integrate it with .NET Core. If you are interested in learning how to use EF Core in a .NET Core Console Application, you may be interested in my article Getting Started with Entity Framework Core and SQLite. I then showed how to use EF Core Migrations to manage the SQLite database in the article EF Core Migrations Tutorial.

Algorithm to Make Change in Python - Dynamic Programming

Making change is another common example of Dynamic Programming discussed in my algorithms classes. This is almost identical to the example earlier to solve the Knapsack Problem in Clash of Clans using Python, but it might be easier to understand for a common scenario of making change. Dynamic Programming is a good algorithm to use for problems that have overlapping sub-problems like this one. Just like with the Clash of Clans example, this is a discrete Knapsack problem allowing repetition.

Dynamic Programming in Python for Clash of Clans

I have been attending several online computer science and algorithm courses and one of the popular examples is the Knapsack problem, which is typically presented when learning Greedy and Dynamic Programming Algorithms. When I learned the Knapsack problem I immediately thought of the popular game Clash of Clans.

EF Core Migrations

In my Getting Started with Entity Framework Core and SQLite article I wrote a simple .NET Core Application that performs CRUD operations on a SQLite Database. Rather than using EF Core Migrations to create migration files to create and update the database, I instead opted to perform these operations in the code using various methods on my custom DbContext Class, called SqliteDbContext.

Entity Framework Core Example Using SQLAlchemy

Earlier I wrote an article, called Getting Started with Entity Framework Core and SQLite, where I walked through an example of using Entity Framework Core to perform CRUD operations against a SQLite Database on macOS using Visual Studio Code. The ultimate goal of that article is to update my ASP.NET Core Web API example to use a database, but I had this desire to first try the same thing with Python using SQLAlchemy.

Getting Started with Entity Framework Core and SQLite

Earlier I created a Reminder Service using ASP.NET Core Web API. For simplicity I used a Hashset in C# as the repository for the reminders. It might be more useful to use a database, and therefore in this example I will create a .NET Core Console Application that uses Entity Framework Core to create a SQLite Database and perform various CRUD operations. Later I will add EF Core and SQLite to the Reminder Service.

Insertion Sort Using Python

I mentioned on twitter that I wrote a quick insertion sort function in Python using Pythonista on my iPad Pro. Searching and sorting algorithms are very popular subjects in my computer since and algorithms classes, so I am familiarizing myself with the more common ones and coding them up using Python and C#. I've already mentioned a couple of popular sorting algorithms in articles: selection sort and merge sort.

Caesar Cipher in Python - Classical Cryptography

Classical cryptography and stenography are very fun to program. They are often used in various capture the flag programmer events. Classical substitution ciphers, like Caesar Cipher, are particularly fun, because they are simple enough to understand and crack with just a little bit of knowledge. One can often find puzzles, called Cryptograms or Cryptoquotes, in newspapers or online that challenge the user to figure out a popular phrase given the ciphertext version of it.

ASP.NET Core MVC Web Template

I've been writing numerous ASP.NET Core Tutorials to document my adventures learning ASP.NET Core MVC, Web API, etc. I've been starting these applications from scratch using Visual Studio Code on macOS. I've purposely avoided wizards so I know what's happening under the covers.

Python Training - Selection Sort with Sample Unit Tests

I am learning Python, and taking several online courses on computer science and algorithms. One of my assigments is to write a Selection Sort in Python. Selection sort is one of the more intuitive sort routines, because it logically works how one would probably sort a small list of items manually.

Using PyCharm's REST Client to Test ASP.NET Core Web API

In a recent article I talked about developing an ASP.NET Core Web API Application on macOS using Visual Studio Code. I also developed a quick Python Script that used the Web API. However, to test the ASP.NET Core Web API I first used PyCharm's REST Client to make sure everything was working accordingly.

ASP.NET Core Web API on macOS Using Visual Studio Code

In this article I will create a simple ASP.NET Core Web API Application that allows me to list, add, and remove reminders. I will also build a quick Python Script that uses the Web API. In addition, I will show an example of ASP.NET Core Dependency Injection. If you haven't read my ASP.NET Core MVC from Scratch Tutorial and you are really new to this, I recommend reading that tutorial first. I spend a fair amount of time explaining the details, which my be really useful for you.

Merge Sort in Python Using Pythonista 3 on iPad Pro

In one of my algorithms courses I just started learning divide and conquer algorithms and the first example provided by my instructor is Merge Sort. My instructor describes Merge Sort using pseudo code, but unapologetically leaves out a lot of details like merging, etc. This is perfect for me since in addition to algorithms I am also learning Python and this gives me a chance to write a Merge Sort using Python for the first time.

Python Web Client Retrieving Factorial Calculations from ASP.NET Core Website

When I wrote the ASP.NET Core Website that calculates the factorial of a number I promised myself I would write a Python client that uses the factorial service. After a minor change to the website to handle custom http headers, I wrote a quick Python client using the Requests http library for Python. Here are the details.

ASP.NET Core Routing and RouteBuilder.MapGet for Calculating a Factorial

Earlier I wrote about ASP.NET Core MVC from scratch as well as ASP.NET Core Routing as part of my learning the fundamentals of ASP.NET Core. I am developing these examples without Visual Studio. Instead, I am using Visual Studio Code on macOS.

Binary Search Example in Python

While learning Python I am attending several online courses on computer science, data structures, and algorithms. One of the topics covered is binary search.

Binary search is all about finding items in a sorted list quickly. The key here is sorted. If I know the list is sorted, I can perform binary search and be assured I can find the item very quickly. There is a formula for how many guesses (x) it will take to find an item from a list containing n items.

ASP.NET Core Routing Tutorial

In an earlier article I built an ASP.NET Core MVC Web Application from scratch on macOS using the dotnet CLI and Visual Studio Code. If you're wondering how to get started with ASP.NET Core, that is a good article that teaches the basics.

Fibonacci Numbers - Tale of Two Algorithms using Python

Earlier I mentioned a recursive function to detect palindromes that I needed to create for my Python computer science course. I was really happy when I grasped the usefulness and power of recursive functions from the lecture and was able to apply it so quickly to palindromes. The class also wrote recursive functions to calculate Fibonacci numbers as well as paying off credit cards.

ASP.NET Core MVC From Scratch

ASP.NET Core was recently released by Microsoft, and I am really excited about this new cross-platform web framework. As soon as it was released I had .NET Core running on my MacBook Pro and was developing console application as well as web applications using Visual Studio Code.

Python Sets and the Hangman Game

Earlier I mentioned a problem set in my computer science course to build the Hangman Game in Python. I am happy to report that I finished the game and it is working wonderfully.

I had also mentioned how the problem set required me to build a few helper functions before constructing the game loop. What excited me most about the development was the use of the set data structure in Python. Previously, I mentioned a helper function I built called word_guessed that returned True or False depending on if the player had guessed the secret word. I went into a discussion of imperative vs. declarative programming and how I prefered the declarative approach using sets in Python.

Python Recursive Function to Detect Palindromes

I'm about 3 weeks into one of my computer science courses using Python and the discussion turned to recursive programming. It sounds a bit mysterious and complicated, but it turns out writing recursive functions in Python is pretty easy. The instructor separated the process of writing recursive functions into a couple of steps. I talk about these steps while I write a recursive Python function to detect Palindromes.

Programming the Hangman Game in Python

My computer science and programming class using Python has a new problem set that involves programming the game Hangman. Part I of the problem involves creating a helper function that takes 1) the secret word and 2) a list of guesses, and returns either True or False depending on if the word has been guessed. The function structure will look like the following.

Find Two Largest Integers in List Using Python

The problem in my introductory computer science class asks me to find the two largest integers in a list. The list is filled only with integers and I can assume that there are at least 2 integers in the list.

Python Program to Calculate Square Roots using Babylonian Method

I need to develop a guess and check program in Python to calculate the square root of a number. In a guess and check program I provide an initial guess for the answer. The program checks to see if that guess is correct. If it is, the program is done. If not, the program generates another guess and checks to see if that guess is correct. The program continues the iterative process of guessing and checking until it finds either the exact answer or a close enough approximation to the answer.