Solution to Tutorial 1: exercise 3, 5, 7

Here are the source code of the solutions:  ex3.py   ex5.py   ex7.py

3)   Convert each character in the input string into its corresponding value then sum them up.
I use two constant strings to store the character and its corresponding value.

str = "ABCDEFGH"
val = "12345678"

#  for c in str  is used to extract each character

inp = input().strip()
sum = 0

#  sum is used to accumulate the value

for c in inp:

#  then get the index of c and use that index
#  to get its value (convert to integer)

    i = str.find(c)
    v = int(val[i])
    sum += v

print(sum)


This is the common trick used since early days of computer programming.  String can be used to store information.  The machine code level also has instructions to support a fast string search.

5)  nested if.   Find a maximum number from three input numbers.  By comparing two numbers at a time, the decision process is formed as a decision tree.  This tree can be programmed by nested if.  First we draw a decision tree,  1:2  means the first number is compare with the second number, etc.  The end of the tree is the answer.

#             1:2
#           /      \
#      1>2 /        \
#         1:3        2:3
#         / \        / \
#    1>3 /   \   2>3/   \
#        1   3     2     3

Use input().strip().split()  to get three numbers.  The result is a list.

inp = input().strip().split()

# convert them to integers

x = int(inp[0])
y = int(inp[1])
z = int(inp[2])

# start decision tree
# Be careful to line up the indentation properly
# the correctness of program depends on it.

if x > y :
    if x > z:
        print(x)
    else:
        print(z)
else:
    if y > z:
        print(y)
    else:
        print(z)

Test to see if this version works properly.
Finally we wrap all up in the loop of getting input from a file.

infile = open("input5.txt","r")

for line in infile:

    inp = line.strip().split()
   
# convert them to integers

    x = int(inp[0])
    y = int(inp[1])
    z = int(inp[2])

# start decision tree

    if x > y :
        if x > z:
            print(x)
        else:
            print(z)
    else:
        if y > z:
            print(y)
        else:
            print(z)

infile.close()


7)   Match all pairs from two inputs.

Use two nested for loop. Start with a version of just two inputs.  x, y are the list of strings.

x = input().strip().split()
y = input().strip().split()

print(x)
print(y)

for i in x:
    xi = int(i)

# We build up the output string  out

    out = ""
    for j in y:
       yi = int(j)
#      print(xi,yi)
       out = out + i + " " + j + " "
       print(out)

In this case we do not actually need xi, yi  because we just want to print the number out  we can use its string directly.

When the fist version is correct, we wrap all up in a loop to read input file until the end of file. We cannot use for-loop because each time in a loop, it can get only one input line.  So, we use while instead. We need to find a way to check when the end of file is reached.  We did  that by observing that the length of input is zero.

infile = open("input7.txt","r")

while True:

# use readline() to read one line at a time

    x = infile.readline().strip().split()
    y = infile.readline().strip().split()

# we stop when there is no input anymore
    if len(x) == 0:
        break

    for i in x:
        xi = int(i)
        out = ""
        for j in y:
            yi = int(j)
            out = out + i + " " + j + " "
        print(out)

infile.close()

Match up all pairs from two sets with two nested loop is very common task. You will see a lot in matrix calculation.


last update 9 Feb 2016