And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to Java 2 Programming Lecture 5 The Collections API.
Stacks, Queues, and Linked Lists
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Container Types in Python
Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Learning Ruby Ruby Hashes. Hashes Hashes introduce a new accessing method – similar to arrays but indexing is by arbitrary keys of any type. Not magic.
CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types: Structured types.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
The Ruby Programming Language
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Associative Arrays Record Types Tuple Types List Types Union Types.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Lists in Python.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
I Power Int 2 Computing Software Development High Level Language Constructs.
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
Data structures Abstract data types Java classes for Data structures and ADTs.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Learning Ruby - 10 Exploiting Classes. Ruby's Class Library Array - an ordered collection of objects Bignum - really big integer values Binding - remembering.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
Expressions and Assignment Statements
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
RUBY by Ryan Chase.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Apache Avro CMSC 491 Hadoop-Based Distributed Computing Spring 2016 Adam Shook.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Sets Set is an unordered list of items
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Working with Collections of Data
Chapter 6: Data Types Lectures # 10.
Containers and Lists CIS 40 – Introduction to Programming in Python
Ruby and other languages….
CS313D: Advanced Programming Language
CIT 383: Administrative Scripting
Data Structures – 1D Lists
String and Lists Dr. José M. Reyes Álamo.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Variables, Lists, and Objects
(more) Python.
Type Systems Terms to learn about types: Related concepts: Type
Terminology and Symbols
Introduction to Computer Science
Presentation transcript:

and other languages…

 Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros = Array.new(5, 0)  Arrays are heterogeneous  length and size return # of elements  Access beyond end of array returns nil  Elements can be accessed similar to strings  Arrays can be dynamically resized (can assign past the end of the array)  Use | and & for union and intersection Compare to Java/C++

 words = %w[here we go again] => [‘here’,’we’,’go’,’again’]  Use << to append  Useful functionality alphabet = ('A'..'Z').to_a alphabet.each {|c| print c }  Not covered: lots of methods, like clear, empty?, compact!, sort, sort!, pop, push, reverse, etc.

 Immutable, interned string (only one copy)  Colon-prefixed, e.g., :one  Commonly used as hash key  Also stores names of classes, methods and variables in symbol table – more efficient than storing as string, can be used with reflection  If your code is using a string as a unique identifier, consider using a symbol instead  Methods available to convert between string and symbol Get used to symbols, they’re used a lot

 Aka maps, associative arrays colors = { :Cyndi => "orange", :Karyl => "purple" } colors2 = { "Cyndi" => "orange", "Karyl" => "purple" } colors3 = { Cyndi: "orange", Karyl: "purple" } puts colors3[:Cyndi] colors.each do |key, value| puts "#{key}'s favorite color is #{value}" end  Best to use immutable objects as keys  Not covered: hash codes Hashes supported directly in Perl, Python (dictionary) and Ruby and in class libraries of Java, C++ and C# Required sometimes, e.g., Python

 Which is better if need to access items in order?  Which is useful for direct access?  Hash: useful for “paired” data

 Purpose determine if a value is in or out of range iteration  Can be any value that implements function (like CompareTo… why is this needed?)  includes 10  1…10 excludes 10 coldwar = coldwar.include? birthdate.year (1..3).to_a parentheses required, else just applies to 3 Subrange introduced in Pascal, also used in Modula-2 and Ada. Others?

 TrueClass singleton, write as true  FalseClass singleton, write as false  nil means no value. Test directly (o == nil) or with nil?  true != 1, false != 0 Compare to Java/C++/C

 FixNum. Int operations that fit in a machine word.  BigNum. Used for larger ints (FixNum will be converted automatically)  Float. Floating point values  Complex. real + imaginary  Rational. e.g., 2/3

 Language Concepts Array  Heterogeneous?  Array operations  Fixed or dynamic size Hash  key restrictions Range  operations Boolean  0/1?  Ruby Arrays  new  each Symbols Hashes Range Boolean