More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

Code breaking using Trace Tables

In the following set of challenges, you will use a trace table to workout the secret code of the following six padlocks. Before attempting this challenge, you may want to find out mode about the use of trace tables when testing or predicting the output of an algorithm.

Padlock #1Padlock #2Padlock #3Padlock #4Padlock #5Padlock #6

Padlock #1:


The following code will help you work out the combination (code) to open padlock #1 (see below)

code = ""
FOR i FROM 1 TO 3
   digit = i * 2
   code = code + STR(digit)
NEXT i
OUTPUT(code)

Trace Table

 Line NumberidigitcodeOUTPUT
1“”
21
32
4“2”
22
34
4“24”
23
36
4“246”
6246

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

Padlock #2:


The following code will help you work out the combination (code) to open padlock #2 (see below)

code = ""
code = code + "9" 
code = code + "7"
code = code + "1"
OUTPUT(code)

Trace Table


Complete the following trace table to work out the 3-digit padlock code:
 Line NumbercodeOUTPUT
1

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

Padlock #3:


The following code will help you work out the combination (code) to open padlock #3 (see below)

code = ""
code = code + "7" 
code = "4" + code
code = code + "3"
OUTPUT(code)

Trace Table


Complete the following trace table to work out the 3-digit padlock code:
 Line NumbercodeOUTPUT
1

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

Padlock #4:


The following code will help you work out the combination (code) to open padlock #4 (see below)

code = 61 
IF code MOD 2 == 0 THEN
   code = code * 3
ELSE
   code = code * 2
END IF
OUTPUT(code)

Trace Table


Complete the following trace table to work out the 3-digit padlock code:
 Line Numbercodecode MOD 2 == 0 ?OUTPUT
1

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

Padlock #5:


The following code will help you work out the combination (code) to open padlock #5 (see below)

code = 24 
IF code > 20 AND code <= 10 THEN
   code = code * 3
ELSE IF code > 20 OR code <= 10 THEN 
   code = code * 10
ELSE 
   code = code * 4
END IF
OUTPUT(code)

Trace Table


Complete the following trace table to work out the 3-digit padlock code:
 Line Numbercodecode>20 ?code<=10 ?OUTPUT
1

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

Padlock #6:


The following code will help you work out the combination (code) to open padlock #6 (see below)

code = 245 
WHILE code < 1000
   code = code + 200
END WHILE
code = code - 200
OUTPUT(code)

Trace Table


Complete the following trace table to work out the 3-digit padlock code:
 Line Numbercodecode<1000 ?OUTPUT
1

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

Using Trace Tables

A trace table is a technique used to test an algorithm and predict step by step how the computer will run the algorithm. It can be used to understand or predict what an algorithm is doing and to identify potential logic errors (when the program compiles but does not produce the expected output).

The animation below demonstrate the use of a trace table used to track the values of variables as they change while the program is running. trace-table-s

Using a trace table to test an algorithm is called dry run testing.

Your Task


Check the following algorithms and complete their trace tables. (Click on any cells of these tables to add content)
Algorithm #1Algorithm #2Algorithm #3Algorithm #4

Algorithm #1: While Loop

i = 0
j = 10

WHILE i < j
   i = i + 1
   j = j - 1
END WHILE

OUTPUT(i)
OUTPUT(j)

Trace Table

 Line Numberiji < jOUTPUT
10
210
4True
51
69
4True

Algorithm #2: Factorial

number = 5
factorial = number

WHILE number>2
   number = number - 1
   factorial = factorial * number
END WHILE

OUTPUT(factorial)

Trace Table

 Line Numbernumberfactorialnumber > 2OUTPUT
15
25

Algorithm #3: Fizz-Buzz Sequence

FOR i FROM 1 TO 20
   IF i MOD 3 == 0 AND i MOD 5 == 0 THEN
      OUTPUT "Fizz-Buzz"
   ELSE IF i MOD 3 == 0 THEN
      OUTPUT "Fizz"
   ELSE IF i MOD 5 == 0 THEN
      OUTPUT "Buzz"
   ELSE
      OUTPUT i
   END IF
NEXT i

OUTPUT("The End")

Trace Table

 Line Numberii MOD 3 == 0i MOD 5 == 0OUTPUT
11
2FalseFalse
4False
6False
8
91
12

Algorithm #4: Max Function

FUNCTION max(a, b)
   IF a>b THEN
      RETURN a
   ELSE 
      RETURN b
   END IF
END FUNCTION

number1 = 7
number2 = 12
number3 = max(number1, number2)
OUTPUT(number3)

Trace Table

 Line Numbernumber1number2number3aba>bRETURNOUTPUT
97
1012
11712
1712

Fibonacci Sequence using LMC

fibonacci-sequence

Did you know?


The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 … where the next number is found by adding up the two numbers just before it.

number-sequence-diagram-5

The first 10 numbers of the Fibonacci number sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

The following table shows how we can calculate each Fibonacci number from this sequence:

Fibonacci Number Calculation
0
1
1 = 0 + 1
2 = 1 + 1
3 = 1 + 2
5 = 2 + 3
8 = 3 + 5
13 = 5 + 8
21 = 8 + 13
34 = 13 + 21

Little Man Computer


Your challenge is to write a program using one of the following online LMC Simulators to calculate and output the first 10 numbers of the Fibonacci sequence.

LMC Instruction Set


Note that in the following table “xx” refers to a memory address (aka mailbox) in the RAM. The online LMC simulator has 100 different mailboxes in the RAM ranging from 00 to 99.

Mnemonic Name Description Op Code
INP INPUT Retrieve user input and stores it in the accumulator. 901
OUT OUTPUT Output the value stored in the accumulator. 902
LDA LOAD Load the Accumulator with the contents of the memory address given. 5xx
STA STORE Store the value in the Accumulator in the memory address given. 3xx
ADD ADD Add the contents of the memory address to the Accumulator 1xx
SUB SUBTRACT Subtract the contents of the memory address from the Accumulator 2xx
BRP BRANCH IF POSITIVE Branch/Jump to the address given if the Accumulator is zero or positive. 8xx
BRZ BRANCH IF ZERO Branch/Jump to the address given if the Accumulator is zero. 7xx
BRA BRANCH ALWAYS Branch/Jump to the address given. 6xx
HLT HALT Stop the code 000
DAT DATA LOCATION Used to associate a label to a free memory address. An optional value can also be used to be stored at the memory address.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Hardware Contest

In this hardware contest, you will be presented with two computers at a time (desktops or laptops). Your task is to compare the technical specifications of both computers and discuss and justify which computer seems to have the highest specifications. You should discuss your findings with your peers.

Computer #1
CPU Clock Speed
Number of Cores
CPU Cache
RAM
Graphic Card
Hard Drive
Optical Drive
NIC Card
Screen Size
Computer #2
CPU Clock Speed
Number of Cores
CPU Cache
RAM
Graphic Card
Hard Drive
Optical Drive
NIC Card
Screen Size

LMC Simulator

LMC900
LMC simulators are based on the Little Man Computer (LMC) model of a computer, created by Dr. Stuart Madnick in 1965. The LMC simulator is generally used to for educational purposes, because it models a simple Von Neumann architecture computer which has all of the basic features of a modern computer. It is programmed using assembly code.

The LMC simulator demonstrates how assembly code is processed by the CPU using the Fetch, Decode and Execute (FDE) Cycle. It shows the impact of the FDE cycles on the main registers found inside the CPU:

  • The PC (Program Counter),
  • The CIR (Current Instruction Register),
  • The MAR (Memory Address Register),
  • The MDR (Memory Data Register),
  • The Accumulator.

The LMC simulator also demonstrates how the CPU interacts with the Random Access Memory (RAM) and how both data and instructions are stored in the RAM. On our LMC simulator the RAM consists of 100 memory cells (also called mailboxes). Each cell has a unique memory location (a number between 00 and 99).

Most assembly instructions consist of an opcode and an operand.
When coding in assembly code we use mnemonics (e.g. INP, ADD, SUB, LDA, STA, etc.) for the opcode of the instruction. A lookup table (See at the end of this blog post) is used to check the corresponding opcode matching a mnemonic. With the LMC simulator, the operand is a memory location (Direct Addressing) of where the data used by the instruction can be fetched from (e.g. LDA, ADD, SUB instructions) or needs to be stored (e.g. STA instruction).

Labels can also be used instead of hard coding memory locations in the code. Labels are extremely useful when using branching instructions (BRA, BRZ, BRP), as they can be used to locate a specific instruction in the code/RAM. They can also be used to name memory cells in the RAM used to temporary store values. (Similar to the use of variables in a high level code).

LMC simulators can be used to implement simple programs to perform basic mathematical operations such as adding or subtracting numbers, comparing or sorting numbers, etc.

Launch our online LMC SimulatorOpen in new tab/window

You will find on this blog a range of LMC challenges that you can complete using our online LMC simulator:

LMC Instruction Set / Lookup Table


Note that in the following table “xx” refers to a memory address (aka mailbox) in the RAM. The online LMC simulator has 100 different mailboxes in the RAM ranging from 00 to 99.

Mnemonic Name Description Op Code
INP INPUT Retrieve user input and stores it in the accumulator. 901
OUT OUTPUT Output the value stored in the accumulator. 902
LDA LOAD Load the Accumulator with the contents of the memory address given. 5xx
STA STORE Store the value in the Accumulator in the memory address given. 3xx
ADD ADD Add the contents of the memory address to the Accumulator 1xx
SUB SUBTRACT Subtract the contents of the memory address from the Accumulator 2xx
BRP BRANCH IF POSITIVE Branch/Jump to the address given if the Accumulator is zero or positive. 8xx
BRZ BRANCH IF ZERO Branch/Jump to the address given if the Accumulator is zero. 7xx
BRA BRANCH ALWAYS Branch/Jump to the address given. 6xx
HLT HALT Stop the code 000
DAT DATA LOCATION Used to associate a label to a free memory address. An optional value can also be used to be stored at the memory address.
Tagged with: ,

The missing key

themissingkeyThe aim of this challenge is to write a computer program that will take two whole numbers (integers) as input and output the result of multiplying these two numbers together. However, to make this task more challenging, you have to assume the * key of your keyboard is broken or missing and hence you are not allowed to use the * operator in your code.

You can complete your code in the trinket below:

Hint?


View Hint #1
5 * 4 = 5 + 5 + 5 + 5
7 * 6 = 7 + 7 + 7 + 7 + 7 +7
8 * 3 = 8 + 8 +8
View Hint #2
Use a loop!
Tagged with:

School Trip Bus Quote

school-busA school teacher is organising a school trip for the whole year group. He expects between 250 and 350 students to attend this trip. To estimate the cost of the trip, the school teacher has contacted a coach company to hire several coaches for a day.

The coach company has two categories of buses:

Bus Number of seats Cost per day
school-bus-sign Large Bus 46 Seats £360
school-mini-bus-sign Small Bus 16 Seats £140

The school teacher would like a computer program that will:

  1. Ask for the number of students taking part in the trip.
  2. Ask for the number of teachers taking part in the trip.
  3. Calculate the total number of participants (by adding the number of students and the number of teachers).
  4. Find out and output the number of large coaches that will be required.
  5. Find out and output the number of small coaches that will be required.
  6. Calculate and output the total cost of hiring these coaches for a day.

Flowchart


Here is the flowchart for this algorithm:
school-trip-bus-quote-flowchart

Python Code


Your task is to use the above flowchart to complete the Python code below:

Testing


Once your code is done, complete the following tests to check that your code is working as it should:

Test # Input Values Expected Output Pass/Fail?
#1 Number of students: 250
Number of teachers: 7
Number of large buses: 5
Number of small buses: 2
Total cost: £2080
#2 Number of students: 300
Number of teachers: 10
Number of large buses: 6
Number of small buses: 3
Total cost: £2580
#3 Number of students: 350
Number of teachers: 12
Number of large buses: 7
Number of small buses: 3
Total cost: £2940
#4 Number of students: 0
Number of teachers: 0
Number of large buses: 0
Number of small buses: 0
Total cost: £0

Extension Task #1


You have noticed that it is more cost effective to hire a large bus (£360) instead of three small buses (3 * £140 = £420) even if the large bus is not full.
So your task is to adapt the above algorithm so that, when calculating the number of large buses, if the number of pupils left (remainder) is greater than 32, your program will hire one extra large bus instead of 3 small buses. It will hence output the most cost effective solution for test #2 and test #3.

The new test plan would be as follows:

Test # Input Values Expected Output Pass/Fail?
#1 Number of students: 250
Number of teachers: 7
Number of large buses: 5
Number of small buses: 2
Total cost: £2080
#2 Number of students: 300
Number of teachers: 10
Number of large buses: 7
Number of small buses: 0
Total cost: £2520
#3 Number of students: 350
Number of teachers: 12
Number of large buses: 8
Number of small buses: 0
Total cost: £2880
#4 Number of students: 0
Number of teachers: 0
Number of large buses: 0
Number of small buses: 0
Total cost: £0

Extension Task #2


To make your program more robust, you can add some validation checks on both inputs. These validation checks will ensure that the user can only input whole numbers (integers). To do so, you should check the following blog post: number only validation in Python.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with:

Real-Time Asteroid Watch

asteroidEvery day asteroids are passing close to planet Earth. Their trajectory is closely monitored by NASA as some of these could potentially pose a threat to our planet. In this python challenge we are going to use the NeoWs API to retrieve some real-time data from NASA about these near Earth asteroids.

You can read more about the NeoWs API on: https://api.nasa.gov/api.html#neows-feed

JSON Data


This API uses JSON to format the data. JSON (JavaScript Object Notation) is a popular lightweight data-interchange format. Its main benefit is that it is easy for humans to read and write and it is easy for machines to parse and generate as you can see in the code provided below. You can read more about JSON on https://www.json.org/

You can see what the JSON data returned by the API looks like:
https://api.nasa.gov/neo/rest/v1/feed?start_date=2015-09-07&end_date=2015-09-08&api_key=DEMO_KEY

Python Code


Check our code to see how we make a call to the API and how we retrieve and extract the requested JSON data.

Your Task


Update this code to show all the near Earth asteroids approaching Earth in the next 7 days.
The JSON data also contains links to web pages for each asteroid (e.g. https://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3745034). Update the Python code to also display the relevant web addresses.

Tagged with:

Maritime Signal Flags Translator

nautical-flagsInternational maritime signal flags are various flags used to communicate with ships. The purpose of the International Code of Signals is to provide ways and means of communication in situations related essentially to safety of navigation. The code uses a set of 26 flags for each letter of the alphabet plus additional flags for the 0 to 9 digits. Combinations of these alphanumeric characters are assigned as codes for various standardised messages. For instance, the master of a ship may wish to communicate with another ship, where his own radio may not be working.

The aim of this challenge was to design a web-based app to convert any message using the relevant flags.

You can use our online translator below and investigate the HTML, CSS and JavaScript code used to implement this translator.

See the Pen
Maritime Signal Flags Translator
by 101 Computing (@101Computing)
on CodePen.

Tagged with: ,

Flip Cards in HTML, CSS & Javascript

flip-cardCheck the following code used to create an interactive flip card that can be added to any existing web page.

Your Task

  • Change the HTML code to create a collection of flip cards using Computer Science terminology. (e.g. Programming Terminology)
  • Change the CSS code to change the look and feel of your flip card (front and back).

See the Pen
Flip Card
by 101 Computing (@101Computing)
on CodePen.

Tagged with: