More results...

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

OOP – Encapsulation – Q&A

Question 1[2 marks]
class Padlock:
  private key
  private locked
  
  //Constructor for the Padlock class:
  procedure new(str_key,bool_locked):
    key = str_key       
    locked = bool_locked  
    
  public function unlock(combination):
    if combination==key:
       locked = False
       return True
    else:
      return False
      
  public procedure lock():
    locked = True
    
  public function getKey():
    return key
    
  public function setKey(combination):
    if length(combination)==4:
      key = combination
      return True
    else:
      return False
What code would you write to instantiate two new padlocks: One called bikeLock that is already locked with a combination of 4321 and one called gardenShedLock that is unlocked with a combination of 1234?
Question 2[4 marks]
Explain using an example from the above code for the Padlock class, what is meant by encapsulation.
Save / Share Answers as a link
unlock-access

Solution...

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

Maths Puzzles…

The aim of this challenge is to write a few algorithms to solve the following Maths puzzles:

In order to solve these puzzles, we will use an iterative approach using nested loops to test all possible combinations of two numbers until we find a match.

You will first look at the code used to solve the following puzzle and then tweak this code to solve the puzzles listed above.
maths-puzzle

Iterative Approach using nested loops…

Here is our code based on nested loops to test all possible pairs of numbers between 0 and 1,000.

unlock-access

Solution...

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


Brunel’s Engineering Algorithms

brunel-topper-hatIsambard Kingdom Brunel (1806-1859) was an English civil engineer who is considered one of the greatest figures of the Industrial Revolution. Brunel achieved many engineering firsts thanks to his hard-work, his engineering knowledge, his innovative mind and his renowned problem solving skills. Some of his achievements include assisting in the building of the first tunnel under the River Thames, the design & development of the SS Great Britain, the largest ship ever built at the time: launched in 1843 the SS Great Britain was the first propeller-driven, ocean-liner ship with an iron hull. Brunel also worked on many architectural projects and viaducts when designing the Great Western Railway (GWR) linking Bristol to London. One of his major project, who was fully completed five years after his death, is the famous Clifton Suspension Bridge over the river Avon in Bristol, UK.

In this set of challenges, we are going to write four short algorithms that could have helped Brunel on his engineering projects. Obviously, computers did not exist in Brunel’s time, but the mathematical concepts used by these algorithms were for sure known and heavily used in Brunel’s engineering calculations. You can use our online Python IDE to write your algorithms/functions in Python.

Algorithm #1: Propeller Inner Angle

When Brunel worked on the design and construction of the SS Great Britain ship, most engine powered ships of the time used paddle wheels. His first design for this large iron hull ship was also based on using paddle wheels on both sides of the hull, however Brunel soon found out that using a propeller would be more effective and therefore he changed his design halfway through the construction stage! Once the decision was approved, one of Brunel’s next decisions was to choose how many blades to use for the propeller.

To assist him with the technical drawing of the propeller, Brunel used the following formula:
propeller-inner-angle-formula

Your task is to write a function called calculateAngle() that takes one parameter, the number of blades of a propeller (as a positive integer value). Your function will calculate and return the inner angle between two adjacent blades (in degrees).

You will then be able to complete the following test plan to check that your function is working as expected:

Test #   Input Value:
Number of blades
Expected Output Actual Output
#1 propeller-2-blades 2 180°
#2 propeller-3-blades 3 120°
#3

propeller-4-blades 4 90°
#4 propeller-6-blades📷 6 60°

Algorithm #2: Viaduct Arches

A viaduct is a specific type of bridge that consists of a series of arches, piers or columns supporting a long elevated railway or road. When working on the Great Western Railway (GWR) linking Bristol to London, Brunel designed several viaducts, each with a different length, height and number of arches. The Wharncliffe viaduct 📷 is a great example of his work. Built in 1837, it was the first railway viaduct to be built with hollow piers. It is 270 meters long and consists of eight arches.


Supposing that each arch has the same width, we can calculate the width of an arch as follows:
viaduct-formula

Your task is to write a function called calculateArchWidth() that takes two parameters, the length of the viaduct in meters and the number of arches (both parameters being given as positive integers). Your function will calculate and return the width of an arch.

You can test your algorithm using the following test plan:

Test #   Input Value: Expected Output Actual Output
#1 Wharncliffe Viaduct 📷 Viaduct Length: 270m
Number of Arches: 8
Arch width: 33.75m
#2 Carnon Viaduct 📷 Viaduct Length: 230m
Number of Arches: 9
Arch width: 25.55m

Algorithm #3: Railway Slope

When designing the Great Western Railway (GWR), one of the main objectives of Brunel was to design a fairly straight route as level as possible in order to promote high speed travel on the line. To do so Brunel had to estimate the percent slope (gradient) of every section of the route to avoid steep slops.

Here is the formula that Brunel used to calculate the percent slope of every section of his planned route:
railway-percent-slope-formula

Your task is to write a function called calculatePercentSlope() that takes two parameters: delta_x (Δx) and delta_h (Δh), the difference in coordinates and altitudes between the starting and ending position of a section. Your function will use these two parameters to calculate and return the percent slope of the section.
You can test your algorithm using the following test plan:

Test #   Input Value: Expected Output Actual Output
#1 Section 1 Δx = 800m
Δh = 32m
Percent Slope: 4%
#2 Section 2 Δx = 500m
Δh = 60m
Percent Slope: 12%
#3 Section 2 Δx = 750m
Δh = 45m
Percent Slope: 6%

Algorithm #4: Suspension Bridge Arc Length

One of Brunel’s most famous project is the Clifton Suspension Bridge, Bristol’s most impressive structure. He started to design this project at the age of 24. The bridge took 33 years to complete!

clifton-suspension-bridge

One of the mathematical puzzle that Brunel had to solve, was to estimate the length of the main arc of the suspension bridge. This arc consists of wrought iron chains to support the load of the main deck as well as the extra load due to the traffic across the bridge. The arc joins the two towers on each side of the river Avon. The two towers are separated by 214 meters and are rising 26m above deck level. The deck itself is 75m above the high water mark!

The following mathematical formulas can be used to calculate the arc length based on the distance between the two towers (l) and the height of each tower (from the deck level) (h)

suspension-bridge-arc-length-formula
Your task is to write a function called calculateArcLength() that takes two parameters: the length (l) and the height (from the deck level) (h) of the suspension bridge. Your function will use these two parameters to calculate and return the length or the arc of the bridge.

You can test your algorithm using the following test plan:

Test #   Input Value: Expected Output Actual Output
#1 Clifton Suspension Bridge length = 214m
height = 26m
Arc Length: 222m

To find out more about Brunel‘s work visit the SS Great Britain museum in Bristol, UK.

unlock-access

Solution...

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


Tagged with: ,

The Climbing Stairs Puzzle

Imagine a staircase with n steps. As you are climbing up this staircase, you either climb one or two steps at a time. The aim of this computing puzzle is to find out, using an algorithm, in how many distinct ways can you climb to the top?

Confused by this puzzle? Let’s look at an example where you are climbing a small staircase of only 4 steps. Remember, for every “step” you make, you can either climb 1 or two steps at a time. Which means there are 5 different ways you can climb this staircase as shown on the pictures below:

staircase-1-1-1-1-stepsstaircase-2-1-1-stepsstaircase-1-2-1-stepsstaircase-1-1-2-stepsstaircase-2-2-steps

A Recursive Approach?

Let’s investigate this problem, “step by step”!!

We can assume that n, the total number of steps in the staircase is a positive integer.
The output of our algorithm is the total number of distinct ways we can climb this stair.

So we can easily deduct that:

  • If n=0, the output should be zero too.
  • If n=1, the output will be 1 (there is only way to climb this step).
    staircase-1step
  • If n=2, the output will be 2 (there are only two ways to climb this step).
    staircase-1-1-stepsstaircase-2-steps
  • For any n number of steps greater than 2, we notice that to reach the nth, we have to first reach either step (n-1) or step (n-2).
    staircase-n-steps

    So to count the number of ways we can reach step n, we can use the following recursive formula:

    if n>2 then countWays(n) = countWays(n-1) + countWays(n-2)
    countWays(2) = 2
    countWays(1) = 1

    where countWays(n) is a function that returns the number of ways you can reach the nth step.

Python Code

To implement an algorithm based on this formula we will use a recursive function called countWays() that takes one parameter, n, the position of the step we aim to reach. The function will return the number of possible distinct ways to reach this step when climbing up from the bottom of the stairs.

Tracing a Recursive Algorithm

You can visualise/trace this recursive algorithm using recursivevisualization.com.

tracing-recursive-function

Your Task

What about if as you are climbing up this staircase, you could climb 1, 2 or 3 steps at a time?

Adapt the above python code to implement this small change in the climbing stairs puzzle.

Fibonacci Numbers

You may have recognised that our countWays() function is based on a very similar recursive formula used to calculate the nth term of the Fibonacci sequence.
fibonacci-sequence


unlock-access

Solution...

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


Tagged with:

French Cuisine – Entity Relationship Diagram (ERD)

french-cuisineFrench Cuisine is a 5-star restaurant serving a selection of carefully selected French recipes in a cosy atmosphere. Their menu is organised into 4 categories as follows: Starters, Main Meals, Deserts & Drinks.

In the dining area, there are 21 tables of different sizes (to serve from 1 to 8 customers per table). Each table has a unique number that is used by waiters & waitresses when they take and deliver an order.

Customers have to book a table in advanced by calling the restaurant. When they do so, the receptionist records the name and phone number of the customer making the booking and the number of guests the booking is for. The restaurant does not need to record the information of all the guests when a booking is made. Depending on the number of guests, the receptionist will allocate a table to the booking. Only 1 table can be booked per booking, so if a customer requires more than one table (for a large group) they will need to make several bookings.

When an order is raised the, the waiter or waitress write downs the menu item codes from the main menu and the quantity required.

Up to now, all the information recorded by both the receptionist when a booking is made and by the waiters/waitresses taking the orders at the table, is handwritten using small notebooks. However the restaurant would like to upgrade their system and intend to provide both the receptionist and the waiters & waitresses with touchscreen tablets to record table bookings and orders on a central system/database.

Your task is to design the Entity Relationship Diagram (ERD) for a relational database that will store information about:

    the different tables in the restaurant (Table number and capacity),
    the menu items that can be ordered from the menu (with a description and a price for each item as well as an indication as whether the item is suitable for vegetarians or not),
    the bookings made (storing information such as date and time of the booking, number of guests, table number),
    the contact details of the customers making the bookings,
    the orders being raised with, for each order, the table number the order is for, a list of all item codes being ordered from the menu and the quantities required.

You can use our online ERD tool to create the Entity Relationship for this 5 star restaurant:
Design Your ERD Online
kitchen-tools

unlock-access

Solution...

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


Tagged with: , , ,

10/02/22 Computing Challenge

As this post was published on the 10th of February 2022 (10/02/22), we would like to use a Python algorithm to solve the following arithmetic puzzle:

10-02-22-arithmetic-puzzleLet’s consider a string consisting of 10 characters “2”: string = “2222222222”

This computing challenge consists of writing an algorithm to work out the largest number we can create by inserting 2 multiplication operators (*) anywhere within this string.

For instance:

  • 2 * 2 * 22222222 = 88,888,888
  • 2 * 22 * 2222222 = 97,777,768
  • 2222 * 2222 * 22 = 108,620,248
  • 222 * 222 * 2222 = 109,509,048
  • etc.

10th February 2022

Python Code

You can use the trinket below to write and test your algorithm… We have started the code for you to test all possible positions of the first multiplication operator. Your task is to complete this code to:

  1. Add a second multiplication sign and test every possible position for this second sign,
  2. Calculate the result of each expression and output the largest value reached.


unlock-access

Solution...

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


EasyFlights – Entity Relationship Diagram (ERD)

easyflights-logoEasyFlights is an international airline company operating all over the world. As part of a total rollout of all their IT systems, they would like you to suggest the most suitable ERD to be used to design the relational database of their new Management Information System.

They would like the EasyFlights database to be the central place to store information about:

    Store information about the different airports they serve,
    Store information about their fleet of aircrafts,
    Store information about their employees (e.g. pilots, flight attendants, mechanics, admin staff etc.),
    Store information about their direct flights,
    Store information about their customers,
    Store information about tickets being booked.

When a customer books a flight (buys a ticket) the following rules apply:

    Customers can only book one direct flight at a time. This means that to book a round trip, a customer will have to make two distinct bookings (buy two tickets).
    All bookings are for direct flights only. If a customer wants to embark on an “indirect flight” (e.g. London – Sydney via Singapore) they will have to book two or more direct flights separately. (e.g. Book a ticket for a direct flight between London and Singapore and another ticket for a direct flight between Singapore and Sydney.)
    There is no option for group bookings: When a family or a group (2 or more customers) want to fly together, each member of the family/group will have to make a separate booking for their flight.

The database will be used to find out all the key information about the different flights such as:

    Date & Time of the flight,
    Departure Airport,
    Destination Airport,
    Aircraft being used for this flight,
    List of all passengers expected on board,
    List of all members of staff expected on board.

Entity Relationship Diagram

Your task is to use our online ERD tool to design the Entity Relationship Diagram for this airline company. Note that you have not been given any information about the pieces of information to store about the customers, employees, planes, airports, flights and bookings so you will have to decide of the most relevant fields to include on your ERD.

Design Your ERD Online
paper-plane

unlock-access

Solution...

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


Tagged with: , , ,

web2.0 – Entity Relationship Diagrams (ERD)

client-side-scriptingHave you ever created a website using HTML, CSS and JavaScript? These three languages are client-side languages which run on your computer through the web-browser and are used to create static websites. HTML is used to add content to a webpage (text, headings, pictures etc.). CSS is used to update the look & feel and the layout of the page and JavaScript is often used to make the page more interactive.

In the early days of the Web (back in the 90s), the World Wide Web was only made of static websites. The pages of such websites were always displaynig the same content.

Web 2.0

server-side-scripting
Progressively, the internet evolved to more dynamic websites. We call this web2.0.

Web2.0 Websites such as Wikipedia, Twitter, Facebook, Amazon, Vinted etc. are database-driven dynamic websites. Their content changes constantly based on the user interactions with the site. They rely on storing information on back-end (server-side) relational databases. They use server-side technologies/languages such as PHP, ASP, .Net, C#, Python to process user requests and access or update data stored in the database.

Your Task…

In this challenge we are going to investigate the design of the relational databases that could be used by popular web2.0 websites such as:

  • E-commerce websites (e.g. Amazon)
  • Social networks (e.g. Twitter)
  • Auction Websites (e.g. eBay)

Our aim will be to try to recreate the Entity Relationship Diagrams (ERD) used by these types of websites. Your task is to use the following tabs to find out about the main characteristics of these different types of dynamic websites to then attempt to draw a relevant ERD for each type of websites.

Client/Server TechnologiesE-Commerce WebsitesAuction WebsitesTwitter vs Facebook

The following diagram summarises some of the key concepts of client-server web-based applications:

client-server-technologies


Most e-commerce websites use a similar database structure (ERD) in order to:

  • Store information about the products they sell,
  • Organise products into categories,
  • Store information about customers,
  • Store information about orders being raised by customers.
  • Store information about reviews left by customers on different products.

Use our online tool to design an ERD for a relational database that would meet the above requirements:
Design Your ERD Online


Most auction websites such as eBay use a similar database structure (ERD) in order to:

  • Store information about the members which can be buyers or sellers,
  • Store information about the products being sold,
  • Store information the bids being raised,
  • Store information about reviews left by buyers on different sellers.

Use our online tool to design an ERD for a relational database that would meet the above requirements:
Design Your ERD Online


We will now investigate the ERDs to be used to replicate the key functionalities of social networks like Facebook and Twitter. We will base our social network on a relational database to:

  • Store information about members,
  • Store information about friendship for Facebook (who is friend with who?) or Follows for Twitter (who follows who?),
  • Store information about posts/tweets,
  • Store information about likes (on posts/tweets).

Use our online tool to design an ERD for a relational database that would meet the above requirements:
Design Your ERD Online

Tagged with: , , ,

Rent-A-Bike – Entity Relationship Diagram (ERD)

rent-a-bikeRent-a-Bike is a company offering a bike rental service in the London area.​ They would like a new IT system to replace their existing booking system.​ You have been asked to investigate their requirements in order to design the Entity Relationship Diagram (ERD) for this system.

For more information about relational databases and Entity Relationship Diagrams, visit the following pages:


The requirements of Rent-A-Bike are as follows:

  • The new system will need to store information about the customers, the bikes, the bookings and the payments made by customers.​
  • In most cases, a customer will book a single bike at a time, however Rent-a-Bike also allows for group bookings. This means that a customer can rent several bikes as part of a single booking,
  • Each booking can be paid in either one or several payments/instalments.

Use our online ERD tool to design the Entity Relationship Diagram for this bike rental company. Note that you have not been given any information about the pieces of information to store about the customers, bikes, bookings or payments so you will have to decide of the most relevant fields to include on your ERD.

Design Your ERD Online

Extension Task

Rent-A-Bike is considering opening new branches to cover all the main UK cities including, but not limited to, Manchester, Leeds, Edinburgh, Glasgow, Cardiff, Belfast, Bristol, Birmingham and Cambridge.

They would like their IT system to store information of each one of their branches/rental shops. Each bike will be allocated to a shop/branch.

The system should also store information about all the employees working at a specific branch. When a booking is made, it will be assigned to an employee who will be in charge of preparing and collecting the bikes and chasing payments from customers.

Your task is to update your ERD to take into consideration these new customer requirements.

bicycle

unlock-access

Solution...

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


Tagged with: , , ,

Driving School – Entity Relationship Diagram (ERD)

learn-2-driveLearn2Drive is a driving school that consists of 5 driving instructors. They would like a new IT system that will be used to store information about their instructors, learners, lessons and vehicles used by the instructors.

When creating such a database driven system, one of the key tasks is to identify what data will need to be stored in the database to then design an ERD: Entity Relationship Diagram.

To do so, a business analyst will first collect lots of information about how the business works and about their requirements. This information will be used to:

  1. Identify the main entities for which the system will store data,
  2. For each entity, identify the pieces of data (fields) that will need to be stored on the system,
  3. Identify whether there are relationships between the different entities,
  4. Use primary keys, foreign keys and link tables when necessary to implement these relationships.

You can find out more about these key relational database concepts on this page.

road-signs-banner

Scenario

Here is the main information that should help you design the first draft of the ERD for the Learn2Drive new IT system:

  • L2D is a driving school consisting of 5 instructors. Each instructor gives driving lessons in their own car which is insured by the school.
  • Students can book lessons with any of the 5 instructors.
  • Most students will need around 30 lessons before passing their driving license.

Use our online ERD tool to design the Entity Relationship Diagram for this driving school. Note that you have not been given any information about the pieces of information to store about learners, instructors, cars and lessons so you will have to decide of the most relevant fields to include on your ERD.

Design Your ERD Online

Solution

You can view/edit our ERD for this scenario:
View Solution!

Extension Task

The driving school would like to use their system to record the attendance of learners to theory lessons. These theory lessons will take place every Monday and Wednesday at the driving school’s main office which can accommodate up to 20 students per lesson. Each lesson will be delivered to the group of students by one instructor.

Your task is to update your ERD to take into consideration these new requirements.

unlock-access

Solution...

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


Tagged with: , ,