Posts

Showing posts from February, 2019

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...