编程作业二

作业链接:Deques and Randomized Queues & Checklist

我的代码:Deque.java & RandomizedQueue.java & Permutation.java

问题简介

Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary data structures using arrays and linked lists, and to introduce you to generics and iterators.

任务摘要

Dequeue. A double-ended queue or deque (pronounced “deck”) is a generalization of a stack and a queue that supports adding and removing items from either the front or the back of the data structure. Create a generic data type Deque that implements the following API:

public class Deque<Item> implements Iterable<Item> {
public Deque() // construct an empty deque
public boolean isEmpty() // is the deque empty?
public int size() // return the number of items on the deque
public void addFirst(Item item) // add the item to the front
public void addLast(Item item) // add the item to the end
public Item removeFirst() // remove and return the item from the front
public Item removeLast() // remove and return the item from the end
public Iterator<Item> iterator() // return an iterator over items in order from front to end
public static void main(String[] args) // unit testing (optional)
}

Randomized queue. A randomized queue is similar to a stack or queue, except that the item removed is chosen uniformly at random from items in the data structure. Create a generic data type RandomizedQueue that implements the following API:

public class RandomizedQueue<Item> implements Iterable<Item> {
public RandomizedQueue() // construct an empty randomized queue
public boolean isEmpty() // is the randomized queue empty?
public int size() // return the number of items on the randomized queue
public void enqueue(Item item) // add the item
public Item dequeue() // remove and return a random item
public Item sample() // return a random item (but do not remove it)
public Iterator<Item> iterator() // return an independent iterator over items in random order
public static void main(String[] args) // unit testing (optional)
}

Iterator. Each iterator must return the items in uniformly random order. The order of two or more iterators to the same randomized queue must be mutually independent; each iterator must maintain its own random order.

Client. Write a client program Permutation.java that takes an integer k as a command-line argument; reads in a sequence of strings from standard input using StdIn.readString(); and prints exactly k of them, uniformly at random. Print each item from the sequence at most once.

详细的要求参见:specification

问题分析

第二次作业比较简单,主要想让我们用数组和链表实现基础的数据结构,介绍 Java 的泛型和迭代器,还可以参考 ResizingArrayStack.javaLinkedStack.java

任务一要求实现双端队列,即在队列的首尾都可以进行插入和删除操作。因为性能要求里要每次操作都保证在常数时间内完成,所以选择链表实现。确定了实现方式,不行再照着课程样例,建议加的哨兵节点也没什么问题。

任务二要求实现随机队列,随机出队,每个位置出去的概率一样,迭代器返回的顺序也要是随机的。Checklist 里的提示降低了很多难度:

What is meant by uniformly at random?

If there are n items in the randomized queue, then you should choose each one with probability 1/n, up to the randomness of StdRandom.uniform(), independent of past decisions. You can generate a pseudo-random integer between 0 and n−1 using StdRandom.uniform(n) from StdRandom.

Given an array, how can I rearrange the entries in random order?

Use StdRandom.shuffle()—it implements the Knuth shuffle discussed in lecture and runs in linear time. Note that depending on your implementation, you may not need to call this method.

所以这个随机队列用数组实现就比较方便,随机数组下标好操作,也符合作业的目的,一个用链表,一个用数组。而且,任务二性能要求空间部分,也保证我们用变长数组才能拿到满分,总之,课程设计还是很厉害的。于是,任务二也没什么问题。

任务三的测试程序,要求从文件里读入 N 个字符串,然后随机输出 K 个。直接把 N 个字符串压入任务二的随机队列,然后输出 K 个就行。但是,还有个额外的挑战:

For an extra challenge, use only one Deque or RandomizedQueue object of maximum size at most k.

挑战不能直接压入 N 个,队列里最多只能放 K 个字符串,大概是这次作业最难的部分了。当初是没有想出来,找到这篇博客才拿到了额外的分数。

测试结果

填坑,继续小号提交原来通过的代码。

Programming Assignment 2: Deques and Randomized Queues的更多相关文章

  1. Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)

    作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...

  2. Deques and Randomized Queues

    1. 题目重述 完成三个程序,分别是双向队列,随机队列,和随机队列读取文本并输出k个数. 2. 分析 2.1 双向队列 题目的性能要求是,操作时间O(1),内存占用最大48n+192byte. 当使用 ...

  3. 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 3.Programming Assignment : Planar data classification with a hidden layer

    Planar data classification with a hidden layer Welcome to the second programming exercise of the dee ...

  4. Algorithms: Design and Analysis, Part 1 - Programming Assignment #1

    自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming ass ...

  5. Algorithms : Programming Assignment 3: Pattern Recognition

    Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...

  6. Programming Assignment 2: Randomized Queues and Deques

    实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...

  7. AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue

    RandomizedQueue 有几个关键点: 1. 选择合适的数据结构,因为需要任意位置删除元素,Linked list 做不到,必须使用resizing arrays. 2. resizing 的 ...

  8. AlgorithmsI PA2: Randomized Queues and Deques Deque

    本次作业考察利用array 或者linked list 实现规定时间复杂度的queue 和stack, 不能使用java 自带的stack 和queue. 目的是让我们掌握自己设计的函数的复杂度. D ...

  9. AlgorithmsI PA2: Randomized Queues and Deques Subset

    本题的bonus是 因此方法是queue的size 达到了K, 就停止增加元素,保证queue.size() 最大时只有k. Java code: import edu.princeton.cs.al ...

随机推荐

  1. 一个实用的却被忽略的命名空间:Microsoft.VisualBasic

    当你看到这个命名空间的时候,别因为是VB的东西就匆忙关掉网页,那将会是您的损失,此命名空间中的资源最初目的是为了简化VB.NET开发而创建的,所以Microsoft.VisualBasic并不属于Sy ...

  2. elasticsearch-head的使用

    ealsticsearch只是后端提供各种api,那么怎么直观的使用它呢?elasticsearch-head将是一款专门针对于elasticsearch的客户端工具 elasticsearch-he ...

  3. js如何判断IE浏览器的版本包括IE11

    IE浏览器真是个坑:从ie6以及以前IE版本,简直就是垃圾,不按照Mozilla国际组织的标准来,乱搞.搞得兼容性很差:   <script type="text/javascript ...

  4. SSM配置JDBC错误: cquisition Attempt Failed!!!

    异常: 警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@20ffa401 -- Acquisition Attempt Fa ...

  5. Spring+SpringMVC+SpringDataJpa整合

    一.思路: (一) Dao层与Service层: applicationContext.xml. a) 数据库连接池 b) 整合jpa c) 配置@service文件扫描器. d) 配置事务管理管理器 ...

  6. 如何灵活利用免费开源图标字体-IcoMoon篇——张鑫旭

    一.温故知新 之前有专门介绍过如何使用类似fontforge软件制作自定义字符字体以及如何在web中实际应用. 不过,文中提到的是利用系统自带的一些特殊字体,如WINGDNG3.ttf字体. 显然,系 ...

  7. HTML5 Form Data 对象的使用

    HTML5 Form Data 对象的使用  MDN: https://developer.mozilla.org/zh-CN/docs/Web/Guide/Using_FormData_Object ...

  8. [js常用]将秒转化为时分秒

    内容引入至网络 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  9. 宝塔面板nginx配置安装Discuz

    Discuz!在Nginx下的Rewrite 需要说明的是,下网上以前一直流传的Rewrite都是有误的. 下面的Rewrite中百分号前面多了个转移字符"",这在Apache中是 ...

  10. task16 表格增减笔记

    trim()方法会创建一个字符串副本,删除前置及后缀所有空格,然后返回结果(中间的空格符无法消除) match()方法可在字符串内检索指定的值,找到一个或多个正则表达式的匹配 正则表达式 匹配中文:[ ...