This repo has the solutions for two exercises that appear on a blog post I wrote on how to solve coding problems.
In the post, I show you the steps you should follow when attempting to solve coding problems.
The solutions come in two forms:
- Pseudocode for the algorithm that solves each problem
- Python code for each solution
If you are learning how to code, and how to solve coding problems, please try them out on your own first before looking at my suggested solutions. That way you learn better as opposed to copying a solution.
Note: The solutions suggested in this guide might not be the most optimal. Nor elegant. This guide just gives you a starting point to have a working piece of code. Then figure the rest on your own.
To run the python code locally in your machine, run each of the following commands in terminal:
-
Run the following command in terminal to clone the repo
git clone "https://github.com/JosephMainaDev/solving-coding-problems.git" -
Change to the directory
cd solving-coding-problems -
Run the Python files
python exercise-1.py -
Edit the files to include your own tests, and run them.
This is the first exercise on that post:
Write a program that receives an alphanumeric string and prints the sum of the numeric digits in the string.
And this an example of the how the program you come up with is expected to do:
>>> numericSum("h3l1o w07ld!")
>>> 11The algorithm should follow these steps to solve the problem. Generally, there are no hard rules on how to write pseudocode. You can do it my way, or a different way. The idea is to be able to figure out the steps in your head so that you have a blueprint to follow when writing out the code. You may have to rewrite the pseudocode several times over. (I do this best with a pen and a piece of paper.)
Iterate through the string
For each character in the string
Check the type of the character
If the character is a number
Increment the total with the number
Otherwise skip the character
Return the totalThe solution in Python code. You can rewrite the code in your favorite language.
def numericSum(string):
# Track the total
total = 0
for char in string:
# The characters in the string are also of type string.
# To tell whether a character is a number, try converting it to an integer.
try:
char = int(char)
# If it cannot be converted to an integer, it is not a number.
# Skip the character.
except TypeError:
pass
# If the character was successfully converted to an integer, add it to the total.
total += char
return totalHere is the second exercise:
Given a list of a pair of points on a Cartesian plane, write a program that returns true if the points are on a straight line, and false otherwise.
>>> checkStraightLine([(1,2), (2,3), (3,4), (4,5)])
>>> True
>>> checkStraightLine([(1,2), (2,3), (3,4), (4,8)])
>>> FalseBefore I give you the algorithm for solving this problem, let's first do a bit of math.
What do we know about points on a straight line? When I researched this problem, the most interesting way I found to solve it is:
- You need at least three points on the line
- If the three points form a triangle, the points are not on a straight line.
- To check whether a triangle is formed compute the area.
- A triangle has non-zero area
- A line has area equal to zero
Given a triangle formed by points
Source: https://www.cuemath.com/geometry/area-of-triangle-in-coordinate-geometry/
With that out of the way, my algorithm goes like this:
Iterate over the points on the line
Grab 3 points at a time
Compute area using the three points
If the area is non-zero
Return false, the three points formed a triangle
Otherwise it is a straight line
Return true
def checkStraightLine(points):
# Grab the first 2 points on the line
x1, y1 = points[0]
x2, y2 = points[1]
# And iterate from the 3rd point to the end of the line
for (point in points[2:]):
# Get a 3rd point
x3, y3 = points
# Compute the area formed by the three points
area = 0.5*(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))
# If area is non-zero, the points are not on a straight line
if(area != 0) return False
# The points are on a straight line
return True