samplelist = [1,2,4,3,5,6]
print(sum(samplelist))
print(max(samplelist))
print(min(samplelist))
samplelist.sort()
print(samplelist)
21
6
1
[1, 2, 3, 4, 5, 6]
# Sample of Python Variables
# variable of type string
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
name = "John Doe"
print("name", name, type(name))
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 2, 2, 2]
[1, 0, 1, 1, 1]
[1, 1, 1, 1, 1]
# variable of type integer
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
age = 18
print("age", age, type(age))
# variable of type float
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
score = 90.0
print("score", score, type(score))
# variable of type list (many values in one variable)
print("What is variable name/key?", "value?", "type?", "primitive or collection?")
print("What is different about the list output?")
langs = ["Python", "JavaScript", "Java"]
print("langs", langs, type(langs), "length", len(langs))
print("- langs[0]", langs[0], type(langs[0]))
print("- langs[1]", langs[1], type(langs[1]))
print("- langs[2]", langs[2], type(langs[2]))
# variable of type dictionary (a group of keys and values)
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
print("What is different about the dictionary output?")
person = {
"name": name,
"age": age,
"score": score,
"langs": langs
}
print("person", person, type(person), "length", len(person))
print('- person["name"]', person["name"], type(person["name"]))
print('- person["age"]', person["age"], type(person["age"]))
print('- person["score"]', person["score"], type(person["score"]))
print('- person["langs"]', person["langs"], type(person["langs"]))
InfoDb = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "John",
"LastName": "Mortensen",
"DOB": "October 21",
"Residence": "San Diego",
"Email": "jmortensen@powayusd.com",
"Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Sunny",
"LastName": "Naidu",
"DOB": "August 2",
"Residence": "Temecula",
"Email": "snaidu@powayusd.com",
"Owns_Cars": ["4Runner"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Shane",
"LastName": "Lopez",
"DOB": "February 27",
"Residence": "San Diego",
"Email": "???@powayusd.com",
"Owns_Cars": ["2021-Insight"]
})
# Print the data structure
print(InfoDb)
# This jupyter cell has dependencies on one or more cells above
# print function: given a dictionary of InfoDb content
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Cars: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
print()
# for loop algorithm iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record) # call to function
for_loop() # call to function
# This jupyter cell has dependencies on one or more cells above
# while loop algorithm contains an initial n and an index incrementing statement (n += 1)
def while_loop():
print("While loop output\n")
i = 0
while i < len(InfoDb):
record = InfoDb[i]
print_data(record)
i += 1
return
while_loop()
# This jupyter cell has dependencies on one or more cells above
# recursion algorithm loops incrementing on each call (n + 1) until exit condition is met
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
return
print("Recursive loop output\n")
recursive_loop(0)
def flood_fill(grid, row, col, target_color, replacement_color):
# Check if the current cell is out of bounds or already filled with replacement_color
if (
row < 0
or row >= len(grid)
or col < 0
or col >= len(grid[0])
or grid[row][col] == replacement_color
):
return
# Check if the current cell has the target color
if grid[row][col] == target_color:
# Fill the current cell with the replacement color
grid[row][col] = replacement_color
# Recursively fill the neighboring cells
flood_fill(grid, row + 1, col, target_color, replacement_color)
flood_fill(grid, row - 1, col, target_color, replacement_color)
flood_fill(grid, row, col + 1, target_color, replacement_color)
flood_fill(grid, row, col - 1, target_color, replacement_color)
grid = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 0, 0, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
]
# Choose a target color and replacement color
target_color = 0
replacement_color = 2
# Choose a starting point for flood fill
start_row = 2
start_col = 2
# Perform flood fill from the starting point
flood_fill(grid, start_row, start_col, target_color, replacement_color)
# Print the updated grid
for row in grid:
print(row)