From 79c85cdcaebc8b052b970eb19ff2474a25f2499b Mon Sep 17 00:00:00 2001 From: William Rivera Date: Fri, 9 Dec 2022 20:18:42 -0500 Subject: [PATCH 01/10] solved questions --- 12_09_practice.py | 174 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 168 insertions(+), 6 deletions(-) diff --git a/12_09_practice.py b/12_09_practice.py index 7914563..fb43de8 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,29 +1,191 @@ +# TODO Question 1 #Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. +def make_it_big(list_input) -> list: + for index, num in enumerate(list_input): # enumerate to keep track + if num > 0: + list_input.insert(index, "big") # insert at index big + list_input.pop(index + 1) # old num shifts up, delete that one + return list_input # return list +print(make_it_big([12, -7, 0, 2])) + +# main takeaway, list methods to not need to be assigned back to list, they directly mutate the list + +# ------------------------------------------ # + +# TODO Question 2 #Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). +# 1, 1, 1 becomes 1, 1, 3 + +def count_positive(list_input) -> list: + count = 0 + for num in list_input: + if num > 0: + count += 1 + list_input.pop(-1) + list_input.append(count) + return list_input + + +print(count_positive([-1, 1, 1, 1])) # 3 positive +# -------------------------------- # + +# TODO Question 3 #SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 +def sum_list(list_input) -> list: + return sum(list_input) + +print(sum_list([0, -1, 1])) +print(sum_list([12, -2, 10])) + +# ----------------------------- # + +# TODO Question 4 #Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 +def ave_list(list_input) -> list: + return sum(list_input)/len(list_input) +print(ave_list([1, 2, 3, 4])) + +# --------- # +# TODO Question 5 #Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 -#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. -# -#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. +def len_list(list_input) -> list: + return len(list_input) + +print(len_list([1, 2, 3, 4])) + +# TODO Question 6 +# Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. +# If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. + +def min_list(list_input) -> list: # I thought if I specify I wouldn't need to use try: except:, still keeping it in tho + try: return min(list_input) # Fancy + except: ValueError + return 1 + +print(min_list([-1, -2, -3])) +print(min_list([])) + +# ----------------------------- # + +# TODO Question 7 +# Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. +# If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. + +def max_list(list_input) -> list: + try: return max(list_input) + except: ValueError + return -1 + +print(max_list([1, 2, 3, 4])) +print(max_list([])) + +# ----------------------------------------------- # + +# TODO Question 8 #Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. +# dictionary + # keys: + # sum + # avg + # min + # len of list + +def ultimate_analyze(list_input) -> list: + dictionary = {"sum":0, "avg":0, "min":0, "max":0, "len":0} + list_input.sort() + dictionary["sum"] = sum(list_input) + dictionary["len"] = len(list_input) + dictionary["avg"] = dictionary["sum"] / dictionary["len"] # effeciency πŸ”₯πŸ‘„πŸ”₯ # this question was fun. + dictionary["max"] = list_input[-1] + dictionary["min"] = list_input[0] + return dictionary -#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. +print(ultimate_analyze([1, 2, 3, 4])) # sum: 10, avg: 2.5, min: 1, max: 4, len: 4 +print(ultimate_analyze([2, 4, 3, 1])) # testing sort -#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, β€œradar” is a palindrome, but β€œradix” is not a palindrome. +# ------------------------------------- # + +# TODO Question 9 +# ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. +# This challenge is known to appear during basic technical interviews. + +def reverse_list(list_input) -> list: + list_input.sort(reverse=True) + return list_input + +print(reverse_list([1, 2, 3, 4])) + +# ----------------------------- # + +# TODO Question 10 +# Ispalindrome- Given a string, write a python function to check if it is palindrome or not. +# A string is said to be palindrome if the reverse of the string is the same as string. For example, β€œradar” is a palindrome, but β€œradix” is not a palindrome. +def is_palindrome(string) -> str: # ❕ Good question. + list_comp = [char for char in string] # list comp [a, b, c] # easier than attempting to use .split() for chars with no spaces or a traditional for loop + list_comp_backwards = list_comp.copy() # copy, reverse [c, b, a] + list_comp_backwards.reverse() + for index in range(len(list_comp) - 1): # index will be same, but will chars? need to compare l_normal[0] = a; l_reversed[0] = c + return list_comp[index] == list_comp_backwards[index] # return false immediately if not true + return # otherwise return true + + +print(is_palindrome("radar")) +print(is_palindrome("radix")) + + +# ------------------------------------- # + +# TODO question 11 #Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: #If the number is a multiple of 3, print β€œFizz” instead of the number. #If the number is a multiple of 5, print β€œBuzz” instead of the number. #If the number is a multiple of 3 and 5, print β€œFizzBuzz” instead of the number. +def FizzBuzz(): + for num in range(101): + if num % 3 == 0: + if num % 5 == 0: + print("FizzBuzz") # 3 and 5 + else: + print("Fizz") # just 3 + elif num % 5 == 0: + print("Buzz") # only 5 + else: + print(num) + +FizzBuzz() + +# --------------------------------------------- # + +# TODO Question 12 + #Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, #F(0) = 0, F(1) = 1 #F(n) = F(n - 1) + F(n - 2), for n > 1. - #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. \ No newline at end of file + #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. + + +def Fibbonacci(target): # this took me hella long to figure out lol. But I did it just off of what I learned πŸ₯³ + start_list = [0, 1] # 0 and 1 kickstart + while start_list[-1] <= target: # while the last number in the list is less than or equal to the target we want to climb to + summation = sum(start_list[-2:]) # sum the list from the second to last, to last number πŸ‘€(list always reads from right to left) + start_list.append(summation) # append the list with the new sum + start_list.pop() # pop last num after exiting the loop b/c it will always be greater than target + num_string = ", ".join(str(num) for num in start_list) # join method just populates an otherwise empty string with iterables and some other flavor like whitespace and/or char + + print(f"Fibbonacci Sequence: {num_string}") # f string w/ num string embedded + +Fibbonacci(100) # will print 0, 1, 1, etc. It's the real deal + +# Main takeaway - the target here is the delimiter, it has nothing to do with any calculations. + + +# test_list = [0, 1] +# print(sum(test_list[-2:])) From e05e3f88612d56d3b986d20d3f8f6f8236664aec Mon Sep 17 00:00:00 2001 From: William Rivera Date: Mon, 12 Dec 2022 21:01:18 -0500 Subject: [PATCH 02/10] lambda homework for 12/12 --- will_hw_lambda.py | 171 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 will_hw_lambda.py diff --git a/will_hw_lambda.py b/will_hw_lambda.py new file mode 100644 index 0000000..6372e01 --- /dev/null +++ b/will_hw_lambda.py @@ -0,0 +1,171 @@ +from functools import reduce +from fractions import Fraction +# QUESTIONS + +# Slide 19 Question + +prog_lang = [ + ('Python', 3.8), + ('Java', 13), + ('JavaScript', 2019), + ('Scala', 2.13)] + +# 1 +# Sort the list by each languages version in ascending order + +prog_lang_version = sorted(prog_lang, key=lambda x: x[1]) +# returns new list sorted by defined iterable +# sorted(iterable, key) +# ascending by default +print(prog_lang_version) + +# 2 +# Sort the list by the length of the name of each language +# in descending order. + +prog_lang_length = sorted(prog_lang, key=lambda x: len(x[0]), reverse=True) +print(prog_lang_length) +# reverse=True == Descending + +# 3 +# Filter the list so that it only contains languages with 'a' in it. +prog_lang_a_only_filter = list( + filter(lambda language: "a" in language[0], prog_lang)) #func, arg +print(prog_lang_a_only_filter) + +# 4 +# Filter the list so that it only contains languages whos version is +# in integer form + +prog_lang_version_int = list( + filter(lambda language : type(language[1]) == int, prog_lang)) +print(prog_lang_version_int) + +# 5 +# transform list, tuples w/ +# lowercase, len of lang string +# can't mutate tuples, need to make new list + +prog_lang_lower_len = map( + lambda language : (language[0].lower(),len(language[0])), prog_lang) + +print(list(prog_lang_lower_len)) + +# the trick here is not to get caught up with trying to use multiple variables, +# all you need to do is have a variable for the given tuple, and assign +# multiple operations for given values in the tuple +# the given values being tuple of index 0 + +# 6 +# generate a tuple in the form +# (all languages, all versions) + +# DEBUG LIST COMP +#names = [language[0] for language in prog_lang] +#print(names) + +#lang = [language[1] for language in prog_lang] +#print(lang) + + +#prog_concat = lambda language: tuple(([language[0] for language in language], [language[1] for language in language])) +#print(prog_concat(prog_lang)) +# DEBUG LIST COMP + + +#EXPLINATION FOR LAMBDA FUNCTION BELOW: +# for all tuples: +# extracting all index 0 vals and concatenating +# extracting all index 1 vals and concatenating +# need to concat as str for both +# wrapped in parenthesis, implicitly defined tuple +# pro tip I learned πŸ’ͺ🏻 you can re-use the same container over and over in different ways if you wrap the function in parenthesis +# container : (operation(s) for container object [0], operation(s) for container object [1]) + +prog_concat_str = lambda language : (", ".join(str(language[0]) for language in language) , ", ".join(str(language[1]) for language in language)) # πŸ”₯ spicy +print(prog_concat_str(prog_lang)) + + +# HACKER-RANK Q'S + +# Map function +# Cubes of fibbonaci numbers + +cube = lambda x : x ** 3 + +def fibbonacci(n): + if n == 0: + return [] # return empty set b/c its asking for an amount of nums, not sequence in range(nums) + start = [0, 1] + for _ in range(n - 1): # same func I wrote before but this one doesn't want to include end range + start.append(sum(start[-2:])) + start.pop() + return start # returns list + +print(fibbonacci(5)) + +print(list(map(cube, fibbonacci(0)))) + + +# reduce +#n rep how many +# num / denom +# product +# LCM + +def product(fracs): + t = reduce(lambda x, y : x * y, fracs) # input from above func + # stored as t + return t.numerator, t.denominator + +# have to read on this module + + + + +# validate email address w/ a filter +# n for n emails +# print only valid emails +# name@name.extension +# max len of extension is 3 + + +def fun(s): + digest = '' + s + if "@" in digest and "." in digest: + at_finder = digest.find("@") + dot_finder = digest.find(".") + #print(digest[at_finder + 1:dot_finder]) + if digest[at_finder + 1 :dot_finder].isalnum() and digest.find("@")!= 0: + #print(digest[at_finder - 1]) + if len(digest[dot_finder + 1:]) <= 3 and digest[dot_finder + 1:].isalpha(): + digest = digest.replace("@", '') + digest = digest.replace(".", '') + #print(digest) + if "-" in digest: + digest = digest.replace("-", '') + if "_" in digest: + digest = digest.replace("_", '') + #print(digest) + if digest.isalnum(): + return s + else: + return False + else: + return False + else: + return False + else: + return False + +list_emails = ["daniyal@gmail.coma", + "its@gmail.com1", + "rase23@ha_ch.com", + "hotdog@hotdog.com", # 🌭 + "sone.com", + "@something.co1", # check extension - done + "@something.com"] # check front - done +list_comp_e = [fun(email) for email in list_emails] +print(list_comp_e) + +# this is super hacky. Regex would come in clutch but this works for now. \ No newline at end of file From f10cb1ee74975f4d1f0f45390b29f0b84db50701 Mon Sep 17 00:00:00 2001 From: William Rivera Date: Mon, 12 Dec 2022 21:11:13 -0500 Subject: [PATCH 03/10] Added Lambda Function Practie Problems Problem sets that involve Lambda Functions and the map, reduce, and filter functions. Added Lambda Function Practice Problems from Slides 19 and 20. Added Practice Problems from Hacker Rank. --- 12_09_practice.py | 172 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) diff --git a/12_09_practice.py b/12_09_practice.py index fb43de8..034b629 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,3 +1,175 @@ +from functools import reduce +from fractions import Fraction +# QUESTIONS + +# Slide 19 Question + +prog_lang = [ + ('Python', 3.8), + ('Java', 13), + ('JavaScript', 2019), + ('Scala', 2.13)] + +# 1 +# Sort the list by each languages version in ascending order + +prog_lang_version = sorted(prog_lang, key=lambda x: x[1]) +# returns new list sorted by defined iterable +# sorted(iterable, key) +# ascending by default +print(prog_lang_version) + +# 2 +# Sort the list by the length of the name of each language +# in descending order. + +prog_lang_length = sorted(prog_lang, key=lambda x: len(x[0]), reverse=True) +print(prog_lang_length) +# reverse=True == Descending + +# 3 +# Filter the list so that it only contains languages with 'a' in it. +prog_lang_a_only_filter = list( + filter(lambda language: "a" in language[0], prog_lang)) #func, arg +print(prog_lang_a_only_filter) + +# 4 +# Filter the list so that it only contains languages whos version is +# in integer form + +prog_lang_version_int = list( + filter(lambda language : type(language[1]) == int, prog_lang)) +print(prog_lang_version_int) + +# 5 +# transform list, tuples w/ +# lowercase, len of lang string +# can't mutate tuples, need to make new list + +prog_lang_lower_len = map( + lambda language : (language[0].lower(),len(language[0])), prog_lang) + +print(list(prog_lang_lower_len)) + +# the trick here is not to get caught up with trying to use multiple variables, +# all you need to do is have a variable for the given tuple, and assign +# multiple operations for given values in the tuple +# the given values being tuple of index 0 + +# 6 +# generate a tuple in the form +# (all languages, all versions) + +# DEBUG LIST COMP +#names = [language[0] for language in prog_lang] +#print(names) + +#lang = [language[1] for language in prog_lang] +#print(lang) + + +#prog_concat = lambda language: tuple(([language[0] for language in language], [language[1] for language in language])) +#print(prog_concat(prog_lang)) +# DEBUG LIST COMP + + +#EXPLINATION FOR LAMBDA FUNCTION BELOW: +# for all tuples: +# extracting all index 0 vals and concatenating +# extracting all index 1 vals and concatenating +# need to concat as str for both +# wrapped in parenthesis, implicitly defined tuple +# pro tip I learned πŸ’ͺ🏻 you can re-use the same container over and over in different ways if you wrap the function in parenthesis +# container : (operation(s) for container object [0], operation(s) for container object [1]) + +prog_concat_str = lambda language : (", ".join(str(language[0]) for language in language) , ", ".join(str(language[1]) for language in language)) # πŸ”₯ spicy +print(prog_concat_str(prog_lang)) + + +# HACKER-RANK Q'S + +# Map function +# Cubes of fibbonaci numbers + +cube = lambda x : x ** 3 + +def fibbonacci(n): + if n == 0: + return [] # return empty set b/c its asking for an amount of nums, not sequence in range(nums) + start = [0, 1] + for _ in range(n - 1): # same func I wrote before but this one doesn't want to include end range + start.append(sum(start[-2:])) + start.pop() + return start # returns list + +print(fibbonacci(5)) + +print(list(map(cube, fibbonacci(0)))) + + +# reduce +#n rep how many +# num / denom +# product +# LCM + +def product(fracs): + t = reduce(lambda x, y : x * y, fracs) # input from above func + # stored as t + return t.numerator, t.denominator + +# have to read on this module + + + + +# validate email address w/ a filter +# n for n emails +# print only valid emails +# name@name.extension +# max len of extension is 3 + + +def fun(s): + digest = '' + s + if "@" in digest and "." in digest: + at_finder = digest.find("@") + dot_finder = digest.find(".") + #print(digest[at_finder + 1:dot_finder]) + if digest[at_finder + 1 :dot_finder].isalnum() and digest.find("@")!= 0: + #print(digest[at_finder - 1]) + if len(digest[dot_finder + 1:]) <= 3 and digest[dot_finder + 1:].isalpha(): + digest = digest.replace("@", '') + digest = digest.replace(".", '') + #print(digest) + if "-" in digest: + digest = digest.replace("-", '') + if "_" in digest: + digest = digest.replace("_", '') + #print(digest) + if digest.isalnum(): + return s + else: + return False + else: + return False + else: + return False + else: + return False + +list_emails = ["daniyal@gmail.coma", + "its@gmail.com1", + "rase23@ha_ch.com", + "hotdog@hotdog.com", # 🌭 + "sone.com", + "@something.co1", # check extension - done + "@something.com"] # check front - done +list_comp_e = [fun(email) for email in list_emails] +print(list_comp_e) + +# this is super hacky. Regex would come in clutch but this works for now. + # TODO Question 1 #Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. def make_it_big(list_input) -> list: From c29459a69bf33abee9895781269fe95c7d4e783e Mon Sep 17 00:00:00 2001 From: William Rivera Date: Wed, 14 Dec 2022 11:04:09 -0500 Subject: [PATCH 04/10] added .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b69bcf7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/ignore From a57c469a891ea38f42d003b90e9e96593e722e27 Mon Sep 17 00:00:00 2001 From: William Rivera Date: Sat, 17 Dec 2022 07:39:43 -0500 Subject: [PATCH 05/10] Added clean copy of Sammi's regex_exercises --- regex_excercises.ipynb | 945 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 945 insertions(+) create mode 100644 regex_excercises.ipynb diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb new file mode 100644 index 0000000..7d1facd --- /dev/null +++ b/regex_excercises.ipynb @@ -0,0 +1,945 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "lbrLwTw4r9jM" + }, + "outputs": [], + "source": [] + }, + { + "metadata": { + "id": "mtZQDWxxos3J" + }, + "cell_type": "markdown", + "source": [ + "# 30 REGULAR EXPRESSION EXERCISES!" + ] + }, + { + "metadata": { + "id": "6jEYHJH3os3Q" + }, + "cell_type": "markdown", + "source": [ + "Regex, you never know when they might come in handy. It's one of the \"good programmer\"'s fundamental yet a few people actually masters them.\n", + "\n" + ] + }, + { + "metadata": { + "trusted": true, + "id": "ghj1l7sQos3R" + }, + "cell_type": "code", + "source": [ + "import re" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "_tO-OHUBos3T" + }, + "cell_type": "markdown", + "source": [ + "1) Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9)." + ] + }, + { + "metadata": { + "trusted": true, + "id": "cXaMTykzos3V", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "a73c9558-3ac4-44e9-ad0f-685a8f627983" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def is_allowed_specific_char(string):\n", + " # Define a regex pattern\n", + " pattern = r''\n", + " # Use the pattern to search for ....\n", + " re.function(pattern, text)\n", + " \n", + "print(is_allowed_specific_char(\"ABCDEFabcdef123450\")) # True\n", + "print(is_allowed_specific_char(\"*&%@#!}{\")) # False" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "None\n", + "None\n" + ] + } + ] + }, + { + "metadata": { + "id": "2oae-B8tos3W" + }, + "cell_type": "markdown", + "source": [ + "2) Write a Python program that matches a string that has an a followed by zero or more b's" + ] + }, + { + "metadata": { + "trusted": true, + "id": "1LHEOOMkos3X" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"ac\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"abbc\")) # Found a match!\n", + "print(text_match(\"bbc\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "AQRTCmJZos3Z" + }, + "cell_type": "markdown", + "source": [ + "3) Write a Python program that matches a string that has an a followed by one or more b's" + ] + }, + { + "metadata": { + "trusted": true, + "id": "H1X2NPQYos3a" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"ab\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "E902uTcRos3a" + }, + "cell_type": "markdown", + "source": [ + "4) Write a Python program that matches a string that has an a followed by zero or one 'b'" + ] + }, + { + "metadata": { + "trusted": true, + "id": "xeva0fRNos3b" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + " \n", + "print(text_match(\"ab\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"abbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Found a match!\n", + "print(text_match(\"bbc\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "whnPaW2kos3c" + }, + "cell_type": "markdown", + "source": [ + "5) Write a Python program that matches a string that has an a followed by three 'b'" + ] + }, + { + "metadata": { + "trusted": true, + "id": "0c_7S3h_os3d" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"abbb\")) # Found a match!\n", + "print(text_match(\"aabbbbbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Not matched\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "V4AsjJiCos3e" + }, + "cell_type": "markdown", + "source": [ + "6) Write a Python program that matches a string that has an a followed by two to three 'b'." + ] + }, + { + "metadata": { + "trusted": true, + "id": "VjA5CXi1os3e" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"ab\")) # Not matched\n", + "print(text_match(\"aabbbbbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "kX7sLRavos3f" + }, + "cell_type": "markdown", + "source": [ + "7) Write a Python program to find sequences of lowercase letters joined with a underscore." + ] + }, + { + "metadata": { + "trusted": true, + "id": "rbxk35n7os3g", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "3ddca986-1bd6-45f1-902c-eee85e8853e8" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"aab_cbbbc\")) # Found a match!\n", + "print(text_match(\"aab_Abbbc\")) # Not matched\n", + "print(text_match(\"Aaab_abbbc\")) # Not matched" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "None\n", + "None\n", + "None\n" + ] + } + ] + }, + { + "metadata": { + "id": "H4sat-Rfos3g" + }, + "cell_type": "markdown", + "source": [ + "8) Write a Python program to find the sequences of one upper case letter followed by lower case letters." + ] + }, + { + "metadata": { + "trusted": true, + "id": "oeUiVHGVos3h" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pas\n", + "\n", + "print(text_match(\"aab_cbbbc\")) # Not matched\n", + "print(text_match(\"aab_Abbbc\")) # Found a match!\n", + "print(text_match(\"Aaab_abbbc\")) # Found a match!" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "RgNBl7aEos3h" + }, + "cell_type": "markdown", + "source": [ + "9) Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'." + ] + }, + { + "metadata": { + "trusted": true, + "id": "YbCsos7Jos3i" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"aabbbbd\")) # Not matched\n", + "print(text_match(\"aabAbbbc\")) # Not matched\n", + "print(text_match(\"accddbbjjjb\")) # Found a match!" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "0oHZnx0Gos3i" + }, + "cell_type": "markdown", + "source": [ + "10) Write a Python program that matches a word at the beginning of a string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "1tlZiGMaos3j" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\" The quick brown fox jumps over the lazy dog.\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "mG0jspZFos3j" + }, + "cell_type": "markdown", + "source": [ + "11) Write a Python program that matches a word at the end of string, with optional punctuation." + ] + }, + { + "metadata": { + "trusted": true, + "id": "cmhnhbZnos3j" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog. \")) # Not matched\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "DBgbyYHXos3k" + }, + "cell_type": "markdown", + "source": [ + "12) Write a Python program that matches a word containing 'z'" + ] + }, + { + "metadata": { + "trusted": true, + "id": "WxuMRyCbos3k" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + " \n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"Python Exercises.\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "V_9_nk8Kos3l" + }, + "cell_type": "markdown", + "source": [ + "13) Write a Python program that matches a word containing 'z', not at the start or end of the word." + ] + }, + { + "metadata": { + "trusted": true, + "id": "VEhCCh7yos3l" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"Python Exercises.\")) # Not matched" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "L0lV07OCos3m" + }, + "cell_type": "markdown", + "source": [ + "14) Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores." + ] + }, + { + "metadata": { + "trusted": true, + "id": "E9NcoWDhos3m" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Not matched\n", + "print(text_match(\"Python_Exercises_1\")) # Found a match!" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "EvKJ8WOoos3n" + }, + "cell_type": "markdown", + "source": [ + "15) Write a Python program where a string will start with a specific number. " + ] + }, + { + "metadata": { + "trusted": true, + "id": "nWp5QysTos3n" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def match_num(string):\n", + " pass\n", + " \n", + "print(match_num('5-2345861')) # True\n", + "print(match_num('6-2345861')) # False" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "r_I1cbvios3t" + }, + "cell_type": "markdown", + "source": [ + "16) Write a Python program to remove leading zeros from an IP address" + ] + }, + { + "metadata": { + "trusted": true, + "id": "cRLX4hc3os3u" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def rewrite_ip(ip):\n", + " # your code here\n", + " pass\n", + "\n", + "ip = \"216.08.094.196\"\n", + "string = rewrite_ip(ip)\n", + "print(string) # 216.8.94.196" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "ZCn9r3o2os3u" + }, + "cell_type": "markdown", + "source": [ + "17) Write a Python program to check for a number at the end of a string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "Q5t9U6n8os3u" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def end_num(string):\n", + " pass\n", + "\n", + "print(end_num('abcdef')) # False\n", + "print(end_num('abcdef6')) # True" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "0tM4eW8Wos3v" + }, + "cell_type": "markdown", + "source": [ + "18) Write a Python program to search the numbers (0-9) of length between 1 to 3 in a given string. " + ] + }, + { + "metadata": { + "trusted": true, + "id": "7QGwzVxDos3v" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def print_digits(string):\n", + " pass\n", + "\n", + "string = \"Exercises number 1, 12, 13, and 345 are important\"\n", + "print_digits(string)\n", + "# Number of length 1 to 3\n", + "# 1\n", + "# 12\n", + "# 13\n", + "# 345" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "43nNb7d_os3v" + }, + "cell_type": "markdown", + "source": [ + "19) Write a Python program to search some literals strings in a string. \n", + "Sample text : 'The quick brown fox jumps over the lazy dog.'\n", + "Searched words : 'fox', 'dog', 'horse'" + ] + }, + { + "metadata": { + "trusted": true, + "id": "KIVHxWN9os3w" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def print_match(patterns, text):\n", + " pass\n", + "\n", + "\n", + "patterns = [ 'fox', 'dog', 'horse' ]\n", + "text = 'The quick brown fox jumps over the lazy dog.'\n", + "print_match(patterns, text)\n", + "# Searching for \"fox\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Matched!\n", + "# Searching for \"dog\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Matched!\n", + "# Searching for \"horse\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Not Matched!" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "XjdROmMhos3w" + }, + "cell_type": "markdown", + "source": [ + "20) Write a Python program to search a literals string in a string and also find the location within the original string where the pattern occurs\n", + "\n", + "Sample text : 'The quick brown fox jumps over the lazy dog.'\n", + "Searched words : 'fox'" + ] + }, + { + "metadata": { + "trusted": true, + "id": "vWViImYios3w" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def print_match_location(pattern, text):\n", + " pass\n", + "\n", + "pattern = 'fox'\n", + "text = 'The quick brown fox jumps over the lazy dog.'\n", + "print_match_location(pattern, text)\n", + "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "lLRotk-Oos3x" + }, + "cell_type": "markdown", + "source": [ + "21) Write a Python program to find the substrings within a string.\n", + "\n", + "Sample text :\n", + "\n", + "'Python exercises, PHP exercises, C# exercises'\n", + "\n", + "Pattern :\n", + "\n", + "'exercises'\n", + "\n", + "Note: There are three instances of exercises in the input string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "XU8613rTos3x" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def find_all_matches(pattern, text):\n", + " pass\n", + "\n", + "text = 'Python exercises, PHP exercises, C# exercises'\n", + "pattern = 'exercises'\n", + "find_all_matches(pattern, text)\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "uvAY6v7Oos3x" + }, + "cell_type": "markdown", + "source": [ + "22) Write a Python program to find the occurrence and position of the substrings within a string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "UFg6rXJnos3y" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def find_all_matches_location(pattern, text):\n", + " pass\n", + "\n", + "\n", + "text = 'Python exercises, PHP exercises, C# exercises'\n", + "pattern = 'exercises'\n", + "find_all_matches_location(pattern, text)\n", + "# Found \"exercises\" at 7:16\n", + "# Found \"exercises\" at 22:31\n", + "# Found \"exercises\" at 36:45" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "Sb3zKxE-os3y" + }, + "cell_type": "markdown", + "source": [ + "23) Write a Python program to replace whitespaces with an underscore and vice versa." + ] + }, + { + "metadata": { + "trusted": true, + "id": "nUxR5Xvjos3z" + }, + "cell_type": "code", + "source": [ + "text = 'Python Exercises'\n", + "\n", + "# Your code\n", + "\n", + "print(text) # Python_Exercises\n", + "\n", + "\n", + "# Your code\n", + "\n", + "print(text) # Python Exercises" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "grqoW-pgos3z" + }, + "cell_type": "markdown", + "source": [ + "24) Write a Python program to extract year, month and date from a an url. " + ] + }, + { + "metadata": { + "trusted": true, + "id": "K2-pacqmos3z" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def extract_date(url):\n", + " pass\n", + " \n", + "url1= \"https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/\"\n", + "print(extract_date(url1)) # [('2016', '09', '02')]" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "gyD5AZW7os3z" + }, + "cell_type": "markdown", + "source": [ + "25) Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format." + ] + }, + { + "metadata": { + "trusted": true, + "id": "bo-jpYqZos30" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def change_date_format(dt):\n", + " pass\n", + "\n", + "dt1 = \"2026-01-02\"\n", + "print(\"Original date in YYY-MM-DD Format: \",dt1) # Original date in YYY-MM-DD Format: 2026-01-02\n", + "print(\"New date in DD-MM-YYYY Format: \",change_date_format(dt1)) # New date in DD-MM-YYYY Format: 02-01-2026" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "IQ2SNL-gos30" + }, + "cell_type": "markdown", + "source": [ + "26) Write a Python program to match if any words from a list of words starting with letter 'P'." + ] + }, + { + "metadata": { + "trusted": true, + "id": "2hP4Twe0os31" + }, + "cell_type": "code", + "source": [ + "# Sample strings.\n", + "def print_words_starting_with_P(words):\n", + " pass\n", + "\n", + "words = [\"Python PHP\", \"Java JavaScript\", \"c c++\"]\n", + "print_words_starting_with_P(words) # ('Python', 'PHP')" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "MEUMKMn0os31" + }, + "cell_type": "markdown", + "source": [ + "27) Write a Python program to separate and print the numbers of a given string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "xG8c9PtUos32" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def print_all_numbers(text):\n", + " pass\n", + "\n", + "# Sample string.\n", + "text = \"Ten 10, Twenty 20, Thirty 30\"\n", + "print_all_numbers(text)\n", + "# 10\n", + "# 20\n", + "# 30" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "krdKu3Mvos32" + }, + "cell_type": "markdown", + "source": [ + "28) Write a Python program to find all words starting with 'a' or 'e' in a given string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "3hsR5278os32" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def get_all_words_containing_a_or_e(text):\n", + " pass\n", + "\n", + "\n", + "# Input.\n", + "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", + "print(get_all_words_containing_a_or_e(text))\n", + "# ['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "CxfIZ5o9os33" + }, + "cell_type": "markdown", + "source": [ + "29) Write a Python program to separate and print the numbers and their position of a given string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "XIuiRvmAos33" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def print_all_numbers_and_their_position(text):\n", + " pass\n", + " \n", + "\n", + "# Input.\n", + "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", + "print_all_numbers_and_their_position(text)\n", + "# 50\n", + "# Index position: 62" + ], + "execution_count": null, + "outputs": [] + }, + { + "metadata": { + "id": "JguO0fUjos34" + }, + "cell_type": "markdown", + "source": [ + "30) Write a Python program to abbreviate 'Road' as 'Rd.' in a given string." + ] + }, + { + "metadata": { + "trusted": true, + "id": "VJE1Xm2Bos34" + }, + "cell_type": "code", + "source": [ + "# Solution\n", + "def abbreviate_road(street):\n", + " pass\n", + "\n", + "street = '21 Ramkrishna Road'\n", + "\n", + "print(abbreviate_road(street)) # 21 Ramkrishna Rd.\n" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From 85ebdfb6b7b61212026c1917f3deb3dfa7a068d6 Mon Sep 17 00:00:00 2001 From: William Rivera Date: Sat, 17 Dec 2022 08:35:38 -0500 Subject: [PATCH 06/10] Solved Questions 1-6 SOLVED: Questions 1-6 ADDED: Added a function to pass to determine if pattern matched or not TODO: Add or modify function to handle passing of patterns --- regex_excercises.ipynb | 772 +++++++++++++++++++++++++++-------------- 1 file changed, 509 insertions(+), 263 deletions(-) diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb index 7d1facd..e43afac 100644 --- a/regex_excercises.ipynb +++ b/regex_excercises.ipynb @@ -1,18 +1,4 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "code", @@ -24,153 +10,219 @@ "source": [] }, { + "cell_type": "markdown", "metadata": { "id": "mtZQDWxxos3J" }, - "cell_type": "markdown", "source": [ "# 30 REGULAR EXPRESSION EXERCISES!" ] }, { + "cell_type": "markdown", "metadata": { "id": "6jEYHJH3os3Q" }, - "cell_type": "markdown", "source": [ "Regex, you never know when they might come in handy. It's one of the \"good programmer\"'s fundamental yet a few people actually masters them.\n", "\n" ] }, { + "cell_type": "code", + "execution_count": 91, "metadata": { - "trusted": true, - "id": "ghj1l7sQos3R" + "id": "ghj1l7sQos3R", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ - "import re" - ], - "execution_count": null, - "outputs": [] + "import re\n", + "\n", + "# going to define a search results function lol\n", + "def return_matched_or_not(match_object):\n", + " if match_object != None:\n", + " return \"Found a match!\"\n", + " else:\n", + " return \"Not matched\"" + ] }, { + "cell_type": "markdown", "metadata": { "id": "_tO-OHUBos3T" }, - "cell_type": "markdown", "source": [ "1) Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9)." ] }, { + "cell_type": "code", + "execution_count": 92, "metadata": { - "trusted": true, - "id": "cXaMTykzos3V", "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "a73c9558-3ac4-44e9-ad0f-685a8f627983" + "id": "cXaMTykzos3V", + "outputId": "a73c9558-3ac4-44e9-ad0f-685a8f627983", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], "source": [ "# Solution\n", "def is_allowed_specific_char(string):\n", " # Define a regex pattern\n", - " pattern = r''\n", + " pattern = r'[a-zA-Z0-9]'\n", " # Use the pattern to search for ....\n", - " re.function(pattern, text)\n", + " search_pattern = re.search(pattern, string)\n", + " if search_pattern != None:\n", + " return True\n", + " else:\n", + " return False\n", " \n", "print(is_allowed_specific_char(\"ABCDEFabcdef123450\")) # True\n", - "print(is_allowed_specific_char(\"*&%@#!}{\")) # False" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "None\n", - "None\n" - ] - } + "print(is_allowed_specific_char(\"*&%@#!}{\")) # False\n", + "# πŸ“Additional Notes: \n", + "# Can I make a better more effecient t/f response other than this loop?\n" ] }, { + "cell_type": "markdown", "metadata": { "id": "2oae-B8tos3W" }, - "cell_type": "markdown", "source": [ "2) Write a Python program that matches a string that has an a followed by zero or more b's" ] }, { + "cell_type": "code", + "execution_count": 93, "metadata": { - "trusted": true, - "id": "1LHEOOMkos3X" + "id": "1LHEOOMkos3X", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"ab*\" # * == zero *or more*\n", + " search_pattern = re.search(pattern, text)\n", + " if search_pattern != None:\n", + " return \"Found a match!\" # πŸ‘€ be mindful not to assume prints with strings\n", + " else:\n", + " return \"Not matched\"\n", + "\n", "\n", "print(text_match(\"ac\")) # Found a match!\n", "print(text_match(\"abc\")) # Found a match!\n", "print(text_match(\"abbc\")) # Found a match!\n", "print(text_match(\"bbc\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "AQRTCmJZos3Z" }, - "cell_type": "markdown", "source": [ "3) Write a Python program that matches a string that has an a followed by one or more b's" ] }, { + "cell_type": "code", + "execution_count": 94, "metadata": { - "trusted": true, - "id": "H1X2NPQYos3a" + "id": "H1X2NPQYos3a", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Not matched\n", + "Not matched\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"ab+\" # one or more\n", + " search_pattern = re.search(pattern, text)\n", + " if search_pattern != None:\n", + " return \"Found a match!\"\n", + " else:\n", + " return \"Not matched\"\n", "\n", "print(text_match(\"ab\")) # Found a match!\n", "print(text_match(\"abc\")) # Found a match!\n", "print(text_match(\"ac\")) # Not matched\n", "print(text_match(\"bbc\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "E902uTcRos3a" }, - "cell_type": "markdown", "source": [ "4) Write a Python program that matches a string that has an a followed by zero or one 'b'" ] }, { + "cell_type": "code", + "execution_count": 95, "metadata": { - "trusted": true, - "id": "xeva0fRNos3b" + "id": "xeva0fRNos3b", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"ab?\" # ? == zero or one \n", + " search_pattern = re.search(pattern,text)\n", + " if search_pattern != None:\n", + " return \"Found a match!\"\n", + " else:\n", + " return \"Not matched\"\n", " \n", "print(text_match(\"ab\")) # Found a match!\n", "print(text_match(\"abc\")) # Found a match!\n", @@ -178,151 +230,204 @@ "print(text_match(\"aabbc\")) # Found a match!\n", "print(text_match(\"ac\")) # Found a match!\n", "print(text_match(\"bbc\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "whnPaW2kos3c" }, - "cell_type": "markdown", "source": [ "5) Write a Python program that matches a string that has an a followed by three 'b'" ] }, { + "cell_type": "code", + "execution_count": 108, "metadata": { - "trusted": true, - "id": "0c_7S3h_os3d" + "id": "0c_7S3h_os3d", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Not matched\n", + "Not matched\n", + "Not matched\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"ab{3}\"\n", + " matched_results = re.search(pattern, text)\n", + " return return_matched_or_not(matched_results)\n", + " \n", + " # if matched_results != None:\n", + " # return \"Found a match!\"\n", + " # else: \n", + " # return \"Not matched\"\n", "\n", "print(text_match(\"abbb\")) # Found a match!\n", - "print(text_match(\"aabbbbbc\")) # Found a match!\n", + "print(text_match(\"aabbbbbc\")) # Found a match! πŸ‘€πŸ‘€# this should not match\n", "print(text_match(\"aabbc\")) # Not matched\n", "print(text_match(\"ac\")) # Not matched\n", "print(text_match(\"bbc\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "V4AsjJiCos3e" }, - "cell_type": "markdown", "source": [ "6) Write a Python program that matches a string that has an a followed by two to three 'b'." ] }, { + "cell_type": "code", + "execution_count": 112, "metadata": { - "trusted": true, - "id": "VjA5CXi1os3e" + "id": "VjA5CXi1os3e", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched\n", + "Found a match!\n", + "Found a match!\n", + "Not matched\n", + "Not matched\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"ab{2,3}\" # πŸ‘€ end range inclusive ⭐\n", + " matched_results = re.search(pattern,text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"ab\")) # Not matched\n", "print(text_match(\"aabbbbbc\")) # Found a match!\n", "print(text_match(\"aabbc\")) # Found a match!\n", "print(text_match(\"ac\")) # Not matched\n", "print(text_match(\"bbc\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "kX7sLRavos3f" }, - "cell_type": "markdown", "source": [ "7) Write a Python program to find sequences of lowercase letters joined with a underscore." ] }, { + "cell_type": "code", + "execution_count": 113, "metadata": { - "trusted": true, - "id": "rbxk35n7os3g", "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "3ddca986-1bd6-45f1-902c-eee85e8853e8" + "id": "rbxk35n7os3g", + "outputId": "3ddca986-1bd6-45f1-902c-eee85e8853e8", + "trusted": true }, - "cell_type": "code", - "source": [ - "# Solution\n", - "def text_match(text):\n", - " pass\n", - "\n", - "print(text_match(\"aab_cbbbc\")) # Found a match!\n", - "print(text_match(\"aab_Abbbc\")) # Not matched\n", - "print(text_match(\"Aaab_abbbc\")) # Not matched" - ], - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "None\n", "None\n", "None\n" ] } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pass\n", + "\n", + "print(text_match(\"aab_cbbbc\")) # Found a match!\n", + "print(text_match(\"aab_Abbbc\")) # Not matched\n", + "print(text_match(\"Aaab_abbbc\")) # Not matched" ] }, { + "cell_type": "markdown", "metadata": { "id": "H4sat-Rfos3g" }, - "cell_type": "markdown", "source": [ "8) Write a Python program to find the sequences of one upper case letter followed by lower case letters." ] }, { + "cell_type": "code", + "execution_count": 114, "metadata": { - "trusted": true, - "id": "oeUiVHGVos3h" + "id": "oeUiVHGVos3h", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", - " pas\n", + " pass\n", "\n", "print(text_match(\"aab_cbbbc\")) # Not matched\n", "print(text_match(\"aab_Abbbc\")) # Found a match!\n", "print(text_match(\"Aaab_abbbc\")) # Found a match!" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "RgNBl7aEos3h" }, - "cell_type": "markdown", "source": [ "9) Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'." ] }, { + "cell_type": "code", + "execution_count": 115, "metadata": { - "trusted": true, - "id": "YbCsos7Jos3i" + "id": "YbCsos7Jos3i", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", @@ -331,25 +436,34 @@ "print(text_match(\"aabbbbd\")) # Not matched\n", "print(text_match(\"aabAbbbc\")) # Not matched\n", "print(text_match(\"accddbbjjjb\")) # Found a match!" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "0oHZnx0Gos3i" }, - "cell_type": "markdown", "source": [ "10) Write a Python program that matches a word at the beginning of a string." ] }, { + "cell_type": "code", + "execution_count": 116, "metadata": { - "trusted": true, - "id": "1tlZiGMaos3j" + "id": "1tlZiGMaos3j", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", @@ -357,25 +471,35 @@ "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\" The quick brown fox jumps over the lazy dog.\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "mG0jspZFos3j" }, - "cell_type": "markdown", "source": [ "11) Write a Python program that matches a word at the end of string, with optional punctuation." ] }, { + "cell_type": "code", + "execution_count": 117, "metadata": { - "trusted": true, - "id": "cmhnhbZnos3j" + "id": "cmhnhbZnos3j", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", @@ -384,25 +508,34 @@ "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\"The quick brown fox jumps over the lazy dog. \")) # Not matched\n", "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "DBgbyYHXos3k" }, - "cell_type": "markdown", "source": [ "12) Write a Python program that matches a word containing 'z'" ] }, { + "cell_type": "code", + "execution_count": 118, "metadata": { - "trusted": true, - "id": "WxuMRyCbos3k" + "id": "WxuMRyCbos3k", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", @@ -411,25 +544,34 @@ " \n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\"Python Exercises.\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "V_9_nk8Kos3l" }, - "cell_type": "markdown", "source": [ "13) Write a Python program that matches a word containing 'z', not at the start or end of the word." ] }, { + "cell_type": "code", + "execution_count": 119, "metadata": { - "trusted": true, - "id": "VEhCCh7yos3l" + "id": "VEhCCh7yos3l", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", @@ -437,25 +579,34 @@ "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\"Python Exercises.\")) # Not matched" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "L0lV07OCos3m" }, - "cell_type": "markdown", "source": [ "14) Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores." ] }, { + "cell_type": "code", + "execution_count": 120, "metadata": { - "trusted": true, - "id": "E9NcoWDhos3m" + "id": "E9NcoWDhos3m", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def text_match(text):\n", @@ -463,25 +614,34 @@ "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Not matched\n", "print(text_match(\"Python_Exercises_1\")) # Found a match!" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "EvKJ8WOoos3n" }, - "cell_type": "markdown", "source": [ "15) Write a Python program where a string will start with a specific number. " ] }, { + "cell_type": "code", + "execution_count": 121, "metadata": { - "trusted": true, - "id": "nWp5QysTos3n" + "id": "nWp5QysTos3n", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def match_num(string):\n", @@ -489,25 +649,33 @@ " \n", "print(match_num('5-2345861')) # True\n", "print(match_num('6-2345861')) # False" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "r_I1cbvios3t" }, - "cell_type": "markdown", "source": [ "16) Write a Python program to remove leading zeros from an IP address" ] }, { + "cell_type": "code", + "execution_count": 122, "metadata": { - "trusted": true, - "id": "cRLX4hc3os3u" + "id": "cRLX4hc3os3u", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], "source": [ "# Solution\n", "def rewrite_ip(ip):\n", @@ -517,25 +685,34 @@ "ip = \"216.08.094.196\"\n", "string = rewrite_ip(ip)\n", "print(string) # 216.8.94.196" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "ZCn9r3o2os3u" }, - "cell_type": "markdown", "source": [ "17) Write a Python program to check for a number at the end of a string." ] }, { + "cell_type": "code", + "execution_count": 123, "metadata": { - "trusted": true, - "id": "Q5t9U6n8os3u" + "id": "Q5t9U6n8os3u", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n", + "None\n" + ] + } + ], "source": [ "# Solution\n", "def end_num(string):\n", @@ -543,25 +720,25 @@ "\n", "print(end_num('abcdef')) # False\n", "print(end_num('abcdef6')) # True" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "0tM4eW8Wos3v" }, - "cell_type": "markdown", "source": [ "18) Write a Python program to search the numbers (0-9) of length between 1 to 3 in a given string. " ] }, { + "cell_type": "code", + "execution_count": 124, "metadata": { - "trusted": true, - "id": "7QGwzVxDos3v" + "id": "7QGwzVxDos3v", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Solution\n", "def print_digits(string):\n", @@ -574,15 +751,13 @@ "# 12\n", "# 13\n", "# 345" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "43nNb7d_os3v" }, - "cell_type": "markdown", "source": [ "19) Write a Python program to search some literals strings in a string. \n", "Sample text : 'The quick brown fox jumps over the lazy dog.'\n", @@ -590,11 +765,13 @@ ] }, { + "cell_type": "code", + "execution_count": 125, "metadata": { - "trusted": true, - "id": "KIVHxWN9os3w" + "id": "KIVHxWN9os3w", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Solution\n", "def print_match(patterns, text):\n", @@ -610,15 +787,13 @@ "# Matched!\n", "# Searching for \"horse\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", "# Not Matched!" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "XjdROmMhos3w" }, - "cell_type": "markdown", "source": [ "20) Write a Python program to search a literals string in a string and also find the location within the original string where the pattern occurs\n", "\n", @@ -627,11 +802,13 @@ ] }, { + "cell_type": "code", + "execution_count": 126, "metadata": { - "trusted": true, - "id": "vWViImYios3w" + "id": "vWViImYios3w", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Solution\n", "def print_match_location(pattern, text):\n", @@ -641,15 +818,13 @@ "text = 'The quick brown fox jumps over the lazy dog.'\n", "print_match_location(pattern, text)\n", "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "lLRotk-Oos3x" }, - "cell_type": "markdown", "source": [ "21) Write a Python program to find the substrings within a string.\n", "\n", @@ -665,11 +840,13 @@ ] }, { + "cell_type": "code", + "execution_count": 127, "metadata": { - "trusted": true, - "id": "XU8613rTos3x" + "id": "XU8613rTos3x", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Solution\n", "def find_all_matches(pattern, text):\n", @@ -681,25 +858,25 @@ "# Found \"exercises\"\n", "# Found \"exercises\"\n", "# Found \"exercises\"" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "uvAY6v7Oos3x" }, - "cell_type": "markdown", "source": [ "22) Write a Python program to find the occurrence and position of the substrings within a string." ] }, { + "cell_type": "code", + "execution_count": 128, "metadata": { - "trusted": true, - "id": "UFg6rXJnos3y" + "id": "UFg6rXJnos3y", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Solution\n", "def find_all_matches_location(pattern, text):\n", @@ -712,25 +889,34 @@ "# Found \"exercises\" at 7:16\n", "# Found \"exercises\" at 22:31\n", "# Found \"exercises\" at 36:45" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "Sb3zKxE-os3y" }, - "cell_type": "markdown", "source": [ "23) Write a Python program to replace whitespaces with an underscore and vice versa." ] }, { + "cell_type": "code", + "execution_count": 129, "metadata": { - "trusted": true, - "id": "nUxR5Xvjos3z" + "id": "nUxR5Xvjos3z", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python Exercises\n", + "Python Exercises\n" + ] + } + ], "source": [ "text = 'Python Exercises'\n", "\n", @@ -742,25 +928,33 @@ "# Your code\n", "\n", "print(text) # Python Exercises" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "grqoW-pgos3z" }, - "cell_type": "markdown", "source": [ "24) Write a Python program to extract year, month and date from a an url. " ] }, { + "cell_type": "code", + "execution_count": 130, "metadata": { - "trusted": true, - "id": "K2-pacqmos3z" + "id": "K2-pacqmos3z", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], "source": [ "# Solution\n", "def extract_date(url):\n", @@ -768,25 +962,34 @@ " \n", "url1= \"https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/\"\n", "print(extract_date(url1)) # [('2016', '09', '02')]" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "gyD5AZW7os3z" }, - "cell_type": "markdown", "source": [ "25) Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format." ] }, { + "cell_type": "code", + "execution_count": 131, "metadata": { - "trusted": true, - "id": "bo-jpYqZos30" + "id": "bo-jpYqZos30", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original date in YYY-MM-DD Format: 2026-01-02\n", + "New date in DD-MM-YYYY Format: None\n" + ] + } + ], "source": [ "# Solution\n", "def change_date_format(dt):\n", @@ -795,25 +998,25 @@ "dt1 = \"2026-01-02\"\n", "print(\"Original date in YYY-MM-DD Format: \",dt1) # Original date in YYY-MM-DD Format: 2026-01-02\n", "print(\"New date in DD-MM-YYYY Format: \",change_date_format(dt1)) # New date in DD-MM-YYYY Format: 02-01-2026" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "IQ2SNL-gos30" }, - "cell_type": "markdown", "source": [ "26) Write a Python program to match if any words from a list of words starting with letter 'P'." ] }, { + "cell_type": "code", + "execution_count": 132, "metadata": { - "trusted": true, - "id": "2hP4Twe0os31" + "id": "2hP4Twe0os31", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Sample strings.\n", "def print_words_starting_with_P(words):\n", @@ -821,25 +1024,25 @@ "\n", "words = [\"Python PHP\", \"Java JavaScript\", \"c c++\"]\n", "print_words_starting_with_P(words) # ('Python', 'PHP')" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "MEUMKMn0os31" }, - "cell_type": "markdown", "source": [ "27) Write a Python program to separate and print the numbers of a given string." ] }, { + "cell_type": "code", + "execution_count": 133, "metadata": { - "trusted": true, - "id": "xG8c9PtUos32" + "id": "xG8c9PtUos32", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Solution\n", "def print_all_numbers(text):\n", @@ -851,25 +1054,33 @@ "# 10\n", "# 20\n", "# 30" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "krdKu3Mvos32" }, - "cell_type": "markdown", "source": [ "28) Write a Python program to find all words starting with 'a' or 'e' in a given string." ] }, { + "cell_type": "code", + "execution_count": 134, "metadata": { - "trusted": true, - "id": "3hsR5278os32" + "id": "3hsR5278os32", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], "source": [ "# Solution\n", "def get_all_words_containing_a_or_e(text):\n", @@ -880,25 +1091,25 @@ "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", "print(get_all_words_containing_a_or_e(text))\n", "# ['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "CxfIZ5o9os33" }, - "cell_type": "markdown", "source": [ "29) Write a Python program to separate and print the numbers and their position of a given string." ] }, { + "cell_type": "code", + "execution_count": 135, "metadata": { - "trusted": true, - "id": "XIuiRvmAos33" + "id": "XIuiRvmAos33", + "trusted": true }, - "cell_type": "code", + "outputs": [], "source": [ "# Solution\n", "def print_all_numbers_and_their_position(text):\n", @@ -910,25 +1121,33 @@ "print_all_numbers_and_their_position(text)\n", "# 50\n", "# Index position: 62" - ], - "execution_count": null, - "outputs": [] + ] }, { + "cell_type": "markdown", "metadata": { "id": "JguO0fUjos34" }, - "cell_type": "markdown", "source": [ "30) Write a Python program to abbreviate 'Road' as 'Rd.' in a given string." ] }, { + "cell_type": "code", + "execution_count": 136, "metadata": { - "trusted": true, - "id": "VJE1Xm2Bos34" + "id": "VJE1Xm2Bos34", + "trusted": true }, - "cell_type": "code", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], "source": [ "# Solution\n", "def abbreviate_road(street):\n", @@ -937,9 +1156,36 @@ "street = '21 Ramkrishna Road'\n", "\n", "print(abbreviate_road(street)) # 21 Ramkrishna Rd.\n" - ], - "execution_count": null, - "outputs": [] + ] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + }, + "vscode": { + "interpreter": { + "hash": "aabebee710dd5019ee905b134c48bb60ba029f425e93630a43ad944c92329650" + } + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 5ae94b93df8a98ff2e9d8f994cdf5554ebe12ffe Mon Sep 17 00:00:00 2001 From: William Rivera Date: Sun, 18 Dec 2022 17:38:30 -0500 Subject: [PATCH 07/10] Completed Exercises --- regex_excercises.ipynb | 428 +++++++++++++++++++++++++++++------------ 1 file changed, 308 insertions(+), 120 deletions(-) diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb index e43afac..327711c 100644 --- a/regex_excercises.ipynb +++ b/regex_excercises.ipynb @@ -30,7 +30,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 1509, "metadata": { "id": "ghj1l7sQos3R", "trusted": true @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 2155, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -106,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 2185, "metadata": { "id": "1LHEOOMkos3X", "trusted": true @@ -151,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 2214, "metadata": { "id": "H1X2NPQYos3a", "trusted": true @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 2242, "metadata": { "id": "xeva0fRNos3b", "trusted": true @@ -243,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 2320, "metadata": { "id": "0c_7S3h_os3d", "trusted": true @@ -264,7 +264,7 @@ "source": [ "# Solution\n", "def text_match(text):\n", - " pattern = r\"ab{3}\"\n", + " pattern = r\"ab{3,}\"\n", " matched_results = re.search(pattern, text)\n", " return return_matched_or_not(matched_results)\n", " \n", @@ -274,7 +274,7 @@ " # return \"Not matched\"\n", "\n", "print(text_match(\"abbb\")) # Found a match!\n", - "print(text_match(\"aabbbbbc\")) # Found a match! πŸ‘€πŸ‘€# this should not match\n", + "print(text_match(\"aabbbbbc\")) # Found a match! # at least 3 b's\n", "print(text_match(\"aabbc\")) # Not matched\n", "print(text_match(\"ac\")) # Not matched\n", "print(text_match(\"bbc\")) # Not matched" @@ -291,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 2346, "metadata": { "id": "VjA5CXi1os3e", "trusted": true @@ -334,7 +334,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 2371, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -348,20 +348,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"^[a-z]+_[a-z]+$\" \n", + " matched_results = re.search(pattern, text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"aab_cbbbc\")) # Found a match!\n", "print(text_match(\"aab_Abbbc\")) # Not matched\n", - "print(text_match(\"Aaab_abbbc\")) # Not matched" + "print(text_match(\"Aaab_abbbc\")) # Not matched\n", + "\n", + "# Notes:πŸ“ \n", + "# ^[a-z] has to start w/ lower, all the way through\n", + "# ^[a-z]* any num of chars, any chars in set\n", + "# [a-z]+ has to end with any num of lower chars" ] }, { @@ -375,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 2395, "metadata": { "id": "oeUiVHGVos3h", "trusted": true @@ -385,20 +392,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Not matched\n", + "Found a match!\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\".*[A-Z][a-z]+\" #^[A-Z][a-z]+\n", + " matched_results = re.match(pattern,text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"aab_cbbbc\")) # Not matched\n", - "print(text_match(\"aab_Abbbc\")) # Found a match!\n", - "print(text_match(\"Aaab_abbbc\")) # Found a match!" + "print(text_match(\"aab_Abbbc\")) # Found a match! \n", + "print(text_match(\"Aaab_abbbc\")) # Found a match!\n", + "# πŸ“ Notes: \n", + "# the current one will match any instance in which an upper case letter is \n", + "# followed by a lower case letter.\n", + "# the greyed out one will match only if the very first letter is an upper case\n", + "# followed by a-z. " ] }, { @@ -412,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 2418, "metadata": { "id": "YbCsos7Jos3i", "trusted": true @@ -422,16 +436,19 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Not matched\n", + "Not matched\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"a.*b$\"\n", + " matched_results = re.match(pattern, text)\n", + " return return_matched_or_not(matched_results)\n", + " \n", "\n", "print(text_match(\"aabbbbd\")) # Not matched\n", "print(text_match(\"aabAbbbc\")) # Not matched\n", @@ -449,7 +466,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 2440, "metadata": { "id": "1tlZiGMaos3j", "trusted": true @@ -459,15 +476,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\"^\\b\", text) # πŸ“ \\b only looks for chars at start or end of a word\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\" The quick brown fox jumps over the lazy dog.\")) # Not matched" @@ -484,7 +502,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 2461, "metadata": { "id": "cmhnhbZnos3j", "trusted": true @@ -494,20 +512,25 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\".*\\b\\.?[^\\s]$\",text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\"The quick brown fox jumps over the lazy dog. \")) # Not matched\n", - "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched" + "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched\n", + "# πŸ“ Notes: \n", + "# any num of chars zero or more times\n", + "# but has a word with optinal punctuation (.)\n", + "# no spce at end" ] }, { @@ -521,7 +544,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 2481, "metadata": { "id": "WxuMRyCbos3k", "trusted": true @@ -531,19 +554,24 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\".*(\\b|\\B)z.*\", text)\n", + " return return_matched_or_not(matched_results)\n", + " \n", "\n", " \n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", - "print(text_match(\"Python Exercises.\")) # Not matched" + "print(text_match(\"Python Exercises.\")) # Not matched\n", + "# πŸ“ Notes: \n", + "# 'factored out' the z\n", + "# any num of chars before or after\n" ] }, { @@ -557,7 +585,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 2500, "metadata": { "id": "VEhCCh7yos3l", "trusted": true @@ -567,15 +595,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\".*\\Bz.*\", text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\"Python Exercises.\")) # Not matched" @@ -592,7 +621,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 2518, "metadata": { "id": "E9NcoWDhos3m", "trusted": true @@ -602,18 +631,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Not matched\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\"^[\\w\\s]*$\", text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Not matched\n", - "print(text_match(\"Python_Exercises_1\")) # Found a match!" + "print(text_match(\"Python_Exercises_1\")) # Found a match!\n", + "# πŸ“ Notes: \n", + "# stats with word or space, ends with word or space but thats it\n", + "# both of any amount\n", + "# * + ? etc go by type or object in set, not by specific char" ] }, { @@ -627,7 +661,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 2535, "metadata": { "id": "nWp5QysTos3n", "trusted": true @@ -637,18 +671,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", - "def match_num(string):\n", - " pass\n", + "def match_num(string, num):\n", + " pattern = \"^\" + str(num) # no need for raw string in this case\n", + " matched_results = re.match(pattern, string)\n", + " return return_matched_or_not(matched_results)\n", " \n", - "print(match_num('5-2345861')) # True\n", - "print(match_num('6-2345861')) # False" + "print(match_num('5-2345861', 5)) # True\n", + "print(match_num('6-2345861', 5)) # False" ] }, { @@ -662,7 +698,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 2551, "metadata": { "id": "cRLX4hc3os3u", "trusted": true @@ -672,19 +708,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "216.8.94.196\n" ] } ], "source": [ "# Solution\n", "def rewrite_ip(ip):\n", - " # your code here\n", - " pass\n", + " return re.sub(r\"0\", \"\", ip) #find, #sub, #source\n", "\n", "ip = \"216.08.094.196\"\n", - "string = rewrite_ip(ip)\n", - "print(string) # 216.8.94.196" + "string = rewrite_ip(ip) # returns str\n", + "print(string) # 216.8.94.196\n", + "# πŸ“ If I wanted to make an exception for zeros in the first set of nums \n", + "# I would have to modify the function" ] }, { @@ -698,7 +735,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 2566, "metadata": { "id": "Q5t9U6n8os3u", "trusted": true @@ -708,16 +745,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Not matched\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def end_num(string):\n", - " pass\n", - "\n", + " search_results = re.search(r\"[0-9]$\", string)\n", + " return return_matched_or_not(search_results)\n", "print(end_num('abcdef')) # False\n", "print(end_num('abcdef6')) # True" ] @@ -733,24 +770,44 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 2580, "metadata": { "id": "7QGwzVxDos3v", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "12\n", + "13\n", + "345\n" + ] + } + ], "source": [ "# Solution\n", "def print_digits(string):\n", - " pass\n", + " search_results = re.findall(r\"[0-9]{1,3}\", string) \n", + " #print(search_results) debug\n", + " string_result = \"\\n\".join(result for result in search_results) # πŸ“ doc this vs finditer\n", + " print(string_result)\n", + "\n", "\n", "string = \"Exercises number 1, 12, 13, and 345 are important\"\n", - "print_digits(string)\n", + "print_digits(string) # looking to return results on new line\n", "# Number of length 1 to 3\n", "# 1\n", "# 12\n", "# 13\n", - "# 345" + "# 345\n", + "# πŸ“ find all will find all patterns, and save them in a list\n", + "# split will split at pattern and save to list\n", + "# find all treats pattern as object to pull \n", + "# split treats pattern as delimiter\n", + "# find all and split are inverses" ] }, { @@ -766,18 +823,45 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 2593, "metadata": { "id": "KIVHxWN9os3w", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Searching for \"fox\" in The quick brown fox jumps over the lazy dog.\n", + "Matched!\n", + "\n", + "Searching for \"dog\" in The quick brown fox jumps over the lazy dog.\n", + "Matched!\n", + "\n", + "Searching for \"horse\" in The quick brown fox jumps over the lazy dog.\n", + "Not Matched!\n", + "\n" + ] + } + ], "source": [ "# Solution\n", "def print_match(patterns, text):\n", - " pass\n", - "\n", + " for pattern in patterns:\n", + " matched_results = re.search(pattern, text)\n", + " result_string = f\"Searching for \\\"{pattern}\\\" in {text}\"\n", + " print(result_string)\n", + " if matched_results != None:\n", + " print(\"Matched!\\n\") # added \\n for readabliy\n", + " else:\n", + " print(\"Not Matched!\\n\")\n", + " \n", + "# πŸ“ Notes: \n", + "# not actually returning search result in first {}, just returning \n", + "# pattern we are on\n", "\n", + "# we have to re-run re.search for every instance\n", "patterns = [ 'fox', 'dog', 'horse' ]\n", "text = 'The quick brown fox jumps over the lazy dog.'\n", "print_match(patterns, text)\n", @@ -803,21 +887,35 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 2605, "metadata": { "id": "vWViImYios3w", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19\n" + ] + } + ], "source": [ "# Solution\n", "def print_match_location(pattern, text):\n", - " pass\n", + " search_results = re.search(pattern, text)\n", + " result_string = f\"Found \\\"{pattern}\\\" in \\\"{text}\\\" from {search_results.span()[0]} to {search_results.span()[1]}\"\n", + " print(result_string)\n", "\n", "pattern = 'fox'\n", "text = 'The quick brown fox jumps over the lazy dog.'\n", "print_match_location(pattern, text)\n", - "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19" + "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19\n", + "\n", + "# Notes: \n", + "# Returns tuple\n", + "# (s,e) end not inclusive" ] }, { @@ -841,16 +939,28 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 2616, "metadata": { "id": "XU8613rTos3x", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\"\n", + "Found \"exercises\"\n", + "Found \"exercises\"\n" + ] + } + ], "source": [ "# Solution\n", "def find_all_matches(pattern, text):\n", - " pass\n", + " compiled_list = re.findall(pattern, text)\n", + " for string in compiled_list:\n", + " print(f\"Found \\\"{string}\\\"\")\n", "\n", "text = 'Python exercises, PHP exercises, C# exercises'\n", "pattern = 'exercises'\n", @@ -871,16 +981,29 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 2626, "metadata": { "id": "UFg6rXJnos3y", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\" at 7:16\n", + "Found \"exercises\" at 22:31\n", + "Found \"exercises\" at 36:45\n" + ] + } + ], "source": [ "# Solution\n", "def find_all_matches_location(pattern, text):\n", - " pass\n", + " c_pattern = re.compile(pattern)\n", + " for match in c_pattern.finditer(text):\n", + " print(f\"Found \\\"{match.group()}\\\" at {match.start()}:{match.end()}\")\n", + "\n", "\n", "\n", "text = 'Python exercises, PHP exercises, C# exercises'\n", @@ -888,7 +1011,12 @@ "find_all_matches_location(pattern, text)\n", "# Found \"exercises\" at 7:16\n", "# Found \"exercises\" at 22:31\n", - "# Found \"exercises\" at 36:45" + "# Found \"exercises\" at 36:45\n", + "\n", + "# notes: \n", + "# experimenting with compiling a pattern and attaching methods to it \n", + "# experimenting with find iter\n", + "# find iter returning match objects, can use start and end per group\n" ] }, { @@ -902,7 +1030,7 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 2635, "metadata": { "id": "nUxR5Xvjos3z", "trusted": true @@ -912,22 +1040,32 @@ "name": "stdout", "output_type": "stream", "text": [ - "Python Exercises\n", + "Python_Exercises\n", "Python Exercises\n" ] } ], "source": [ "text = 'Python Exercises'\n", - "\n", + "def under_swap(text):\n", + " choices = [\" \", \"_\"]\n", + " if \" \" in text:\n", + " pass\n", + " elif \"_\" in text:\n", + " choices.reverse()\n", + " substitution = re.sub(choices[0], choices[1], text)\n", + " return substitution\n", "# Your code\n", "\n", - "print(text) # Python_Exercises\n", - "\n", + "spaces = under_swap(text)\n", + "print(spaces)\n", "\n", - "# Your code\n", + "reversed = under_swap(spaces)\n", + "print(reversed)\n", "\n", - "print(text) # Python Exercises" + "# Note: I'm assuming string will have either white spaces or underscores but not both. \n", + "# Second Note: trying to reverse a list with \\s causes a crash, python doesn't like that\n", + "# for some reason, so I opted for a literal textual representation of a white space." ] }, { @@ -941,7 +1079,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": 2643, "metadata": { "id": "K2-pacqmos3z", "trusted": true @@ -951,14 +1089,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "[('2016', '09', '02')]\n" ] } ], "source": [ "# Solution\n", "def extract_date(url):\n", - " pass\n", + " year_month_day = r\"([0-9]{4})/([0-9]{2})/([0-9]{2})\"\n", + " extract_search = re.findall(year_month_day, url)\n", + " return extract_search\n", " \n", "url1= \"https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/\"\n", "print(extract_date(url1)) # [('2016', '09', '02')]" @@ -975,7 +1115,7 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": 2650, "metadata": { "id": "bo-jpYqZos30", "trusted": true @@ -986,14 +1126,18 @@ "output_type": "stream", "text": [ "Original date in YYY-MM-DD Format: 2026-01-02\n", - "New date in DD-MM-YYYY Format: None\n" + "New date in DD-MM-YYYY Format: 02-01-2026\n" ] } ], "source": [ - "# Solution\n", + "# Solution # 🌟 Good question\n", "def change_date_format(dt):\n", - " pass\n", + " capture_year_month_day = r\"(\\d{4})-(\\d{2})-(\\d{2})\" # 1y - 2m - 3d\n", + " captured_result = re.search(capture_year_month_day, dt)\n", + " year, month, day = captured_result.groups() # πŸ‘€ search yields a match obj w/ groups\n", + " # print(year, month, day) debug\n", + " return f\"{day}-{month}-{year}\"\n", "\n", "dt1 = \"2026-01-02\"\n", "print(\"Original date in YYY-MM-DD Format: \",dt1) # Original date in YYY-MM-DD Format: 2026-01-02\n", @@ -1011,19 +1155,38 @@ }, { "cell_type": "code", - "execution_count": 132, + "execution_count": 2656, "metadata": { "id": "2hP4Twe0os31", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Python', 'PHP')\n" + ] + } + ], "source": [ "# Sample strings.\n", "def print_words_starting_with_P(words):\n", - " pass\n", + " pattern = r\"(\\bP[a-zA-Z]*)\" # has to start with P, but any chars after that is fine # 0\n", + " P_iter = re.compile(pattern) # 1\n", + " match_list = [tuple(P_iter.findall(word)) for word in words] # list of tuples # 2, 3\n", + " final_string = \" \".join(str(collection) for collection in match_list if len(collection) > 0) # 4\n", + " print(final_string)\n", + "\n", "\n", "words = [\"Python PHP\", \"Java JavaScript\", \"c c++\"]\n", - "print_words_starting_with_P(words) # ('Python', 'PHP')" + "print_words_starting_with_P(words) # ('Python', 'PHP')\n", + "# pattern becomes object # 0, 1\n", + "# pattern has find all method # 2\n", + "# makes a list of all words that match from original list # 2\n", + "# list converted to tuple # 3\n", + "# final string, tuple converted to string if tuple is not empty # 4\n", + "# print" ] }, { @@ -1037,16 +1200,28 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": 2661, "metadata": { "id": "xG8c9PtUos32", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "20\n", + "30\n" + ] + } + ], "source": [ "# Solution\n", "def print_all_numbers(text):\n", - " pass\n", + " search_results = re.finditer(r\"[0-9]+\",text) # 🌟\n", + " for result in search_results:\n", + " print(result.group())\n", "\n", "# Sample string.\n", "text = \"Ten 10, Twenty 20, Thirty 30\"\n", @@ -1067,7 +1242,7 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": 2665, "metadata": { "id": "3hsR5278os32", "trusted": true @@ -1077,15 +1252,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']\n" ] } ], "source": [ "# Solution\n", "def get_all_words_containing_a_or_e(text):\n", - " pass\n", - "\n", + " find_all_list = re.findall(r\"([ae][a-zA-Z]+)\",text) # πŸ‘€ ^ is w.r.t literal start of string, not word\n", + " return find_all_list\n", "\n", "# Input.\n", "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", @@ -1104,16 +1279,28 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": 2668, "metadata": { "id": "XIuiRvmAos33", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50\n", + "Index position: 62\n" + ] + } + ], "source": [ "# Solution\n", "def print_all_numbers_and_their_position(text):\n", - " pass\n", + " find_all_numbers = re.finditer(\"[0-9]+\", text)\n", + " for num in find_all_numbers: \n", + " print(num.group())\n", + " print(f\"Index position: {num.start()}\")\n", " \n", "\n", "# Input.\n", @@ -1134,7 +1321,7 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": 2670, "metadata": { "id": "VJE1Xm2Bos34", "trusted": true @@ -1144,14 +1331,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "21 Ramkrishna Rd.\n" ] } ], "source": [ "# Solution\n", "def abbreviate_road(street):\n", - " pass\n", + " replace = re.sub(\"Road\",\"Rd.\", street)\n", + " return replace\n", "\n", "street = '21 Ramkrishna Road'\n", "\n", From 4c7294117dcba04808e440115f93d3124dc4d605 Mon Sep 17 00:00:00 2001 From: William Rivera Date: Sun, 18 Dec 2022 17:38:30 -0500 Subject: [PATCH 08/10] Completed Regex Exercises FIX: Changed commit message from 'Completed Exercises' to the more aptly named message above. --- regex_excercises.ipynb | 428 +++++++++++++++++++++++++++++------------ 1 file changed, 308 insertions(+), 120 deletions(-) diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb index e43afac..327711c 100644 --- a/regex_excercises.ipynb +++ b/regex_excercises.ipynb @@ -30,7 +30,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 1509, "metadata": { "id": "ghj1l7sQos3R", "trusted": true @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 2155, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -106,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 2185, "metadata": { "id": "1LHEOOMkos3X", "trusted": true @@ -151,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 2214, "metadata": { "id": "H1X2NPQYos3a", "trusted": true @@ -195,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 2242, "metadata": { "id": "xeva0fRNos3b", "trusted": true @@ -243,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 2320, "metadata": { "id": "0c_7S3h_os3d", "trusted": true @@ -264,7 +264,7 @@ "source": [ "# Solution\n", "def text_match(text):\n", - " pattern = r\"ab{3}\"\n", + " pattern = r\"ab{3,}\"\n", " matched_results = re.search(pattern, text)\n", " return return_matched_or_not(matched_results)\n", " \n", @@ -274,7 +274,7 @@ " # return \"Not matched\"\n", "\n", "print(text_match(\"abbb\")) # Found a match!\n", - "print(text_match(\"aabbbbbc\")) # Found a match! πŸ‘€πŸ‘€# this should not match\n", + "print(text_match(\"aabbbbbc\")) # Found a match! # at least 3 b's\n", "print(text_match(\"aabbc\")) # Not matched\n", "print(text_match(\"ac\")) # Not matched\n", "print(text_match(\"bbc\")) # Not matched" @@ -291,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 2346, "metadata": { "id": "VjA5CXi1os3e", "trusted": true @@ -334,7 +334,7 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 2371, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -348,20 +348,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"^[a-z]+_[a-z]+$\" \n", + " matched_results = re.search(pattern, text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"aab_cbbbc\")) # Found a match!\n", "print(text_match(\"aab_Abbbc\")) # Not matched\n", - "print(text_match(\"Aaab_abbbc\")) # Not matched" + "print(text_match(\"Aaab_abbbc\")) # Not matched\n", + "\n", + "# Notes:πŸ“ \n", + "# ^[a-z] has to start w/ lower, all the way through\n", + "# ^[a-z]* any num of chars, any chars in set\n", + "# [a-z]+ has to end with any num of lower chars" ] }, { @@ -375,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 2395, "metadata": { "id": "oeUiVHGVos3h", "trusted": true @@ -385,20 +392,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Not matched\n", + "Found a match!\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\".*[A-Z][a-z]+\" #^[A-Z][a-z]+\n", + " matched_results = re.match(pattern,text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"aab_cbbbc\")) # Not matched\n", - "print(text_match(\"aab_Abbbc\")) # Found a match!\n", - "print(text_match(\"Aaab_abbbc\")) # Found a match!" + "print(text_match(\"aab_Abbbc\")) # Found a match! \n", + "print(text_match(\"Aaab_abbbc\")) # Found a match!\n", + "# πŸ“ Notes: \n", + "# the current one will match any instance in which an upper case letter is \n", + "# followed by a lower case letter.\n", + "# the greyed out one will match only if the very first letter is an upper case\n", + "# followed by a-z. " ] }, { @@ -412,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 2418, "metadata": { "id": "YbCsos7Jos3i", "trusted": true @@ -422,16 +436,19 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Not matched\n", + "Not matched\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " pattern = r\"a.*b$\"\n", + " matched_results = re.match(pattern, text)\n", + " return return_matched_or_not(matched_results)\n", + " \n", "\n", "print(text_match(\"aabbbbd\")) # Not matched\n", "print(text_match(\"aabAbbbc\")) # Not matched\n", @@ -449,7 +466,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 2440, "metadata": { "id": "1tlZiGMaos3j", "trusted": true @@ -459,15 +476,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\"^\\b\", text) # πŸ“ \\b only looks for chars at start or end of a word\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\" The quick brown fox jumps over the lazy dog.\")) # Not matched" @@ -484,7 +502,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 2461, "metadata": { "id": "cmhnhbZnos3j", "trusted": true @@ -494,20 +512,25 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\".*\\b\\.?[^\\s]$\",text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\"The quick brown fox jumps over the lazy dog. \")) # Not matched\n", - "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched" + "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched\n", + "# πŸ“ Notes: \n", + "# any num of chars zero or more times\n", + "# but has a word with optinal punctuation (.)\n", + "# no spce at end" ] }, { @@ -521,7 +544,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 2481, "metadata": { "id": "WxuMRyCbos3k", "trusted": true @@ -531,19 +554,24 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\".*(\\b|\\B)z.*\", text)\n", + " return return_matched_or_not(matched_results)\n", + " \n", "\n", " \n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", - "print(text_match(\"Python Exercises.\")) # Not matched" + "print(text_match(\"Python Exercises.\")) # Not matched\n", + "# πŸ“ Notes: \n", + "# 'factored out' the z\n", + "# any num of chars before or after\n" ] }, { @@ -557,7 +585,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 2500, "metadata": { "id": "VEhCCh7yos3l", "trusted": true @@ -567,15 +595,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\".*\\Bz.*\", text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", "print(text_match(\"Python Exercises.\")) # Not matched" @@ -592,7 +621,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 2518, "metadata": { "id": "E9NcoWDhos3m", "trusted": true @@ -602,18 +631,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Not matched\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def text_match(text):\n", - " pass\n", + " matched_results = re.match(r\"^[\\w\\s]*$\", text)\n", + " return return_matched_or_not(matched_results)\n", "\n", "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Not matched\n", - "print(text_match(\"Python_Exercises_1\")) # Found a match!" + "print(text_match(\"Python_Exercises_1\")) # Found a match!\n", + "# πŸ“ Notes: \n", + "# stats with word or space, ends with word or space but thats it\n", + "# both of any amount\n", + "# * + ? etc go by type or object in set, not by specific char" ] }, { @@ -627,7 +661,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 2535, "metadata": { "id": "nWp5QysTos3n", "trusted": true @@ -637,18 +671,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Found a match!\n", + "Not matched\n" ] } ], "source": [ "# Solution\n", - "def match_num(string):\n", - " pass\n", + "def match_num(string, num):\n", + " pattern = \"^\" + str(num) # no need for raw string in this case\n", + " matched_results = re.match(pattern, string)\n", + " return return_matched_or_not(matched_results)\n", " \n", - "print(match_num('5-2345861')) # True\n", - "print(match_num('6-2345861')) # False" + "print(match_num('5-2345861', 5)) # True\n", + "print(match_num('6-2345861', 5)) # False" ] }, { @@ -662,7 +698,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 2551, "metadata": { "id": "cRLX4hc3os3u", "trusted": true @@ -672,19 +708,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "216.8.94.196\n" ] } ], "source": [ "# Solution\n", "def rewrite_ip(ip):\n", - " # your code here\n", - " pass\n", + " return re.sub(r\"0\", \"\", ip) #find, #sub, #source\n", "\n", "ip = \"216.08.094.196\"\n", - "string = rewrite_ip(ip)\n", - "print(string) # 216.8.94.196" + "string = rewrite_ip(ip) # returns str\n", + "print(string) # 216.8.94.196\n", + "# πŸ“ If I wanted to make an exception for zeros in the first set of nums \n", + "# I would have to modify the function" ] }, { @@ -698,7 +735,7 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 2566, "metadata": { "id": "Q5t9U6n8os3u", "trusted": true @@ -708,16 +745,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n", - "None\n" + "Not matched\n", + "Found a match!\n" ] } ], "source": [ "# Solution\n", "def end_num(string):\n", - " pass\n", - "\n", + " search_results = re.search(r\"[0-9]$\", string)\n", + " return return_matched_or_not(search_results)\n", "print(end_num('abcdef')) # False\n", "print(end_num('abcdef6')) # True" ] @@ -733,24 +770,44 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 2580, "metadata": { "id": "7QGwzVxDos3v", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "12\n", + "13\n", + "345\n" + ] + } + ], "source": [ "# Solution\n", "def print_digits(string):\n", - " pass\n", + " search_results = re.findall(r\"[0-9]{1,3}\", string) \n", + " #print(search_results) debug\n", + " string_result = \"\\n\".join(result for result in search_results) # πŸ“ doc this vs finditer\n", + " print(string_result)\n", + "\n", "\n", "string = \"Exercises number 1, 12, 13, and 345 are important\"\n", - "print_digits(string)\n", + "print_digits(string) # looking to return results on new line\n", "# Number of length 1 to 3\n", "# 1\n", "# 12\n", "# 13\n", - "# 345" + "# 345\n", + "# πŸ“ find all will find all patterns, and save them in a list\n", + "# split will split at pattern and save to list\n", + "# find all treats pattern as object to pull \n", + "# split treats pattern as delimiter\n", + "# find all and split are inverses" ] }, { @@ -766,18 +823,45 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 2593, "metadata": { "id": "KIVHxWN9os3w", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Searching for \"fox\" in The quick brown fox jumps over the lazy dog.\n", + "Matched!\n", + "\n", + "Searching for \"dog\" in The quick brown fox jumps over the lazy dog.\n", + "Matched!\n", + "\n", + "Searching for \"horse\" in The quick brown fox jumps over the lazy dog.\n", + "Not Matched!\n", + "\n" + ] + } + ], "source": [ "# Solution\n", "def print_match(patterns, text):\n", - " pass\n", - "\n", + " for pattern in patterns:\n", + " matched_results = re.search(pattern, text)\n", + " result_string = f\"Searching for \\\"{pattern}\\\" in {text}\"\n", + " print(result_string)\n", + " if matched_results != None:\n", + " print(\"Matched!\\n\") # added \\n for readabliy\n", + " else:\n", + " print(\"Not Matched!\\n\")\n", + " \n", + "# πŸ“ Notes: \n", + "# not actually returning search result in first {}, just returning \n", + "# pattern we are on\n", "\n", + "# we have to re-run re.search for every instance\n", "patterns = [ 'fox', 'dog', 'horse' ]\n", "text = 'The quick brown fox jumps over the lazy dog.'\n", "print_match(patterns, text)\n", @@ -803,21 +887,35 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 2605, "metadata": { "id": "vWViImYios3w", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19\n" + ] + } + ], "source": [ "# Solution\n", "def print_match_location(pattern, text):\n", - " pass\n", + " search_results = re.search(pattern, text)\n", + " result_string = f\"Found \\\"{pattern}\\\" in \\\"{text}\\\" from {search_results.span()[0]} to {search_results.span()[1]}\"\n", + " print(result_string)\n", "\n", "pattern = 'fox'\n", "text = 'The quick brown fox jumps over the lazy dog.'\n", "print_match_location(pattern, text)\n", - "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19" + "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19\n", + "\n", + "# Notes: \n", + "# Returns tuple\n", + "# (s,e) end not inclusive" ] }, { @@ -841,16 +939,28 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 2616, "metadata": { "id": "XU8613rTos3x", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\"\n", + "Found \"exercises\"\n", + "Found \"exercises\"\n" + ] + } + ], "source": [ "# Solution\n", "def find_all_matches(pattern, text):\n", - " pass\n", + " compiled_list = re.findall(pattern, text)\n", + " for string in compiled_list:\n", + " print(f\"Found \\\"{string}\\\"\")\n", "\n", "text = 'Python exercises, PHP exercises, C# exercises'\n", "pattern = 'exercises'\n", @@ -871,16 +981,29 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 2626, "metadata": { "id": "UFg6rXJnos3y", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\" at 7:16\n", + "Found \"exercises\" at 22:31\n", + "Found \"exercises\" at 36:45\n" + ] + } + ], "source": [ "# Solution\n", "def find_all_matches_location(pattern, text):\n", - " pass\n", + " c_pattern = re.compile(pattern)\n", + " for match in c_pattern.finditer(text):\n", + " print(f\"Found \\\"{match.group()}\\\" at {match.start()}:{match.end()}\")\n", + "\n", "\n", "\n", "text = 'Python exercises, PHP exercises, C# exercises'\n", @@ -888,7 +1011,12 @@ "find_all_matches_location(pattern, text)\n", "# Found \"exercises\" at 7:16\n", "# Found \"exercises\" at 22:31\n", - "# Found \"exercises\" at 36:45" + "# Found \"exercises\" at 36:45\n", + "\n", + "# notes: \n", + "# experimenting with compiling a pattern and attaching methods to it \n", + "# experimenting with find iter\n", + "# find iter returning match objects, can use start and end per group\n" ] }, { @@ -902,7 +1030,7 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 2635, "metadata": { "id": "nUxR5Xvjos3z", "trusted": true @@ -912,22 +1040,32 @@ "name": "stdout", "output_type": "stream", "text": [ - "Python Exercises\n", + "Python_Exercises\n", "Python Exercises\n" ] } ], "source": [ "text = 'Python Exercises'\n", - "\n", + "def under_swap(text):\n", + " choices = [\" \", \"_\"]\n", + " if \" \" in text:\n", + " pass\n", + " elif \"_\" in text:\n", + " choices.reverse()\n", + " substitution = re.sub(choices[0], choices[1], text)\n", + " return substitution\n", "# Your code\n", "\n", - "print(text) # Python_Exercises\n", - "\n", + "spaces = under_swap(text)\n", + "print(spaces)\n", "\n", - "# Your code\n", + "reversed = under_swap(spaces)\n", + "print(reversed)\n", "\n", - "print(text) # Python Exercises" + "# Note: I'm assuming string will have either white spaces or underscores but not both. \n", + "# Second Note: trying to reverse a list with \\s causes a crash, python doesn't like that\n", + "# for some reason, so I opted for a literal textual representation of a white space." ] }, { @@ -941,7 +1079,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": 2643, "metadata": { "id": "K2-pacqmos3z", "trusted": true @@ -951,14 +1089,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "[('2016', '09', '02')]\n" ] } ], "source": [ "# Solution\n", "def extract_date(url):\n", - " pass\n", + " year_month_day = r\"([0-9]{4})/([0-9]{2})/([0-9]{2})\"\n", + " extract_search = re.findall(year_month_day, url)\n", + " return extract_search\n", " \n", "url1= \"https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/\"\n", "print(extract_date(url1)) # [('2016', '09', '02')]" @@ -975,7 +1115,7 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": 2650, "metadata": { "id": "bo-jpYqZos30", "trusted": true @@ -986,14 +1126,18 @@ "output_type": "stream", "text": [ "Original date in YYY-MM-DD Format: 2026-01-02\n", - "New date in DD-MM-YYYY Format: None\n" + "New date in DD-MM-YYYY Format: 02-01-2026\n" ] } ], "source": [ - "# Solution\n", + "# Solution # 🌟 Good question\n", "def change_date_format(dt):\n", - " pass\n", + " capture_year_month_day = r\"(\\d{4})-(\\d{2})-(\\d{2})\" # 1y - 2m - 3d\n", + " captured_result = re.search(capture_year_month_day, dt)\n", + " year, month, day = captured_result.groups() # πŸ‘€ search yields a match obj w/ groups\n", + " # print(year, month, day) debug\n", + " return f\"{day}-{month}-{year}\"\n", "\n", "dt1 = \"2026-01-02\"\n", "print(\"Original date in YYY-MM-DD Format: \",dt1) # Original date in YYY-MM-DD Format: 2026-01-02\n", @@ -1011,19 +1155,38 @@ }, { "cell_type": "code", - "execution_count": 132, + "execution_count": 2656, "metadata": { "id": "2hP4Twe0os31", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Python', 'PHP')\n" + ] + } + ], "source": [ "# Sample strings.\n", "def print_words_starting_with_P(words):\n", - " pass\n", + " pattern = r\"(\\bP[a-zA-Z]*)\" # has to start with P, but any chars after that is fine # 0\n", + " P_iter = re.compile(pattern) # 1\n", + " match_list = [tuple(P_iter.findall(word)) for word in words] # list of tuples # 2, 3\n", + " final_string = \" \".join(str(collection) for collection in match_list if len(collection) > 0) # 4\n", + " print(final_string)\n", + "\n", "\n", "words = [\"Python PHP\", \"Java JavaScript\", \"c c++\"]\n", - "print_words_starting_with_P(words) # ('Python', 'PHP')" + "print_words_starting_with_P(words) # ('Python', 'PHP')\n", + "# pattern becomes object # 0, 1\n", + "# pattern has find all method # 2\n", + "# makes a list of all words that match from original list # 2\n", + "# list converted to tuple # 3\n", + "# final string, tuple converted to string if tuple is not empty # 4\n", + "# print" ] }, { @@ -1037,16 +1200,28 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": 2661, "metadata": { "id": "xG8c9PtUos32", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "20\n", + "30\n" + ] + } + ], "source": [ "# Solution\n", "def print_all_numbers(text):\n", - " pass\n", + " search_results = re.finditer(r\"[0-9]+\",text) # 🌟\n", + " for result in search_results:\n", + " print(result.group())\n", "\n", "# Sample string.\n", "text = \"Ten 10, Twenty 20, Thirty 30\"\n", @@ -1067,7 +1242,7 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": 2665, "metadata": { "id": "3hsR5278os32", "trusted": true @@ -1077,15 +1252,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']\n" ] } ], "source": [ "# Solution\n", "def get_all_words_containing_a_or_e(text):\n", - " pass\n", - "\n", + " find_all_list = re.findall(r\"([ae][a-zA-Z]+)\",text) # πŸ‘€ ^ is w.r.t literal start of string, not word\n", + " return find_all_list\n", "\n", "# Input.\n", "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", @@ -1104,16 +1279,28 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": 2668, "metadata": { "id": "XIuiRvmAos33", "trusted": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50\n", + "Index position: 62\n" + ] + } + ], "source": [ "# Solution\n", "def print_all_numbers_and_their_position(text):\n", - " pass\n", + " find_all_numbers = re.finditer(\"[0-9]+\", text)\n", + " for num in find_all_numbers: \n", + " print(num.group())\n", + " print(f\"Index position: {num.start()}\")\n", " \n", "\n", "# Input.\n", @@ -1134,7 +1321,7 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": 2670, "metadata": { "id": "VJE1Xm2Bos34", "trusted": true @@ -1144,14 +1331,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "21 Ramkrishna Rd.\n" ] } ], "source": [ "# Solution\n", "def abbreviate_road(street):\n", - " pass\n", + " replace = re.sub(\"Road\",\"Rd.\", street)\n", + " return replace\n", "\n", "street = '21 Ramkrishna Road'\n", "\n", From 423355499fbadbd159293a30845d7d194822cb63 Mon Sep 17 00:00:00 2001 From: William Rivera Date: Mon, 19 Dec 2022 11:41:12 -0500 Subject: [PATCH 09/10] Created Python XML Objects with dirty Movies.xml USING: 'Dirty' Movies xml Added Python Objects described below: ADDED: Anime Genre ADDED: Anime decade 2000s ADDED: Anime Movie 'Cowboy Bebop The Movie' --- xml_append_remove_add_exercise.py | 121 ++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 xml_append_remove_add_exercise.py diff --git a/xml_append_remove_add_exercise.py b/xml_append_remove_add_exercise.py new file mode 100644 index 0000000..793dcb5 --- /dev/null +++ b/xml_append_remove_add_exercise.py @@ -0,0 +1,121 @@ +import xml.etree.ElementTree as ET + +#### EXERCISE #### +## ADD, APPEND, REMOVE ## +### WITH ELEMENT TREE XML API ### + +def xml_check(): + print(f"\t\t\t {'#' * 10} XML PRINTOUT {'#' * 10}\n\n\n" ) + print(ET.tostring(collection, encoding='utf8').decode('utf8')) + + + +# hw, create new decade within anime +# add a movie +# also move batman movie into anime +# delete other copy of batman + +# Instructions: +# 1. +# Create an Anime Genre +# 2. +# Add a movie into the Anime Genre +# 3. +# Append Batman into the Anime Genre +# 4. +# Remove Batman from previous Genre + +# Note πŸ“ I'm working with the original +# 'Dirty' .XML file segment copied from the website + + +# SETTING UP THE OBJECTS # +##### +# First we have to import the module, +# preferably import with an alias, which I did. +##### + +##### +# next we have to assign a variable to the xml file +##### +movie_xml_file = ET.parse("xml/movies_dirty.xml") + +# we can verify a file object was created by printing +# print(movie_xml_file) + +#### +# Next we can assign a variable to the root of the file object +### + +# Because I know the root of this xml is collection, +# I'm going to just call it collection. +collection = movie_xml_file.getroot() + +# Let's take a peek into the xml file with the +# tostring() function + +entire_movies_xml = ET.tostring(collection, encoding='utf8').decode('utf8') + +print(entire_movies_xml) + +# collection is root + # decade is a tag + # decade has the attribute 'years' + +# movie is the next tag + # movie has several attributes: 'favorite', 'title' + +#movie has sub-tags +# only the format tag has an attribute: 'multiple' +# every other subtag just has string info + +## CREATING A GENRE +# We have to add genre tags to the root + +# sub element added to root, tag name genre, attribute 'category' is Anime +Anime = ET.SubElement(collection, 'genre') +Anime.attrib["category"] = 'Anime' + +Anime_Decade = ET.SubElement(Anime, 'decade') +Anime_Decade.attrib["years"] = '2000s' + +Anime_Movie = ET.SubElement(Anime_Decade, 'movie') +Anime_Movie.attrib["favorite"] = "False" +Anime_Movie.attrib["title"] = "" + +Anime_Format = ET.SubElement(Anime_Movie, 'format') +Anime_Format.attrib["multiple"] = "Yes" +Anime_Format.text = "Blu_Ray, DVD, Online" +year_rating_description = { + "year":"2001", + "rating":"R", + "description":"A space-faring bounty hunter group must stop a bio-terrorist."} +for item in year_rating_description: + Sub = ET.SubElement(Anime_Movie, item) + Sub.text = year_rating_description[item] + + + + +print("\t\t\t #### XML BEGIN ### ") +# print(entire_movies_xml) +print(ET.tostring(collection, encoding='utf8').decode('utf8')) # 🌟 +# list, dict + +# ⚠ I learned that you have to make these on-the-fly, +# they can't be stored and re run if you want to see +# the most updated info about your XML file. +# To prevent storage I've created a function vs assigning to a variable + +# Let's move batman to the anime section. +# because decade comes after genre we have to make +# the decade subtag in the anime seciton. + + +# I guess if you had a lot of tags you can +# copy-paste a segment or write a function to +# auto-generate every time. I'm going to just write +# out the lines for now. + +xml_check() + From 8127dce0dfe68dda6ed8c45ce78fe35f14f71ec1 Mon Sep 17 00:00:00 2001 From: William Rivera Date: Mon, 19 Dec 2022 12:43:36 -0500 Subject: [PATCH 10/10] Completed xml exercise CHANGED: Added batman to Anime genre CHANGED: moved comments to more appropriate positions REMOVED: batman from Comedy genre KNOWN BUGS: xml file produced won't automatically nest xmls for me. --- xml_append_remove_add_exercise.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml_append_remove_add_exercise.py b/xml_append_remove_add_exercise.py index 793dcb5..409408c 100644 --- a/xml_append_remove_add_exercise.py +++ b/xml_append_remove_add_exercise.py @@ -117,5 +117,5 @@ def xml_check(): # auto-generate every time. I'm going to just write # out the lines for now. -xml_check() +#xml_check()