Skip to main content

Posts

Showing posts from October, 2017

How NOT To Write Code / How To Decipher Bad Code

Confusing code is bad code. In this post I'll be talking about one of the worst coding practices: using nondescript variables. As an example, we'll be looking at a simple problem from Project Euler (#28). I'll give you an example using really bad code and then we'll look at how we can decipher it and make it more clear and understandable. First, here's the problem: Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: 21  22 23 24  25 20   7   8   9  10 19  6   1   2 11 18   5   4   3  12 17  16 15 14  13 It can be verified that the sum of the numbers on the diagonals is 101. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? Ok, so now check out this solution:             int i = 1; int j = 1; int s = 0; ...

Project Euler Problem # 25 1000-digit Fibonacci number

Spoiler Alert: As the About page of Project Euler says, "Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery." If you are currently attempting to solve this problem in the archives, please do not continue to read this post. I personally worked through all the problems I post on this blog and it has brought me immense satisfaction. Problem: The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term, F12, is the first term to contain three digits. What is the index of the first term in the Fibonacci sequence to contain 1000 digits? Solution: Using the BigInteger class, this problem can be solved very simply using C#:             //Time the process         ...

Project Euler Problem #19 Counting Sundays

Spoiler Alert: As the About page of Project Euler says, "Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery." If you are currently attempting to solve this problem in the archives, please do not continue to read this post. I personally worked through all the problems I post on this blog and it has brought me immense satisfaction.  Problem: You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? Solution:  Although this problem sounds a little ...

Project Euler Problem #1 Multiples of 3 and 5

Spoiler Alert: As the About page of Project Euler says, "Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery." If you are currently attempting to solve this problem in the archives, please do not continue to read this post. I personally worked through all the problems I post on this blog and it has brought me immense satisfaction.  Problem:  If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. Solution:  This problem can be solved brute-force without too much difficulty by simply doing a for loop from 1 to 999, incrementing by one each time, checking each number to see if it is divisible by either 3 or 5, and if so, adding it to a list. Once the for loop terminates, we can sum all the numbers in the list.  In C#, you do something like this:  ...

Get An Instant Statistical Summary Of Any Selected Range in Excel

The past few weeks I've been leading an Excel VBA boot camp for the company where I work. In our last session, I showed how easy it is to create subroutines that can be available to you in any workbook you open. In this post, I'll show you how to create a macro that will allow you to select a range in any workbook, hit Ctrl + Shift + S, and get an instant statistical summary of the data plus a line graph to visualize the data. Let's get started: First, you'll want to open a new workbook in Excel. If the Developer tab is not available in your ribbon, you will want to add it. On the Developer tab, click "Record Macro". Image 1. Select "Record Macro" After you that you will be presented with an options screen. You need to give your macro a name. Here, I have chosen to call mine "Statistical_Summary". Then, set the shortcut key you want to use to access your macro in any workbook you open. I chose Ctrl + Shift + S, but you ca...

Book Review: Learn WPF MVVM - XAML, C# and the MVVM pattern by Arnaud Weil

Last week I was looking for inexpensive ways to learn about applying the MVVM pattern to WPF programming (I still am; please leave any tips in the comments section) and ran across Arnaud Weil's $10 digitial book on Amazon. It is brief, less than two hundred pages, but obviously economical and I was impressed with how interactive it was. Pros: The book teaches the user how to design a XAML page, how to use data binding, and how to set up the Model-View-View Model structure to conform to commonly accepted standards. The feature that impressed me the most was that Weil has provided a number of interactive exercises to teach you how to create a WPF-MVVM application. After each block of material, there are "Now it's your turn to code:" sections in which you are expected to create a XAML control, write a method, bind a control to a data object, etc. Afterward there is a "Solution" section in which Weil leads you step-by-step through the assignment, in ca...

How I Got Into Programming

Working with computers runs in my family. My dad is a Java programmer. My uncle does .NET. I have an aunt and uncle who used to own a computer store. I have a cousin who does IT work for the college campus back home.  But I never thought the same blood ran through my veins.   I hated my MATLAB programming class in college; I actually had to take it twice. Nothing seemed to click. The professor, Jaqueline Huntley would respond to questions with, "Just think like a computer." That never seemed to do me any good.  I've always had a penchant for learning human languages. I loved learning Koine Greek from one of my favorite teachers, Bob Waldron, in high school, and Spanish came to me quickly as I worked in construction during high school summers and I continued studying both languages in college, attaining fluency in Spanish and feeling comfortable enough with Greek.  But computer languages seemed like an entirely different ballgame. I used to tell my dad, "I...

One little sentence...

For hours I had been struggling with a connection to a SQL Server database on our network... I had tried different connection strings , we had enabled TCP/IP and Named Pipes protocols on the server, I had "Allow remote connections" enabled, and nothing seemed to be working. Finally I read this:  "If the target SQL instance is a named instance, you also need to make sure SQL Browser is running on the target machine." Doing that instantly allowed my application to connect. Thank you, Microsoft .

Online Relational Database Service Offers

This brief post will present a 30,000 foot view of the two behemoth online relational database service providers, Microsoft and Amazon. At the end, I'll give you my favorite pick.  Microsoft Azure SQL Database  Right now you can get a free Azure account with $200 in credits to be used toward any Azure product during your first 30 days. After the first month, you can continue to use free Azure products, provided you upgrade your account and remove the spending limit. With a free account, you currently get 12 months of free access to SQL Database . According to the Microsoft rep I spoke with, this includes " 250 GB of SQL Database standard S0 instance with 10 database transaction units ". Microsoft bases SQL DB package pricing ("Service Tiers")  on Elastic Database Transaction Units (eDTUs). They've provided a handy calculator to help you figure out how much you might need to shuck out.   Amazon Relational Database Service (RDS)  Amazon RDS appea...