reading-notes

Class 7 - Guided Project 2

Lab 7 - Ten Thousand 2

Overview

Extend Ten Thousand game started in previous class to get the game in playable state.

Feature Tasks and Requirements

Implementation Notes

User Acceptance Tests

Stretch Goals

Code Challenge

Feature Tasks

1. Kth from End

Example

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

Unit Tests

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.

Stretch Goal

Once you’ve achieved a working solution, implement a method that finds the node at the middle of the Linked List.

Written Class Notes

Read 7 - Ten Thousand 2

Resources Link/Pages

Bookmark and Review

Answer

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

Reading Questions

  1. 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.

    • Variable scope in Python defines the regions of code where a particular variable can be accessed or modified.
    • There are two main scopes: local scope and global scope.
      • Local Scope: Variables defined within a function have local scope and are accessible only within that function.
      • Global Scope: Variables defined outside any function have global scope and can be accessed from any part of the code.

    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.

  2. How do the global and nonlocal keywords work in Python, and in what situations might you use them?

    • Global Keyword: Used to modify global names within functions. It maps a local variable to the corresponding global variable.
      • Example:
        global counter
        counter = 0
        
        def update_counter():
            global counter
            counter += 1
        
        update_counter()
        print(counter)  # Output: 1
        
    • Nonlocal Keyword: Used to modify nonlocal names within nested functions. It allows a nested function to access and modify a variable in its enclosing function’s scope.
      • Example:
        def outer_function():
            var = 100
        
            def inner_function():
                nonlocal var
                var += 50
        
            inner_function()
            print(var)  # Output: 150
        
        outer_function()
        

    When to Use:

    • Global: Use when you need to modify a global variable within a function. However, it’s generally considered better practice to avoid excessive use of global variables.
    • Nonlocal: Use when dealing with nested functions and you want to modify a variable in the enclosing (nonlocal) scope.
  3. In your own words, describe the purpose and importance of Big O notation in the context of algorithm analysis.

    • Big O notation is the assessment of an algorithm’s efficiency by expressing its upper bound or worst-case performance concerning input size. It allows for the comparison of algorithms, aiding in the selection of the most suitable approach for specific scenarios.
  4. 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}")
      

Things I want to know more about

Learning Journal

Reflection

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?