Implementing nested loops with dynamic step in Python3

Introduction

The other day, my son asked me about the result of evaluation of program in Java that he had in his AP Computer Science A (APCSA) course. This course is as a part of his high school senior curriculum. The problem was quite simple of nested for loops in the control flow section. The code is as below.

    a = 0;
        for (i = 0; i < 10; i++) {
            for (k = 0; k <= 5; k++) {
                for (z = 1; z <= 16; z = z * 2) {
                    a++;
                }
            }
       }
    print a

The problem

As you can see, the problem is simple enough. I had done this kind of for loop in shell scripting and in C language when I was learning it a long time ago and somehow I thought that this was the same way you run a for loop in Python as well. Of course I know Python quite well, so I thought I would quickly port this code in Python and run it to verify the answer of the problem (My son had gotten it right by the way and through a very simple mechanism, I must add).

The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object. However, Python doesn't allow the developer to define both the iteration step and halting condition. Instead Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

The syntax for a for loop in Python is very simple and intuitive like so:

    for i in some_list:
        <do something>

Most common use case is to iterate some operation a certain number of times. And the easiest way to do it is to use the range function. In the range function, the structure contains the start point, end point and the step. The step takes an integer as an input and it does not accept an expression as a step. Similarly as Python doesn't allow the developer to define the iteration step in the for loop, it posed a problem for me as I the program above required to have the step in the powers of 2 for each loop.

The research

I tried various combinations and options to change the step size in range function, but as the step is a positional argument and doesn't accept either keyword argument or an expression, the choices were limited to only integers (Range also accepts floats but it for another day). I searched far and wide for a solution but to no avail. Finally I hit on the a possible solution on (where else) Stack Overflow.

The person who asked the question had a similar goal as me. The answer to the question was kind of cryptic, but I was able to figure it out as the answer pointed me in the right direction.

The solution

The solution in the end was embarrassingly simple. Here's the final code for this: There are certain commented statements here which I have left here that I used to debug and understand what was going on.

    #########################################
    # Implement nested for loops in Python  #
    # Programmer: Mukul Dharwadkar          #
    # Date: 24 September 2021               #
    #########################################

    a = 0
    for i in range(10):
        for j in range(6):
            c=1
            for k in range(1, 17, c):
                while c < 17:
                    # print(f'Index c is {c}')
                    c = c * 2
                    #print(f"Index i is {i}")
                    #print(f"Index j is {j}")
                    a += 1
                #print(f"The value of a when is i, j, k and c is {i}, {j}, {k}, {c} is {a}")
    print(f'The value of a is {a}') 

As you can see, I needed to initialize a new counter c that would then be used as the step size. In the innermost for loop, I needed to create a stop condition as the counter was completely independent of all other variables. The while loop above creates that stop condition. Inside the while loop, I am incrementing the counter by the powers of two.

The actual operation of interest is the value of a which is incremented by one everytime the loop is executed. So essentially, the easy way once you figure out how many times each loop is executing is to multiple all those values (5 times 6 times 10 in this case) and arrive at the final answer of 300.

    The value of a is 300

Updating a DynamoDB attribute with a hyphen or dash (-) in the name using CLI or SDK

Background

As a part of my personal growth plan and work commitments, I am working on the AWS Certified Developer - Associate certification using the Linux Academy platform. In one of the lab exercises that I was doing on DynamoDB, there were requirements for updating DynamoDB attribute using SDK and perform conditional updates and atomic counters on the tables. Being what I am, I did not use the examples they had provided, but created by own table to create a database of books I own and proceeded to create my own attribute names for the items.

The problem

As it happened, I created attributes like book-title, book-author, book-price, etc. which in itself is not a problem. However, the lab exercise had me perform the item updates using the BOTO3 Python SDK which got me excited to learn new things. I used the example files that the trainer had provided and modified it to suit my environment and ran the script.

UpdateExpression='SET book-price = :val',
ExpressionAttributeValues={
    ':val': {'N': '15.37'},  
    ':currval': {'N': '0'} 
},
ConditionExpression='book-price = :currval',
ReturnValues="ALL_NEW"

To my dismay, I started encountering errors.

Traceback (most recent call last):
  File "conditional_write.py", line 18, in 
    ReturnValues="ALL_NEW"
  File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "-", near: "book-price"

The Solution

I reviewed my code to ensure that I had not introduced any bugs myself. After ensuring that I had not introduced any bugs by adding new attributes to an item without any dashes and running the script successfully, I starting practicing my Google-Fu. There I found this awesome post on stackoverflow along with a link to official AWS documentation. The official documentation however only talks about a dot being a special character and it doesn't list a dash (-). After following the instructions from the stackoverflow post, my new code looked like this:

UpdateExpression='SET #bp = :val',
ExpressionAttributeValues={
    ':val': {'N': '15.37'},  # Make sure we keep this line the same
    ':currval': {'N': '0'}  # What was the current value?
},
ExpressionAttributeNames={
    "#bp": "book-price"
    },
ConditionExpression='#bp = :currval',
ReturnValues="ALL_NEW"

And once I implemented this code it all started working correctly. I have left a feedback for the AWS documentation team and hopefully they will update the documentation. I just want to make sure that all the cases are at listed and documented so that developers and wannabes like me are not stuck.

Rock, Paper, Scissors… in Python

For a long, long time, I have been an enthusiast of Python programming language. I am embarrassed to say that I have been trying to learn it fully for the past 10 or so years. In this much time with proper focus and dedication, I would have been a guru. But here I am, still a novice. But that's beside the point.

Now that summer vacation is here, I have decided to teach my kids Python programming as much as I know. I want to show them the path and take them with me as far as I can go and then I hope they will go further from there. While Some time ago (maybe a couple of years ago) I had given myself a challenge to create a program that I had no source or coaching. So I had decided to create a game in Python. I chose the simple game of Rock, Paper, Scissors.  Over time, I changed computers and somehow I lost the source code of the program.

When my daughter started learning, she reminded me of the program and wanted to play the game. So I got started to write the program again. I'm happy to report that I have successfully re-written program. I'm sure there is a lot of scope of improvement in the coding style, code, and overall structure. But I am just starting and hope to improve. I'm posting the source code of my program and I will welcome all constructive criticism of the code and suggestions for improvement.

# An all-time favorite game of Rock Paper Scissors.
# An all-time favorite game of Rock Paper Scissors.
# Programmer: Mukul Dharwadkar
# Date: June 27 2017

import random

def instructions():
"""Displays the game instructions"""
print \ """ Today we will play the perennial favorite game of... Rock! Paper!! Scissors!!!.
The objective of the game is to outthink your opponent (in this case me) and defeat.
The rules are very simple
1.Paper covers the Rock
2.Rock breaks the Scissors
3.Scissors cut the Paper
Choose your move from the following:
1. Paper (p)
2. Rock (r)
3. Scissors (s)
Are you ready? Alright then, let's play... """

def get_name():
"""Get player's name"""
print \
"""First of all, let's get to know each other a little better.
My name is Compy...
What's yours?
"""
player_name = raw_input("What is your name: ")
return player_name

def greet_player(name):
"""Let's be polite and greet each other properly"""
print "How are are you doing %s?" % name

def legal_moves():
"""Define the legal moves"""
legal_moves = ("r", "p", "s")
return legal_moves

def player_move():
"""Players choose their move"""
move = None
while move not in moves:
move = raw_input("What is your move %s? --> " % name)
return move

def computer_move():
"""The computer will choose its move in this function"""
move = random.choice(moves)
print "Computer's move is %s" % move
return move

def compare_moves(p_move, c_move):
"""We will now compare the moves the human and computer make and then take the output to declare the winner"""
#This is a very crude way of writing this comparison code. Is there a better way to do this?
#TODO: Find out optimized way of writing this block of code.
if p_move == "r" and c_move == "p":
return "computer"
elif p_move == "r" and c_move == "s":
return "human"
elif p_move == "p" and c_move == "s":
return "computer"
elif p_move == "p" and c_move == "r":
return "human"
elif p_move == "s" and c_move == "r":
return "computer"
elif p_move == "s" and c_move == "p":
return "human"

def declare_winner(winner):

if winner == "human":
print "%s wins. Congratulations and well played!!!" % name
elif winner == "computer":
print "Computer wins. Better luck next time %s." % name
else: print "It's a tie"

#main body of the program
instructions()
name = get_name()
moves = legal_moves()
greet_player(name)
p_move = player_move()
c_move = computer_move()
winner = compare_moves(p_move, c_move)
declare_winner(winner)

Note: The indentation of the code is lost.
To do: Create a GUI interface for the program if I can.