Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay
Session: Sequences
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1
Sequences IIT Bombay
• Sequence is the most fundamental data type in computer
science. • Informally, a sequence is a collection of values of the same type, placed in a particular order. • The order in which the values occur is important. • At the lowest level, all data in a computer is a sequence of bits. • Need to represent arbitrary values using sequence of bits.
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2
Definition of Sequences IIT Bombay
• Sequences are defined in a way very similar to numbers.
• All elements in a sequence are of the same type. • A sequence of type T contains elements of type T. • Different types of sequences depending on the type T. • Sequence of bits, sequence of characters, sequence of numbers, sequence of sequence of numbers etc. • Many operations on sequences do not depend on T.
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3
Definition of Sequences IIT Bombay
• For any type T, the empty sequence denoted Ф, is a
sequence of type T. • If S is a sequence of type T, and x is an element of type T, then push(x,S) is a sequence of type T. • If a property holds for the empty sequence, and assuming it holds for a sequence S, it holds for the sequence push(x,S) for all values x of type T, then it holds for all sequences of type T.
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4
Examples IIT Bombay
• The sequence of bits 110101 is the value
push(1,push(1,push(0,push(1,push(0,push(1,Ф)))))) • The sequence of characters “abcdef “ is the value push(a,push(b,push(c,push(d,push(e,push(f,Ф)))))) • The sequence containing only the number 10 is push(10,Ф) • Note that the sequence containing the value 10 is a different value than the number 10 itself.
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5
Operations on Sequences IIT Bombay
• Operations on sequences defined similarly to operations on
numbers. • Define for the empty sequence. • Assuming it is defined for a sequence S, define it for the sequence push(x,S) for all values x of type T. • This defines it for all sequences of type T. • Many definitions do not depend on the type T.
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6
Length IIT Bombay
• Informally, the length of a sequence is the number of
elements in it. • Formally, length(Ф) = 0 length(push(x,S)) = next(length(S)) for all values x
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7
Concatenation IIT Bombay
• Concatenate sequences S1 and S2
concatenate(Ф, S2) = S2 for all sequences S2 concatenate (push(x,S1),S2)) = push(x,concatenate(S1,S2)) • Defines concatenate(S1,S2) for all sequences S1, S2 .
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 9
Exercises IIT Bombay
• Prove that for any two sequences S1, S2
length(concatenate(S1,S2)) = add(length(S1),length(S2)) • Prove that for any sequence S reverse(reverse(S)) = S • Prove that for any sequences S1,S2 reverse(concatenate(S1,S2)) = concatenate(reverse(S2), reverse(S1))
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 10