Ch 10 - Arrays Flashcards
Think Julia
What is an array?
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.
Think Julia
Must all elements in a array be the same type?
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.
Think Julia
How can the type of the data in an array be determined?
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.
Think Julia
How can the dimensions of an array be determined?
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.
Think Julia
How does the ∈ operator work in an array?
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.
Think Julia
Are arrays mutable or immutable?
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.
Think Julia
How can the indexes of an array be traversed in an array?
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.
Think Julia
What happens if you use the slice opperator on an array without arguments?
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.
Think Julia
When working with arrays, what happens if a slice operator is placed on the left side of an assignment?
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.
Think Julia
How can you add a new element to the end of an array?
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.
Think Julia
How can the elements of a second array be added to the end of a first array?
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.
Think Julia
What is the difference between sort! and sort ?
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.
Think Julia
What does ! do to a function?
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.
Think Julia
How can all the elements of an array be added up?
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.
Think Julia
What is the term for an opperation that combines a sequence of elements to one value?
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.
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.
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.
Think Julia
What is dot syntax?
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.
Think Julia
Can dot syntax work on arrays?
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.
Think Julia
What is a: splice!()
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.
Think Julia
What is a: pop!()
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.
Think Julia
What is a: popfirst!()
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.
Think Julia
What is a: pushfirst!() and push!()
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.
Think Julia
What is a: deleteat!()
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.
Think Julia
What is a: insert!()
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.