Posts

Showing posts from January, 2020

[DSA in Python] sum_of_squares.py

This was the fourth exercise of Chapter 1 (Python Primer) in the book 'Data Structures and Algorithms in Python' by Goodrich et al.

Problem: "Write a short Python function that takes a positive integer n and returns the sum of the squares of all the positive integers smaller than n."

Solution:
The solution is very straightforward. We want to take all the numbers that are less than n and then square each of the numbers. We then take these squared numbers and add them all up.
We can also use list comprehension and Python's built-in function sum to make the code shorter:

Remarks:
  • Comprehension provides a short way of constructing sequences such as lists, sets, dictionaries, and so on. 
    • Let's say we want to create a list consisting of numbers up to, but not including, n
    • Let n = 5, then we can create a list like so: m = [1, 2, 3, 4]
      • We set variable m to hold a list
    • However, we can also write it like so: m = [i for i in range(n)], where n = 5
      • As we can see, we can easily change n to be something else besides 5. 
  • You can read more about comprehensions here.

New Years Resolutions

Here's to a new year! Cheers!

My 2020 resolutions:
  1. Learn ReactJS and build many cool apps with it
  2. Improve Web Design skills
  3. Improve sleep hygiene
  4. Improve time management
  5. Get fit
  6. Study hard, play hard
  7. Get an internship or a full-time job in web dev
  8. Improve general problem-solving skills (leetcode, hackathons, etc)
  9. Have no regrets, be more confident, be more decisive
  10. Always believe in the iterative process