Lời mở đầu Các bạn sinh viên thân mến, hiện với xu hướng phát triển ngày càng mạnh về các lĩnh vực hệ thống thông minh trong đời sống. Trong đó, những hệ thống tự động về trí tuệ nhân tạo kết hợp với các kỹ thuật công nghệ mới đã cho ra đời nhiều sản phẩm thông minh. Những sản phẩm này thường là sự kết hợp giữa các nền tảng như IoTs, Cloud Computing, Machine Learning,… Với các nhà cung cấp dịch vụ như Google, Microsoft, IBM,… Đã hỗ trợ rất nhiều cho các nhà phát triển phần mềm triển khai nhanh chóng các sản phẩm thiết thực, đáp ứng những công việc trong cuộc sống hằng ngày. Giáo trình “Thực hành công nghệ mới trong phát triển ứng dụng” ra đời cũng muốn hỗ trợ cho các em sinh viên, đặc biệt là các em sinh viên ngành Khoa học máy tính tham gia học tập tại Bộ môn, nghiên cứu và phát triển các ứng dụng.
Trong quá trình biên soạn, tác giả cũng mong muốn các em sinh viên, đồng nghiệp đóng góp thêm các ý kiến nhằm hoàn thiện hơn cho giáo trình này. Xin chân thành cám ơn! 2 Mục lục Phần 1: Làm quen với ngôn ngữ Python. 4 Bài thực hành số 1. 4 Bài thực hành số 2.
6 Bài thực hành số 3. 8 Bài thực hành số 4. 9 Bài thực hành số 5. 11 Phần 2: Machine learning & Cloud Computing.
13 Bài thực hành số 1: Cài đặt môi trường triển khai ứng dụng machine-learning trên windows azure. 13 Bài thực hành số 2: Giới thiệu R và xử lý dữ liệu với Python. 15 Bài thực hành số 3: Phân tích dữ liệu trên AzureML. 18 Bài thực hành số 4: Thực thi công cụ R, nhúng Python trên AzureML.
21 Phần 3: Internet of Things (IoT). 1 Bài 1: Lập trình với Keypad & Led. 1 Bài 2: Lập trình với Led & Button. 25 Bài 3: Lập trình với Keypad & LCD.
31 Bài 4: Lập trình với Keypad, Buzz, Led. 36 Bài 5: Lập trình với Led và Wi-Fi. 42 3 Phần 1: Làm quen với ngôn ngữ Python Bài thực hành số 1. Nội dung: • Cài đặt chương trình soạn thảo đơn giản cho Python • Thực hành viết chương trình đơn giản trên Python • Thực hành các toán tử trong ngôn ngữ lập trình Python Yêu cầu: Công cụ soạn thảo Python Chi tiết: 1.
Write a program that asks the user about textbook prices and reports how overpriced the textbooks are. (You may wish to read this number from the user with the input command. You can round numbers with the round command.) You should match the following output - How much do you want to pay for each textbook? 50 - How much did the average textbook actually cost? 80 - How many books did you buy? 5 - Each book is overpriced by $30 ( 60% ) - You got ripped off by $150 total You may assume that the user enters a positive integer for each input value above. If you ever find yourself buying a house, you'll want to know what your monthly payment for the loan is going to be.
Write a complete program that asks for information about a loan and prints the monthly payment. The formula for computing monthly mortgage payments involves the loan amount, the total number of months involved (a value we call n) and the monthly interest rate (a value we call c). The payment formula is given by the following equation: 4 An example run of your program might produce the following output (user input is underlined): - This program computes monthly loan payments. - Loan amount? 275000 - Number of years? 30 - Interest rate? 6.75 - Your payment is $1783 Note that the numbers being read as input don't match the way they should be used in the formula.
The term of the loan is read as years instead of months. The interest rate percentage is being read in as a yearly rate instead of a monthly rate and as a whole number rather than a true percentage, so the value 6.75 should actually become 0.005625 by dividing it by 12 and by 100. You will have to make these conversions as part of your program. Write a program that counts a number's factors and determines whether the number is prime.
- What is your favorite number? 24 - 24 has 8 factors - 24 is not prime - What is your favorite number? 31 - 31 has 2 factors - 31 is prime Hint: To count the factors of some integer n, use a loop that tests every integer less than n to see whether n is divisible by it. (How do you know whether one integer is divisible by another?) 5 Bài thực hành số 2. Nội dung: • Thực hành các nhập xuất chuỗi, số trên Python • Thực hành các câu lệnh rẽ nhánh, lặp…trên Python Yêu cầu: Công cụ soạn thảo Python Chi tiết: You may not know that credit card numbers contain several pieces of information for performing validity tests. For example, Visa card numbers always begin with 4, and a valid Visa card number also passes a digit-sum test known as the Luhn checksum algorithm.
Luhn's algorithm states that if you sum the digits of the number in a certain way, the total sum must be a multiple of 10 for a valid Visa number. Systems that accept credit cards perform a Luhn test before contacting the credit card company for final verification. This lets the company block fake or mistyped credit card numbers. The algorithm for summing the digits is the following.
Consider each digit of the credit card to have a zero-based index: the first is at index 0, and the last is at index 15. Start from the rightmost digit and process each digit one at a time. For digits at even-numbered indexes (the 14th digit, 12th digit, etc.), simply add that digit to the cumulative sum. For digits at odd-numbered indexes (the 15th, 13th, etc), double the digit's value, then if that doubled value is less than 10, add it to the sum.
If the doubled number is 10 or greater, add each of its digits separately into the sum. The following pseudocode describes the Luhn algorithm to sum the digits: sum = 0. for each digit of credit card number, starting from right, if the digit's index is even, add the digit to sum. else, double the digit's value.
if the doubled value is less than 10, add the doubled value to sum. else, split the doubled value into its two digits. add the first digit to sum. add the second digit to sum.
6 4111111111111111 and 4408041254369873 are example credit card numbers that pass the Luhn algorithm. The following figure shows the algorithm summing the latter number in detail. Notice how digits at even indexes are doubled and potentially split into two digits if they exceed 10 when doubled. For example, the number 7 at index 8 which is doubled to 14 which split to make 1+4.
An example checksum using the Luhn algorithm. CC # 4408 0412 5436 9873 4 4 0 8 0 4 1 2 7 4 3 6 9 8 5 3 Scale *2 *2 *2 *2 *2 *2 *2 *2 -------------------------------------------------------------------- 8 4 0 8 0 4 2 2 14 4 6 6 18 8 10 3 Sum = 8 + 4 + 0 + 8 + 0 + 4 + 2 + 2 + 1+4 + 4 + 6 + 6 + 1+8 + 8 + 1+0 + 3 = 70 70 is divisible by 10, therefore this card number is valid. Write a program where the user can type in a credit card number and receive a message stating whether the number was valid. 7 Bài thực hành số 3.
Nội dung: • Thực hành các xử lý file (đọc, thống kê) trên Python • Thao tác trực tiếp File dữ liệu (ghi, ghi file cấu trúc) trên Python Yêu cầu: Công cụ soạn thảo Python Chi tiết: Suppose we have this hours.txt data: - 123 Suzy 9.5 Compute each worker's total hours and hours/day. - Assume each worker works exactly five days. - Suzy ID 123 worked 31.3 / day - Brad ID 456 worked 36.36 / day - Jenn ID 789 worked 39.9 / day input = open("hours.txt") for line in input: id, name, mon, tue, wed, thu, fri = line.split() # cumulative sum of this employee's hours hours = float(mon) + float(tue) + float(wed) + \ float(thu) + float(fri) print(name, "ID", id, "worked", \ hours, "hours: ", hours/5, "/ day" 8 Bài thực hành số 4. Nội dung: • Thực hành các hàm đồ họa cơ bản trên Python • Cách trình diễn các đối tượng đồ họa cơ bản Yêu cầu: Công cụ soạn thảo Python Chi tiết: Writing code below: from drawingpanel import * panel = DrawingPanel(400, 300) panel.set_background("yellow") panel.create_rectangle(100, 50, 200, 300) from drawingpanel import * panel = DrawingPanel(400, 300) panel.create_rectangle(100, 50, 200, 200, outline="red", fill="yellow") panel.create_oval(20, 10, 180, 70, fill="blue") from drawingpanel import * 9 panel = DrawingPanel(200, 200) panel.create_polygon(100, 50, 150, 0, 150, 100, fill="green") panel.create_line(10, 120, 20, 160, 30, 120, 40, 175) DrawingPanel panel = new DrawingPanel(200, 200); panel.LIGHT_GRAY); Graphics g = panel.fillRect(80, 40, 30, 20); 10 Bài thực hành số 5.
Nội dung: • Thực hành các hàm đồ họa trên Python • Sử dụng các cấu trúc đồ họa phức tạp Yêu cầu: Công cụ soạn thảo Python Chi tiết: 1. Write a program that draws the following figure: 2. Write a program that draws the following figure: Part of the challenge is using loops to reduce the redundancy of the figure. Write a program that draws the following figure: Part of the challenge is using loops to reduce the redundancy of the figure.
12 Phần 2: Machine learning & Cloud Computing Bài thực hành số 1: Cài đặt môi trường triển khai ứng dụng machine-learning trên windows azure. Nội dung: • Cài đặt được bộ free Microsoft Azure ML workspace • Cấu hình được Setup Azure ML workspace với existing Azure subscription • Cài đặt Python cho môi trường phát triển • Cài đăth công cụ R phần thống kê Yêu cầu: Chuẩn bị tài nguyên (phần mềm, internet, máy tính) Chi tiết: Đăng ký tài khoản free tại. • Microsoft Account (signup at https://signup.com) Cài đặt phần mềm R_Studio https://cran.com hoặc https://www.com/products/rstudio/download/ 13 Cài đặt Python từ https://www.io/downloads 14 Bài thực hành số 2: Giới thiệu R và xử lý dữ liệu với Python Nội dung: • Sử dụng được công cụ R vào ứng dụng thống kê trên dữ liệu • Dùng được các hàm thống kê trong Python Yêu cầu: Đã cài đặt các công cụ ở buổi 6 Chi tiết: Chạy Rstudio. Gõ Script # Generate a numeric series from 1 to 30 and assign it to variable x x <- seq(1, 30) # Create copy of x as variable y y <- x # Generate 30 uniform distributed random number each ranges between -1 to 1 noise <- runif(30, -1, 1) # Create variable ywnoise as in Excel ywnoise <- y + noise * 2 15 # Plot values of x and ywnoise without labels plot(x, ywnoise, xlab = NA, ylab = NA) # Combine columns x and ywnoise to create two column grid named linoise linoise <- cbind(x, ywnoise) # Save the variable linoise as a CSV file on the local disk write.csv(linoise, file = "linoise.names = FALSE) Kiểm tra output Chạy IDE Python 16 Gõ Script # Import required libraries to be used.
csv for csv file output, pyplot for plotting etc. import numpy as np import matplotlib.