This week is based on Lesson 3 of the Udacity course CS101 "Introduction to Computer Science". It would be best if you sign up and watch these lectures.
We will cover:
Read Section 4.1 "Strings" of "Knights Programming".
Read Subsection 3.1.2 "Strings" of The Python Tutorial.
String | List |
---|---|
sequence of characters | sequence of anything |
|
|
|
|
|
|
# <list> -> [<Expression>,<Expression>,...]
[]
[3]
[1,2,3]
# Given the variable
days_in_month=[31,28,31,30,31,30,31,31,30,31,30,31]
# define a procedure, how_many_days, that takes as
# input a number representing a month, and outputs
# the number of days in that month
# Given the variable
days_in_month=[31,28,31,30,31,30,31,31,30,31,30,31]
# define a procedure, how_many_days, that takes as
# input a number representing a month, and outputs
# the number of days in that month
def how_many_days(month):
return days_in_month[month - 1]
mixed_up = ['apple',3,'oranges',28,[1,4,['alpha','beta']]]
# Given the variable countries defined as:
# Name Capital Populations (millions)
countries = [['China','Beijing',1350],
['India','Delhi',1210],
['Romania','Bucharest',21],
['United States','Washington',307]]
# Write code to print out the capital of India
# by accessing the array.
# Given the variable countries defined as:
# Name Capital Populations (millions)
countries = [['China','Beijing',1350],
['India','Delhi',1210],
['Romania','Bucharest',21],
['United States','Washington',307]]
# Write code to print out the capital of India
# by accessing the array.
print(countries[1][1])
# Given the variable countries defined as:
# Name Capital Populations (millions)
countries = [['China','Beijing',1350],
['India','Delhi',1210],
['Romania','Bucharest',21],
['United States','Washington',307]]
# What multiple of the population of the USA is the population
# of China?
# Given the variable countries defined as:
# Name Capital Populations (millions)
countries = [['China','Beijing',1350],
['India','Delhi',1210],
['Romania','Bucharest',21],
['United States','Washington',307]]
# What multiple of the population of the USA is the population
# of China?
print(countries[0][2] / countries[3][2])
s='Hello' # step 1
s='Yello' # step 2
s=s+'w' # step 3
step 1
s----->'Hello'
s='Hello' # step 1
s='Yello' # step 2
s=s+'w' # step 3
step 2
s--+ 'Hello'
|
+--> 'Yello'
# Note: whenever a string is assigned to s, a new string is created
s='Hello' # step 1
s='Yello' # step 2
s=s+'w' # step 3
step 3
s--+ 'Hello'
|
| 'Yello'
|
+--> 'Yellow'
# Note: whenever a string is assigned to s, a new string is created
p=['H','e','l','l','o'] # step 1
p[0]='Y' # step 2
p.append('w') # step 3
# step 1
0 1 2 3 4 5
+---+---+---+---+---+
p ---> |'H'|'e'|'l'|'l'|'o'|
+---+---+---+---+---+
p=['H','e','l','l','o'] # step 1
p[0]='Y' # step 2
p.append('w') # step 3
# step 2
0 1 2 3 4 5
+---+---+---+---+---+
p ---> |'Y'|'e'|'l'|'l'|'o'|
+---+---+---+---+---+
# Note: no new list is created; the old list is changed ("mutated")
p=['H','e','l','l','o'] # step 1
p[0]='Y' # step 2
p.append('w') # step 3
# step 3
0 1 2 3 4 5 6
+---+---+---+---+---+---+
p ---> |'Y'|'e'|'l'|'l'|'o'|'w'|
+---+---+---+---+---+---+
# Note: no new list is created; the old list is changed ("mutated")
p=['H','e','l','l','o'] # step 1
p[0]='Y' # step 2
q=p # step 3
q[4]='!' # step 4
# step 1
0 1 2 3 4 5
+---+---+---+---+---+
p -----> |'H'|'e'|'l'|'l'|'o'|
+---+---+---+---+---+
p=['H','e','l','l','o'] # step 1
p[0]='Y' # step 2
q=p # step 3
q[4]='!' # step 4
# step 2
0 1 2 3 4 5
+---+---+---+---+---+
p -----> |'Y'|'e'|'l'|'l'|'o'|
+---+---+---+---+---+
p=['H','e','l','l','o'] # step 1
p[0]='Y' # step 2
q=p # step 3
q[4]='!' # step 4
# step 3
0 1 2 3 4 5
+---+---+---+---+---+
p --+--> |'Y'|'e'|'l'|'l'|'o'|
| +---+---+---+---+---+
|
q --+
# Note: both p and q point refer to the same object, whose value is the string 'Yello'
p=['H','e','l','l','o'] # step 1
p[0]='Y' # step 2
q=p # step 3
q[4]='!' # step 4
# step 4
0 1 2 3 4 5
+---+---+---+---+---+
p --+--> |'Y'|'e'|'l'|'l'|'!'|
| +---+---+---+---+---+
|
q --+
# Note: both p and q refer to the same object, which has changed its value to the string 'Yell!'
s='Hello'
s[0]='Y'
# 'str' object does not support item assignment
p=['H','e','l','l','o']
q=p
p[0]='Y'
print(p) # => Yello
# not surprising
print(q) # => Yello
# this could appear surprising because we did not use q directly
# What is the value of agent[2] after the
# following code runs:
spy=[0,0,7]
agent=spy
spy[2]=agent[2]+1
# What is the value of agent[2] after the
# following code runs:
spy=[0,0,7]
agent=spy
spy[2]=agent[2]+1
print(agent[2]) # => 8
# Define a procedure, replace_spy,
# that takes as its input a list of
# three numbers, and modifies the
# value of the third element in the
# input list to be one more than its
# previous value.
# For instance:
spy = [0,0,7]
replace_spy(spy)
print(spy) # => [0,0,8]
# Define a procedure, replace_spy,
# that takes as its input a list of
# three numbers, and modifies the
# value of the third element in the
# input list to be one more than its
# previous value.
def replace_spy(p):
p[2] = p[2] + 1 # no return value
spy=[0,0,7]
replace_spy(spy)
print(spy) # => [0,0,8]
def replace_spy(p):
p[2] = p[2] + 1 # no return value
spy=[0,0,7]
replace_spy(spy)
print(spy) # => [0,0,8]
# compare this to
def inc(n):
n = n + 1 # also no return value
a=3
inc(a)
print(a) # => 3
def replace_spy(p):
# How can you create a true clone q of p so q does not change when p is changed?
# The following obviously does not work.
p = [1,2,3]
q = p
p.append(4)
p # => [1,2,3,4]
q # => [1,2,3,4]
# How can you create a true clone q of p so q does not change when p is changed?
def replace_spy(p):
# How can you create a true clone q of p so q does not change when p is changed?
p = [1,2,3]
q = p[:]
p.append(4)
p # => [1,2,3,4]
q # => [1,2,3]
# another possibility would be
q = p + []
# <list>.append(<element>)
#
# <list> + <list>
#
# len(<list>)
a = [1,2,3]
a.append(4)
b = [5,6]
c = a + b
c # => [1,2,3,4,5,6]
len(c) # => 6
# What is output after running:
p=[1,2]
q=[3,4]
p.append(q)
print(len(p))
print(p)
q[1]=5
print(p)
# What is output after running:
p=[1,2]
q=[3,4]
p.append(q)
print(len(p)) # => 3
print(p) # => [1,2,[3,4]]
# mutation again !
q[1]=5
print(p) # => [1,2,[3,5]]
# while <test>:
# <Block>
def print_all_elements(p):
i = 0
while # fill in the correct test condition:
print(p[i])
i = i + 1
# while <test>:
# <Block>
def print_all_elements(p):
i = 0
while i < len(p):
print(p[i])
i = i + 1
# for <name> in <list>:
# <Block>
def print_all_elements(p):
for e in p:
print(e)
# Define a procedure, sum_list,
# that takes as its input a
# list of numbers, and returns
# the sum of all the elements in
# the input list.
def sum_list(p):
# Define a procedure, sum_list, that takes as its input a
# list of numbers, and returns the sum of all the elements in the input list.
def sum_list(p):
sum = 0
for e in p:
sum = sum + e
return sum
# Define a procedure, find_element, that takes as its inputs a list
# and a value of any type, and returns the index of the first element
# in the input list that matches the value.
# If there is no matching element,
# return -1.
def find_element(list, target):
# Define a procedure, find_element, that takes as its inputs a list
# and a value of any type, and returns the index of the first element
# in the input list that matches the value.
# If there is no matching element,
# return -1.
def find_element(list, target):
pos = 0
while pos < len(list):
if list[pos] == target:
return pos
pos = pos + 1
return -1
print(find_element([1,2,3],3)) # => 2
print find_element(['alpha','beta'],'gamma') # => -1
# Define a procedure, find_element, that takes as its inputs a list
# and a value of any type, and returns the index of the first element
# in the input list that matches the value.
# If there is no matching element,
# return -1.
def find_element(list, target):
pos = 0
for e in list:
if e == target:
return pos
pos = pos + 1
return -1
print(find_element([1,2,3],3)) # => 2
print find_element(['alpha','beta'],'gamma') # => -1
# <list>.index(<value>)
# if <value> is in the <list>, returns
# the first position where <value> is
# found in <list>.
# Otherwise, produces an error.
p = [0,1,2]
print(p.index(2)) # => 2
p = [0,1,2,2,2]
print(p.index(2)) # => 2 always give the first position
print(p.index(3)) # => 3 is not in list
# Define a procedure, find_element, that takes as its inputs a list
# and a value of any type, and returns the index of the first element
# in the input list that matches the value.
# If there is no matching element,
# return -1.
def find_element(list, target):
# Define a procedure, find_element, that takes as its inputs a list
# and a value of any type, and returns the index of the first element
# in the input list that matches the value.
# If there is no matching element,
# return -1.
def find_element(list, target):
if target not in list:
return -1
return list.index(target)
# alternative solution
def find_element(list, target):
if target in list:
return list.index(target)
else:
return -1
# Define a procedure, union,
# that takes as inputs two lists.
# It should modify the first input
# list to be the set union of the two
# lists. You may assume the first list
# is a set, that is, it contains no
# repeated elements.
def union(first, second):
# Define a procedure, union,
# that takes as inputs two lists.
# It should modify the first input
# list to be the set union of the two
# lists. You may assume the first list
# is a set, that is, it contains no
# repeated elements.
def union(first, second):
for e in second:
if e not in first:
first.append(e)
return first
a = [1,2,3]
b = [2,4,6]
union(a,b)
print a # => [1,2,3,4,6]
print b # => [2,4,6]
# <list>.pop() -> <element>
#
# mutates the list by removing and returning its last element
# Assume p refers to a list with two elements.
# Which of these code fragments does not change the final value of p:
# 1 # 3
p.append(3) x = pop()
p.pop() p.append(x)
# 2 # 4
x=p.pop() x = p.pop()
y=p.pop() y = p.pop()
p.append(x) p.append(y)
p.append(y) p.append(x)
# Assume p refers to a list with two elements.
# Which of these code fragments does not change the final value of p:
# 1 # 3
p.append(3) x = pop()
p.pop() p.append(x)
# 2 # 4
x=p.pop() x = p.pop()
y=p.pop() y = p.pop()
p.append(x) p.append(y)
p.append(y) p.append(x)
# Solution: #1, #3, #4