Mandatory Assignment 1, INF 4130, 2017

Similar documents
Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Recitation: Loop Jul 7, 2008

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

Object Oriented Programming. Java-Lecture 6 - Arrays

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CSCI 1103: File I/O, Scanner, PrintWriter

JAVA PROGRAMMING (340)

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Reading Text Files. 1 Reading from text files: Scanner

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Mandatory Exercise 1 INF3110

CSci 1103 Final. Name: Student ID:

Files & Exception Handling. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Introduction to Java Unit 1. Using BlueJ to Write Programs

Fundamentals of Programming Data Types & Methods

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Reading Input from Text File

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Java Applications

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Files. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS159 Midterm #1 Review

Worksheet 3: Predictive Text Entry

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

You must bring your ID to the exam.

COSC 2011: Assignment 1 (v.4)

AP Computer Science Unit 1. Writing Programs Using BlueJ

CIS November 14, 2017

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Arrays. Eng. Mohammed Abdualal

In this lab we will practice creating, throwing and handling exceptions.

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Input/Output (I/0) What is File I/O? Writing to a File. Writing to Standard Output. CS111 Computer Programming

Introduction to Software Development (ISD) Week 3

CSCI 1103: File I/O, Scanner, PrintWriter

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Introduction to the Java Basics: Control Flow Statements

CIS October 16, 2018

Input-Output and Exception Handling

Exercise. Write a program which allows the user to enter the math grades one by one (-1 to exit), and outputs a histogram.

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

CS2 Practical 1 CS2A 22/09/2004

F I N A L E X A M I N A T I O N

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Simple Java Reference

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Midterm Examination (MTA)

2.8. Decision Making: Equality and Relational Operators

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

STUDENT LESSON A12 Iterations

COMP 110 Project 1 Programming Project Warm-Up Exercise

C17a: Exception and Text File I/O

CS165 Practice Final Exam Answer Key

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Principles of Computer Science

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

Full file at

Full file at

Cleveland State University. CIS260 Lecture Notes Feb 22 Chapter 6 Iterations Files (updated Wed. Mar 3) V. Matos

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Computer Science 1 Ah

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

CS 2223 B15 Term. Homework 1 (100 pts.)

Exceptions Handeling

1 Looping Constructs (4 minutes, 2 points)

Object-Oriented Programming in Java

CS171 Final Practice Exam

Assignment 17 Deadline: Oct pm COSC211 CRN15376 Session 14 (Oct. 29)

CS159. Nathan Sprague

EXCEPTIONS. Fundamentals of Computer Science I

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

When we reach the line "z = x / y" the program crashes with the message:

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

MSc/ICY Software Workshop Exception Handling, Assertions Scanner, Patterns File Input/Output

CS165 Practice Final Exam

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Chapter 7. Iteration. 7.1 Multiple assignment

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Array. Prepared By - Rifat Shahriyar

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

MACS 261J Final Exam. Question: Total Points: Score:

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CS141 Programming Assignment #6

AP Computer Science Unit 1. Programs

Supplementary Test 1

Transcription:

Mandatory Assignment 1, INF 4130, 2017 Deadline: Monday October 2. First: Read the general requirements for mandatory assignments at the department here: Norwegian: https://www.uio.no/studier/admin/obligatoriske-aktiviteter/mn-ifi-obliger-retningslinjer.html English: http://www.uio.no/english/studies/admin/compulsory-activities/mn-ifi-guidelines.html Comments concerning this mandatory assignment: This assignment consists of two exercises, and each student must turn in his/her own, independent solution to both exercises. Programs must be written in Java. Comment your code properly! (Javadoc is an easy and good way to comment Java code, but is not required.) Further specifications for handing in your solution are given at the end of this document. Your programs will be tested automatically, before a more thorough check is done. It is therefore very important that your output is exactly as specified in the text. If further comments or corrections to this assignment is necessary, they will appear as a message on the course page. Check regularly. Exercise 1 (Dynamic Programming) The problem Sum of Selections (SOS) is defined as follows: Given a sequence of n positive integers t 1, t 2,..., t n and a positive integer K, we are asked to make a selection of the t i s with sum exactly K (or determine that no such selection exists). Example: For n = 5, let the integers be 2, 5, 6, 3, 10 and K = 8. For this instance of the problem we can select 2 and 6, which sums to 8. If we only want a YES/NO answer, the answer is YES for this instance. In several of the questions below we are, however, interested in the selection itself, not merely a YES or NO. The exercise is divided into multiple parts to illustrate dynamic programming, with and without memoization, to show the differences between the two methods, and how they are both based on the same recursive formula. 1

a) During execution the sum of different selections can assume values in the interval [0, S] where S is the sum of all the t i s.to find out, using dynamic programming, whether a certain instance is solvable, you can use a boolean array U with dimension [n] [S]. Explain what you want the values of this table to mean. Give the recursive formula you base your algorithm on, and explain briefly why it is correct. Explain where the answer will occur. b) Implement a standard bottom up dynamic programming algorithm that solves SOS. Explain how you initialise your table. Use U as the variable name for the array in your code. Your program should answer YES or NO depending on whether a selection with the correct sum is possible or not, and for YES-instances also output the selection itself (see output example below). c) If we are only interested in a YES or NO answer, not the actual selection, we can limit the space requirements, and solve b) with space O(n) by using a single row of the table from a) (still with boolean values!). Explain how. d) Write a recursive, memoized algorithm (top-down) using the formula from a) above to solve SOS. Use an array U with dimensions as described above in a). Output should be as described for b). e) For an implementation as described in c) we now also want to output the selection, if it exists, still using a one-dimensional array. Explain how this can be implemented. (Hint: Try with something other than boolean values in the array.) Input The program is to read its input from a designated file. The filename is given to the program, together with the name of an output file, as commend line arguments. The input file contains a series of instances, one on each line; each instance consists of the number n, then the number K, and finally the t i s (all integers, separated by blanks). An input file with two instances can look like this: 5 14 1 9 6 3 10 5 4 2 5 6 3 10 Output For each instance in the input file the output should contain: A line starting with INSTANCE: followed by the numbers: n K t 1 t n A new line with either YES or NO depending on whether a selection with sum K could be made or not, If YES, yet another line starting with: SELECTION: followed by the selection given as a sequence of n pairs (<t i >,<1 or 0, depending on whether t i is in the selection or not>). ll of this is to be written to file given as the second argument to the program. Only one solution is to be written for each instance. If more than one solution is possible, 2

choose one. There should be one blank line between the two or three lines for each instance. Output for the instances in the input example above should be: INSTANCE: 5 14 1 9 6 3 10 YES SELECTION: (1,1)(9,0)(6,0)(3,1)(10,1) INSTANCE: 5 4 2 5 6 3 10 NO Implementation tips When reading from and writing to files, the classes java.util.scanner and java.io.printwriter can be used. In the following example an interger is read from an input file, and the string <input-number> * 2 = <number times two> is written to an output file: public static void main(string[] args) throws FileNotFoundException { Scanner in = new Scanner(new File(args[0])); int input; try { input = in.nextint(); // see documentation for java.util.scanner for more examples finally { in.close(); int output = input * 2; PrintWriter out = new PrintWriter(new File(args[1])); try { out.printf("%d * 2 = %d\n", input, output); // or out.println finally { out.close(); To avoid passing all data needed by the recursive method as parameters to every call, you should store much of it as attributes in a suitable object, and make the method local to the class of that object. The array containing the values of solutions to subproblems should be one such attribute. This object is easily set up by a main method that also starts the recursion. Test data is easy to produce, but there will also be a link on the course web page to a file containing a test set. 3

Exercise 2 (Tries) Tries and suffix-trees are described in chapter 20 of the textbook (B&P). Write a program that builds an uncompressed trie from a collection C of strings, and then searches this trie for given strings. For simplicity we assume the strings consist of only lower case letters. Repeated insertions of already existing strings are accepted. You are free to choose how to implement access from a node to its subnodes. Usually the correct subnode is accessed (or created) from the next symbol in the input string, and the subtrees accessed in alphabetical order. Space and time complexity of your implementation is not overly important, BUT explain the pros and cons of your implementation briefly. Input The program is to read its input from a designated file. The filename is given to the program, together with the name of an output file, as command line arguments. The input file contains an integer N and a set of strings (words) separated by newlines. The first N strings are to be inserted into the trie, the remaining strings are to be looked up in the finished data structure. Output Output is again written to the file given as the second argument to the program. For each of the first N strings, the string itself and the data structure as it looks after insertion of the string is to be written. Output the data structure in prefix format with parentheses around each subtree, but not around single letters in long paths. List the subtrees in alphabetical order. If the first three strings we insert into our tree are internet, interview, and inter, the output should look like this: internet (internet) interview (inter(net)(view)) inter (inter(net)(view)) Likewise, if the first three strings we insert are inter inter internet, the output should look like this: inter (inter) inter (inter) internet (inter(net)) A prefix representation of the trie in figure 20.7 in B&P looks like this: (al(gorithm)(l))(inter(n(ally)(et))(view))(w(eb)(orld)) 4

For the remaining strings (after the first N ones), the string itself, and the result of the lookup in the data structure built with the first N strings is to be written, like this: internet YES interney NO Note that a string that only exits as a prefix, not as separately inserted word, like al in the trie in figure 20.7 should give a negative answer. Implementation tips See Exercise 1 above for I/O-handling in Java. Test data should be easy to write for this exercise too, but there is a link to a file containing a simple set on the course web page. It will be helpful to add an attribute in nodes with information that a word ends. Another possibility can be to add a child node with some character with signals end of a word. Test-data and how to handing it all in Details about this will occur on the INF4130 course page within a few days. [end] 5