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.