Generating a PDF Calendar

by Dan Duda

Requirements

  • HTML and CSS Experience
  • C# Experience
  • Visual Studio 2017 Community

Introduction

In the age of smart phones and online calendars I find I still like to also have a paper calendar at my desk. Having a single page per month gives a lot of space to jot down notes and quickly glance at upcoming appointments, etc. Over the years I have come across downloadable year calendars in PDF format that you can print out and staple together. But I wanted more control to add my own recurring “special” dates and holidays. I also wanted to be able to include colorful icons with my events.

My first concern was how to render the calendar. I wanted to keep it simple so I decided to use just plain HTML and tables with CSS to style them. This will also enable the use of the web browser for printing and PDF conversion without having to write anything myself. There were some things I learned along the way that I hope may help someone else. One in particular was the use of media queries to override the browsers default print themes. Another was discovering the FontAwesome library for icons.

Project Setup

For this project I decided to create a console application in C# and the .Net framework. I’ll be using Visual Studio 2017 Comminity Edition. The first step is to create a “C# console application”. I’m using .net framework version 4.7.


By default Visual Studio gives us a single class file for console applications called Program.cs with a single method called Main. I didn’t feel I really needed a GUI for this and wanted to keep it simple so chose the console application approach which means it will run on the command line. The program should take a single command line paramter for the year to generate. We’ll check that the input is a valid number and then also check that it’s a pratical year. Yes, I’m pretty optimistic putting in 2400 as an end year. :)

Read More

Build a recursive word finding algorithm with Python - Part 4

by Dan Duda

Requirements

  • Python 3.x
  • PyCharm Community (optional)
  • A text file of English words
  • Basic Python knowledge

Goto Part 3

Introduction

Is there a way we could store the list of words so that it’s more efficient to check if a string of letters is a valid word? The answer of course is yes. One way that I’m particularly fond of is to create a tree made up of nodes representing letters. The tree would start with all 26 letters and the child nodes of each letter node would form a path to valid words. Let’s look at a diagram to help explain it.


Here is a partial sample of what our tree might look like. From this we can have many optimizations. For example, say we’re checking the word part, ‘cb’. If we traverse our sample tree we first go to the ‘c’ node. We then check if the ‘c’ node has a child node of ‘b’. It does not so we can stop processing right there. If we had a bunch of remaining letters our current algorithm would keep on adding every combination of the letters onto ‘cb’ and checking against our word list. If we know ‘cb’ is not a valid path, then we know there can be no words starting with ‘cb’ so we can quit processing early.

Now since multiple words can start with the same combination of letters like “cat” and “cater” we need to know when a path is a valid word. To do this we can just add a flag to our nodes indicating if it’s a valid word. So, the first ‘c’ node under the root node would be set to false because there are words that start with ‘c’ but would not be a valid word itself. Same with the ‘a’ node under the ‘c’ node’. But when we get to the ‘b’ node we’d signify that it’s a valid word. The same with the ‘T’ node which would give us “cat”.

Read More

Build a recursive word finding algorithm with Python - Part 3

by Dan Duda

Requirements

  • Python 3.x
  • PyCharm Community (optional)
  • A text file of English words
  • Basic Python knowledge

Goto Part 2

Introduction

In the last tutorial we created a program that can find all the words that can be made from a string of letters. We also noticed that our program wasn’t very fast or efficient. Let’s fix that.

Before we try and optimize our code we should deternmine if that’s even needed. Our program did run very slow when using more than a few letters but to be sure we should add some timers to our code. We’ll also restructure our code into classes so that it’s easier to test.

Adding a class

Let’s add a new file to our project folder called “word_finder_simple.py”. We’ll pull our algorithm out of our main words.py and into this new file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class WordFinderSimple:
def __init__(self, word_file_path):
f = open(word_file_path, "r")
self.words = [l.rstrip() for l in f.readlines()]
f.close()

def find_words(self, letters):
found = []
self.search_words('', letters, found)
found.sort()
return found

def search_words(self, word_part, remaining_letters, found_words):
if len(word_part) >= 3 and word_part not in found_words and word_part in self.words:
found_words.append(word_part)

for i in range(len(remaining_letters)):
remaining_letters_copy = remaining_letters.copy()
next_letter = remaining_letters_copy[i]
del remaining_letters_copy[i]
self.search_words(word_part + next_letter, remaining_letters_copy, found_words)
Read More

Build a recursive word finding algorithm with Python - Part 2

by Dan Duda

Requirements

  • Python 3.x
  • PyCharm Community (optional)
  • A text file of English words
  • Basic Python knowledge

Goto Part 1

Introduction

In the last tutorial we setup our project to read in a list of words from a text file and then prompt the user for a word. We then print if the word was found in the list or not. In this tutorial we’ll create an algorithm to check all the combinations of letters that we enter to see what words, if any, we can make from them.

Recursive functions are functions that call themselves. In theory any recursive algorithm can be written non-recursively using just loops but depending on the scenario it can be much more complex to write. The downside of recursive functions is that they can take up a lot more memory depending on the scenario. Each recursive call to the function means that execution must leave the current function call by adding another call onto the stack in memory. Depending on how deep the recursion goes you could end up with a stack overflow error. A recursive function will have a base case and a recursive case. The base case causes the recursive function to end (stop calling itself) whereas the recursive case will call itself again. We can visualize recursion using a simple example of calculating factorials. A factorial is the product of all positive integers less than or equal to n, where n is a non-negative integer, and is denoted by n!. So for example:

1
5! = 5 X 4 X 3 X 2 X 1 = 120
Read More

Build a recursive word finding algorithm with Python - Part 1

by Dan Duda

Requirements

  • Python 3.x
  • PyCharm Community (optional)
  • A text file of English words
  • Basic Python knowledge

Introduction

I’ve always loved word games like PopCap’s Bookworm series and Microsoft’s Wordament. I’m also am a programmer and love to hack. So it’s only natural that I’d want to develop a “solver” for word games. A lot of programming concepts can be learned in creating this type of application such as recursion, tree data structures, and optimization. In the following set of tutorials we’ll build an algorithm to find English words based on a set of letters either scrambled like the game Scrabble or grid of letters like in the game Boggle. We’ll start off with a basic data structure and then learn how to better store our data to make searching lightning fast.

Getting a list of words

Before we beging writing any code we’ll need a list of words to use for our lookups. Any large list of words should work but here is one I found, Click here and then click on the “Download” button to download a copy of the words list. If clicking on the “Download” button just brings up the list of words in the browser then just right-click on the list and choose “Save as” and save the “words_alpha.txt” to your computer. This list contains around 479k English words which will really test our algorithm’s performance.

Read More