VNUHCM
Bài trước Bài kế tiếp
  • Nội dung học
  • Trợ giúp
    Bạn có thắc mắc khi đang học?
    Hướng dẫn cách học Những câu hỏi thường gặp Email cho giáo vụ
    • Tiếng Việt
    • English
    • Thông tin Thành viên
    • Khoá học đăng ký
    • Đăng xuất
  • Cohota
  • HƯỚNG DẪN HỌC TẬP

  • Xem chi tiết >>
    Bạn đã hoàn thành 0% khoá học
  • HƯỚNG DẪN SINH VIÊN ĐĂNG NHẬP HỆ THỐNG
    • Hướng dẫn đăng nhập
    • Hướng dẫn vào khóa học
  • Introduction
    • Welcome
  • Unit 1: Values
    • Introduction - Unit 1: Values
    • Get Started With Values
    • Play with Values
    • Playground Basics
    • Naming and Identifiers
    • Simulation
    • Strings
    • Constants and Variables
    • Word Games
    • Build a PhotoFrame App
    • Design for People
  • Episode 1: The TV Club
    • Introduction - Episode 1: The TV Club
    • Searching for Content
    • Sharing Personal Information
    • Ordering Online
    • Reflection: Episode 1
  • Unit 2: Algorithms
    • Introduction - Unit 2: Algorithms
    • Get Started with Algorithms
    • Play with Programs
    • Functions
    • Types
    • Parameters and Results
    • Making Decisions
    • BoogieBot
    • Data Visualization
    • Build a QuestionBot App
    • Design an Experience
  • Episode 2: The Viewing Party
    • Introduction - Episode 2: The Viewing Party
    • Accessing the Show
    • Streaming on the Network
    • Reflection: Episode 2
  • Unit 3: Organizing Data
    • Introduction - Unit 3: Organizing Data
    • Get Started with Organizing Data
    • Play with Complex Data
    • Instances, Methods, and Properties
    • Arrays and Loops
    • Structures
    • Enums and Switch
    • Testing Code
    • Processing Data
    • Pixel Art
    • Password Security
    • Visualization Revisited
    • Build a BouncyBall App
    • Design a Prototype
  • Episode 3: Sharing Photos
    • Introduction - Episode 3: Sharing Photos
    • Capturing Images
    • Posting on Social Media
    • Reflection: Episode 3
  • Unit 4: Building Apps
    • Introduction - Unit 4: Building Apps
    • Get Started with App Development
    • Play with App Components
    • Color Picker
    • ChatBot
    • Rock, Paper, Scissors
    • MemeMaker
    • Build an ElementQuiz App
    • Design for Impact
  • Appendix
    • Episode Technical Concepts
    • Glossary
Tổng quan điểm khóa học
Đánh giá

Tiến độ
Tên tiêu chí Trọng số (%) Điểm Tiến độ (%)
Unit 2: Algorithms

Get Started with Algorithms

Unit 2: Algorithms|Get Started

Algorithms

An algorithm is a set of instructions for accomplishing a task. In fact, you’ve been working with algorithms already in this course, even if you didn’t know it!

Explore this:

Try the shoelace challenge to see how good you are at giving clear, precise instructions.

Pair with someone wearing laced shoes.

First, write down all the steps involved in tying a shoelace. Then read your instructions out loud, and have your partner follow them.

  • Did you miss any steps?
  • Did your partner need you to clarify your instructions?

As you practice articulating algorithms to solve problems, you’ll naturally begin writing in pseudocode. Pseudocode is a mix of English and code, and it’s a good way to plan the steps of an algorithm before getting caught up in the intricacies of writing and debugging actual code.

There are three essential building blocks in all algorithms: sequencing, selection, and iteration. In Unit 1, you learned about sequencing (though you didn’t know it by that name). You’ll tackle selection in this unit, and you’ll learn about iteration in Unit 3.

Sequencing

For most tasks, the order of instructions is important (and often crucial). Think about a process like pitching a ball or executing a dance move. These tasks involve a series of ordered steps.

illustration of the poses for pitching a ball in baseball

Explore this:

Take photos and arrange them to illustrate a sequence of actions.

Your photos could show the sequence of poses in your signature dance, your favorite sporting event, a special beauty routine, or a ridiculous handshake.

Arranging instructions in order is called sequencing, and it’s one of the basic techniques for creating an algorithm. Put another way, an algorithm has a list (a sequence) of instructions, and the computer processes those instructions in order. Swift uses sequencing as it executes your code line by line and from top to bottom.

Selection

In sequencing, the steps in your algorithm progress one after the other. But what if you wanted to include choices, so that the sequence might flow in one direction or an entirely different direction? Think about how a flowchart works.

Image of a flowchart describing a choice of clothing based on the weather

At each decision point, the sequence travels one way or another based on a test that’s either true or false, such as “Is it raining?” This decision process is known as selection. Selection lets you describe multiple paths through your algorithm. By using selection, you can examine the conditions at the time your code runs (or the decisions the user makes), and your program will proceed along one of many possible paths.

Explore this:

Create a flowchart that helps you choose the best music to suit your mood.

What true/false questions would you ask to guide the user to the best album?

Try to include at least four different moods and matched music in your flowchart.

Functions

Think about the steps in getting dressed. When you were a toddler, you learned what “It’s time to get dressed!” meant. Now you probably use that phrase all the time, and you know all the steps involved and in what order those steps need to occur. The phrase “getting dressed” gives you a way to refer to the process of getting dressed without needing to communicate every single detail. You use the phrase as an abstraction for all the steps in the entire process.

In Swift, you use functions to group instructions together and give them a name. In some other programming languages, functions are referred to as procedures. By breaking a problem into pieces, you can write functions to solve each piece independently. Once you know all the functions work, you can combine them to solve the bigger problem. The approach of grouping instructions into a unit and giving that unit a name is called procedural abstraction. Remember, the key feature of any kind of abstraction—such as naming, from Unit 1—is to hide, or encapsulate, the details of how something works.

Explore this:

Imagine it’s your turn to cook dinner.

Write down all the steps involved in preparing, making, and serving the meal.

Now try grouping those steps into functions and giving each function a name, like “findRecipe” or “chopOnions” or “setTable.”

By grouping instructions into functions, you’ve hidden the details and made the overall process clearer.

After you’ve written and tested a function, you no longer have to think about how it works, just about what it does. You can then use the function at a higher level of abstraction to solve the problem at hand. Most programs use multiple levels of abstraction—functions that use other functions—to build ever higher procedural concepts. You could look at a complete program as the highest level of procedural abstraction.

Here’s an example.

  • A program to plan a potluck dinner
  • A module to send invitations to a list of friends
  • A procedure to compose an invitation
  • A procedure to send an email to a friend
  • A module to manage RSVPs
  • A procedure to scan an inbox for RSVP emails
  • A procedure to mark a person as "yes," "no," or "maybe"
  • A procedure to remind nonrespondents to RSVP
  • A module to manage a potluck menu
  • A procedure to add and remove items
  • A procedure to check whether all courses and beverages are represented

A program like the one described is highly modular, and the hallmark of modular code is good organization. With modular code, you can focus on discrete tasks and combine them to solve a problem. Modular code also makes for easy reuse. Once you’ve solved a particular problem, the code that solves it can be used whenever a similar situation arises. For example, if you were to write another program to organize birthday parties, you could reuse the code you wrote to send invitations to a list of friends, as well as the code to manage RSVPs.

Types

In real life, using the right kind of object is very important. If you’re playing tennis, it’s important to use a ball instead of an egg. Even using another kind of ball is against the rules—you have to use a tennis ball. On the other hand, having a dozen tennis balls is not very useful if you’re trying to make an omelette.

Different kinds of things have different properties and work in different ways. A tennis ball is filled with air and bounces if you drop it. An egg is filled with egg white and yolk and breaks if you drop it. Understanding the kind of thing you have helps you understand what you can do with it.

Explore this:

Imagine you are helping your young neighbor build a car with their construction set.

  • What are the different types of parts you’ll need for the project?
  • How would you describe the properties of each type?
  • What additional types would you need to add functional doors to your model?

Image of a pile of LEGO-like (but not obviously Lego!) building blocks of different types: standard bricks, wheels, axles, and axle connectors

In Swift, you work with many different values. Swift uses the term type to describe each different kind of value. You’ve already worked with two types in Swift: strings and numbers. You’ve seen that there are rules for each type—for example, you can only perform mathematical operations on values expressed as numbers. Just as in the car model, you can use many different types within the same program.

Parameters

Earlier, you compared “getting dressed” to a function, because it groups a number of instructions and gives them a name. But what if you were getting dressed for a soccer match or to go to a party? You need more information to help you choose the right outfit. In programming, you use parameters to define the input that goes into a function. So our getDressed function would become getDressed(for: soccer) or getDressed(for: party). The process is the same, but the output changes depending on the input.

Explore this:

Think back to the functions you created to cook dinner.

Now imagine you need to be more specific about the inputs based on who’s coming to dinner.

Think about how you’d rewrite your functions using parameters to accommodate the following information: Six people are coming; one is a vegetarian; and another one can’t eat dairy.

Each parameter for a function has a type. For example, you might have a function in your dinner plan to set the table. The number of places to set could be a parameter of that function, and that parameter has a type: a number. A different function, like selecting a recipe for the main course, would have different parameters, like being vegetarian or dairy-free. Neither of those parameters is best represented using a number—they could be strings or other types.

By adding parameters to functions, you can make them more flexible so they can be used in a variety of situations.

Making Decisions with Booleans

Remember when you explored selection earlier? One of the simplest ways to describe a choice is with a yes-or-no question. Questions with simple answers can be very powerful. In the game 20 questions, you try to guess an object—any object—just by asking questions that have a yes-or-no answer.

The yes-and-no answers to these kinds of questions are represented in Swift as “true” and “false.” They are known as Boolean values, named after George Boole, a 19th century mathematician who realized how important it is to ask clear questions with simple answers. The Bool type is used in Swift to represent Boolean values.

Explore this:

Imagine you’re programming a robot to navigate a maze independently, but the robot has a tendency to get stuck in corners.

The robot has sensors to detect collisions and can also tell how many seconds have elapsed between collisions.

  • How would you design an algorithm to get the robot out of a corner and back onto the correct path?
  • How would that algorithm rely on the Bool type?

Use pseudocode to outline your algorithm, and share it with a partner. Compare your algorithms to see whose is the most efficient.

Báo lỗi