0% found this document useful (0 votes)
10 views7 pages

2 - Exercises - Data Structures and Iteration

The document outlines programming exercises focused on data structures and iteration in Python, including manipulating lists and strings, creating a bug collector program, calculating factorials, predicting species population growth, and generating patterns using loops. It emphasizes the importance of understanding concepts through practical coding tasks and encourages the use of pseudocode and flowcharts for program design. The exercises aim to reinforce programming principles covered in lectures.

Uploaded by

umapathyashwin23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

2 - Exercises - Data Structures and Iteration

The document outlines programming exercises focused on data structures and iteration in Python, including manipulating lists and strings, creating a bug collector program, calculating factorials, predicting species population growth, and generating patterns using loops. It emphasizes the importance of understanding concepts through practical coding tasks and encourages the use of pseudocode and flowcharts for program design. The exercises aim to reinforce programming principles covered in lectures.

Uploaded by

umapathyashwin23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSP1150

Programming Principles
Week 3: Data Structures and Iteration
Question 1 – Concept Revision in the Python Shell
Let’s revise the concepts from this module by typing some simple statements into the Python Shell

Feel free to deviate from the examples and experiment!


1. nums = [1, 2, 4, 15]

Using the information in Lecture 3 and the Python documentation for data structures and built-in functions, write
statements that manipulate the nums list in the following ways:
(remember, you can check the content of a variable simply by typing its name)
• a) Change the last item in nums from 15 to 16.
• b) Insert a new item with a value of 8 at index 3 (i.e. between the 4 and the 16).
• c) Append a new item with a value of 32 to the end of nums.
• d) Determine the sum of all of the items in nums.
• e) Reverse the order of the items in nums.

Which of the previous tasks could be performed on nums if it was a tuple?


(remember, tuples are immutable)
2. text = 'concatenation'

Using the information in the “Referring to Items” slides of Lecture 3, write statements that refer to parts of the
text variable in the following ways:
(remember, indexes start at 0 – the first letter of a string has an index of 0)
• a) The fifth letter of text (“a”)
• b) The third last letter of text (“i”)
• c) The first three letters of text (“con”)
• d) The fourth, fifth and sixth letters of text (“cat”)
• e) The last six letters of text (“nation”)
Question 1 – Concept Revision in the Python Shell

3. range(1, 10)
While a range is like a list, it is not the same. Creating a range does not simply create a list
of the appropriate numbers…

4. list(range(1, 10))
…However, you can convert a range to a list if you want a list of numbers that are easy to
generate using a range.

5. evenNumsBelow25 = list(range(2, 25, 2))

This creates a list containing [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24].
Question 2 – Bug Collector Program.
Create a new file in the Python Editor and write the code to implement the following
program. While this program does not achieve anything particularly useful, it practices
your usage of a number of the concepts we’ve covered so far.
All of the information and examples needed are available in lectures and the Python
documentation. Before writing any code for the program described below, think about
how you will approach it and write pseudocode and draw a flowchart of your program
design.

A bug collector collects bugs every day for five days. Write a program that keeps a a
record of the number of bugs collected during the five days. The loop should ask for the
number of bugs collected for each day, and when the loop is finished, the program should
display the total number of bugs collected, the maximum number, and the minimum
number of bugs collected during the course of five days.
Task 3 – Factorial of a number
In mathematics, the notation n! represents the factorial of the nonnegative
integer n. The factorial of n is the product of all the nonnegative integers
from 1 to n. For example,
7! = 1 × 2 × 3 × 4 × 5 × 6 × 7 = 5,040
and
4! = 1 × 2 × 3 × 4 = 24
Write a program that lets the user enter a nonnegative integer then uses a
loop to calculate the factorial of that number. Display the factorial. The
program should also display an error message if a negative number is
entered as an input.
Task 4 – Species Population Calculator
Write a program that predicts the approximate size of a population of organisms. The
application should allow the user to enter the starting number of organisms, the average
daily population increase (as a percentage), and the number of days the organisms will be
left to multiply. For example, assume the user enters the following values:
– Starting number of organisms: 2
– Average daily increase: 30%
– Number of days to multiply: 10
Display the population of these species of each day in the following format;
– Approximate population at the end of day 1 = 2
– Approximate population at the end of day 2 = 2.6
– ….
Task 5 – Pattern generator
a) Write a program that uses nested loops to draw this pattern:
*******
******
*****
****
***
**
*
b) Write a program that uses nested loops to draw this pattern:
##
##
# #
# #
# #
# #
c) Write a program to prompt the user to enter the number of layers of a pyramid made using ‘*’ symbols. For
example, a pyramid with 3 layers looks like
*
***
*****

You might also like