开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
UCSDGraphs
/
src
/
basicgraph
/
GraphGrader.java
UCSDGraphs
/
src
/
basicgraph
/
GraphGrader.java
GraphGrader.java 7.41 KB
一键复制 编辑 原始数据 按行查看 历史
Lu Ning 提交于 2016年04月18日 16:19 +08:00 . initial commit
package basicgraph;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import util.GraphLoader;
/**
* @author UCSD MOOC Development Team
* Grader for Module 1, Part 2
*/
public class GraphGrader {
private String feedback;
private int correct;
private static final int TESTS = 16;
/** Turn a list into a readable and printable string */
public static String printList(List<Integer> lst) {
String res = "";
for (int i : lst) {
res += i + "-";
}
// Last character will be a '-'
return res.substring(0, res.length() - 1);
}
/** Format readable feedback */
public static String printOutput(double score, String feedback) {
return "Score: " + score + "\nFeedback: " + feedback;
}
/** Format test number and description */
public static String appendFeedback(int num, String test) {
return "\n** Test #" + num + ": " + test + "...";
}
public static void main(String[] args) {
GraphGrader grader = new GraphGrader();
grader.run();
}
/** Run a test case on an adjacency list and adjacency matrix.
* @param i The graph number
* @param desc A description of the graph
* @param start The node to start from
* @param corr A list containing the correct answer
*/
public void runTest(int i, String desc, int start, List<Integer> corr) {
GraphAdjList lst = new GraphAdjList();
GraphAdjMatrix mat = new GraphAdjMatrix();
feedback += "\n\nGRAPH: " + desc;
feedback += appendFeedback(i * 2 - 1, "Testing adjacency list");
// Load the graph, get the user's answer, and compare with right answer
GraphLoader.loadGraph("data/graders/mod1/graph" + i + ".txt", lst);
List<Integer> result = lst.getDistance2(start);
judge(result, corr);
feedback += appendFeedback(i * 2, "Testing adjacency matrix");
GraphLoader.loadGraph("data/graders/mod1/graph" + i + ".txt", mat);
result = mat.getDistance2(start);
judge(result, corr);
}
/** Run a road map/airplane route test case.
* @param i The graph number
* @param file The file to read the correct answer from
* @param desc A description of the graph
* @param start The node to start from
* @param corr A list containing the correct answer
* @param type The type of graph to use
*/
public void runSpecialTest(int i, String file, String desc, int start, List<Integer> corr, String type) {
GraphAdjList lst = new GraphAdjList();
GraphAdjMatrix mat = new GraphAdjMatrix();
String prefix = "data/graders/mod1/";
feedback += "\n\n" + desc;
feedback += appendFeedback(i * 2 - 1, "Testing adjacency list");
// Different method calls for different graph types
if (type.equals("road")) {
GraphLoader.loadRoadMap(prefix + file, lst);
GraphLoader.loadRoadMap(prefix + file, mat);
} else if (type.equals("air")) {
GraphLoader.loadRoutes(prefix + file, lst);
GraphLoader.loadRoutes(prefix + file, mat);
}
List<Integer> result = lst.getDistance2(start);
judge(result, corr);
feedback += appendFeedback(i * 2, "Testing adjacency matrix");
result = mat.getDistance2(start);
judge(result, corr);
}
/** Compare the user's result with the right answer.
* @param result The list with the user's result
* @param corr The list with the correct answer
*/
public void judge(List<Integer> result, List<Integer> corr) {
// Correct answer if both lists contain the same elements
if (result == null) {
feedback += "FAILED. Result returned was NULL. ";
}
else if (result.size() != corr.size() || !result.containsAll(corr)) {
feedback += "FAILED. Expected " + printList(corr) + ", got " + printList(result) + ". ";
if (result.size() > corr.size()) {
feedback += "Make sure you aren't including vertices of distance 1. ";
}
if (result.size() < corr.size()) {
feedback += "Make sure you're exploring all possible paths. ";
}
} else {
feedback += "PASSED.";
correct++;
}
}
/** Read a correct answer from a file.
* @param file The file to read from
* @return A list containing the correct answer
*/
public ArrayList<Integer> readCorrect(String file) {
ArrayList<Integer> ret = new ArrayList<Integer>();
try {
BufferedReader br = new BufferedReader(new FileReader("data/graders/mod1/" + file));
String next;
while ((next = br.readLine()) != null) {
ret.add(Integer.parseInt(next));
}
} catch (Exception e) {
// shouldn't happen
feedback += "\nCould not open answer file! Please submit a bug report.";
}
return ret;
}
/** Run the grader */
public void run() {
feedback = "";
correct = 0;
ArrayList<Integer> correctAns;
try {
correctAns = new ArrayList<Integer>();
correctAns.add(7);
runTest(1, "Straight line (0->1->2->3->...)", 5, correctAns);
correctAns = new ArrayList<Integer>();
correctAns.add(4);
correctAns.add(6);
correctAns.add(6);
correctAns.add(8);
runTest(2, "Undirected straight line (0<->1<->2<->3<->...)", 6, correctAns);
correctAns = new ArrayList<Integer>();
for (int i = 0; i < 9; i++) {
correctAns.add(0);
}
runTest(3, "Star graph - 0 is connected in both directions to all nodes except itself (starting at 0)", 0, correctAns);
correctAns = new ArrayList<Integer>();
for (int i = 1; i < 10; i++)
correctAns.add(i);
runTest(4, "Star graph (starting at 5)", 5, correctAns);
correctAns = new ArrayList<Integer>();
for (int i = 6; i < 11; i++)
correctAns.add(i);
runTest(5, "Star graph - Each 'arm' consists of two undirected edges leading away from 0 (starting at 0)", 0, correctAns);
correctAns = new ArrayList<Integer>();
runTest(6, "Same graph as before (starting at 5)", 5, correctAns);
correctAns = readCorrect("ucsd.map.twoaway");
runSpecialTest(7, "ucsd.map", "UCSD MAP: Intersections around UCSD", 3, correctAns, "road");
correctAns = readCorrect("routesUA.dat.twoaway");
runSpecialTest(8, "routesUA.dat", "AIRLINE MAP: Airplane routes around the world", 6, correctAns, "air");
if (correct == TESTS)
feedback = "All tests passed. Great job!" + feedback;
else
feedback = "Some tests failed. Check your code for errors, then try again:" + feedback;
} catch (Exception e) {
feedback += "\nError during runtime: " + e;
e.printStackTrace();
}
System.out.println(printOutput((double)correct / TESTS, feedback));
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

暂无描述
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/luning/UCSDGraphs.git
git@gitee.com:luning/UCSDGraphs.git
luning
UCSDGraphs
UCSDGraphs
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /