Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong.

Similar presentations


Presentation on theme: "LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong."— Presentation transcript:

1 LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong

2 Administrivia Homework 5 graded Terminal log from last time
Homework 5 review Quick Homework 6 (Heads Up: Homework 7 coming on Thursday) Perl Hashes and Python dicts

3 Terminal log from last time
Desktop$ perl -le = qw-a aa aaa A AA AAA ( ) { }-; print a aa aaa A AA AAA ( ) { } Desktop$ perl -le = qw-a aa aaa A AA AAA ( ) { = print ( ) A AA AAA a aa aaa { } Desktop$ perl -le = qw-a aa aaa A AA AAA ( ) { = sort {\$a cmp print Desktop$ perl -le = qw ( ) { = sort {\$a <=> print 0 ( ) { }

4 Terminal log from last time
Desktop$ perl -le = qw-a b c A B C-; use feature = sort {fc(\$a) cmp print a A b B c C Desktop$ perl -le = qw-A B C a b c-; use feature = sort {fc(\$a) cmp print A a B b C c Desktop$ perl -le = qw a = print A a Desktop$ perl -le = (0, 5, 3, 2, 1, 99, 'a', = print Desktop$

5 Homework 5 Review Perl Resources:
sub substr length if-then-else

6 Homework 5 Review palindrome.py Desktop$ python3 pallindrome.py dad True Desktop$ python3 pallindrome.py dada False Desktop$ python3 pallindrome.py racecar Desktop$

7 Homework 5 Review Let's write the Perl program! length($word)
substr($word, -1) substr EXPR,OFFSET,LENGTH substr($word, 0, 1) substr($word, 1, -1) or substr($word, 1, length($word)-2)

8 Homework 5 Review (Bonus)
One student tried '好不好' (good-NOT-good => OK?), but the palindrome code didn't work. Chinese tag questions: 对不对 true (or not)? 有没有 exists (or not)? 是不是 yes (or no)? Perl pre-dates Unicode: and Unicode support is not turned on by default for backwards compatibility. perl -C [number/list] A elements are expected to be strings encoded in UTF-8. O STDOUT will be in UTF-8. I STDIN is assumed to be in UTF-8. S I + O + E.

9 Quick Homework 6 Due tomorrow night: reviewed on Thursday
Question 1: what's the difference between a) and b)? = 4 x 4; = (4) x 4; read Question 2: what does split do here for a) vs. b)? = split " ", 'this is a sentence.'; = split //, 'this is a sentence.'; Due tomorrow night: reviewed on Thursday

10 Perl Hashes dict in Python Note: Perl […] Perl hash: % {…}

11 no quotes needed for strings
Perl Hashes Notes on arrays and hashes arrays are indexed using integers hashes are like arrays with user-defined indexing (aka associative array or hash table or dictionary (Python)) initialization (use list notation (or shortcut): round brackets and commas) @a = ("zero", "one", "two", "three", "four"); %h = ("zero", 0, "one", 1, "two", 2, "three", 3, "four", 4); (key/value pairs) access to individual elements (square brackets vs. curly braces) $a[1] “one” $h{zero} 0 Shortcut: no quotes needed for strings 'zero' "zero"

12 Perl Hashes Notes on arrays and hashes output
zeroonetwothreefour print zero one two three four print %h three3one1zero0two2four4 (note: different order) print "%h" %h (literal, no interpolation done)

13 More on Hash tables Looping over arrays: Output:

14 More on Hash tables Exists: Unique key constraint:
exists $fruitColor{apple} exists $fruitColor{orange}

15 Dictionaries (dict) Lists and tuples are indexed by whole numbers (from 0) Dictionaries are indexed by a key (usually a string) and return some value associated with the key Note: use of curly braces Dictionaries are not ordered (like sets) – see next slide Methods keys(), values(), items() Refer to key + value as an item: encoded as a tuple

16 Dictionaries (dict)

17 Python dict Dictionary order preservation depends on the version of Python used … Python 3.6

18 Python dict list comprehension

19 Python dict How to print the contents of a dictionary?
Use a for-loop and method items(): k,v is a tuple

20 Python dict All values are lists Advantage: simplifies the code
Trade-off Values are lists or a string Advantage: simpler-looking dict

21 Python dict Works too! Less transparent: relies on a
Python-particular quirk …

22 Python dict function zip() pairs up elements from two lists into an iterable

23 Python dict function zip() doesn't always work quite the same …

24 Python list ranges Perl has a range operator: ..
less powerful in some ways, more powerful in others


Download ppt "LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong."

Similar presentations


Ads by Google