320SC Python

Java Python Computer Science 320SC – (2024)
Programming Assignment 5
Due: Oct 13 2024 (11:59pm)
Academic Integrity
Before attempting to solve the assignment, please read the message below very carefully.
As described on https://academicintegrity.cs.auckland.ac.nz/, you must NOT
Use all or part of another student’s solution to the assignment. Changing variable names or
substituting words in a sentence does not make it your solution.
Allow someone else to complete all or part of the assignment for you.
Solicit answers for the assignment on contract-cheating websites such as Chegg.com and
Bartleby.com.
Use code from Internet sources such as StackOverffow or generative-AI tools such as ChatGPT.
You are encouraged to learn from Internet sources and tools, but you need to come up
with your own implementation of the code to show your learning.
Allow another student to copy all or part of your solution to the assignment.
Do all or part of an assignment for someone else.
Share code that can lead to the solution of an assignment.
Post the assignment anywhere online or share it with anyone else. The assignment material
is copyrighted and sharing or posting them online violates our copyright.
Post your solution online on public websites. Your online solutions will encourage other
students to copy your solution. Private GitHub repositories and other private online storage
drives are acceptable, and you can also share your solution privately with prospective
employers.
Reuse your own work unless discussed otherwise with the lecturer.
Leave your computers, devices, and belongings unattended — you must secure these at all
times to prevent anyone having access to your assessments or solutions.
Last year, out of 218 students, there were 11 misconducted cases found on A5 - Task 1. We kept the
submissions from the last few years to run MOSS at https://theory.stanford.edu/~aiken/moss/.
I hope that there will be no cases this year!
1Requirements
This 5th assignment lets you get familiar with dynamic programming design and development. It is
worth 5% of your total course marks. We would like you to implement efffcient dynamic programming
algorithms for two tasks: Task 1: Finding partner and Task 2: Killing enemies.
An excessive number of submissions (over 10) for a particular problem will accrue a 20% penalty per that
problem if you eventually solve it. Therefore, please write a bruteforce algorithm and test your
dynamic programming version with your own generated inputs at scale before submitting
to the automated marker.
We only accept Python programs that use built-in packages (i.e. packages that do not require pip
install).
1 Task 1: Meet your partner at skyscraper
1.1 Problem description
You are standing at the ground ffoor and your partner is waiting at the top ffoor of a skyscraper. You
will have to use an algorithmically designed lift L to reach your partner. The lift L is designed in the
manner that if you use the lift at the ffoor i, you are able to reach any ffoor from i + 1 to i + L[i] where
L[i] is a positive integer that presents the capacity of the lift at the i-th ffoor. Each time you use the
lift costs $1.
Assume that the skyscraper has n ffoors and you are at the ffoor 0. Your partner is at the ffoor n − 1
and waiting for you to see the sky view. The lift information L[i] for 0 ≤ i < n is available at the
ground ffoor. Write a function to return the minimum cost, i.e. the number of time using the lift, to
r 320SC、Python each your partner.
O(n) solutions are preferred since we have set the running time limit on the automarker.
1.2 Test case description
Your input will be a sequence of n integers, each value per line corresponding to the lift information
L[i] (e.g. the capacity of the lift) on the i-th ffoor. The ffrst line is for the 0-th ffoor. The last line is
for the (n − 1)-th ffoor, which is a redundant information :-). Your output will be an integer.
There are 4 test cases.
1. A trial test case of n = 10 has no mark.
2. A test case of n = 100, 000 and has 1 mark.
3. A test case of n = 1000, 000 and has 2 marks.
2Sample Input 1:
Sample Output 1:
1
You only need to use the lift once since L[0] = 8 is sufffcient to get you to the 4th ffoor.
Sample Input 2:
Sample Output 2:
2
You only need to use the lift twice. The ffrst one with L[0] = 2 to the 1st ffoor, and L[1] = 5 is sufffcient
to get you to the 4th ffoor.
32 Task 2: Arrange tanks to eliminate enemies
2.1 Problem description
You have a queue of n tanks hidden in a forest. Due to the UAV of enemies, only 1 tank is used per
day, and the used tank can only be taken from the front or rear of the queue for some security reasons.
Each tank has a number indicating the number of potential units the tank can eliminate. Since tanks
are hurrily queued up during the night, you cannot organize the tank in the good order to use. Instead,
you have a queue of n values, each reffects the number of potential eliminated units for each tank in
the queue.
Since the war is more and more severe, the number of potential eliminated units dramatically increases
day-by-day. Let the labels of the number of eliminated units from n tanks in the queue be t1,t2, . . . ,tn.
In the i-th day, the used tank k will eliminate i ∗ tk units.
As a commander, for each day, your task is to give an order 1 or 0 corresponding to whether the front
or the rear tank in the queue is used. Write a program to compute the best order of using n tanks for
n days to eliminate maximum number of enemies’ units.
Since there might be several orderings that output the same number of eliminated units, you would
need to output the maximum number of eliminated units only.
You might see that the best solution runs in O(n
2
) time asymptotically. However, a program with low
memory usage (e.g. O(n)) is preferred since the automarker has limited resources, and we have set the
running time limit on the automarker.
2.2 Test case description
Your input will be a sequence of n integers, each value per line i corresponding to the amount of eliminated
units of the tank ti
. The ffrst and last lines correspond to the front and rear tanks, respectively.
Your output is an integer in range [0..2
31
] corresponding to the maximum number of eliminated units.
There are 2 test cases.
1. A trial test case of n = 10 has no mark.
2. A test case of n = 10, 000 has 2 marks.
4Sample Input 1:
Sample Output 1:
128
The order is {1, 0, 0, 1, 1}, and the maximum number of detroyed units is 4 * 1 + 10 * 2 + 4 * 3 + 8 *
4 + 12 * 5 = 128. Note that for the last tank (#3), any order of 1 or 0 does not matter.
Sample Input 2:
Sample Output 2:
261
The maximum number of detroyed units is 261 and the order is {1, 1, 1, 0, 0, 0, 0, 1}. Note that for the
last tank (#4), any order of 1 or 0 does not matter.
Submission Procedure
Submit your program solutions to https://www.automarker.cs.auckland.ac.nz         

Python中,“sc表”这一术语并不常见,可能是指与Spark(`pyspark.sql`中的`SparkSession`或`SQLContext`)相关的“系统表”(System Catalog)或“元数据表”,通常使用`spark.catalog`进行操作。此外,“sc”也可能是`SparkContext`的缩写,它是Spark应用程序的核心入口点,用于与集群通信并管理作业执行。 以下是一些与Python中“sc”相关的内容及其操作方式: ### 获取SparkContext(sc) 在PySpark中,通常通过`SparkSession`获取`SparkContext`: ```python from pyspark.sql import SparkSession spark = SparkSession.builder.appName("example").getOrCreate() sc = spark.sparkContext ``` ### 查询Spark中的系统表(Catalog) Spark 2.0及以上版本提供了`spark.catalog`来查询数据库、表、函数等元数据信息: ```python # 列出所有数据库 spark.catalog.listDatabases() # 列出所有表 spark.catalog.listTables() # 列出所有函数 spark.catalog.listFunctions() ``` ### 示例:查询某个数据库下的所有表 ```python spark.catalog.setCurrentDatabase("default") tables = spark.catalog.listTables() for table in tables: print(table.name) ``` ### 获取表的元数据信息 可以使用SQL语句查询表的结构和属性: ```python spark.sql("DESCRIBE TABLE default.your_table_name").show() ``` ### 示例:获取所有表名并显示 ```python tables_df = spark.sql("SHOW TABLES IN default") tables_df.show() ``` ### 使用Spark SQL查询表数据 一旦表存在,可以使用`spark.sql()`执行SQL查询: ```python df = spark.sql("SELECT * FROM default.your_table_name") df.show() ``` ### 注册临时表并进行查询 可以将DataFrame注册为临时视图并进行SQL查询: ```python df.createOrReplaceTempView("temp_table") result = spark.sql("SELECT * FROM temp_table WHERE age > 30") result.show() ``` ### SparkContext常用操作 - **广播变量**: ```python broadcast_var = sc.broadcast([1, 2, 3]) print(broadcast_var.value) ``` - **累加器**: ```python accum = sc.accumulator(0) sc.parallelize([1, 2, 3, 4]).foreach(lambda x: accum.add(x)) print(accum.value) ``` - **读取文本文件**: ```python text_file = sc.textFile("README.md") counts = text_file.flatMap(lambda line: line.split(" ")) \ .map(lambda word: (word, 1)) \ .reduceByKey(lambda a, b: a + b) counts.collect() ``` 如果你所指的“sc表”是其他上下文(如数据库、配置表、系统表等),请提供更具体的定义或应用场景,以便给出更准确的解释和代码示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值