workshop 1 solutions PDF

Title workshop 1 solutions
Author Ho Koh
Course Coding Technologies
Institution Edith Cowan University
Pages 3
File Size 263.4 KB
File Type PDF
Total Downloads 106
Total Views 131

Summary

solutions...


Description

CSG5190: Coding Technologies Workshop 1: Solutions Important Note I do not consider these solutions to be “absolute” in any way. There are usually multiple ways to solve any problem, and these solutions attempt to present (and contextualise) a good solution. That does not necessarily mean the “best” solution, but it is the solution that I feel is most appropriate for the task asked and the context in which the task was presented. If your solution to a workshop task differs from the provided solution, compare the two and try to understand the meaning and significance of the differences. Comparing different solutions to a problem is a great way to improve your skill. Contact your tutor if you wish to discuss workshop solutions, or post on the Blackboard discussion boards to see what other students have to say.

Task 1 – Computational Thinking This task involved writing an algorithm to determine whether or not a number, “N” is a prime number or not. The workshop gave enough detail about prime numbers to be able to work out a suitable algorithm, but let’s take it one step at a time. Decomposition: In order to work out whether or not N is a prime, we need to work out whether it has any whole-number factors other than 1 and itself. That is simple enough: Divide N by another number and check if the remainder is 0. If so, it’s a whole-number factor. Pattern Recognition: Now that we know what we need to do, let’s figure out what values we need to do it for. Dividing by 1 will always be a whole-number factor, so our lower limit is 2. And the upper limit is obviously N itself… or is it? If you think about it a little more you can quickly realise that there’s no point dividing by any number greater than half of N. There’s simply no way that a value which is greater than half of N can fit evenly into N. A little bit of extra thought has eliminated half of the range – potentially doubling the efficiency of our solution. Abstraction: Now that we know what we need to do and the values we need to do it on, we can abstract the process into a simple and generic description. Here’s what I came up with: Divide N by every number between 2 and half of N. If the remainder of a division is 0, N is not a prime. If none of the divisions have a remainder of 0, N is a prime. Algorithms: The last step is to turn our abstraction into an algorithm. At this point, we’re really just adding detail to our abstraction in order to make it an unambiguous series of steps. Using the example of algorithms from the workshop, here’s what I came up with:

CSG5190

Workshop 1 Solutions

Page 1

Algorithm For each number between 2 and half of N If the remainder of N divided by the current number is 0 Return “No” as the end result Return “Yes” as the end result

It’s quite concise, but a couple of important optimisations baked into how it has been structured: 1) We’re returning the end result of “No” as soon as a whole-number factor is found. 2) We don’t need any kind of check before returning “Yes” – merely getting that far is enough. The fist optimisation recognises that there’s no point checking any further numbers once you’ve found a whole-number factor – since it only takes one of them to determine that N is not a prime. The second point is acknowledging that the only way to have reached the final step of the algorithm is to have gone through all of the numbers between 2 and half of N without having found a wholenumber factor – and hence having determined that N is a prime. Could this algorithm be refined further? Absolutely! In fact, what we’ve done is just a basic version of the “trial division” technique of testing for a prime. A little bit of extra pattern recognition leads us to the realisation that √N (square root of N) is actually a suitable upper limit, as explained here.

Task 2 – Syntax Familiarisation Below I’ve indicated the correct answer for each group of statements:

# Prompt for a username... A. input('Username':) B. input('Username: ') C. input 'Username: ' D. input(Username: )

# Round pi to 2 decimals... E. round(3.14159, 2.0) F. round[3.14159, 2] G. round('3.14159', '2') H. round(3.14159, 2)

# Set name value to 'Bob'... I. name = 'Bob' J. 'Bob' = name K. 'name' = Bob L. name = ‘Bob’

# Print text and name value... M. print(My name is name) N. print('My name is', name) O. print('My name is', 'name') P. print('My name is (name)')

The first group (A-D) is fairly simple – the text that you want to show as a prompt for input must be in matching quotes inside the parentheses. The second group (E-H) introduces another function – “round”, which rounds a specified number to a specified number of decimal places. Unlike text, numbers (whether whole or decimal, known as integers and floats) should not have quote marks around them – otherwise they will be interpreted and treated as bits of text rather than numbers. Option E is not valid since the round “function” requires a float (the number to round) and an integer (the amount of decimal places to round it to) – not two floats, even if 2.0 is essentially just 2. CSG5190

Workshop 1 Solutions

Page 2

In the third group (I-L) we are trying to assign a value (the text of ‘Bob’) to a variable named “name”, i.e. we are associating a value with a name so that we can refer to it later in a program. Assigning a value to a variable is always written as [variable name] = [value], not the other way around (J). Option L uses angled quote marks, which are not a recognised way of indicating text. The fourth group (M-P) uses the “print” function in a more sophisticated way – telling it to print both a piece of text and the content of a variable. Like when using “round”, we put a comma between the two things. Quote marks are placed around the text, but not the name of the variable – otherwise we would just print “name” rather than referring to the variable named “name”.

One final note on this task: The first two groups (A-D and E-H) would not be very useful as complete lines of code, since the “input” and “round” functions produce a result once they complete their task. “input” produces the text that the user types, and “round” produces the rounded number. You then need to do something with the result that is produced, such as storing it in a variable, e.g.: username = input('Username: ') roundedNumber = round(3.14159, 2) If appropriate, you can also combine functions in any way that makes sense, e.g. print('Pi rounded to two decimal places is', round(3.14159, 2)) The “round” function has been nested inside the “print” function. When this statement is run, it will (just like in mathematics) run the innermost thing first – determining the result of the “round” function and then using that in the “print” function. It will take a bit of time and exposure to pick up, but what we’re starting to see here is the “grammar” and “sentence structure” of programming languages – the ways in which you can combine things into valid sentences. The main syntax rules that these examples emphasised were:  A comma is placed between things when you have multiple things to list.  Numbers do not have quote marks around them.  Functions may require values of certain types in a certain order.  [variable name] = [value] will assign a value to a variable.

CSG5190

Workshop 1 Solutions

Page 3...


Similar Free PDFs