Reference no: EM133059353
Learning Activity 1 - Programming Exercises
Exercise 1. Print numbers in reverse order
Write a Python program that outputs all integers from a given number down to 1, separated by comma. The program will first ask the user to input a positive integer, it will then output all the numbers from that number (inclusive) to 1. For example, if the user types in 10, your program should output 10,9,8,7,6,5,4,3,2,1. If the user types in 5, your program should output 5,4,3,2,1.
Sample input/output:
>>> Enter a positive integer:
>>> 20
>>> 20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1
This learning activity asks you to write a program.
Please upload your code as a Python code file (.py file) to Module 3.1 Programming Exercises- Easy Discussion Forum and do not copy and paste your code in the discussion forum text editor as the indentation may be lost. Indentations in Python indicate code blocks and scope and is part of the code.
Please check other students' code and comment on why you agree, disagree or can provide help with another students' post. Please reply to at least two other students' posts.
Exercise 2. Even numbers
Write a Python program that outputs all the even numbers from 2 to a number (inclusive) provided by the user, separated by comma. The program will first ask the user to input an integer that is greater than 2, it will then output all the even numbers from 2 to that number. For example, if the user types in 10, your program should output 2,4,6,8,10. If the user types in 13, your program should output 2,4,6,8,10,12.
Sample input/output:
>>> Enter an integer greater than 2:
>>> 11
>>> 2,4,6,8,10
This learning activity asks you to write a program.
Please upload your code as a Python code file (.py file) to Module 3.1 Programming Exercises- Easy Discussion Forum and do not copy and paste your code in the discussion forum text editor as the indentation may be lost. Indentations in Python indicate code blocks and scope and is part of the code.
Please check other students' code and comment on why you agree, disagree or can provide help with another students' post. Please reply to at least two other students' posts.
Exercise 3. Find a particular number in a random list
NOTE: this learning activity requires Python 3.6 or higher. In Module 1.1, you were asked to install any Python version higher than 3.6. If you use a version lower than Python 3.6, some of the code will not work.
In this learning activity, we will implement an algorithm that we discussed in Module 2.2. We asked you to draw a flowchart and write some pseudocode for a linear search algorithm, that is, to find a particular value, key, in an arbitrary list.
Your program will first start with the following two lines of code:
import random
l = random.choices(range(100), 50)
These two lines of code create a list, l, which contains 50 integers randomly samples from range [0, 100). This range includes the left-hand number 0, but does not include the right-hand side number 100.
You now need to ask the user the type in a number, then perform a linear search over the random list l. If you can find the number from the list, then print "Yes, I found it!", otherwise, print "No, it does not exist in the list".
Sample input/output:
>>> Enter a number:
>>> 100
>>> No, it does not exist in the list
This learning activity asks you to write a program.
Please upload your code as a Python code file (.py file) to Module 3.1 Programming Exercises- Easy Discussion Forum and do not copy and paste your code in the discussion forum text editor as the indentation may be lost. Indentations in Python indicate code blocks and scope and is part of the code.
Please check other students' code and comment on why you agree, disagree or can provide help with another students' post. Please reply to at least two other students' posts.
Exercise 4. Find a particular number in a random list
NOTE: this learning activity requires Python 3.6 or higher. In Module 1.1, you were asked to install any Python version higher than 3.6. If you use a version lower than Python 3.6, some of the code will not work.
In this learning activity, we will implement an algorithm that we discussed in Module 2.2. We asked you to draw a flowchart and write some pseudocode for a linear search algorithm, that is, to find a particular value, key, in an arbitrary list.
Your program will first start with the following two lines of code:
import random
l = random.choices(range(100), k = 50)
These two lines of code create a list, l, which contains 50 integers randomly samples from range [0, 100). This range includes the left-hand number 0, but does not include the right-hand side number 100. For example, this can be a list generated by these two lines of code:
[16, 56, 85, 8, 98, 50, 71, 81, 56, 12, 75, 24, 10, 60, 38, 25, 7, 98, 89, 3, 89, 27, 69, 46, 14, 15, 23, 15, 22, 47, 8,
81, 24, 94, 97, 40, 69, 2, 29, 59, 44, 32, 69, 37, 53, 91, 46, 14, 72, 94]
Every time you run these two lines of code you are likely to get a different list of numbers.
You now need to ask the user the type in a number, then perform a linear search over the random list l. If you
can find the number from the list, then print "Yes, I found it!", otherwise, print "No, it does not exist in the list".
Sample input/output:
>>> Enter a number:
>>> 100
>>> No, it does not exist in the list