Extend Ten Thousand game started in previous class to get the game in playable state.
ll.kthFromEnd(k)
Input ll | Arg k | Output
——————————————|——-|——-
head -> {1} -> {3} -> {8} -> {2} -> X | 0 | 2
head -> {1} -> {3} -> {8} -> {2} -> X | 2 | 3
head -> {1} -> {3} -> {8} -> {2} -> X | 6 | Exception
Write tests for the following scenarios, and any other cases that help you ensure your code is working as expected.
Unit tests must be passing before you submit your final solution code.
Once you’ve achieved a working solution, implement a method that finds the node at the middle of the Linked List.
global and nonlocal keywords. You can skim the rest.Bookmark and Review
Statement on why this topic matter as it relates to what I’m studying in this module:
Learning the global and nonlocal keywords in Python is important because they are crucial for understanding and controlling variable scope
Explain the concept of variable scope in Python and describe the difference between local and global scope. Provide an example illustrating the usage of both.
Example:
# Global Scope
global_var = 10
def example_function():
# Local Scope
local_var = 5
print("Local Variable:", local_var)
print("Global Variable:", global_var)
example_function()
Output:
Local Variable: 5
Global Variable: 10
In this example, global_var has global scope and can be accessed inside the function, while local_var has local scope limited to the function.
How do the global and nonlocal keywords work in Python, and in what situations might you use them?
global counter
counter = 0
def update_counter():
global counter
counter += 1
update_counter()
print(counter) # Output: 1
def outer_function():
var = 100
def inner_function():
nonlocal var
var += 50
inner_function()
print(var) # Output: 150
outer_function()
When to Use:
In your own words, describe the purpose and importance of Big O notation in the context of algorithm analysis.
Based on the Rolling Dice Example, explain how you would simulate a dice roll using Python. Describe how you would use code to calculate the probability of rolling a specific number (e.g., the probability of rolling a 6) over a large number of trials.
To simulate a dice roll in Python, you can use the random package to generate a random number between 1 and 6. The first program example demonstrates this:
import random
print(random.randint(1, 6))
This generates and prints a random integer between 1 and 6.
For simulating multiple dice rolls and calculating the probability of rolling a specific number, like 6, you can create a function to roll a single die, then use a loop to roll the dice a specified number of times (e.g., 1000) and count how many times the desired outcome occurs. The second program example illustrates this:
import random
count = 0
def roll():
return random.randint(1, 6)
for i in range(1, 1001):
if roll() == 6:
count += 1
print(count)
In this example, the roll() function returns a random number between 1 and 6. The loop runs 1000 times, and each time the outcome is 6, the count variable is incremented. Finally, the program prints the count, representing the number of times 6 was rolled in 1000 attempts.
To calculate the probability, you divide the count of successful outcomes (rolling a 6) by the total number of trials (1000 in this case). The probability can be expressed as a fraction or percentage. For example:
probability = count / 1000
print(f"The probability of rolling a 6 is: {probability}")
Write a brief reflection on your learning today, or use the prompt below to get started.
Motivation and goals are an important part of adult learning; so is adapting to and gaining insight from things you did not anticipate. What would you say is the most unexpected part of this experience, and how have you learned from it?