Posts

Python - Getting Started

After spending over 25 years in tech industry, I realized that something is wrong with me. I am not coding anymore. And perhaps I have forgotten how to program. So I have taken the 2020 goal of getting myself back to programming. The first question I had for myself is how to get started? The answer is simple. Just program! I will start writing a series of programs using Python. I don't have any past experience with the language. And my programming is rusty. We will see where we go from here. Cheers

Python - Week 10 - Math Problems

Few more Math problems solved using Python Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. So for example sum of squares of first 100 natural numbers from 1 to 100 is 25502500. And square of sum of first 100 natural numbers from 1 to 100 is 338350. Here is the code: Again, a simple solution to a simple problem. I have separated the two functions for modularity Line 4 shows how comprehensions can be used to solve these kind of problems. Line 6 is the way to sum over iterables. Here sum just takes in an iterable (could have been a dictionary or a map as well) and does the magic. No need to write additional code here. An object is iterable if it is either a physically stored sequence in memory, or an object that generates one item at a time in the context of an iteration operation. List objects are considered iterable because they support the iteration protocol—they respond to the iter call with an ...

Python - Week 9 - Simple Math problems

This week I tackled 3 math problems. Not much of Python here, but I wanted to check out how Math problems are handled. In the end, it all depends on the algorithm deployed and the libraries/constructs available. Here is the first 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. This is not a complicated problem and the solution itself is pretty straightforward. The only Pythonic construct used here is the else for the loop which is executed only if the for loop runs successfully (which it anyway does here). The more interesting use of this else statement would have been the cases where we may break the loop. Now lets look at the second problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ......

Python - Week 8 - Dictionaries, Tuples, Sorting and Filtering

I spent last week understanding Python's sorting with Tuples and Dictionaries. Here is the first snippet that demonstrates how tuples are sorted. You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and score are numbers. The tuples are input by console. The sort criteria is: 1: Sort based on name; 2: Then sort based on age; 3: Then sort by score. The priority is that name > age > score. If the following tuples are given as input to the program: Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85 Then, the output of the program should be: [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')] Here is the code: The code looks pretty concise, as if not much has been accomplished. But there are few points worth me...

Python - Week 7 - Strings, Regular Expressions, Assignment Operator, None

Lets explore Python constructs related to strings. Here is the first problem: A website requires the users to input email address and password to register. Write a program to check the validity of password input by users. Following are the criteria for checking the password: 1. At least 1 letter between [a-z] 2. At least 1 number between [0-9] 3. At least 1 letter between [A-Z] 4. At least 1 character from [$#@] 5. Minimum length of transaction password: 6 6. Maximum length of transaction password: 12 Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma. Example If the following passwords are given as input to the program: ABd1234@1,a F1#,2w3E*,2We3345 Then, the output of the program should be: ABd1234@1 Notes on the implementation: None of the string methods accepts patterns—for pattern-based text processing, ...

Python - Week 6 - String join, List append, else with for loop and Fractions module

This week I worked on 2 problems. Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number. The numbers obtained should be printed in a comma-separated sequence on a single line. Pretty straight forward solution but it highlights use of a couple of new Python constructs. First the code: Now some notes: Lists are positionally ordered collections of arbitrarily typed objects, and they have no fixed size. The range statements I have used in the past have defaulted to 0 as the lower bound. Here the lower bound is specified as 1000. Line 11 shows a simple way to iterate over characters in string. You do not need to open up the string. The for loop does it automagically. Notice the else statement in Line 16. Only if the loop (which started in Line 11) succeeds without a break, this else is executed. Python provides this as a construct to eliminate use of flags. allevens is declared as an emp...

Python - Week 5 - Comprehensions

As I settle in new job in a new country, starting February, I am making additional efforts to learn Python. So I will tackle a problem this week, with multiple solutions, each more Pythonic than the previous one. Here is the problem statement: Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed. Example: 0100,0011,1010,1001 Then the output should be: 1010 I deviated from the problem statement (not a good thing!). But I wanted to learn new tricks with Python. So I will excuse myself here. The way I approached the problem was to take a number (num) and find all binaries starting from 0 until num. Now for each of the binary, I checked if the equivalent decimal is divisible by 5. Some interesting points to note: Line 7 shows a way to convert a number to its binary string. Here b stands for binary and it uses forma...