‹ Kinyanjui Wangonya

Page 4


Test-Driven Development is great. If you’re a Pythonista, it gets even better with pytest - a framework that makes building simple and scalable tests easy.

In this series, I’ll be exploring the cool features that pytest offers to help make testing Python code a breeze.

Often when working with collections of data, you may want to find the smallest or largest item. It’s easy enough to write a function that iterates through the items and returns the smallest or largest one, or use the builtin min(), max(), or sorted() functions. Another interesting way may be implementing a heap (priority) queue.

Python provides a pretty convenient module called heapq that does that for you. heapq comes with a cool set of inbuilt functions that you can read more about in the docs.

I’m going to show Javascript implementations of three more sorting algorithms:

  • Quick sort
  • Heap sort
  • Counting sort

Again, this is not intended to be an in-depth explanation on the ins and outs of how the algorithms work and their performance. If you’d rather read about that, here’s a nice resource I found: Sorting Algorithms

I’ve been learning a lot about data structures and algorithms lately and I’ve noticed in my reading that there aren’t a lot of examples showing implementations of algorithms in Javascript. You’ll mostly find examples in Java, Python, C, C++ etc. Maybe there’s a reason for preferring these languages over Javascript? I’m not sure.

In this first part, I’m going to show Javascript implementations of three sorting algorithms:

  • Merge sort
  • Insertion sort
  • Bubble sort

This is not intended to be an in-depth explanation on the ins and outs of how the algorithms work and their performance. If you’d rather read about that, here’s a nice resource I found: Sorting Algorithms

Regular expressions are patterns used to match character combinations in strings. They help us work with strings in a very performant way.

By formulating regex with a special syntax, you can:

  • search text in a string
  • replace substrings in a string
  • extract information from a string

If all this is completely new to you, take a look at the mdn web docs for more info.

For this post, I’ll focus on one of the easier (and very useful) ways you can use regex.

If you’re using an array in your code, chances are, you’ll need to iterate over the values in the array. There’s a couple of ways you can do that, some better and more efficient than others depending on what you want to accomplish.

For this post, I’ll focus on two ways: some() and every().

Arrow functions were introduced in ES6 as a new syntax for writing Javascript functions. Thanks to their short syntax, they encourage the use of small functions, which make code look cleaner (and () => just looks cooler 😄).

This’ll be a quick one.

Say you want to check if a specific element exists in an array. There’s a couple of ways to do that:

« Older posts Newer posts »