Ch 10 - Arrays Flashcards

1
Q

Think Julia

What is an array?

A

An Array Is a Sequence

Like a string, an array is a sequence of values. In a string, the values are characters; in an array, they can be any type. The values in an array are called elements or sometimes items.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3482-3485). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Think Julia

Must all elements in a array be the same type?

A

The elements of an array don’t have to be the same type. The following array contains a string, a float, an integer, and another array:

[” spam”, 2.0, 5, [10, 20]]

An array within another array is nested.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3494-3501). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Think Julia

How can the type of the data in an array be determined?

A

The function typeof can be used to find out the type of the array:

julia > typeof( cheeses)
Array{ String, 1}
julia > typeof( numbers)
Array{ Int64,1}
julia > typeof( empty)
Array{ Any, 1}

The number indicates the dimensions (we’ll talk more about this in “Arrays”). The array empty contains values of type Any; that is, it can hold values of all types.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3514-3526). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Think Julia

How can the dimensions of an array be determined?

A

The function typeof can be used to find out the type of the array:

julia > typeof( cheeses)
Array{ String, 1}
julia > typeof( numbers)
Array{ Int64,1}
julia > typeof( empty)
Array{ Any, 1}

The number indicates the dimensions (we’ll talk more about this in “Arrays”). The array empty contains values of type Any; that is, it can hold values of all types.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3514-3526). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Think Julia

How does the ∈ operator work in an array?

A

The ∈ operator also works on arrays:

julia > cheeses = [” Cheddar”, “Edam”, “Gouda”];
julia > “Edam” ∈ cheeses
true
julia > “Brie” in cheeses
false

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3556-3564). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Think Julia

Are arrays mutable or immutable?

A

Unlike strings, arrays are mutable; that is, their values can be changed. When the bracket operator appears on the left side of an assignment, it identifies the element of the array that will be assigned:

julia > numbers = [42, 123];
julia > numbers[ 2] = 5
5
julia > print( numbers)
[42, 5]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3535-3542). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Think Julia

How can the indexes of an array be traversed in an array?

A

Traversing an Array

The most common way to traverse the elements of an array is with a for loop. The syntax is the same as for strings:

for cheese in cheeses
….println( cheese)
end

This works well if you only need to read the elements of the array. But if you want to write or update the elements, you need the indices. One technique is to use the built-in function eachindex:

for i in eachindex( numbers)
….numbers[ i] = numbers[ i] * 2
end

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3565-3581). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Think Julia

What happens if you use the slice opperator on an array without arguments?

A

Using the slice operator without arguments makes a copy of the whole array:

julia > print( t[:])
[’ a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]

Since arrays are mutable, it is often useful to make a copy before performing operations that modify arrays.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3612-3618). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Think Julia

When working with arrays, what happens if a slice operator is placed on the left side of an assignment?

A

A slice operator on the left side of an assignment can update multiple elements:

julia > t[ 2: 3] = [’ x’, ‘y’];
julia > print( t)
[’ a’, ‘x’, ‘y’, ‘d’, ‘e’, ‘f’]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3618-3624). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Think Julia

How can you add a new element to the end of an array?

A

Julia provides functions that operate on arrays. For example, push! adds a new element to the end of an array:

julia > t = [’ a’, ‘b’, ‘c’];
julia > push!( t, ‘d’);
julia > print( t) [’ a’, ‘b’, ‘c’, ‘d’]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3625-3634). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Think Julia

How can the elements of a second array be added to the end of a first array?

A

append! adds the elements of the second array to the end of the first:

julia > t1 = [’ a’, ‘b’, ‘c’];
julia > t2 = [’ d’, ‘e’];
julia > append!( t1, t2);
julia > print( t1)
[’ a’, ‘b’, ‘c’, ‘d’, ‘e’]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3635-3644). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Think Julia

What is the difference between sort! and sort ?

A

sort! arranges the elements of the array from low to high:

julia > t = [’ d’, ‘c’, ‘e’, ‘b’, ‘a’];
julia > sort!( t);
julia > print( t)
[’ a’, ‘b’, ‘c’, ‘d’, ‘e’]

sort returns a copy of the elements of the array in order:

julia > t1 = [’ d’, ‘c’, ‘e’, ‘b’, ‘a’];
julia > t2 = sort( t1);
julia > print( t1)
[’ d’, ‘c’, ‘e’, ‘b’, ‘a’]
julia > print( t2)
[’ a’, ‘b’, ‘c’, ‘d’, ‘e’]

As a style convention in Julia, ! is appended to names of functions that modify their arguments.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3665-3669). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Think Julia

What does ! do to a function?

A

sort! arranges the elements of the array from low to high:

julia > t = [’ d’, ‘c’, ‘e’, ‘b’, ‘a’];
julia > sort!( t);
julia > print( t)
[’ a’, ‘b’, ‘c’, ‘d’, ‘e’]

sort returns a copy of the elements of the array in order:

julia > t1 = [’ d’, ‘c’, ‘e’, ‘b’, ‘a’];
julia > t2 = sort( t1);
julia > print( t1)
[’ d’, ‘c’, ‘e’, ‘b’, ‘a’]
julia > print( t2)
[’ a’, ‘b’, ‘c’, ‘d’, ‘e’]

As a style convention in Julia, ! is appended to names of functions that modify their arguments.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3665-3669). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Think Julia

How can all the elements of an array be added up?

A

Adding up the elements of an array is such a common operation that Julia provides it as a built-in function, sum:

julia > t = [1, 2, 3, 4];
julia > sum( t)
10

An operation like this that combines a sequence of elements into a single value is sometimes called a reduce operation.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3687-3698). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Think Julia

What is the term for an opperation that combines a sequence of elements to one value?

A

Adding up the elements of an array is such a common operation that Julia provides it as a built-in function, sum:

julia > t = [1, 2, 3, 4];
julia > sum( t)
10

An operation like this that combines a sequence of elements into a single value is sometimes called a reduce operation.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3687-3698). Kindle Edition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Think Julia

function addall( t)
….total = 0
….for x in t
……..total + = x
….end
….total
end

What is a term that describes what the variable ‘total’ is doing in this function?

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3675-3677). Kindle Edition.

A

As the loop runs, total accumulates the sum of the elements; a variable used this way is sometimes called an accumulator.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3685-3687). Kindle Edition.

17
Q

Think Julia

What is dot syntax?

A

Dot Syntax

For every binary operator, like ^, there is a corresponding dot operator, like .^, that is automatically defined to perform the operation element-by-element on arrays. For example, [1, 2, 3] ^ 3 is not defined, but [1, 2, 3] .^ 3 is defined as computing the elementwise result [1 ^ 3, 2 ^ 3, 3 ^ 3]:

julia > print([ 1, 2, 3] .^ 3)
[1, 8, 27]

Any Julia function f can be applied elementwise to any array with the dot syntax. For example, to capitalize an array of strings, we don’t need an explicit loop:

julia > t = uppercase.([” abc”, “def”, “ghi”]);
julia > print( t)
[” ABC”, “DEF”, “GHI”]

This is an elegant way to create a map. The function capitalizeall can be implemented by a one-liner:

function capitalizeall( t)
….uppercase.( t)
end

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3751-3753). Kindle Edition.

18
Q

Think Julia

Can dot syntax work on arrays?

A

Dot Syntax

For every binary operator, like ^, there is a corresponding dot operator, like .^, that is automatically defined to perform the operation element-by-element on arrays. For example, [1, 2, 3] ^ 3 is not defined, but [1, 2, 3] .^ 3 is defined as computing the elementwise result [1 ^ 3, 2 ^ 3, 3 ^ 3]:

julia > print([ 1, 2, 3] .^ 3)
[1, 8, 27]

Any Julia function f can be applied elementwise to any array with the dot syntax. For example, to capitalize an array of strings, we don’t need an explicit loop:

julia > t = uppercase.([” abc”, “def”, “ghi”]);
julia > print( t)
[” ABC”, “DEF”, “GHI”]

This is an elegant way to create a map. The function capitalizeall can be implemented by a one-liner:

function capitalizeall( t)
….uppercase.( t)
end

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3751-3753). Kindle Edition.

19
Q

Think Julia

What is a: splice!()

A

There are several ways to delete elements from an array. If you know the index of the element you want, you can use splice!:

julia > t = [’ a’, ‘b’, ‘c’];
julia > splice!( t, 2)
‘b’: ASCII/ Unicode U + 0062 (category Ll: Letter, lowercase)
julia > print( t) [’ a’, ‘c’]

splice! modifies the array and returns the element that was removed.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3760-3765). Kindle Edition.

20
Q

Think Julia

What is a: pop!()

A

pop! deletes and returns the last element:

julia > t = [’ a’, ‘b’, ‘c’];
julia > pop!( t)
‘c’: ASCII/ Unicode U + 0063 (category Ll: Letter, lowercase)
julia > print( t) [’ a’, ‘b’]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3765-3772). Kindle Edition.

21
Q

Think Julia

What is a: popfirst!()

A

popfirst! deletes and returns the first element:

julia > t = [’ a’, ‘b’, ‘c’];
julia > popfirst!( t)
‘a’: ASCII/ Unicode U + 0061 (category Ll: Letter, lowercase)
julia > print( t) [’ b’, ‘c’]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3773-3780). Kindle Edition.

22
Q

Think Julia

What is a: pushfirst!() and push!()

A

The functions pushfirst! and push! insert an element at the beginning and end, respectively, of the array.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3780-3784). Kindle Edition.

23
Q

Think Julia

What is a: deleteat!()

A

If you don’t need the removed value, you can use the function deleteat!:

julia > t = [’ a’, ‘b’, ‘c’];
julia > print( deleteat!( t, 2))
[’ a’, ‘c’]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3784-3790). Kindle Edition.

24
Q

Think Julia

What is a: insert!()

A

The function insert! inserts an element at a given index:

julia > t = [’ a’, ‘b’, ‘c’];
julia > print( insert!( t, 2, ‘x’))
[’ a’, ‘x’, ‘b’, ‘c’]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3791-3797). Kindle Edition.

25
Q

Think Julia

What is a: collect()

A

A string is a sequence of characters and an array is a sequence of values, but an array of characters is not the same as a string. To convert from a string to an array of characters, you can use the function collect:

julia > t = collect(“ spam”);
julia > print( t)
[’ s’, ‘p’, ‘a’, ‘m’]

The collect function breaks a string or another sequence into individual elements.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3799-3808). Kindle Edition.

26
Q

Think Julia

What is a: split()

A

If you want to break a string into words, you can use the split function:

julia > t = split(“ pining for the fjords”);
julia > print( t)
SubString{ String}[” pining”, “for”, “the”, “fjords”]

An optional argument called a delimiter specifies which characters to use as word boundaries (we talked briefly about optional arguments in “Exercise 8-7”). The following example uses a hyphen as a delimiter:

julia > t = split(“ spam-spam-spam”, ‘-‘);
julia > print( t)
SubString{ String}[” spam”, “spam”, “spam”]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3820-3822). Kindle Edition.

27
Q

Think Julia

How do you use a delimiter with the split function?

A

If you want to break a string into words, you can use the split function:

julia > t = split(“ pining for the fjords”);
julia > print( t)
SubString{ String}[” pining”, “for”, “the”, “fjords”]

An optional argument called a delimiter specifies which characters to use as word boundaries (we talked briefly about optional arguments in “Exercise 8-7”). The following example uses a hyphen as a delimiter:

julia > t = split(“ spam-spam-spam”, ’-‘);
julia > print( t)
SubString{ String}[” spam”, “spam”, “spam”]

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3820-3822). Kindle Edition.

28
Q

Think Julia

What is a: join()

A

join is the inverse of split. It takes an array of strings and concatenates the elements:

julia > t = [” pining”, “for”, “the”, “fjords”];
julia > s = join( t, ‘ ‘)
“pining for the fjords”

In this case the delimiter is a space character. To concatenate strings without spaces, you don’t specify a delimiter.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3823-3833). Kindle Edition.

29
Q

Think Julia

What is a: object

A

Objects and Values

An object is something a variable can refer to. Until now, you might have used “object” and “value” interchangeably.

If we run these assignment statements:

a = “banana”
b = “banana”

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3833-3839). Kindle Edition.

30
Q

Think Julia

How can you tell if two variables refer to the same object?

A

To check whether two variables refer to the same object, you can use the ≡ (\ equiv TAB) or = = = operator:

julia > a = “banana”
“banana”
julia > b = “banana”
“banana”
julia > a ≡ b
true

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3845-3856). Kindle Edition.

31
Q

Think Julia

What is the difference between equivalent and identical?

A

But when you create two arrays, you get two objects:

julia > a = [1, 2, 3];
julia > b = [1, 2, 3];
julia > a ≡ b
false

In this case we would say that the two arrays are equivalent, because they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identical.

To be precise, an object has a value. If you evaluate [1, 2, 3], you get an array object whose value is a sequence of integers. If another array has the same elements, we say it has the same value, but it is not the same object.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3869-3874). Kindle Edition.

32
Q

Think Julia

What is the association of a variable with an object called?

A

Aliasing If a refers to an object and you assign b = a, then both variables refer to the same object:

julia > a = [1, 2, 3];
julia > b = a;
julia > b ≡ a
true

The association of a variable with an object is called a reference. In this example, there are two references to the same object.

An object with more than one reference has more than one name, so we say that the object is aliased.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3887-3890). Kindle Edition.

33
Q

Think Julia

What is the term when a object is referenced to more than one variable?

A

Aliasing If a refers to an object and you assign b = a, then both variables refer to the same object:

julia > a = [1, 2, 3];
julia > b = a;
julia > b ≡ a
true

The association of a variable with an object is called a reference. In this example, there are two references to the same object.

An object with more than one reference has more than one name, so we say that the object is aliased.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3887-3890). Kindle Edition.

34
Q

Think Julia

What happens when an array is passed to a function?

A

Array Arguments

When you pass an array to a function, the function gets a reference to the array. If the function modifies the array, the caller sees the change. For example, deletehead! removes the first element from an array:

function deletehead!( t)
popfirst!( t)
end

Here’s how it is used:

julia > letters = [’ a’, ‘b’, ‘c’];
julia > deletehead!( letters);
julia > print( letters)
[’ b’, ‘c’]

The parameter t and the variable letters are aliases for the same object. The stack diagram looks like Figure 10-5.

Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 3904-3919). Kindle Edition.

35
Q

Think Julia

Fix

A