Chapter 10 Simple Problems
In this section, we will show how to convert your ideas into code through a series of six examples. These examples follow the progression of less to more difficult.
10.1 Sum of Integers
Question: Write a Python program that computes the sum integers from 1 to 100 - i.e. (1 + 2 + …..+ 100).
10.1.1 Step 1
Even though the question asks for sum from 1 to 100, always simplify the problem to something much smaller. Let us see, whether we can let Python tell us the sum of interges from 1 to 5.
You can write the solution as -
sum = 1+2+3+4+5
print(sum)
However, that solution cannot be modified easily without typing all integers manually. In your code, you should always try to introduce some kind of recurring pattern that can be substituted with loops. For example, you can compute the sum in many steps as follows.
sum=0
sum=sum + 1
sum=sum + 2
sum=sum + 3
sum=sum + 4
sum=sum + 5
print(sum)
We will show the advantage in the following step.
10.1.2 Step 2
Now that you have a lot of repetitive code, you can replace it with a loop. Remember how we did the same in the previous chapter, when we wrote the multiplication tables.
sum = 0
for i in [1, 2, 3, 4, 5]:
sum = sum + i
print(sum)
10.1.3 Step 3
Now that you have a list of integers, you can replace it with range.
sum=0
for i in range(1,6):
sum = sum + i
print(sum)
So all that remains here is to replace that 6 with 101, and you get the sum of integers from 1 to 100.
10.2 Sum of Odd Numbers
Question: Write a Python program that computes the sum of odd numbers from 1 to 99 - (1 + 3 + 5 + …..+ 99).
10.2.1 Step 1
You can follow the same steps as previous example, and you will come with -
sum = 0
sum=sum + 1
sum=sum + 3
sum=sum + 5
print(sum)
10.2.2 Step 2
Once again, you can replace the repetitive code with a for loop. The numbers 1, 3, 5 go as the list indices.
sum = 0
for i in [1,3,5]:
sum = sum + i
print(sum)
10.2.3 Step 3
Now that you have a list of integers, you can replace it with range.
sum=0
for i in range(1,6,2):
sum = sum + i
print(sum)
So all that remains here is to replace that 6 with 100, and you get the solution of the given problem.
10.3 Sum of Squares
Question: Write a Python program that computes the sum of squares from 1 to 100 - (1^2 + 2^2 + …..+ 100^2).
10.3.1 Step 1
This time, you can simplify the problem into the following code -
sum = 0
sum=sum + 1*1
sum=sum + 2*2
sum=sum + 3*3
print(sum)
10.3.2 Step 2
Many students struggle at this stage, because they think they have create a loop with list indices of [1, 4, 9]
and then scale it up to “100^2.” There is an easier way. You can use the list [1,2,3]
and then change the code inside loop to i*i
.
sum = 0
for i in [1,2,3]:
sum = sum + i*i
print(sum)
10.3.3 Step 3
Now that you have a list of integers, you can replace it with range.
sum=0
for i in range(1,4):
sum = sum + i*i
print(sum)
Once again, replacing 4 with 101 gets you the sum of squares.
10.4 Finding Highest Number from a List
Question: Write a Python program that computes the highest number from any given list.
10.4.1 STEP 1
We will take a list of 3 numbers and write code explicitly to find the highest number. We will assume that the numbers are all positive. Therefore, we can start with an assignment of 0 for the variable highest
and then compare with all members of the list to continually update it.
numbers=[23, 4, 5]
highest=0
if(numbers[0]>highest):
highest=numbers[0]
if(numbers[1]>highest):
highest=numbers[1]
if(numbers[2]>highest):
highest=numbers[2]
print(highest)
10.4.2 STEP 2, 3
numbers=[23, 4, 5]
highest=0
for i in numbers:
if i>highest:
highest=i
print(highest)
You can now run the above code for lists of any size. Also note that you can relax the condition of all numbers being positive by assigning highest
to one of the numbers in the list instead of 0.
numbers=[23, 4, 5, 6,7, 12,2]
highest=numbers[0]
for i in range(len(numbers)):
if numbers[i]>highest:
highest=numbers[i]
print(highest)
10.5 Shortest Nucleotide Sequence
Question: From a list of nucleotide sequences, find the shortest one.
We will skip over the steps, because this question is similar to the previous one.
seq=["AAA", "T", "ATGTATG", ATGTA", "AA", "TGTGTTGGTGAGTC", "ATGC"]
shortest=seq[0]
for i in seq:
if len(i)>len(shortest):
shortest=i
print(shortest)
10.6 Find Position of the Shortest Sequence
Question: From a list of nucleotide sequences, find the shortest one along with its position.
This time, you will need to track two variables - shortest
and pos
. After every comparison with a list member, both of those variables need to be updated. Also, the loop needs to be go over the positions instead of the sequences themselves.
seq=["AAA", "T", "ATGTATG", ATGTA", "AA", "TGTGTTGGTGAGTC", "ATGC"]
shortest=seq[0]
pos=0
for i in range(len(seq)):
if len(seq[i])>len(shortest):
shortest=seq[i]
pos=i
print(pos)
10.7 Exercises
10.7.1 Sum of Powers of Three
Given a positive integer n, add 3^0+3^1+...3^n
. Is there an easy rule to get the sum?
10.7.2 Sum of Powers of Two
Given a positive integer n, add 2^0+2^1+...2^n
. Is there an easy rule to get the sum?
10.7.3 Factorial
Given N, print factorial N.