跳至主要內容

COMP1012_Assignment 3

AI悦创原创2023年11月3日大约 7 分钟...约 1997 字

Task 1

Task 1: For a sequence a consisting of n (1n301 \leq n \leq 30) positive integers, it is beautiful if the following condition is held for every element (e.g., aia_i) of a: either ai=1a_i=1, or at least one of the numbers ai1a_i-1 and ai2a_i-2 exists in the sequence.

The prompt should be

"Please enter a sequence: "

The output should be formatted as either

"It is a beautiful sequence."

or

"It is not a beautiful sequence."

NOTE: you should strictly follow the prompt/output format to get full marks, and it is recommended to copy the prompt from OnlineGDB.

Example1

(The contents highlighted in blue are inputs from the user):

Please enter a sequence: 1

It is a beautiful sequence.

Example2

(The contents highlighted in blue are inputs from the user):

It is a beautiful sequence.

Example3

(The contents highlighted in blue are inputs from the user):

Please enter a string: 3 6 1

It is not a beautiful sequence.

Task 3

Get two n×nn \times n (1n301 \leq n \leq 30) square matrices a and b from the user, then calculate the result of a @ b. The elements are all integers, and @ means matrix multiplication. Each matrix will be given in one input, and we separate rows and columns by “; ” and “, ”, respectively.

The prompt should be:

"Please enter the first matrix: "

"Please enter the second matrix: "

The output prompt should be formatted as

"The result is:\n"

After which there should be n rows of outputs, each row contains n integers and adjacent elements should be separated by spaces.

NOTE: you should strictly follow the prompt/output format to get full marks, and it is recommended to copy the prompt from OnlineGDB.

Rules: You should implement the logic by yourself instead of calling functions built in other libraries (e.g., numpy). Otherwise you will get zero mark for this task even the result is correct.

Example

(The contents highlighted by blue are inputs from the user):

Please enter the first matrix: 1, 2; 3, 4

Please enter the second matrix: 2, 2; 3, 4

The result is:

8 10

18 22

Task 4

Task 4: Given a file FILE1, the content of the file is separated by spaces or “\n”. Please read the file and show all valid numbers, then give the sum of these numbers (preserve 4 digits after the decimal point). You should write the results to FILE2. Note that the files have been opened for you (as handles of fin and fout). You just have to write the code for reading and writing. Please do not reopen the files.

Words following one of the rules can be regarded as valid numbers:

  1. All characters are digits, e.g., “0”, “1012”;
  2. There is one and only one dot (decimal point), e.g., “3.14”;
  3. There is one and only one “+” or “-” at the beginning of the word, and the remaining part follows rule 1) or 2).

There is no need to consider other numbers like “0xFF”, “2.3e4”, and you should ignore the words partially composed of digits, e.g., “3D”.

The output should be formatted as

"The numbers are: {:s} ... {:s}, the sum is {:.4f}."

NOTE: you should strictly follow the prompt/output format to get full marks, and it is recommended to copy the prompt from OnlineGDB. The number of {:s} can be changed as appropriate. You have to use a '\n' to move to a new line when calling file.write(). It is different from the print() which adds a '\n' as the ending char by default.

Example1

Suppose the file contains:

This is a text file with 11 words and 10 spaces

The numbers are: 11 10, the sum is 21.0000.

Example2

Suppose the file contains:

The numbers are: 3.14159 6 3.14, the sum is 12.2816.

Code

Task 5: Given counting.csv in the Data folder, please write a Python program to estimate the counts 2 minutes and 30 seconds after each timestamp. Then, store the counts for every 2 minutes and 30 seconds in a csv file named results_T1.csv in the Results folder. Plot the counts for every 2 minutes and 30 seconds and save as a png file named result_plot_T1.png in the results folder.

For example, supposed we have:

TimeCounts
26/10/2023 7:35:00 AM6
26/10/2023 7:40:00 AM8
26/10/2023 7:45:00 AM10

We are going to estimate the counts at 26/10/2023 7:37:30 AM, 26/10/2023 7:42:30 AM, and 26/10/2023 7:47:30 AM, respectively, and store it as a csv file.

TimeCounts
26/10/2023 7:35:00 AM6
26/10/2023 7:37:30 AMEstimated counts
26/10/2023 7:40:00 AM8
26/10/2023 7:42:30 AMEstimated counts
26/10/2023 7:45:00 AM10
26/10/2023 7:47:30 AMEstimated counts

Requirements: Please use pandas to read and write the csv file.

Tips: You can use interpolation methods provided by NumPy or SciPy to estimate the counts. You can also try using linear regression in scikit-learn for the estimation (see the reference: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html and https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html).

公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQ

方法二:微信:Jiabcdefh

你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
通知
关于编程私教&加密文章