CS S-17 Recursion IV 1. ArrayLists give some extra functionality to arrays (automatic resizing, code for inserting, etc)

Similar documents
CS 2102 Exam 2 B-Term 2013

Hash Table. Ric Glassey

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

RECURSION. Prof. Chris Jermaine

CS1020 Data Structures and Algorithms I Lecture Note #15. Hashing. For efficient look-up in a table

Collections and Maps

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Fall 2017 Mentoring 7: October 9, Abstract Data Types

More sophisticated behaviour Lecture 09

CS S-24 Final Review I 1


CS 314 Exam 2 Spring

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

University of Maryland College Park Dept of Computer Science

Algorithms. Produced by. Eamonn de Leastar

11/27/12. CS202 Fall 2012 Lecture 11/15. Hashing. What: WiCS CS Courses: Inside Scoop When: Monday, Nov 19th from 5-7pm Where: SEO 1000

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )

(Refer Slide Time: 01.26)

CONTAİNERS COLLECTİONS

CS205: Scalable Software Systems

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

MapReduce Algorithm Design

MIT AITI Lecture 18 Collections - Part 1

Introduction to Computer Science II CS S-16 Recursion III

Search and Optimization

Sets and Maps. Part of the Collections Framework

Lecture #39. Initial grading run Wednesday. GUI files up soon. Today: Dynamic programming and memoization.

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

(Refer Slide Time: 00:51)

Collections Framework: Part 2

EXAMINATIONS 2017 TRIMESTER 2

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Arrays. myints = new int[15];

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

CS61B Lecture #35. [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees.

Finding optimal configurations Adversarial search

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

INTERFACES IN JAVA. Prof. Chris Jermaine

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

Midterm Exam 2 CS 455, Fall 2014

The Array Tools Lab. Introduction In this laboratory, you will learn how to construct functions that operate on Java arrays of type

CSE 143 Au03 Final Exam Page 1 of 15

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS61BL: Data Structures & Programming Methodology Summer 2014

Sets and Maps. Sets Maps The Comparator Interface Sets and Maps in Java Collections API. Review for Exam Reading:

(Provisional) Lecture 32: Estimated Value, Minimax, Functional Data 10:00 AM, Nov 20, 2017

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

ESC/Java2 extended static checking for Java Erik Poll Radboud University Nijmegen

1. Answer as shown or -2 unless question allows partial credit. No points off for differences in spacing, capitalization, commas, and braces

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Java Fundamentals. Problem solving. Rudiments of Java. Backus Naur Form (BNF) NIM

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CS 106A, Lecture 20 ArrayLists and HashMaps

Model Solutions. COMP 103: Mid-term Test. 21st of August, 2014

CS 161 Fall 2015 Final Exam

EXAMINATIONS 2016 TRIMESTER 2

Practice Midterm 1. Problem Points Score TOTAL 50

Garbage Collection (1)

CS 170, Section /3/2009 CS170, Section 000, Fall

CSE 214 Computer Science II Searching

Tables and Priority Queues

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

Lecture 11: Recursion (hard)

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

ESC/Java extended static checking for Java Erik Poll, Joe Kiniry, David Cok University of Nijmegen; Eastman Kodak Company

Tables and Priority Queues!

CP222 Computer Science II. Searching and Sorting

CS61B Lecture #2. Public Service Announcements:

APCS Semester #1 Final Exam Practice Problems

Computer Science 1 Ah

CS 314 Midterm 2 Fall 2012

Test 1: CompSci Exam 1 Solutions

Midterm Exam 2 CS 455, Spring 2015

CSC 1351: Quiz 6: Sort and Search

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Iteration Abstraction

Chapter 8 Algorithms 1

Homework. Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

Lecture 6: ArrayList Implementation

9/16/2010 CS Ananda Gunawardena

Programming Problems 22nd Annual Computer Science Programming Contest

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList

CS Ananda Gunawardena

Intro to Computer Science II

APHID: Asynchronous Parallel Game-Tree Search

Project 1. due date Sunday July 8, 2018, 12:00 noon

COMP 103 : Test. 2018, Sept 12//(Corrected)

CS 151 Name Final Exam December 17, 2014

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

CS111: PROGRAMMING LANGUAGE II

Hashing. Reading: L&C 17.1, 17.3 Eck Programming Course CL I

Transcription:

CS112-2012S-17 Recursion IV 1 17-0: HashMap Arrays allow us to store elements in a list, using ints to reference locations ArrayLists give some extra functionality to arrays (automatic resizing, code for inserting, etc) Be nice to have a data struture that used Strings (or any arbitrary object) to reference locations Conceptually, soundsmade["cat"] = "meow" 17-1: HashMap HashMap<String, String> hm = new HashMap<String,String>(); Creates a new HashMap key are Strings, values are Strings Can add key / value pairs Can get the value associated with a key Can check if a key is in the hashmap 17-2: HashMap HashMap<String, String> sounds = new HashMap<String, String>(); sounds.put("cat", "meow"); sounds.put("dog", "bark"); sounds.put("cow", "moo"); System.out.println(sounds.get("cat")); System.out.println(sounds.get("dog")); System.out.println(sounds.get("cow")); 17-3: HashMap boolean containskey(object key) boolean containsvalue(object value) V get(object key) isempty() V put(k key, V value) V remove(object key) int size() 17-4: Recursion imum What is a really easy (small!) version of the problem, that I could solve immediately? (Base case) How can I make the problem smaller? Assuming that I could magically solve the smaller problem, how could I use that solution to solve the original problem (Recursive Case) 17-5: Recursion imum Write a recursive function that returns the smallest value in the first size elements of an array of Comparable objects

CS112-2012S-17 Recursion IV 2 int minimum(int A[], int size) 17-6: Recursion int minimum(int A[], int size) return null; if (size == 1) return A[0]; int smallest = minimum(a, size - 1); if (smallest < A[size - 1]) return smallest; return A[size - 1]; 17-7: Recursion imum Write a tail-recursive function that returns the smallest value in the first n elements of an array of Comparable objects int minimum(int A[], int size, int smallest) 17-8: Recursion int minimum(int A[], int size, int smallest) return smallest; if (smallest < A[size-1]) return minimum(a, size - 1, smallest); return minimum(a, size - 1, A[n-1]); int minimum(int A[]) return minimum(a, A.length, Integer.MAX_VALUE); 17-9: Problems... Some of the problems from this lecture are taken from javabat.com Really nice way to practice Java programming, check it out! Especially good for studying for final! 17-10: Recursion Group Sum Input: An array of integers, and a target sum Output: true of a subset of the integers add up to the sum, false otherwise Examples: [3, 5, 7, 11, 13], 15 == true [3, 5, 7, 11, 13], 9 == false [3, 5, 7, 11, 13], 23 == true [3, 5, 7, 11, 13], 40 == false 17-11: Recursion Group Sum Add an extra paramter: Number of elements in array to consider (much like minimum, above)

CS112-2012S-17 Recursion IV 3 boolean groupsum(int A[], int size, int target) 17-12: Recursion Group Sum boolean groupsum(int A[], int size, int target) return target == 0; if (groupsum(a, size - 1, target)) return true; return groupsum(a, size - 1, target - A[size - 1]); 17-13: Recursion Group Sum Second version: starting index rather than ending index Show on codebat int groupsum(int start, int A[], int target) 17-14: Recursion Group Sum boolean groupsum(int A[], int start, int target) if (start == A.length) return target == 0; if (groupsum(a, start + 1, target)) return true; return groupsum(a, start + 1, target - A[start]); 17-15: Recursion SplitArray Given a list of numbers, can it be split into 2 different sublists that sum to the same value See javabat (codebat) 17-16: Recursion SplitArray public boolean splitarray(int[] nums) return splithelper(nums, nums.length, 0); public boolean splithelper(int[] nums, int size, int excess) return excess == 0; return (splithelper(nums, size-1, excess + nums[size-1])) (splithelper(nums, size-1, excess - nums[size-1])); 17-17: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half H splits the board horizontally, selects one half V tries to minimize the final value, H tries to maximize the final value

CS112-2012S-17 Recursion IV 4 17-18: Two player games Board-Splitting Game We assume that both players are rational (make the best possible move) How can we determine who will win the game? 17-19: Two player games Board-Splitting Game We assume that both players are rational (make the best possible move) How can we determine who will win the game? Examine all possible games! 17-20: Two player games Vertical Horizontal Horizontal Vertical Vertical Vertical Vertical 14 12 5 13 15 16 3 1 11 19 4 7 10 6 8 2 17-21: Two player games 14 12 5 13 15 16 3 1 11 19 4 7 10 6 8 2

CS112-2012S-17 Recursion IV 5 17-22: Two player games 8 13 8 13 3 7 8 14 13 16 3 19 7 10 8 14 12 5 13 15 16 3 1 11 19 4 7 10 6 8 2 17-23: Two player games A computer could do this to figure out which move to make Examine all possible moves Examine all possible responses to each move... all the way to the last move Caclulate the value of each move (assuming opponent plays perfectly) 17-24: Two player games Could we do this for a real game? Checkers / Chess / Conenct-4 / etc 17-25: Two player games Could we do this for a real game? Checkers / Chess / Conenct-4 / etc No! Too many possible games! 17-26: Two player games What can we do instead? Create a board evauluation function Positive # s are good for one player, negative # s good for the other Checkers: # of red pieces - # of black pieces (Can also take position / piece value into account) Search a set number of spaces ahead, use the board evaluation function 17-27: Two player games Recursion (knew we d get there eventually...) Write two recursive functions int min(board B, int level)

CS112-2012S-17 Recursion IV 6 Returns the value of the current board, looking level moves ahead, assuming that the minimizer goes next int max(board B, int level) Returns the value of the current board, looking level moves ahead, assuming that the maximizer goes next 17-28: Two player games int min(board B, int level) What is the base case? 17-29: Two player games int min(board b, int level) if (level == 0) return b.evalfunction();... 17-30: Two player games int min(board b, int level) if (level == 0) return b.evalfunction(); best = Integer.MAX_VALUE; for each possible move n we could make b.domove(n); moveval = max(b, n - 1); if (moveval < best) best = moveval; b.undomove(n); return best; 17-31: Two player games int max(board b, int level) if (level == 0) return b.evalfunction(); best = - Integer.MIN_VALUE; for each possible move n we could make b.domove(n) moveval = max(b, n - 1); if (moveval > best) best = moveval; b.undomove(n); return best; 17-32: Problems... Go to codingbat.com Navigate all java - recursion2 Do groupsum6, groupsumclump If time, look at other problems (splitodd10 particularly interesting)