Reference no: EM133059356
Learning Activity: Programming Exercises
Exercise 1. Find a particular number in a random list (return index)
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.
Following the previous learning activity, you will now implement something slightly more advanced. Implement a linear search algorithm, but this time, if you find the number in the list, return the index of that number, otherwise, return -1.
Once again, your program will first start with the following two lines of code: import random
l = random.choices(range(100), k = 50)
For example, this sequence could be 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]
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 output the index of that number, otherwise output -1.
For example, if user type in 50, you should output 5. 5 is the index of number 50 in the list. If user types in 100, you should output -1 as you cannot find 100 in the list.
Sample input/output:
>>> Enter a number:
>>> 100
>>> -1
Sample input/output:
>>> Enter a number:
>>> 56
>>> 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 Exercise - Medium Difficulty 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. Summation
Write a Python program that computes the summation from 1 to a given number. That is, for a given number n, calculate:
∑i=1n
i = 1 + 2 + 3 + ....+ (n-1) + n
Your program should first ask the user to type in a positive integer, your program will then calculate the summation from 1 to that number (inclusive). For example, if the user types in 6, your program should output 21. If the user types in 100, your program should output 5050. You cannot use the sum() function in this exercise.
Sample input/output:
>>> Enter a positive number:
>>> 1000
>>> 500500
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 Exercise - Medium Difficulty 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. Factorial
Write a Python program that computes the product from 1 to a given number. That is, for a given number n, calculate:
∏ni=1 i = 1 x 2 x 3 x .... x (n-1) x n
Your program should first ask the user to type in a positive integer, your program will then calculate the product from 1 to that number (inclusive), that is the factorial of that number. For example, if the user types in 6, your program should output 720. If the user types in 20, your program should output 2432902008176640000.
Sample input/output:
>>> Enter a positive number:
>>> 5
>>> 120
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 Exercise - Medium Difficulty 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. Square numbers
Write a Python program that allows the user to type in a number, then outputs all square numbers that are smaller or equal to that number starting from 1, separated by comma. For example, if the user type in number 20, your program then should output 1, 4, 9, 16.
Sample input/output:
>>> Enter a positive number:
>>> 100
>>> 1, 4, 9, 16, 25, 36, 49, 64, 81, 100