Python Homework 2

# Put your name in the my_name variable.

my_name = 'My Name'

 

# Tuples

# 1. a. Create a tuple consisting of an int, a float, and a string

int_float_string = ___

# 1. b. Create a tuple with a single item

atomic_tuple = ___

 

# 2. Write a function that takes a pair and returns that pair

# as a tuple, reversed. 

# I.e., if you pass in (1, b) the return should be (b, a)

def swap(pair):

    # your implementation here

    pass

 

# Sets

# 3. Find:

#    a. the intersection of the given sets

#    b. the union of the given sets

#    c. the symmetric difference 

#      (which values appear in exactly one of the sets)

# Hint: help(set)

six_primes = {2, 3, 5, 7, 11, 13}

fibonacci = {1, 1, 2, 3, 5, 8, 13}

intersection = ___

union = ___

symm_diff = ___

 

# Regex

 

# 4. construct patterns that match:

#    a. a string that starts with a (positive) number

#    b. a string that ends with a word

# Hint: read the docs! Lots of examples online, too.

# Suggestion: create a few strings that fit the pattern,

#             and a few that don't, and make sure your

#             patterns match (or don't) those strings

# Patterns that work at the beginning/end of a string need "anchors"

import re

starts_with_number = r'___'

ends_with_word = r'___'

 

# These are considerably harder.  I'll talk more about regex in 

# the next class

# 5. construct patterns that match and capture as described:

#    a. matches a string that contains one or more integers, and

#       captures the first one found. Here, an integer is meant

#       to be a "standalone" number: "1235" is an integer, "a12345" is not,

#       "9.30" is not. Yiou'll want to test strings that start with

#       an integer (like "123 foo") as well as ones that do not (like

#       "  1234   ") as well as strings that don't contain any integers

#       (like "9.42")

#       Hint: r'\s' matches a single character of whitespace, 

#             r'.*' matches zero or more arbitrary characters

#             remember the "|" (or) operator

#             using "?:" at the beginning of a (group) means that you want

#             to isolate that group in the pattern, but not capture it

#    b. matches and captures a dollar amount. A dollar amount should

#       begin with a dollar sign ($) and a positive number, and may have cents

#       Capture just the numeric value -- don't include the dollar sign

#       Dollar amount examples: "$1", "$2.30"

#       Not dollar amounts: "$ 1", "$2.3", "$2x30"

#

# Notes:

#

# (a.) requires you to know about "greedy" vs. non-greedy matching,

#     which I did not cover in class. I will talk about this next class

#     but do feel free to look for this topic in the docs. The trick here

#     is to deal with strings that start immediately with an integer, as well

#     as ones that don't, and if a string doesn't, you don't want to "throw 

#     away" too much of the string as matching arbitrary characters, which is 

#     where greedy vs non-greedy matching comes into play.

# (b.) Read the docs!  Note, in particular, that the dot/period has a special 

#     meaning in regex (match any character) so you'll need to figure out how 

#     to match an actual dot/period.

first_integer = r'___'

show_me_the_money = r'___'

Need a custom answer at your budget?

This assignment has been answered 3 times in private sessions.

Or buy a ready solution below.

Buy ready solution

Solution previews

© 2024 Codify Tutor. All rights reserved