Assignment 1 (LAB)
Assignment 1 (LAB)
Instructions:
Grace Days: This assignment has 1 grace day associated with it.
Submission: Assignment is to be submitted on LMS in a .zip or .rar file. The folder should contain
a report in .pdf format showing the code output and the .py files for all the tasks.
Question 1:
Remove all the unnecessary tests from the nested conditional statement below.
Question 2:
Genetic algorithm is one of the algorithms that can be used for optimization problem. Two sub-
steps of this algorithm are:
1. We take two sub-portions of two equal sizes lists and create a third list using those sub-
portions.
2. We randomly change one of the value in the array that we got in step 1.
To further illustrate the above, consider the example below:
List 1:
1 2 3 4 5 6
Problem Based Learning Assignment
List 2:
11 12 13 14 15 16
Write a python program that takes input of two lists of same length n and performs the above two
steps. Both the output lists should be displayed at the end.
Hint: random value can be generated using a package in Python or can be taken as input from the
user.
Question 3:
Sorting of lists is required in many applications. There are several algorithms that can be used for
sorting offering different advantages and disadvantages. One of these algorithms is the selection
sort algorithm. Simply put, the selection sort algorithm searches for the smallest element in the
array and brings it to the first position by swapping. Now, we search for the second smallest
element in the array and bring it to second position in the array by swapping.
Write a Python program that takes a list of length n and sorts the list using selection sort. The
unsorted and sorted list should be displayed. DO NOT USE THE BUILT IN SORT FUNCTION.
Question 4:
You are given a String Code containing a message composed entirely of decimal digits (0~9). Each
digit consists of some number of dashes (see diagram below). You have to count the total number
of dashes in the whole number.
0- Consists of 6 dashes.
1- Consists of 2 dashes.
2- Consists of 5 dashes.
3- Consists of 5 dashes.
4- Consists of 4 dashes.
5- Consists of 5 dashes.
6- Consists of 6 dashes.
7- Consists of 3 dashes.
8- Consists of 7 dashes.
9- Consists of 6 dashes.
Examples:
13579 returns: 21 024268 returns: 28
Question 5:
As a crude form of hashing, Lars wants to sum the digits of a number. Then he wants to sum the
digits of the result and repeat until he has only one digit left. He learnt that this is called the digital
Problem Based Learning Assignment
root of a number. For example, if the number is 187 then adding the digits (1 + 8 + 7) would give
16. 16 still has two digits so the process would be repeated (1 + 6). This would give 7 which is a
single digit and hence the process is stopped.
Your job is to help Lars by writing a Python Program that takes a number as input from the user
and returns the digital root of that number.