<jupyter_start><jupyter_text>Assignment 2 | Programming LogicReminder: in all of the assignments this semester, the answer is not the only consideration, but also how you get to it. It's OK (suggested even!) to use the internet for help. But you _should_ be able to answer all of these questions using only the programming techniques you have learned in class and from the readings.A few keys for success:- Avoid manual data entry- Emphasize logic and clarity- Use comments, docstrings, and descriptive variable names- In general, less code is better. But if more lines of code makes your program easier to read or understand what its doing, then go for it.  Problem 1 Write a Python program to count the number of even and odd numbers from a list of numbers. Test your code by running it on a list of integers from 1 to 9. No need to make this a function unless you want to.<jupyter_code>'''Create a list of numbers from 1 to 9'''
nums = range(1, 10)

'''Count the even numbers'''
evens = sum([1 if num % 2 == 0 else 0 for num in nums])
odds = len(nums) - evens

'''Print the results'''
print('Even numbes:\t{}\nOdd numbers:\t{}'.format(evens, odds))<jupyter_output>Even numbes:	4
Odd numbers:	5<jupyter_text>Problem 2Write a Python function that takes a list of numbers and returns a list containing only the even numbers from the original list. Test your function by running it on a list of integers from 1 to 9.<jupyter_code>'''Define the function'''
def find_evens(lst):
    '''
    takes a list of numbers and 
    returns a list containing only the 
    even numbers from the original list
    '''
    return [num for num in lst if num % 2 == 0]

'''Test the function'''
find_evens(nums)<jupyter_output><empty_output><jupyter_text>Problem 31. Create a function that accepts a list of integers as an argument and returns a list of floats which equals each number as a fraction of the sum of all the items in the original list.2. Next, create a second function which is the same as the first, but limit each number in the output list to two decimals.3. Create another function which builds on the previous one by allowing a "user" pass in an argument that defines the number of decimal places to use in the output list.4. Test each of these functions with a list of integers<jupyter_code>'''Define the function'''
def extract_floats(lst):
    '''
    Accepts a list of integers as an argument 
    and returns a list of floats which equals 
    each number as a fraction of the sum of 
    all the items in the original list.
    '''
    # Get the total numbers
    counts = len(lst)
    return [num / counts for num in lst]

    
'''Define another function'''
def extract_floats_2(lst):
    '''
    Same as the first, but limit each number 
    in the output list to two decimals
    '''
    # Get the total numbers
    counts = len(lst)
    return [round(num / counts, 2)  for num in lst]

'''Define the third function'''
def extract_floats_3(lst, num_decimals):
    '''
    Allowing a "user" pass in an argument 
    that defines the number of decimal places to use in the output list.
    '''
    # Get the total numbers
    counts = len(lst)
    return [round(num / counts, num_decimals) for num in lst]

'''Testing with a list of integers from 1 to 9'''
# extract_floats
print(extract_floats(nums))
# extract_floats_2
print(extract_floats_2(nums))
# extract_floats_3
print(extract_floats_3(nums, 5))<jupyter_output>[0.1111111111111111, 0.2222222222222222, 0.3333333333333333, 0.4444444444444444, 0.5555555555555556, 0.6666666666666666, 0.7777777777777778, 0.8888888888888888, 1.0]
[0.11, 0.22, 0.33, 0.44, 0.56, 0.67, 0.78, 0.89, 1.0]
[0.11111, 0.22222, 0.33333, 0.44444, 0.55556, 0.66667, 0.77778, 0.88889, 1.0]<jupyter_text>Problem 4A prime number is any whole number greater than 1 that has no positive divisors besides 1 and itself. In other words, a prime number must be:1. an integer2. greater than 13. divisible only by 1 and itself.Write a function is_prime(n) that accepts an argument `n` and returns `True` (boolean) if `n` is a prime number and `False` if n is not prime. For example, `is_prime(11)` should return `True` and `is_prime(12)` should return `False`.<jupyter_code>'''Define the is_prime function'''
def is_prime(n):
    '''
    Accepts an argument n and returns True (boolean) 
    if n is a prime number and False if n is not prime
    '''
    if type(n) != int or n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True
'''Testing'''
print("-1", is_prime(-1))
print("1", is_prime(1))
print("2", is_prime(2))
print("2.0", is_prime(2.0))
print("3", is_prime(3))
print("4", is_prime(4))
print("5", is_prime(5))
print("6", is_prime(6))
print("7", is_prime(7))
print("8", is_prime(8))
print("9", is_prime(9))
print("11", is_prime(11))
print("12", is_prime(12))
print("23", is_prime(23))<jupyter_output>-1 False
1 False
2 True
2.0 False
3 True
4 False
5 True
6 False
7 True
8 False
9 False
11 True
12 False
23 True<jupyter_text>Problem 5 1. Create a class called `Housing`, and add the following attributes to it:  - type  - area  - number of bedrooms  - value (price)  - year built.2. Create two instances of your class and populate their attributes (make 'em up)3. Create a method called `rent()` that calculates the estimated monthly rent for each house (assume that monthly rent is 0.4% of the value of the house)4. Print the rent for both instances.<jupyter_code>'''Create the class'''
class Housing:
    def __init__(self, _type, area, n_beds, value, year):
        self._type = _type
        self.area = area
        self.n_beds = n_beds
        self.value = value
        self.year = year
        
    def rent(self):
        return self.value * 0.004
'''Create two instances'''
house1 = Housing('townhome', 'south', 2, 380000, 2005)
house2 = Housing('condo', 'east', 4, 493000, 2018)
'''Print the rent'''
print("Rent of house 1:", house1.rent())
print("Rent of house 2:", house2.rent())<jupyter_output>Rent of house 1: 1520.0
Rent of house 2: 1972.0
{'fraction_non_alphanumeric': 0.09932242604528177, 'fraction_numerical': 0.060981655924640554, 'mean_word_length': 3.8493589743589745, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '627156', 'license': 'CNRI-Python', 'n_tokens_mistral': 2143, 'n_tokens_neox': 1870, 'n_words': 843, 'path': 'assignments/assignment_2/assignment_2_mengying_ju.ipynb', 'repo_name': 'hua-mike/CYPLAN255', 'size': '6051'}