diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b69bcf7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/ignore diff --git a/12_09_practice.py b/12_09_practice.py index 7914563..034b629 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,29 +1,363 @@ +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: + 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 -#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 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 -#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. +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 + +# ------------------------------------- # + +# 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:])) diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb new file mode 100644 index 0000000..327711c --- /dev/null +++ b/regex_excercises.ipynb @@ -0,0 +1,1379 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "lbrLwTw4r9jM" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mtZQDWxxos3J" + }, + "source": [ + "# 30 REGULAR EXPRESSION EXERCISES!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6jEYHJH3os3Q" + }, + "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": 1509, + "metadata": { + "id": "ghj1l7sQos3R", + "trusted": true + }, + "outputs": [], + "source": [ + "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" + }, + "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": 2155, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cXaMTykzos3V", + "outputId": "a73c9558-3ac4-44e9-ad0f-685a8f627983", + "trusted": true + }, + "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'[a-zA-Z0-9]'\n", + " # Use the pattern to search for ....\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\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" + }, + "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": 2185, + "metadata": { + "id": "1LHEOOMkos3X", + "trusted": true + }, + "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", + " 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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AQRTCmJZos3Z" + }, + "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": 2214, + "metadata": { + "id": "H1X2NPQYos3a", + "trusted": true + }, + "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", + " 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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E902uTcRos3a" + }, + "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": 2242, + "metadata": { + "id": "xeva0fRNos3b", + "trusted": true + }, + "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", + " 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", + "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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "whnPaW2kos3c" + }, + "source": [ + "5) Write a Python program that matches a string that has an a followed by three 'b'" + ] + }, + { + "cell_type": "code", + "execution_count": 2320, + "metadata": { + "id": "0c_7S3h_os3d", + "trusted": true + }, + "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", + " 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! # 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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V4AsjJiCos3e" + }, + "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": 2346, + "metadata": { + "id": "VjA5CXi1os3e", + "trusted": true + }, + "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", + " 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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kX7sLRavos3f" + }, + "source": [ + "7) Write a Python program to find sequences of lowercase letters joined with a underscore." + ] + }, + { + "cell_type": "code", + "execution_count": 2371, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rbxk35n7os3g", + "outputId": "3ddca986-1bd6-45f1-902c-eee85e8853e8", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched\n", + "Not matched\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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\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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H4sat-Rfos3g" + }, + "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": 2395, + "metadata": { + "id": "oeUiVHGVos3h", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched\n", + "Found a match!\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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!\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. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RgNBl7aEos3h" + }, + "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": 2418, + "metadata": { + "id": "YbCsos7Jos3i", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched\n", + "Not matched\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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", + "print(text_match(\"accddbbjjjb\")) # Found a match!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0oHZnx0Gos3i" + }, + "source": [ + "10) Write a Python program that matches a word at the beginning of a string." + ] + }, + { + "cell_type": "code", + "execution_count": 2440, + "metadata": { + "id": "1tlZiGMaos3j", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mG0jspZFos3j" + }, + "source": [ + "11) Write a Python program that matches a word at the end of string, with optional punctuation." + ] + }, + { + "cell_type": "code", + "execution_count": 2461, + "metadata": { + "id": "cmhnhbZnos3j", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched\n", + "Not matched\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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\n", + "# πŸ“ Notes: \n", + "# any num of chars zero or more times\n", + "# but has a word with optinal punctuation (.)\n", + "# no spce at end" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DBgbyYHXos3k" + }, + "source": [ + "12) Write a Python program that matches a word containing 'z'" + ] + }, + { + "cell_type": "code", + "execution_count": 2481, + "metadata": { + "id": "WxuMRyCbos3k", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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\n", + "# πŸ“ Notes: \n", + "# 'factored out' the z\n", + "# any num of chars before or after\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V_9_nk8Kos3l" + }, + "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": 2500, + "metadata": { + "id": "VEhCCh7yos3l", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L0lV07OCos3m" + }, + "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": 2518, + "metadata": { + "id": "E9NcoWDhos3m", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\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!\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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EvKJ8WOoos3n" + }, + "source": [ + "15) Write a Python program where a string will start with a specific number. " + ] + }, + { + "cell_type": "code", + "execution_count": 2535, + "metadata": { + "id": "nWp5QysTos3n", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched\n" + ] + } + ], + "source": [ + "# Solution\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', 5)) # True\n", + "print(match_num('6-2345861', 5)) # False" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r_I1cbvios3t" + }, + "source": [ + "16) Write a Python program to remove leading zeros from an IP address" + ] + }, + { + "cell_type": "code", + "execution_count": 2551, + "metadata": { + "id": "cRLX4hc3os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "216.8.94.196\n" + ] + } + ], + "source": [ + "# Solution\n", + "def rewrite_ip(ip):\n", + " return re.sub(r\"0\", \"\", ip) #find, #sub, #source\n", + "\n", + "ip = \"216.08.094.196\"\n", + "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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZCn9r3o2os3u" + }, + "source": [ + "17) Write a Python program to check for a number at the end of a string." + ] + }, + { + "cell_type": "code", + "execution_count": 2566, + "metadata": { + "id": "Q5t9U6n8os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def end_num(string):\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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0tM4eW8Wos3v" + }, + "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": 2580, + "metadata": { + "id": "7QGwzVxDos3v", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "12\n", + "13\n", + "345\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_digits(string):\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) # looking to return results on new line\n", + "# Number of length 1 to 3\n", + "# 1\n", + "# 12\n", + "# 13\n", + "# 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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "43nNb7d_os3v" + }, + "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'" + ] + }, + { + "cell_type": "code", + "execution_count": 2593, + "metadata": { + "id": "KIVHxWN9os3w", + "trusted": true + }, + "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", + " 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", + "# 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!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XjdROmMhos3w" + }, + "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'" + ] + }, + { + "cell_type": "code", + "execution_count": 2605, + "metadata": { + "id": "vWViImYios3w", + "trusted": true + }, + "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", + " 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\n", + "\n", + "# Notes: \n", + "# Returns tuple\n", + "# (s,e) end not inclusive" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lLRotk-Oos3x" + }, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 2616, + "metadata": { + "id": "XU8613rTos3x", + "trusted": true + }, + "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", + " 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", + "find_all_matches(pattern, text)\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uvAY6v7Oos3x" + }, + "source": [ + "22) Write a Python program to find the occurrence and position of the substrings within a string." + ] + }, + { + "cell_type": "code", + "execution_count": 2626, + "metadata": { + "id": "UFg6rXJnos3y", + "trusted": true + }, + "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", + " 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", + "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\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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sb3zKxE-os3y" + }, + "source": [ + "23) Write a Python program to replace whitespaces with an underscore and vice versa." + ] + }, + { + "cell_type": "code", + "execution_count": 2635, + "metadata": { + "id": "nUxR5Xvjos3z", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python_Exercises\n", + "Python Exercises\n" + ] + } + ], + "source": [ + "text = 'Python Exercises'\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", + "spaces = under_swap(text)\n", + "print(spaces)\n", + "\n", + "reversed = under_swap(spaces)\n", + "print(reversed)\n", + "\n", + "# 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." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "grqoW-pgos3z" + }, + "source": [ + "24) Write a Python program to extract year, month and date from a an url. " + ] + }, + { + "cell_type": "code", + "execution_count": 2643, + "metadata": { + "id": "K2-pacqmos3z", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('2016', '09', '02')]\n" + ] + } + ], + "source": [ + "# Solution\n", + "def extract_date(url):\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')]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gyD5AZW7os3z" + }, + "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": 2650, + "metadata": { + "id": "bo-jpYqZos30", + "trusted": true + }, + "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: 02-01-2026\n" + ] + } + ], + "source": [ + "# Solution # 🌟 Good question\n", + "def change_date_format(dt):\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", + "print(\"New date in DD-MM-YYYY Format: \",change_date_format(dt1)) # New date in DD-MM-YYYY Format: 02-01-2026" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IQ2SNL-gos30" + }, + "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": 2656, + "metadata": { + "id": "2hP4Twe0os31", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Python', 'PHP')\n" + ] + } + ], + "source": [ + "# Sample strings.\n", + "def print_words_starting_with_P(words):\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')\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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MEUMKMn0os31" + }, + "source": [ + "27) Write a Python program to separate and print the numbers of a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 2661, + "metadata": { + "id": "xG8c9PtUos32", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "20\n", + "30\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_all_numbers(text):\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", + "print_all_numbers(text)\n", + "# 10\n", + "# 20\n", + "# 30" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "krdKu3Mvos32" + }, + "source": [ + "28) Write a Python program to find all words starting with 'a' or 'e' in a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 2665, + "metadata": { + "id": "3hsR5278os32", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['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", + " 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", + "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']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CxfIZ5o9os33" + }, + "source": [ + "29) Write a Python program to separate and print the numbers and their position of a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 2668, + "metadata": { + "id": "XIuiRvmAos33", + "trusted": true + }, + "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", + " 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", + "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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JguO0fUjos34" + }, + "source": [ + "30) Write a Python program to abbreviate 'Road' as 'Rd.' in a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 2670, + "metadata": { + "id": "VJE1Xm2Bos34", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "21 Ramkrishna Rd.\n" + ] + } + ], + "source": [ + "# Solution\n", + "def abbreviate_road(street):\n", + " replace = re.sub(\"Road\",\"Rd.\", street)\n", + " return replace\n", + "\n", + "street = '21 Ramkrishna Road'\n", + "\n", + "print(abbreviate_road(street)) # 21 Ramkrishna Rd.\n" + ] + } + ], + "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 +} 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 diff --git a/xml_append_remove_add_exercise.py b/xml_append_remove_add_exercise.py new file mode 100644 index 0000000..409408c --- /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() +