UIL Computer Science Contest Advanced Content - PowerPoint PPT Presentation

Loading...

PPT – UIL Computer Science Contest Advanced Content PowerPoint presentation | free to download - id: 3e336-YzY0Y



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

UIL Computer Science Contest Advanced Content

Description:

auto boxing and unboxing. Doing programming problems. Computer Science - Advanced Content ... Computer Science - Advanced Content. Auto Boxing and Unboxing ... – PowerPoint PPT presentation

Number of Views:251
Avg rating:3.0/5.0
Slides: 47
Provided by: sco1
Learn more at: http://www.cs.utexas.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: UIL Computer Science Contest Advanced Content


1
UIL Computer Science Contest Advanced Content
  • Mike Scott
  • University of Texas at Austin

2
Agenda
  • A brief look at Computer Science
  • New topics for contest (Java 5.0)
  • enhanced for loop
  • Regular Expressions
  • printf
  • Scanner class
  • Generic Collections
  • new general data structures
  • auto boxing and unboxing
  • Doing programming problems

3
A Brief Look at Computer Science
  • The UIL CS contest emphasizes programming
  • Most introductory CS classes, both at the high
    school and college level, teach programming
  • and yet, computer science and computer
    programming are not the same thing!
  • So what is Computer Science?

4
What is Computer Science?
  • Poorly named in the first place.
  • It is not so much about the computer as it is
    about Computation.
  • Computer Science is more the study of managing
    and processing information than it is the study
    of computers. -Owen Astrachan, Duke University

5
So why Study Programming?
  • Generally the first thing that is studied in
    Chemistry is stoichiometry.
  • Why? It is a skill necessary in order to study
    more advanced topics in Chemistry
  • The same is true of programming and computer
    science.

6
  • What is the linking thread which gathers these
    disparate branches into a single discipline? it
    is the art of programming a computer. It is the
    art of designing efficient and elegant methods of
    getting a computer to solve problems, theoretical
    or practical, small or large, simple or
    complex. - C. A. R. Hoare
  • Sir Tony Hoare. Turing Award Winner. Inventor of
    the quicksort algorithm

7
  • Programming is unquestionably the central topic
    of computing. In addition to being important,
    programming is an enormously exciting
    intellectual activity. In its purest form, it is
    the systematic mastery of complexity. For some
    problems, the complexity is akin to that
    associated with designing a fine mechanical
    watch, i.e., discovering the best way to assemble
    a relatively small number of pieces into a
    harmonious and efficient mechanism. For other
    problems, the complexity is more akin to that
    associated with putting a man on the moon, i.e,
    managing a massive amount of detail. In
    addition to being important and intellectually
    challenging, programming is a great deal of fun.
    Programmers get to build things and see them
    work.. What could be more satisfying? - John
    V. Guttag, Professor at MIT research in AI,
    medical systems, wireless networking

8
Outcomes of Computer Science
9
Computer Science Skills
  • Yes, the number of pure programming jobs has
    declined
  • But, the total number of jobs in information
    technology as surpassed the previous peak from
    2000
  • Even as computer science students are being
    encouraged to take more courses outside their
    major, students in other disciplines are finding
    more often that they need to use, design and
    sometimes write computer programs. - Steve
    Lohr, NY Times, August 2005

10
New Topics for UIL
  • Newest version of Java is 5.0
  • A major release that added many new features,
    many of which make it easier to write programs
  • more features more flexibility greater
    complexity

11
The enhanced for loop
  • a.k.a. the for-each loop
  • alternative for iterating through a set of values
  • for(Type loop-variable set-expression)
  • statement
  • Set-expression is an array or a collection such
    as ArrayList
  • more generally, anything that implements the
    Iterable interface
  • Type must be the data type of the elements of the
    array or collection
  • logic error (not a syntax error) if try to modify
    an element in array via enhanced for loop

12
Enhanced for loop
public static int sumListOld(int list) int
total 0 for(int i 0 i lt list.length
i) total listi System.out.println(
listi ) return total
public static int sumListEnhanced(int
list) int total 0 for(int val
list) total val System.out.println( val
) return total
13
Enhanced for loop
int list 1, 2, 3, 4for( int val
list) val val 2 // logic error
System.out.println( val ) for(int val
list) System.out.print( val )
14
Regular Expressions
  • A regular expression (abbreviated as regexp,
    regex or regxp) is a string that describes or
    matches a set of strings, according to certain
    syntax rules. Regular expressions are used by
    many text editors and utilities to search and
    manipulate bodies of text based on certain
    patterns. Many programming languages support
    regular expressions for string manipulation.

15
Regular Expressions in Java
  • In Java the Pattern class represents regular
    expressions
  • Regular expressions are specified as Strings
  • Regular expressions used for the split method in
    the String class to parse the String based on the
    regular expression

16
Syntax of Regular Expressions
17
More Regular Expressions
  • traditional split questions
  • What if there were two spaces between each word
    and the regular expression was just \s?

String s Fairs got nothing to do with
it.String result s.split( \\s )//
regular expression for 1 or more white space
characters.// The split method breaks use the
regular expression as// delimiters. for(String
val result) System.out.println( val )
18
Another Regex Example
  • Given the regular expression ab
  • What is the output of the following code?

String s Someabrandombtextabout nothing
aaaabdoes itaaamakeabSense?String result
s.split( ab )for(String val
result) System.out.println( val )
19
Regular Expression Example
  • Given ab
  • Where do matches occur in
  • Someabrandombtextabout nothingaaaabdoes
    itaaamakeabSense?
  • Someabrandombtextabout nothingaaaabdoes
    itaaamakeabSense?
  • 5 matches

20
Regular Expression Example
  • Order matters
  • ab is not the same as ba
  • Given ba and the previous String
  • Someabrandombtextabout nothingaaaabdoes
    itaaamakeabSense?
  • Someabrandombtextabout nothingaaaabdoes
    itaaamakeabSense?
  • 12 matches

21
printf
From Cay Horstmans homepage. The march of
progress. 1980 C  printf("10.2f", x) 1988
C cout ltlt setw(10) ltlt setprecision(2) ltlt
showpoint ltlt x 1996 Java java.text.NumberFormat
formatter java.text.NumberFormat.getNumberIn
stance() formatter.setMinimumFractionDigits(2)
formatter.setMaximumFractionDigits(2) String s
formatter.format(x) for (int i s.length()
i lt 10 i) System.out.print('
') System.out.print(s) 2004
Java System.out.printf("10.2f", x)
22
printf details
  • public PrintStream printf(String format,
    Object... args)
  • format - A format string as described in Format
    string syntax. May contain fixed text and one or
    more embedded format specifiers
  • args - Arguments referenced by the format
    specifiers in the format string. If there are
    more arguments than format specifiers, the extra
    arguments are ignored. The number of arguments is
    variable and may be zero.

23
printf details Formatter class
  • The format specifiers for general, character, and
    numeric types have the following syntax
    argument_indexflagswidth.precisionconv
    ersion
  • The optional argument_index is a decimal integer
    indicating the position of the argument in the
    argument list. The first argument is referenced
    by "1", the second by "2", etc.
  • The optional flags is a set of characters that
    modify the output format. The set of valid flags
    depends on the conversion.
  • The optional width is a non-negative decimal
    integer indicating the minimum number of
    characters to be written to the output.
  • The optional precision is a non-negative decimal
    integer usually used to restrict the number of
    characters. The specific behavior depends on the
    conversion
  • The required conversion is a character indicating
    how the argument should be formatted. The set of
    valid conversions for a given argument depends on
    the argument's data type.

24
Conversion Types for Printf
25
printf Flags
26
printf Examples
double d 12.555 System.out.printf("8.2f",
d) System.out.printf(8.2f,
d) System.out.printf(8.2d, d) System.out.prin
tf(-8.2f, (int)d) System.out.printf(010.4f
, d) double gpas //code to init String
names //code to init for(int i 0 i lt
gpas.length i) System.out.printf(Name -s
GPA 5.2f, namesi, gpasi)
27
Scanner class
  • A new class to allow simple parsing of input.
  • Makes it easier to get input from System.in
  • Methods for getting nextLine, nextInt,
    nextDouble, next String
  • Set delimiters with regular expressions, default
    is whitespace

Scanner s new Scanner(System.in) System.out.p
rint("Enter your name ")String name
s.nextLine()System.out.print("Press Enter to
continue ")s.nextLine()
28
Hooking a Scanner up to a File
import java.util.Scanner import
java.io.File import java.io.IOException public
class ReadAndPrintScores public static void
main(String args) try Scanner s
new Scanner( new File("scores.dat") )
while( s.hasNextInt() ) System.out.println(
s.nextInt() ) catch(IOException
e) System.out.println( e )
12 35 12 12 45 12 12 13 57
scores.dat
29
Quick and Dirty Version
import java.util. import java.io. public
class ReadAndPrintScores public static void
main(String args) throws IOException
Scanner s new Scanner( new File("scores.dat")
) while( s.hasNextInt() ) System.out.println(
s.nextInt() )
30
Generic Classes
  • Java 5.0 introduces generics
  • Previously genericity was achieved through
    inheritance and polymorphism
  • every object in Java was an Object
  • Caused a lot of casting and uncertainty about
    whether data types were correct
  • UIL will focus on generic collections

31
Generic ArrayList
  • old ArrayListArrayList list new
    ArrayList()list.add(UIL)list.add(CS)Stri
    ng s (String)list.get(0)
  • new ArrayListArrayListltStringgt list new
    ArrayListltStringgt()list.add(UIL)list.add(CS
    )String s list.get(0)
  • Old style still works
  • raw collections that generate compile time
    warnings

32
New Data Structures Hash Table
  • Hash Table
  • no Java class
  • HashSet and HashMap use Hash Tables as the
    internal storage container to implement a Set and
    a Map
  • Hash Tables rely on fast access into arrays if
    index is known
  • Can achieve O(1) performance for add, access, and
    remove
  • challenge is handling collisions

33
New Data Structures Priority Queue
  • Priority Queue
  • like a Queue, but every item added has a priority
  • items get to move in front of other items in
    already present in Queue that have a lower
    priority
  • example how patients are seen in an emergency
    room
  • PriorityQueue class in the Java Standard Library

34
New Data Structures - Heap
  • A complete binary tree where every node has a
    value more extreme (greater or less) than or
    equal to the value of its parent
  • min and max heaps
  • example below is a min heap
  • min value at the root

5
12
7
32
9
45
13
37
55
50
35
Auto Boxing and Unboxing
  • In Java there is a big difference between the
    primitive variables and objects
  • Collections could only contain objects
  • Adding primitives was a painArrayList list
    new ArrayList()for(int i 0 i lt 10
    i) list.add( new Integer(i) )for(int i 0
    i lt list.size() i) System.out.println(
    ((Integer)list.get(i)).intValue() )
  • Primitives have to be boxed or wrapped into
    objects

36
Auto boxing and unboxing
  • The system now automatically boxes and unboxes
    primitives when adding to collections or other
    times an object is neededArrayListltIntegergt
    list new ArrayListltIntegergt()for(int i 0 i
    lt 10 i) list.add( i )for(int i 0 i lt
    list.size() i) System.out.println(
    list.get(i) 2 )// ORfor( int val list
    ) System.out.println( val 2 )

37
Programming Problems
  • Regional and State level only, for now
  • I am terrible artist
  • if I practiced I might get better
  • Programming problems
  • the only way to get better is to pratice
  • Sources of problems
  • UIL CS website. Old problems
  • online contests and judges

38
Keys to Success
  • Reading from an input file
  • setting up a Scanner
  • Hard problems -gt Design first
  • Test your solution against the sample data
  • Realize judges data is different and may test
    boundary cases you didnt consider

39
Example Problem 1
Brain Plan Program Name brain.java Input File
brain.in Researchers are developing non-invasive
devices that allow patients to control robotic
arms using their minds. These devices examine
the brainwave readings in patients to determine
what action the robotic arm should take. Before
the device can function, it needs to be
programmed to associate certain brainwave
patterns with robotic arm movements. To aid in
this effort, two test subjects have had their
brainwave readings taken when trying to get the
robotic arm to perform specific actions. Your
job is to generalize the sets of brainwave
readings into brainwave patterns that the device
can use for comparisons in upcoming trials.
Brainwave scans from the test subjects show only
active and inactive portions of the brain. From
these scans, a pattern can be deduced by
determining where the scans agree and where they
disagree. Areas of agreement indicate portions
of the pattern that should be active (or
inactive) while disagreements indicate portions
of the pattern that should be marked as
unimportant.
40
Example Problem 1
Brainwave scans are strings of 18 characters
where each character represents a portion of the
brain that is either 'A'Active or 'I'Inactive.
Patterns are also strings of 18 characters where
'A'Active, 'I'Inactive, and ''Unimportant. Fo
r instance, the following pair of brainwave
scans AAAAIIIIIIIIIAAIAI IIAAIIIIAIIIIAIIAI give
s rise to the pattern AAIIIIIIIIAIAI
41
Example Problem 1
Input The first line will contain a single
integer n indicating the number brainwave scans
pairs that need to have their patterns
calculated. Each pair of the next 2n lines will
contain brainwave scans for different
actions. Output For each brainwave scan pair in
the input, output the corresponding brainwave
pattern on its own line.

Example Input File
3 AAAAIIIIIIIIIAAIAI IIAAIIIIAIIIIAIIAI AAAAAAAAAI
IIIIIIII IIIIIIIIIAAAAAAAAA AIAIAIAIAAIAIAIAIA IAI
AIAIAIAIAAAIAAI Example Output To
Screen AAIIIIIIIIAIAI
AIAAIA
42
Brain Problem Design
  • Hook up Scanner to file.
  • No path info!
  • Read line for number of data sets
  • For each data set
  • read in line 1
  • read in line 2
  • iterate through each line and compare to create
    result
  • print out result
  • a relatively easy problem

43
Example Problem 2
Juggling Numbers Program Name juggle.java Input
File juggle.in Did you ever wonder how jugglers
can keep track of all of those balls while not
letting any of them fall? They do it by
mastering a certain number of basic throws and
then chaining them together in exciting ways. One
convenient side-effect of this technique is that
it is possible to represent a jugging pattern
fairly well with a string of single digits such
as 5313 or 441441. Each digit represents a
single throw, with the height of the throw
corresponding to the size of the number (the
exception is the 0 digit, which represents no
ball is thrown during that step). For instance,
a ball thrown with a height of 5 will have to be
caught five steps later in the sequence. Not all
sequences are possible for jugglers, however,
since they can only catch one ball during any
given step. In the sequence 321 all three
balls would be landing during step 4! Its very
useful to be able to determine whether or not a
sequence is possible.
44
Input The first line of input will contain a
single integer n indicating the number of
datasets. The following n lines will each contain
a sequence of digits (0-9) representing a
juggling pattern. Each sequence will contain
from 1 to 40 digits. Output For each dataset in
the input, determine if there is any step where
more than one ball must be caught. If there is
no such step, then the sequence is valid and the
string VALID should be displayed. Otherwise,
for sequences with steps where multiple balls
have to be caught simultaneously, the sequence is
invalid, and we want to know the number of balls
that will drop the first time the juggler misses.
This value should be one less than the total
number of balls that need to be caught during the
first step where the juggler has to catch more
than one. In the case of an invalid sequence,
display DROPPED X on step Y, where the first
drop occurs on step Y, with X balls missed, and
steps are numbered starting at 1. Note the
sequences will likely end with some balls still
in the air. Solutions should treat this
situation as if the sequence ended in just enough
zeros (0) to ensure all balls were caught.
45
Example Input File 4 333333333 441441441441 333321
445441441441 Example Output To
Screen VALID VALID DROPPED 2 on step 7 DROPPED 1
on step 8
46
Judges data file for Juggle
Expected Output VALID VALID DROPPED 2 on step
7 DROPPED 1 on step 8 VALID VALID VALID VALID DROP
PED 8 on step 10 DROPPED 1 on step 11
10 333333333 441441441441 333321 445441441441 5 09
90 0123456789 9876543210 0009070301
About PowerShow.com