4 - Sequences and iteration Flashcards

1
Q

What are some of the operations used to create sequences in M269?

A

Slicing, concatenating, and sorting are operations used to create new sequences from existing ones in M269.

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

How do you access a cell within a tuple table, and what are the alternative methods?

A

To access a cell in a tuple table, you first access the row and then the column. Alternatively, you can directly access the cell using indices, such as games_by_row[1][2] to access the cell in the 2nd row and 3rd column.

note that:
Indexing is left-associative, and the first index selects the row, while the second index selects the column.

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

When problem-solving, what type of problem involves finding one or more items that satisfy specific criteria?

A

A search problem involves finding one or more items that satisfy certain criteria.

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

What are the inputs and preconditions for the slicing operation?

A

Inputs: values (a sequence), start (an integer), and end (an integer). Preconditions: 0 ≤ start ≤ end ≤ │values│.

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

What is a subsequence in a sequence?

A

A subsequence is a sequence that can be obtained from another sequence by deleting zero or more, not necessarily consecutive, items.

for example, in the sequence s = [1, 2, 3, 4, 5, 6, 7], the subsequence s1 = [1, 3, 5, 7] is obtained by deleting certain items from s.

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

What does the indexing operation allow you to do with a sequence, how is it written, and what is its time complexity in M269?

A

The indexing operation allows you to retrieve an element from a sequence using its index.

In mathematics, it’s written as “𝑠𝑖” for a sequence s and index i, while in computing, it’s more commonly denoted as “s[i].”

In M269, the indexing operation is assumed to have a constant time complexity because all data is stored in RAM, Any RAM position can be accessed in the same time, so we assume that indexing takes constant time..

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

In problem-solving, what type of problem involves checking a property of the input and making a decision?

A

A decision problem involves checking a property of the input.

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

What is the worst-case complexity of lexicographic comparison operations on sequences?

A

The worst-case complexity is Θ(min(│left│, │right│)) because it may take until the end of the shorter sequence to make a decision in a worst-case scenario.

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

What is the output of the slicing operation, and what are the postconditions?

A

Output: substring (a sequence). Postconditions: If start = end, then substring is (), otherwise substring includes items from values[start] to values[end - 1].

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

What is a key feature of the list data type in Python, and how does it compare to other sequence types like tuples, strings, and ranges?

A

Lists in Python are mutable, meaning they can be modified, and they may contain heterogeneous elements. All operations that work on tuples also work on lists.

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

What are some common aspects to consider when testing sequences, and what are the elements to test for?

A

When testing sequences, it’s important to consider edge cases, including testing with an empty sequence (if allowed by precondition) and a single element in the sequence.

Additionally, test for both odd and even length sequences, as operations might behave differently in these cases.

If preconditions allow, test with duplicate items, unique items, heterogeneous sequences (different data types), and homogeneous sequences (same data types).

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

What is a substring in sequences?

A

If sequences s1, s2, s3, and s satisfy s1 + s2 + s3 = s, then s2 is a substring of s.

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

What is lexicographic comparison in the context of sequences?

A

Lexicographic comparison of two sequences involves pairwise comparison of items, one by one, until a decision can be made.

It continues until items differ or one sequence ends before the other. If two sequences are equal until one ends, the shorter sequence is considered ‘less than’ the longer one.

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

What does the concatenation operation do in M269?

A

The concatenation operation in M269 forms a new sequence by joining both input sequences, written as left + right.

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

What’s important to remember about operations on strings in M269?

A

Operations on strings are carried out on a character by character basis, not the entire string sequence. This is crucial when considering algorithms that use the string data type.

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

What defines a sorted sequence?

A

A sequence is sorted if it is ordered by ascending or descending value and requires all items to be pairwise comparable.

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

How can sequences be categorized in terms of data types?

A

Sequences can be categorized as:

  • homogeneous - with all items of the same data type
  • heterogeneous - with items varying in data types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How is the size of a sequence typically stored and updated in memory and what is the complexity of looking up the size of a sequence?

A

The size of a sequence is typically stored in memory along with the sequence, and it is computed and updated when the sequence is created and modified.

with these asumptions the size/length operation can look up the size in constant time, which means it has a constant time complexity.

19
Q

What is the runtime complexity of the concatenation operation in terms of the lengths of the input sequences?

A

The runtime of concatenation is proportional to the number of items copied, which is │left│ + │right│.

It has a linear runtime in the total length of the inputs: Θ(│left│ + │right│).

20
Q

How does Python perform lexicographic comparison on strings?

A

Python’s lexicographic comparison of strings uses the character ordering of the Unicode standard.

21
Q

What is a linear search, and what does it involve?

A

A linear search is an algorithm used to systematically go through a sequence and check each element to find one or more elements that match a given condition. It’s commonly used to solve search problems involving sequences.

22
Q

What is the output of the concatenation operation, and what are the postconditions?

A

Output: joined (a sequence).

Postconditions: joined is formed by copying all items of left and all items of right in order.

23
Q

What is the complexity of the slicing operation, and why does it have this complexity?

A

The complexity of the slicing operation is Θ(end - start).

This complexity arises because the operation copies all items in the slice to a new sequence. The time it takes to copy the items is directly proportional to the size of the slice, which is determined by the difference between the end and start indices. Therefore, the complexity is linear in the size of the slice, and it’s denoted as Θ(end - start).

24
Q

What is a search problem, and how is a linear search performed?

A

A search problem involves finding items in a sequence that satisfy specific conditions.

A linear search checks every item of the sequence one by one to find the desired items.

25
Q

What are the inputs and preconditions for the concatenation operation?

A

Inputs: left (a sequence) and right (a sequence). Preconditions: True.

26
Q

What are the inputs and outputs of the indexing operation

A

The indexing function takes a sequence and an index as inputs

The indexing operation returns the value from that sequence, as the abstract data type (ADT) “object” is used to represent the data type being returned.

27
Q

How does the “in” operator work with tuples, and what does it check for?

A

The “in” operator checks for membership in tuples, not for substrings as in strings. If an element is a member of the tuple, it returns true.

28
Q

What is a sequence?

A

A sequence is an ordered collection of zero or more items.

29
Q

What is the significance of including a comma when concatenating tuples in Python, and why is it important?

A

When concatenating tuples in Python, including a comma is essential to indicate that you’re working with a tuple with a single element.

Omitting the comma can lead to unintended results, such as the sum of the elements rather than tuple concatenation.

For example, (3) + (4) results in 7, while (3,) + (4,) gives the correct result of (3, 4).

30
Q

What is the difference in performance between an algorithm with linear complexity and one with quadratic complexity as the input size increases?

A

When the input size doubles, a linear algorithm takes double the time, while a quadratic algorithm takes 2² = 4 times as long.

If the input is a thousand times as long, a linear algorithm takes a thousand times as long, but a quadratic algorithm takes 1000² = 1,000,000 times as long.

This demonstrates that a quadratic algorithm becomes significantly slower than a linear one as the input size increases.

31
Q

How do you define a prefix and suffix in sequences?

A

If sequences s1 and s2 satisfy s1 + s2 = s, then s1 is a prefix of s, and s2 is a suffix of s.

32
Q

What are the characteristics of tuples in Python?

A

Tuples in Python are immutable, possibly heterogeneous, and may contain other tuples, allowing them to represent tabular data.

33
Q

What are some functions that allow us to obtain information about a sequence?

A

Functions related to sequences include:

  • size
  • indexing
  • membership
  • comparison.
34
Q

What kind of problem in problem-solving requires assigning a category to each input?

A

A classification problem involves assigning a category to each input.

35
Q

What does the “Membership” operation in inspecting sequences check, how is it written, and what are its best-case and worst-case complexities?

A

The “Membership” operation checks if a value “v” is an element of a sequence “s.”

It can be written as “v in s” or “v ∈ s.”

The best-case complexity is Θ(1) when the value is the first element, requiring one comparison.

The worst-case complexity is Θ(│values│), which occurs when the value is the last element or not in the sequence, resulting in a linear time operation.

36
Q

What is the best-case complexity of lexicographic comparison operations on sequences?

A

The best-case complexity is Θ(1) because a decision can be made after one comparison in the best-case scenario.

37
Q

What does the slicing operation do with a sequence?

A

The slicing operation extracts a consecutive sequence of items from the input sequence.

38
Q

What does the size of a sequence represent and how is it written in english/maths?

A

The size of a sequence s, denoted as │s│, represents the number of elements in the sequence.

39
Q

What is an in-place algorithm?

A

An in-place algorithm works directly on the input/output sequence, without using an additional sequence.

40
Q

What can lead to unexpected behavior when comparing strings in Python?

A

Unexpected behavior can occur in string comparisons when, for example, ‘A-Z’ comes before ‘a-z’ in Unicode, resulting in cases like ‘aardvark’ < ‘Zeus’ returning False.

41
Q

When can the minimum and maximum operations be applied to determine the smallest and largest values in a sequence?

A

The minimum and maximum operations can be applied to determine the smallest and largest values in a sequence when the items of the sequence are pairwise comparable, meaning each item can be compared to every other item.

42
Q

What are the input and output of the “Membership” operation in inspecting sequences?

A

The input for the “Membership” operation is a sequence “values” and a value “v” to check for membership.

The output is a Boolean, “is member,” which is true if and only if there’s an integer index such that “values[index] = value.”

43
Q

How is the complexity of a loop determined

A

the complexity of a loop is determined by the number of iterations multiplied by the complexity of the instructions within the loop. The general rule is that the complexity of a loop is Θ(I x e), where I is the number of iterations, and e is the complexity of the loop’s body.

44
Q

What are the characteristics of Python’s
* str,
* range,
* tuple,
* and list types,
*
* and how can tuples and lists be used to represent tables?

A
  • str is immutable and represents immutable strings.
  • range represents immutable sequences of integers.
  • tuple represents immutable sequences,
  • while list represents mutable sequences.

Tuples and lists can contain items of any type, including other tuples or lists, making them suitable for representing tables in Python.

Immutable types like str and range cannot be modified, so operations like replacing and removing elements are not possible.