SOLUTION: UP Flowcharts Pseudocodes & Programming Languages Python Exam Practice

1
FILE: PYTHON_2012_ONE
This is the material that would
have been covered in Week One,
which was the Bank Holiday
instead. NOT IN TESTS / EXAM!
ONE – HOW YOU WILL BE ASSESSED IN THIS MODULE
There are five separate components to your final module mark:
EXAM (60%)
ASSIGNMENT (25%)
THREE MULTIPLE-CHOICE TESTS (Each 5%)
TWO – HOW HAVE COMPUTERS CHANGED OUR LIVES?
Computers have changed the way we live.
Using word processors to create many different types of document
Spreadsheets used for calculations in businesses
Computers controlling essential equipment in hospitals
Computer graphics used in films to create realistic sets
The World Wide Web providing access to a vast amount of information
Computers helping scientific research, like CERN and the Mars rovers
The eventual possibility, perhaps, of intelligent robots
Maybe there are some negative things as well:
Lonely people who say they have never actually met their ‘best friends’.
Spying on people by intercepting email.
People never reading real books
The eventual possibility, perhaps, of intelligent robots
THREE – WHAT DO ALL THESE HAVE IN COMMON ?
It is clear that there are a huge number of things that computers get up
to. Can we analyse these and try and find something that they all have in
common?
We can begin to understand what computers actually are doing by
thinking about an experiment once carried out by an American
astronomer, Frank Drake, who decided to use his (very early) radio
telescope to transmit a radio signal rather than receive them from distant
stars and galaxies.
2
He wanted to let any aliens out there know that human beings existed,
and even to send them a picture of what we looked like. Of course he
could only send a series of radio pulses, ‘high’ and ‘low’, or ‘long’ and
‘short’, or some other way which we could write down like this:
Frank’s signal to outer space:
00101111111010000100010100101011011
If you count you will find that there are 35 digits altogether. Frank Drake
hoped that any aliens receiving his signal would realise that 35 is the
product of two prime numbers, 5 and 7. If the signal is now written out
again as seven rows of five digits a very simple picture of a ‘stick man’
appears:
00101
11111
10100
00100
01010
01010
11011
Of course Frank Drake used a much bigger number and so was able to
code a moderately realistic picture. He made sure though that the total
number of digits he sent was the product of two prime numbers to show
the aliens that the code was for a particular rectangular shape. (Actually
two possible rectangles as you can swap round rows for columns or vice
3
versa.) Other numbers of digits would not make decoding so clear. if you
don’t use prime numbers there are two many possibilities. For example
60 isn’t much good:
60 = 2 X 30
60 = 3 X 20
60 = 4 X 15
60 = 5 x 12
60 = 6 X 10
60 = 10 X 6
60 = 12 X 5
60 = 15 X 4
60 = 20 X 3
60 = 30 x 2
Here is an example you can try. What is this picture?
000100000100000101000101000100011100010100100000100110010
000010011001000001001100000000000110000101000010100001000
01000111000111000000011100000
Remember the stages you have to follow in decoding this signal:
1. Count the number of digits
2. Find which two prime numbers have been multiplied together
3. Shade in a rectangle with this number of rows and columns
FOUR – COMPUTERS PROCESS DATA INTO INFORMATION
In producing your picture you have been converting the raw data of the
signal into useful (!) information, the picture that had been coded.
This is essentially what computers do. They process data into
information. They are capable of processing vast amounts of data very
quickly. This is why they have become so important in nearly every
aspect of society.
One way of expressing this is to say that a computer accepts INPUT and
produces OUTPUT.
4
FIVE – ALGORITHMS
In order to produce our picture we followed a set of clear, unambiguous
rules:
1. Count the number of digits
2. Find which two prime numbers have been multiplied together
3. Shade in a rectangle with this number of rows and columns
A set of rules like this for solving a particular problem is called an
algorithm. When a programmer writes a new program first they need to
know what the relevant algorithms are.
(Note here that the accepted spelling for ‘program’ in a computing
context is the American one. The British ‘programme’ should of course
be used in the sense of ‘I dropped my theatre programme under the
seat.’ or ‘I am following an intensive programme of study at university.)
[Practical exercise. Work through ‘integral value algorithm’ first on paper,
then on white board]
What does this algorithm do?
1. Begin with a decimal number, like 7.2
2. Subtract 1
3. If the answer is above zero repeat stage 1
4. Add one to the answer
5. Subtract the answer from the original number
SIX – BINARY ARITHMETIC
We usually represent numbers as a decimal system. This means that we
have ten separate digits we can use, 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 and
that the value of any digit in a number depends upon its position relative
to the other digits. For example the decimal system number 763 means
7 hundreds plus 6 tens plus 3 units. The place value of each digit is ten
times the value of the digit to its right.
There is no reason why we should use ten as the basis for a system of
numbers. In the binary system there are only two digits, 0 and 1. The
value of each digit in this system is twice the value of the digit on its
right. The binary number 1101 is one eight plus one four plus no twos
plus one unit. We could write this as:
1101 = (1 X 8) = (1 X 4) + (0 X 2) + (1 x 1) = 13
5
Computers use binary arithmetic.
The 0 and 1 of binary arithmetic are called ‘bits’, a contraction of ‘binary
digit’.
SEVEN – WHY COMPUTERS USE BINARY ARITHMETIC
Some of the very early computers did not use binary arithmetic.
However they all do now. It is easy to represent a 0 or a 1 physically
within an electronic system, for example two different levels of voltage.
Anything which could exist in either two states could theoretically
represent a binary number. Remember that computers depend basically
upon two-state physical devices. These are something that can only be
one way or another, like a light switch which is ON or OFF (or, in the
notorious joke by the philosopher John Searle, a beer can which can be
EMPTY or FULL. American students can’t leave a half full beer can
alone, we assume!)
The two state devices inside a computer are usually electrical
voltages in the minute wires inside the computer. They can also be
patterns of electrical charge, magnetic polarity on an old-fashioned
floppy, the different reflecting layers on the surface of a CD etc. The
important thing is that each device is in one state or another. These can
be represented mathematically as the 1 or 0 of binary arithmetic. A
single two state device will correspond to a single binary digit, or bit.
This represents 0
This represents 1
So far this computer
only has one bit of
memory!
6
The number of separate items that can be held in a computer
memory will depend upon how many bits there are available:
ONE BIT – This can be 0 or 1. There are TWO possibilities.
TWO BITS – Now there can be 00, 01, 10 or 11. There are FOUR
possible things that can be represented by two bits. (This is 2 X 2.)
THREE BITS – This allows 000, 001, 010, 011, 100, 101, 110, 111. Now
there are EIGHT possible separate combinations. (This is 2 X 2 X 2.)
FOUR BITS – There can be 0000, 0001, 0010, 0011, 0100, 0101, 0110,
0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111. There are
SIXTEEN different combinations. (This is 2 X 2 X 2 X 2.)
FIVE BITS – The total set of possible combinations is 00000, 00001,
00010, 00011, 00100, 00101, 00110, 00111, 01000, 01001, 01010,
01011, 01100, 01101, 01110, 01111, 10000, 10001, 10010, 10011,
10100, 10101, 10110, 10111, 11000, 11001, 11010, 11011, 11100,
11101, 11110, 11111. There are now THIRTY TWO, or (2 X 2 X 2 X 2 X
2.)
It can be seen that if there are N bits the number of separate
combinations that exist will be 2 raised to the power N.
An important number of bits is eight. This is perhaps historical as
much as anything. The earliest, useful computers worked with eight bits.
The address bus and the data bus inside these computers consisted of
eight separate tracks of wire, each capable of carrying a high or low
voltage representing a 1 or a 0. The first cheap mass-produced
microcomputers were all 8-bit machines. (16-bit microcomputers didn’t
appear until the early 1980s. Eight bits is called a byte.
EIGHT – BINARY ARITHMETIC IS EASIER FOR A COMPUTER
Binary arithmetic is actually easier to perform than decimal arithmetic.
When young children first learn arithmetic they have to memorise a large
number of ‘number bonds’ like these:
2+4=6
1+ 3 = 4
7
8 + 5 = 13
Etc
(There are one hundred number bonds altogether)
All a computer needs is:
0+0=0
0+1=1
1+0=1
1 + 1 = 10
NINE – WHAT ABOUT LETTERS?
Obviously computers do more than work with numbers. Handling text is
an essential role. This is achieved by letting numbers represent letters.
For example capital A is 65, capital Z is 90, with corresponding values
for the other letters in between. Lower case letters have a value 32 more
than the capital letter. So lower case a is 97 and lower case z is 122.
This choice of number was quite arbitrary and established by agreement
between various computer manufacturers and other organisations as
American Standard Code for Information Interchange, or ASCII. Later it
was expanded to create Unicode.
Here is an important secret message written in ASCII code. Can you
translate it?
Hard version in binary:
100 0011
100 0001
101 0100
101 0011
100 0101
100 0001
101 0100
100 0110
100 1001
101 0011
100 1000
8
Easier version in decimal:
67
65
84
83
The answer is:
CATS EAT FISH
69
65
84
70
73
83
72
Similar, and arbitrary, conventions exist for representing negative
numbers and fractions. The important thing to remember is that
computers only ‘know about’ binary numbers. (eg represent physically a
large (stupendous!) number of bits each as incredibly minute two state
devices.)
TEN – MACHINE CODE
At its most basic level a computer only ‘understands’ instructions given
to it in the form of binary numbers. These directly affect the way the
hardware of the computer behaves. Some programmers have to work at
this level, for example when a new processor is being developed.
ELEVEN – ASSEMBLY LANGUAGES
Binary numbers are difficult to remember and work with. an assembly
language gets round this problem by having short words, more easy to
remember, associated with each machine code instruction. Assembly
programming is still difficult though. It is a low level language.
9
TWELVE – HIGH LEVEL LANGUAGES
High level languages are easier to program. Each instruction in a high
level language will correspond to a large number of machine code
instructions. All the ‘hard work’ has been done by the low level
programmers who have produced the interpreter or compiler which
converts the high level ‘source code’ into the low level ‘object code’ that
the computer needs.
There have been a large number of different high level languages
developed since the 1950s. At one time people hoped that there might
be some ideal general purpose computer language that would be
appropriate for every possible application, but this has never happened.
Some high level languages have been:
Ada
Algol
Basic
C
Cobol
Fortran
Java
Lisp
Pascal
Prolog
Currently visual programming, object-oriented programming and ‘Internet
friendly’ languages are important. This course will use Python, which is
regarded as a good first language for students and which Portsmouth
University has adopted.
THIRTEEN – SYSTEMS PROGRAMS
A computer uses a lot of software which remains ‘invisible’ to the user.
Such systems software is essential for the everyday ‘housekeeping’ of a
computer, for example reading a keypress or reading from a memory
device.
FOURTEEN – UPLOADING PYTHON
You do not have to purchase Python. It is a free download. You need to
go to the official Python Website to download it to your own machine.
Python is NOT on Moodle.
10
1
File: NEW_TWO
UPDATED: FEB 21 2019
Content:
Computer languages
Pseudo Z80 assembly
Addition program
Subtraction program
Coding an algorithm into
assembly
Comments in Python
Decisions in Python
Loops in Python
Programming glossary
Exercises
ONE – COMPUTER LANGUAGES
At its most basic level a computer only ‘understands’ instructions given to
it in the form of binary numbers. These directly affect the way the hardware
of the computer behaves. Some programmers have to work at this
machine code level, for example when a new processor is being
developed. Binary numbers are difficult to remember and work with. An
assembly language gets round this problem by having short words,
easier to remember, associated with each machine code instruction. High
level languages are easier to program. Each instruction in a high level
language will correspond to a large number of machine code instructions.
All the ‘hard work’ has been done by the low level programmers who have
produced the interpreter or compiler which converts the high level ‘source
code’ into the low level ‘object code’ that the computer needs. There have
been a large number of different high level languages developed since the
1950s. At one time people hoped that there might be some ideal general
purpose computer language that would be appropriate for every possible
application, but this has never happened. Some high level languages
have been: Ada, Fortran, Java, Lisp, Squint, Pascal, Prolog, Python,
Algol, Basic, C, Cobol… (Warning: One of that list is a silly joke.)
2
TWO – WORKING IN ASSEMBLY LANGUAGE
In addition to learning the high level language Python you will also learn
about assembly language. The examples, and exercises you will
complete, will all be written in a simulation of assembly language which
actually runs on a special Excel spreadsheet. This sheet has been called
ZILOG and the instructions in the simulation are similar to a very small
subset of Z80 assembly language which was developed by the firm Zilog
for their Z80 microprocessor.
This is the Excel spreadsheet which runs the assembly simulation. It is on
Moodle with the filename ZILOG.
Here is a very short program written in the simulated assembly language.
All it does is load the accumulator with the number 14. This number of
course has to be shown as 8-bit binary. Since 14 = 8 + 4 + 2 the binary
number will be 00001110. The instruction to put a number into the
accumulator is LDA and so the program will be:
LDA 00001110
(There is exactly one character space between the LDA and the
00001110.)
This is what the ZILOG sheet looks like when the program is entered:
3
In order to see what this program does you have to load the code for the
simulation (which I have written in Visual Basic for Applications, or VBA)
on to the sheet. This is how you do it.
First click on any empty cell on the sheet. Then hold the Alt key down and
press F11 at the same time. The display will become:
4
Now click on the Insert tab:
Select User Form:
Choose the Command Button from the Tool Box (Third row, second
column!) and drag a button on to the User Form:
5
Click on the User Form outside the button. Then double-click inside. A
code window will appear:
This is where you have to copy the VBA code. The code itself is here.
(Rather long I’m afraid.) At the end of the code you will see the next screen
shot showing what the Zilog sheet will look like when the code has been
pasted into the Command Button:
Here is the assembly simulation code. The font size has been reduced to
save space but the code will work if you copy and paste it into an Excel
command button.
‘ ASSEM_SIM_V3.3
‘ Patrick Hall
‘Version FEB_2018_A
‘THIS VERSION NOT TESTED AT ALL. HAS BEEN A BIT NOW. DECC WORKS OKAY
‘DECC has been added
‘ JPC condition changed to >= from just >
‘INCONSISTENCY over use of Copy variable anyway!!!!!!!!!!!!!!!!!
Dim zset(15) As String ‘Fake Z80 instruction set
Dim user_code As String ‘Code entered by user
Dim program(20) As String ‘Array containing individual program words
Dim PC As Integer ‘Program counter
Dim L As Integer ‘Number of characters in user code
Dim I, J As Integer ‘Loop control variables
Dim N As Integer ‘Number of words in program
Dim C As String ‘Individual characters in user code
Dim A_REG, B_REG, C_REG AS String ‘Displayed content of registers
Dim A_VAL, B_VAL, C_VAL As Integer ‘Decimal value of register content
Dim Mess As String ‘Messages to user
Dim Code_okay As Integer ‘Checking syntax
Dim Word_okay As Integer ‘Checking syntax
Dim Copy As Integer
Dim JPC, JPU, LABC, LABU As String ‘Jump instructions
Dim LC, LU As Integer ‘Jump locations
Dim JPCF, JPUF, LABCF, LABUF As Integer ‘Jump flags
‘Instruction set
zset(0) = “LDA”
zset(1) = “LDB,A”
6
zset(2) = “LDC,A”
zset(3) = “ADDA,B”
zset(4) = “ADDA,C”
zset(5) = “ADDC,B”
zset(6) = “SLA”
zset(7) = “SRA”
zset(8) = “INC”
zset(9) = “DEC”
zset(10) = “JPC”
zset(11) = “JPU”
zset(12) = “LABC”
zset(13) = “LABU”
zset(14) = “DECC”
‘Obtain user code
user_code = Cells(2,1)
‘Parse user code
L = Len(user_code)
N=0
‘Parse program
For I = 1 To L
C = Mid$(user_code , I, 1)
If CChr(32) Then
program(N) = program(N) + C
Else
N= N+ 1
End If
Next I
‘Check syntax
Mess = “”
Code_okay = 1
For I = 0 To N
Word_okay = 0
‘Check present in instruction set
For J = 0 To 15
If program(I) = zset(J) Then
Word_okay = 1
End If
Next J
‘Check valid signed byte
If Abs(Numerical(program(I))) N Or LU > N Then
Word_okay = 0
End If
‘Generate error message
If Word_okay = 0 Then
Mess = Mess + “Error: ” + program(I) + Chr(10)
Code_okay = 0
End If
Next I
‘Check for consistency of jump instructions
For I = 0 To N
If program(I) = “JPC” Then
JPCF = 1
End If
If program(I) = “JPU” Then
JPUF = 1
End If
If program(I) = “LABC” Then
LABCF = 1
End If
If program(I) = “LABU” Then
LABUF = 1
End If
Next I
If JPCF LABCF Then
Mess = Mess + “Error: JPC or LABC” + Chr(10)
Code_okay = 0
End If
If JPUF LABUF Then
Mess = Mess + “Error: JPU or LABU” + Chr(10)
Code_okay = 0
End If
‘Execute program
If Code_okay = 1 Then
A_REG = “00000000”
B_REG = “00000000”
C_REG = “00000000”
A_VAL = 0
B_VAL = 0
C_VAL = 0
PC = -1
Do While PC N Then
A_REG = A_REG + Chr(10) + program(PC+1)
A_VAL = Numerical(program(PC+1))
End If
‘Check for LDB,A
If program(PC) = zset(1) Then
B_VAL = A_VAL
B_REG = B_REG + Chr(10) + Show_binary(B_VAL)
B_VAL = A_VAL ‘Restore value
End if
‘Check for LDC,A
If program(PC) = zset(2) Then
C_VAL = A_VAL
C_REG = C_REG + Chr(10) + Show_binary(C_VAL)
C_VAL = A_VAL ‘Restore value
End if
‘Check for ADDA,B
If program(PC) = zset(3) Then
A_VAL = A_VAL + B_VAL
Copy = A_VAL
A_REG = A_REG + Chr(10) + Show_binary(A_VAL)
A_VAL = Copy
End if
‘Check for ADDA,C
If program(PC) = zset(4) Then
A_VAL = A_VAL + C_VAL
Copy = A_VAL
A_REG = A_REG + Chr(10) + Show_binary(A_VAL)
A_VAL = Copy
End if
‘Check for ADDC,B
If program(PC) = zset(5) Then
C_VAL = C_VAL + B_VAL
Copy = C_VAL
C_REG = C_REG + Chr(10) + Show_binary(C_VAL)
C_VAL = Copy
End if
‘Check for SLA
If program(PC) = zset(6) Then
A_VAL = A_VAL * 2
A_REG = A_REG + Chr(10) + Show_binary(A_VAL)
End if
‘Check for SRA
If program(PC) = zset(7) Then
A_VAL = A_VAL / 2
A_REG = A_REG + Chr(10) + Show_binary(A_VAL)
End if
‘Check for INC
If program(PC) = zset(8) Then
A_VAL = A_VAL + 1
Copy = A_VAL
A_REG = A_REG + Chr(10) + Show_binary(A_VAL)
A_VAL = Copy
End if
‘Check for DEC
If program(PC) = zset(9) Then
A_VAL = A_VAL – 1
A_REG = A_REG + Chr(10) + Show_binary(A_VAL)
A_VAL = Copy
End if
‘Check for JPC
If program(PC) = zset(10) And C_VAL >= 0 The …
Purchase answer to see full
attachment

Order a unique copy of this paper
(550 words)

Approximate price: $22

Our Basic features
  • Free title page and bibliography
  • Plagiarism-free guarantee
  • Unlimited revisions
  • Money-back guarantee
  • 24/7 support
Our Options
  • Writer’s samples
  • Expert Proofreading
  • Overnight delivery
  • Part-by-part delivery
  • Copies of used sources
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

AcademicWritingCompany guarantees

Our customer is the center of what we do and thus we offer 100% original essays..
By ordering our essays, you are guaranteed the best quality through our qualified experts.All your information and everything that you do on our website is kept completely confidential.

Money-back guarantee

Academicwritingcompany.com always strives to give you the best of its services. As a custom essay writing service, we are 100% sure of our services. That is why we ensure that our guarantee of money-back stands, always

Read more

Zero-plagiarism tolerance guarantee

The paper that you order at academicwritingcompany.com is 100% original. We ensure that regardless of the position you are, be it with urgent deadlines or hard essays, we give you a paper that is free of plagiarism. We even check our orders with the most advanced anti-plagiarism software in the industry.

Read more

Free-revision guarantee

The Academicwritingcompany.com thrives on excellence and thus we help ensure the Customer’s total satisfaction with the completed Order.To do so, we provide a Free Revision policy as a courtesy service. To receive free revision the Academic writing Company requires that the you provide the request within Fifteen (14) days since the completion date and within a period of thirty (30) days for dissertations and research papers.

Read more

Privacy and Security policy

With Academicwritingcompan.com, your privacy is the most important aspect. First, the academic writing company will never resell your personal information, which include credit cards, to any third party. Not even your lecturer on institution will know that you bought an essay from our academic writing company.

Read more

Adherence to requirements guarantee

The academic writing company writers know that following essay instructions is the most important part of academic writing. The expert writers will, therefore, work extra hard to ensure that they cooperate with all the requirements without fail. We also count on you to help us provide a better academic paper.

Read more

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2020 at 10:52 AM
Total price:
$26
The price is based on these factors:
Customer Academic level
Number of pages required
Urgency of paper