# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Hands-on Python Exercises
#
# ## Functions
#
#
# Functions allow a programmer to break a problem into pieces that can be reused. They can also help a programmer focus on one part a larger problem at a time. As a result, writing functions is often an important part of developing larger pieces of software. 
#
# ### Objectives
#
# The exercises in this section will help you practice these skills:
#
# - Define a function for later use 
# - Pass one or more values into a function
# - Write docstring (documentation) for a function
# - Perform a complex calculation within a function
# - Return one or more results from a function 
# - Call a function that you have defined previously
#

# ### Exercise: Median of Three Values 
#
#
# Write a function that takes the lengths of the two shorter sides of a right triangle as its parameters. Return the hypotenuse of the triangle, computed using Pythagorean theorem, as the function’s result. Include a main program that reads the lengths of the shorter sides of a right triangle from the user, uses your function to compute the length of the hypotenuse, and displays the result.

# +
def median(a, b, c):
    """Docstring placeholder"""
    pass 

def compute_median(a, b, c):
    """Display the median of three values"""
    median(a, b, c)
    pass


# +
# # %load solutions/median.py
# -

# ## Loops 
#
#
# ### Objectives
#
# - Complete exercises using `for`, `while` loops
# - Use Assertion
# - In some cases the exercise specifies what type of loop to use. In other cases you must make this decision yourself.
#
# ### Exercise 1: Square Root
#
#
# Write a program that implements Newton's method to compute and display the square root of a number:
#
# $$x_{n+1}=\frac{1}{2}\left ( x_n + \frac{a}{x_n} \right )$$
#
#
# The algorithm for Newton’s method follows:
#
# - Read x from the user
# - Initialize guess to x/2 
# - While guess is not good enough do 
#   - Update guess to be the average of guess and x/guess
#
#
# ---
#
# **NOTE:**
#
# When this algorithm completes, guess contains an approximation of the square
# root. The quality of the approximation depends on how you define **good enough**. For this exercise' solution, guess can be considered good enough when the absolute value of the difference between `guess x guess` and `x` is less than or equal to $10^{-12}$.
#
# ---
#

# +
# # %load solutions/square_root.py
# -

# ### Exercise 2: Decimal to Binary
#
# Write a program that converts a decimal (base 10) number to binary (base 2). Read the decimal number from the user as an integer and then use the division algorithm shown below to perform the conversion. When the algorithm completes, result contains the binary representation of the number. Display the result, along with an appropriate message.
#
# The algorithm works as follows:
#
# - Let `result` be an empty string 
# - Let `q` represent the number to convert 
# - **repeat** 
#     - Set `r` equal to the remainder when q is divided by 2 
#     - Convert `r` to a string and add it to the beginning of `result` 
#     - Divide `q` by 2, discarding any remainder, and store the result back into `q`
# - **until** `q` is 0

# +
# # %load solutions/decimal_to_binary.py
# -

# ## Lists
#
# ### Objectives
#
# To solve the exercises in this section you should expect to:
#     
# - Create a variable that holds a list of values 
# - Modify a list by appending, inserting, updating and deleting elements
# - Search a list for a value
# - Display some or all of the values in a list
# - Write a function that takes a list as a parameter
# - Write a function that returns a list as its result
#

# ### Exercise: The Sieve of Eratosthenes 
#
#
# The Sieve of Eratosthenes is a technique that was developed more than 2,000 years ago to easily find all of the prime numbers between 2 and some limit, say 100. 
#
# A description of the algorithm follows:
#
# - Write down all of the numbers from 0 to the limit 
# - Cross out 0 and 1 because they are not prime
#
# - Set p equal to 2 
# - **While** p is less than the limit **do**
#   - Cross out all multiples of p (but not p itself) 
#   - Set p equal to the next number in the list that is not crossed out
# - Report all of the numbers that have not been crossed out as prime 
#
#
# ---
#
# **NOTE:**
#
# The key to this algorithm is that it is relatively easy to cross out every nth number on a piece of paper. This is also an easy task for a computer—a for loop can simulate this behavior when a third parameter is provided to the range function. When a number is crossed out, we know that it is no longer prime, but it still occupies space on the piece of paper, and must still be considered when computing later prime numbers. As a result, you should not simulate crossing out a number by removing it from the list. Instead, you should simulate crossing out a number by replacing it with 0. Then, once the algorithm completes, all of the non-zero values in the list are prime. 
#
# ---
#
#
# Create a Python program that uses this algorithm to display all of the prime
# numbers between 2 and a limit entered by the user. If you implement the algorithm correctly you should be able to display all of the prime numbers less than 1,000,000 in a few seconds.

# +
# # %load solutions/sieve.py
# -

# ## Dictionaries
#
# Dictionaries are another data structure that Python programmers can use to manage larger amounts of data. 
#
# Completing the exercises in this section will help you learn to:
#
# - Create a new variable that holds a dictionary 
# - Add a key-value pair to a dictionary
# - Update the value associated with a key in a dictionary 
# - Iterate over all of the keys and/or values in a dictionary
# - Write functions that take dictionaries as parameters
#
#
# ### Exercise: Unique Characters
#
#
# Create a program that determines and displays the number of unique characters in a string entered by the user. For example, `Hello, World!` has 10 unique characters while `zzz` has only one unique character. Use a dictionary or set to solve this problem.

# +
# # %load solutions/unique_chars.py
# -

# ## Going Further
#
# - [Python General Guide](https://ncar-hackathons.github.io/python-general/intro)
# - [The Python Tutorial](https://docs.python.org/3/tutorial/index.html)

StarcoderdataJupyterScriptsDedupFiltered