From 31f95f8933ff22ec2d0d572463848c8ff729b653 Mon Sep 17 00:00:00 2001 From: suneetha-2022 Date: Fri, 9 Dec 2022 21:54:54 -0500 Subject: [PATCH 01/22] first commit --- 12_09_practice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/12_09_practice.py b/12_09_practice.py index 7914563..849c571 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -26,4 +26,4 @@ #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. \ No newline at end of file From c1fe3b6eb58581e2b19b6c3b140619f685f2ea2b Mon Sep 17 00:00:00 2001 From: suneetha-2022 Date: Sun, 11 Dec 2022 22:23:36 -0500 Subject: [PATCH 02/22] Python Practice Problems --- #Count_Positives.py | 13 ++ ...actice.py => Python_Practice_Assignment.py | 178 +++++++++++++++--- avg_list.py | 11 ++ fibchk.py | 19 ++ fizz_buzz.py | 23 +++ list_length.py | 5 + list_suavmimax.py | 23 +++ make_it_big.py | 19 ++ max_list.py | 11 ++ min_list.py | 11 ++ palindrome.py | 12 ++ reverse_list.py | 14 ++ sum_list.py | 8 + 13 files changed, 318 insertions(+), 29 deletions(-) create mode 100644 #Count_Positives.py rename 12_09_practice.py => Python_Practice_Assignment.py (50%) create mode 100644 avg_list.py create mode 100644 fibchk.py create mode 100644 fizz_buzz.py create mode 100644 list_length.py create mode 100644 list_suavmimax.py create mode 100644 make_it_big.py create mode 100644 max_list.py create mode 100644 min_list.py create mode 100644 palindrome.py create mode 100644 reverse_list.py create mode 100644 sum_list.py diff --git a/#Count_Positives.py b/#Count_Positives.py new file mode 100644 index 0000000..9e377b4 --- /dev/null +++ b/#Count_Positives.py @@ -0,0 +1,13 @@ +#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). +def count_positives(array): + count = 0 + for val in array: + if val > 0: + count += 1 + array[len(array)-1] = count + return array +print(count_positives([-1,1,1,1])) + + + + diff --git a/12_09_practice.py b/Python_Practice_Assignment.py similarity index 50% rename from 12_09_practice.py rename to Python_Practice_Assignment.py index 849c571..00b24d0 100644 --- a/12_09_practice.py +++ b/Python_Practice_Assignment.py @@ -1,29 +1,149 @@ -#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]. - -#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). - -#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 - -#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 - -#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. - -#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. - -#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. - -#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. - -#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. - -#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 + +#Python Practice Problems Assignment +#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(array): + for i in range(len(array)): + if array[i]>0: + array[i]= "big" + return array +print(make_it_big([-1, 3, 5, -5])) + +#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). +def count_positives(array): + count = 0 + for val in array: + if val > 0: + count += 1 + array[len(array)-1] = count + return array +print(count_positives([-1,1,1,1])) + +#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 sumlist(lista): + sumtotal = 0 + for i in lista: + sumtotal = sumtotal + i + return sumtotal +a = print(sumlist([1, 2, 3,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 avglist(lista): + sumvalue = 0 + avgvalue = 0 + for i in lista: + sumvalue = sumvalue + i + avgvalue = sumvalue/len(lista) + return avgvalue + +avg1 = print(avglist([1,2,3,4])) + +#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 +def lenlist(list1): + return len(list1) +len1 = print(lenlist([1,2,3,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. +def min_list(list1): + if list1 == []: + return False + else: + return min(list1) + +min1 = print(min_list([1,2,3,4])) +min2 = print(min_list([-1,-2,-3])) +min3 = print(min_list([])) + +#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(list1): + if list1 == []: + return False + else: + return max(list1) + +max1 = print(max_list([1,2,3,4])) +max2 = print(max_list([-1,-2,-3])) +max3 = print(max_list([])) + +#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. +def d_suavmima(list1): + sumlist = sum(list1) + avglist = sum(list1)/len(list1) + minlist = min(list1) + maxlist = max(list1) + lenlist = len(list1) + list2 = ['sumlist', 'avglist', 'minlist', 'maxlist', 'lenlist'] + list3 = [sum(list1), sum(list1)/len(list1), min(list1), max(list1), len(list1)] + i = iter(list2) + j = iter(list3) + dic1 = dict(zip(i, j)) + return dic1 + +dict1 = print(d_suavmima([1,2,3,4])) + +#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(list1): + left = 0 + right = len(list1) - 1 + while (left < right): + temp = list1[left] + list1[left] = list1[right] + list1[right] = temp + left+=1 + right-=1 + return list1 + +revlist1 = print(reverse_list([1,2,3,4,5])) + +#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 palindromchk(str1): + revstr1 = "" + for i in str1: + revstr1 = i + revstr1 + if (str1 == revstr1): + return 'Yes' + else: + return 'No' + +chk1 = print(palindromchk('12321')) + +#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(x): + i =1 + for i in range(x+1): + if i%3 == 0 and i%5 != 0: + i = 'Fizz' + print(i) + elif (i%5 == 0 and i%3 != 0): + i = 'Buzz' + print(i) + elif i%3 == 0 and i%5 == 0: + i = 'FizzBuzz' + print(i) + else: + i = i + print(i) + +fizzbuzz(100) + +#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. +def fibchk(n): + n1 = 0 + n2 = 1 + sum = 0 + for i in range(n): + sum = n1 + n2 + n1 = n2 + n2 = sum + i+=1 + print(sum) + + +fibchk(50) + + diff --git a/avg_list.py b/avg_list.py new file mode 100644 index 0000000..1199b9a --- /dev/null +++ b/avg_list.py @@ -0,0 +1,11 @@ + +#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 avglist(lista): + sumvalue = 0 + avgvalue = 0 + for i in lista: + sumvalue = sumvalue + i + avgvalue = sumvalue/len(lista) + return avgvalue + +avg1 = print(avglist([1,2,3,4])) \ No newline at end of file diff --git a/fibchk.py b/fibchk.py new file mode 100644 index 0000000..2483a22 --- /dev/null +++ b/fibchk.py @@ -0,0 +1,19 @@ + +#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. +def fibchk(n): + n1 = 0 + n2 = 1 + sum = 0 + for i in range(n): + sum = n1 + n2 + n1 = n2 + n2 = sum + i+=1 + print(sum) + + +fibchk(50) + diff --git a/fizz_buzz.py b/fizz_buzz.py new file mode 100644 index 0000000..d72b4af --- /dev/null +++ b/fizz_buzz.py @@ -0,0 +1,23 @@ + +#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(x): + i =1 + for i in range(x+1): + if i%3 == 0 and i%5 != 0: + i = 'Fizz' + print(i) + elif (i%5 == 0 and i%3 != 0): + i = 'Buzz' + print(i) + elif i%3 == 0 and i%5 == 0: + i = 'FizzBuzz' + print(i) + else: + i = i + print(i) + +fizzbuzz(100) + \ No newline at end of file diff --git a/list_length.py b/list_length.py new file mode 100644 index 0000000..ad2264f --- /dev/null +++ b/list_length.py @@ -0,0 +1,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 +def lenlist(list1): + return len(list1) +len1 = print(lenlist([1,2,3,4])) diff --git a/list_suavmimax.py b/list_suavmimax.py new file mode 100644 index 0000000..34259fc --- /dev/null +++ b/list_suavmimax.py @@ -0,0 +1,23 @@ + +#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. +def d_suavmima(list1): + sumlist = sum(list1) + avglist = sum(list1)/len(list1) + minlist = min(list1) + maxlist = max(list1) + lenlist = len(list1) + list2 = ['sumlist', 'avglist', 'minlist', 'maxlist', 'lenlist'] + list3 = [sum(list1), sum(list1)/len(list1), min(list1), max(list1), len(list1)] + i = iter(list2) + j = iter(list3) + dic1 = dict(zip(i, j)) + return dic1 + +dict1 = print(d_suavmima([1,2,3,4])) + + + + + + + \ No newline at end of file diff --git a/make_it_big.py b/make_it_big.py new file mode 100644 index 0000000..9dba18b --- /dev/null +++ b/make_it_big.py @@ -0,0 +1,19 @@ +#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(array): + for i in range(len(array)): + if array[i]>0: + array[i]= "big" + return array + +print(make_it_big([-1, 3, 5, -5])) + +def count_positives(array): + count = 0 + for val in array: + if val > 0: + count += 1 + array[len(array)-1] = count + return array + + +print(count_positives([-1,1,1,1])) \ No newline at end of file diff --git a/max_list.py b/max_list.py new file mode 100644 index 0000000..30debc2 --- /dev/null +++ b/max_list.py @@ -0,0 +1,11 @@ + +#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(list1): + if list1 == []: + return False + else: + return max(list1) + +max1 = print(max_list([1,2,3,4])) +max2 = print(max_list([-1,-2,-3])) +max3 = print(max_list([])) diff --git a/min_list.py b/min_list.py new file mode 100644 index 0000000..ad0926a --- /dev/null +++ b/min_list.py @@ -0,0 +1,11 @@ + +#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(list1): + if list1 == []: + return False + else: + return min(list1) + +min1 = print(min_list([1,2,3,4])) +min2 = print(min_list([-1,-2,-3])) +min3 = print(min_list([])) diff --git a/palindrome.py b/palindrome.py new file mode 100644 index 0000000..a9c48d6 --- /dev/null +++ b/palindrome.py @@ -0,0 +1,12 @@ +#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 palindromchk(str1): + revstr1 = "" + for i in str1: + revstr1 = i + revstr1 + if (str1 == revstr1): + return 'Yes' + else: + return 'No' + +chk1 = print(palindromchk('12321')) + diff --git a/reverse_list.py b/reverse_list.py new file mode 100644 index 0000000..ae8aaef --- /dev/null +++ b/reverse_list.py @@ -0,0 +1,14 @@ + +#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(list1): + left = 0 + right = len(list1) - 1 + while (left < right): + temp = list1[left] + list1[left] = list1[right] + list1[right] = temp + left+=1 + right-=1 + return list1 + +revlist1 = print(reverse_list([1,2,3,4,5])) diff --git a/sum_list.py b/sum_list.py new file mode 100644 index 0000000..6c36ad7 --- /dev/null +++ b/sum_list.py @@ -0,0 +1,8 @@ + +#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 sumlist(lista): + sumtotal = 0 + for i in lista: + sumtotal = sumtotal + i + return sumtotal +a = print(sumlist([1, 2, 3,4])) \ No newline at end of file From ba65d8f48f2d195b76ac550e6ae1da4ccab838f7 Mon Sep 17 00:00:00 2001 From: suneetha-2022 Date: Mon, 12 Dec 2022 22:53:51 -0500 Subject: [PATCH 03/22] Python_Practice2 --- Python_Practice2.py | 109 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 Python_Practice2.py diff --git a/Python_Practice2.py b/Python_Practice2.py new file mode 100644 index 0000000..194b36c --- /dev/null +++ b/Python_Practice2.py @@ -0,0 +1,109 @@ + +#Without List Comprehension +my_list = [] +for e in range(1, 10, 2): + my_list.append(e**2) +print(my_list) +#With List Comprehension +my_list = [e**2 for e in range(1, 10, 2)] +print(my_list) + +#Without List Comprehension +my_list = [] +for e in range(10): + if e%2 == 1: + my_list.append(e**2) +print(my_list) +#With comprehension +my_list = [e**2 for e in range(10) if e%2 == 1] +print(my_list) + +#multiple conditional statements using ternary operators: +grades = [95, 55, 83, 75, 91] +grades_l = [('A' if g>=90 else ('B' if g>=75 else 'C')) for g in grades] +print(grades_l) +# OR + +grades_l = [] +for g in grades: + if g >= 90: + grades_l.append('A') + elif g >= 75: + grades_l.append('B') + else: + grades_l.append('C') +print(grades_l) + +#nested for loops +pairs = [] +for i in range(3): + for j in range(2): + pairs.append((i, j)) +print(pairs) + +#Generator Example1: +def powers_of_two(): + n = 1 + while True: + n *= 2 + yield n + +powers = powers_of_two() +for _ in range(20): + print(next(powers)) + squares = (i**2 for i in range(20)) +for s in squares: + print(s) + +#Generator Example2 +def even_n(max_n = 1): + n =1 + while n <= max_n: + yield n*2 + n +=1 + +i = even_n(3) +print(next(i)) +print(next(i)) +print(next(i)) + +#Prime_number Generator +def primes_gen(): + for n in range(2, 100): # n starts from 2 to end + for x in range(2, n): # check if x can be divided by n + if n % x == 0: # if true then n is not prime + break + else: # if x is found after exhausting all values of x + yield n # generate the prime + +gen = primes_gen() +for _ in range(10): + print(next(gen), end=' ') + +#Unique Letters generator +def unique_str(my_str): + char_seen = [] + for char in my_str: + if char not in char_seen: + char_seen.append(char) + yield (''.join(char_seen)) + + +# For loop to reverse the string +for char in unique_str("Hello"): + print(char, end = ' ') + +#Sort the list by each language's version in ascending order. +prog_lang = [('Python', 3.8), ('Java', 13), ('JavaScript', 2019), ('Scala', 2.13)] +new_list = sorted(prog_lang, key = lambda x : x[1], reverse = True) +print(new_list) + +#Sort the list by the length of the name of each language in descending order. +prog_lang = [('Python', 3.8), ('Java', 13), ('JavaScript', 2019), ('Scala', 2.13)] +new_list2 = sorted(prog_lang, key = lambda x : x[1], reverse = False) +print(new_list2) + +new_list4 = list(filter(lambda x: x[1] 'a', prog_lang)) +print(new_list4) + + From 511898916bc60db376615d225ebec09d8290df17 Mon Sep 17 00:00:00 2001 From: suneetha-2022 Date: Thu, 15 Dec 2022 21:43:05 -0500 Subject: [PATCH 04/22] My Python XML Practice --- Python_xml.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Python_xml.py diff --git a/Python_xml.py b/Python_xml.py new file mode 100644 index 0000000..e32b227 --- /dev/null +++ b/Python_xml.py @@ -0,0 +1,104 @@ + +import xml.etree.ElementTree as ET +import re + +tree = ET.parse('movies.xml') +root = tree.getroot() + +print(tree) +print(root) +print(root.tag) +print(len(root)) + +#for child in root: +# print(child) + +print(len(root)) + +for child in root: + print(child.tag, child.attrib) + +print([i.tag for i in root.iter()]) + +print(ET.tostring(root, encoding = 'utf8').decode('utf8')) + +for movie in root.iter('movie'): + print(movie.attrib) + +for description in root.iter('description'): + print(description.text) + +for movie in root.findall("./genre/decade/movie/[year='1992']"): + print(movie.attrib) + +for movie in root.findall("./genre/decade/movie/format/[@multiple = 'Yes']"): + print(movie.attrib) + +#back = root.find("./genre/decade/movie[@title = 'Back to the Future']") +#print(back) + +#back.attrib['title'] = "Back 2 the Future" +#print(back.attrib) + +tree.write('movies.xml') +tree = ET.parse('movies.xml') +root = tree.getroot() + +for movie in root.iter('movie'): + print(movie.attrib) + +for form in root.findall("./genre/decade/movie/format"): + print(form.attrib, form.text) + +for form in root.findall("./genre/decade/movie/format"): + match = re.search(',', form.text) + if match: + form.set('multiple', 'Yes') + else: + form.set('multiple', 'No') + + print(form.attrib, form.text) + +for decade in root.findall("./genre/decade"): + print(decade.attrib) + for year in decade.findall("./movie/year"): + print(year.text, '\n') + +for movie in root.findall("./genre/decade/movie/[year = '2000']"): + print(movie.attrib) + +add = root.find("./genre[@category = 'Action']") +new_dec = ET.SubElement(add, 'decade') +print(new_dec) + +for genre in root.findall("./genre"): + print(genre.attrib) + +print(ET.tostring(root, encoding = 'utf8').decode('utf8')) +new_dec.attrib['years'] = '2000s' + +#xmen = root.find("./genre/decade/movie/[@title = 'X-Men']") +#dec2000 = root.find("./genre[@category = 'Action']/decade[@years = '2000s']") +#dec2000.append(xmen) +#dec1990 = root.find("./genre/[@category = 'Action']/decade[@years = '1990s']") +#dec1990.remove(xmen) +#tree.write('movies.xml') +#print(ET.tostring(root, encoding = 'utf8').decode('utf8')) + +new_anime1 = ET.SubElement(root, 'genre') +print(new_anime1) + +new_anime1.attrib['category'] = 'Anime' +print(ET.tostring(root, encoding='utf8').decode('utf8')) + +add1 = root.find("./genre[@category = 'Anime']") +new_dec2 = ET.SubElement(add1, 'decade') +print(new_dec2) + +new_dec2.attrib['years'] = '2000s' +btman = root.find("./genre/decade/movie/[@title = 'Batman Returns']") +dec_2000 = root.find("./genre[@category = 'Anime']/decade[@years = '2000s']") +dec_2000.append(btman) +dec_1990 = root.find("./genre/[@category = 'Action']/decade[@years = '1990s']") +dec_1990.remove(btman) + From 715fde636a7dd23602eb6cefbd0b4f2f05af97c3 Mon Sep 17 00:00:00 2001 From: suneetha-2022 Date: Wed, 21 Dec 2022 16:44:37 -0500 Subject: [PATCH 05/22] Regex Excersise --- regex_excercises.ipynb | 1341 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1341 insertions(+) create mode 100644 regex_excercises.ipynb diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb new file mode 100644 index 0000000..6e4d9b3 --- /dev/null +++ b/regex_excercises.ipynb @@ -0,0 +1,1341 @@ +{ + "cells": [ + { + "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": 92, + "metadata": { + "id": "lbrLwTw4r9jM" + }, + "outputs": [], + "source": [ + "import datetime" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "id": "ghj1l7sQos3R", + "trusted": true + }, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "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": 94, + "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(text):\n", + " # Define a regex pattern\n", + " pattern = r'[a-zA-Z][0-9]'\n", + " # Use the pattern to search for ....\n", + " match = re.search(pattern, text)\n", + " if match:\n", + " return 'True' \n", + " else:\n", + " return 'False'\n", + " \n", + "a = print(is_allowed_specific_char(\"ABCDEFabcdef123450\")) # True\n", + "b = print(is_allowed_specific_char(\"*&%@#!}{\")) # False" + ] + }, + { + "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": 95, + "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'a(b*)'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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": 96, + "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'a(b+)'\n", + " if re.search(pattern, text):\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": 97, + "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'a(b){0,1}'\n", + " if re.search(pattern, text):\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": 98, + "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'a(b){3}'\n", + " if re.search(pattern, text):\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(\"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": 99, + "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'a(b){2,3}'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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": 100, + "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]+_{1}[a-z]+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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" + }, + "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": 101, + "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]{1}[a-z]{1,})'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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!" + ] + }, + { + "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": 102, + "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{1}.+b{1}$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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": 103, + "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", + " pattern = r'^\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\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": 104, + "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", + " pattern = r'\\w+\\S*?$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + " \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" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DBgbyYHXos3k" + }, + "source": [ + "12) Write a Python program that matches a word containing 'z'" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "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", + " pattern = r'\\w+z{1}\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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" + ] + }, + { + "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": 106, + "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", + " pattern = r'\\w+z{1}\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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": 107, + "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", + " pattern = r'^\\w+$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\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!" + ] + }, + { + "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": 108, + "metadata": { + "id": "nWp5QysTos3n", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Solution\n", + "def match_num(text):\n", + " pattern = r'^[5]\\w*'\n", + " if re.search(pattern, text):\n", + " return 'True'\n", + " else:\n", + " return'False'\n", + " \n", + "print(match_num('5-2345861')) # True\n", + "print(match_num('6-2345861')) # 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": 109, + "metadata": { + "id": "cRLX4hc3os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "216.8.94.196\n" + ] + } + ], + "source": [ + "# Solution\n", + "def rewrite_ip(ip1, ip2):\n", + " ip2 = re.sub('\\.[0]*', '.', ip1)\n", + " return ip2\n", + "\n", + "ip3 = \"216.08.094.196\"\n", + "ip4 = \"\"\n", + "string = rewrite_ip(ip3, ip4)\n", + "print(string) # 216.8.94.196" + ] + }, + { + "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": 110, + "metadata": { + "id": "Q5t9U6n8os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "# Solution\n", + "def end_num(text):\n", + " pattern = r'\\d$'\n", + " if re.search(pattern, text):\n", + " return 'True'\n", + " else:\n", + " return'False'\n", + "\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": 111, + "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, pattern):\n", + " pattern = r'(\\d{1,3})'\n", + " string2 = string.split() \n", + " for i in string2:\n", + " if re.search(pattern, i):\n", + " print(i.replace(',', ''))\n", + "\n", + "\n", + "string3 = \"Exercises number 1, 12, 13, and 345 are important\"\n", + "pattern2 = \"\"\n", + "print_digits(string3, pattern2)\n", + "# Number of length 1 to 3\n", + "# 1\n", + "# 12\n", + "# 13\n", + "# 345" + ] + }, + { + "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": 112, + "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", + "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!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_match(patterns, text):\n", + " for i in patterns:\n", + " print('Searching for ' +'\"' +str(i) +'\"' +' in ' +'\"' +text +'\"' +'->')\n", + " if re.search(i, text): \n", + " print('Matched!')\n", + " else:\n", + " print('Not matched!')\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!" + ] + }, + { + "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": 113, + "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", + " match1 = re.search(r'(f{1}\\w+)', text)\n", + " if match1:\n", + " start = match1.start()\n", + " end = match1.end() \n", + " print('Found ' +'\"' +pattern +'\"'+' in ' +' \"The quick brown fox jumps over the lazy dog.\" ' +' from ' +str(start) +' to ' +str(end))\n", + " else:\n", + " print('No match')\n", + " \n", + "\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" + ] + }, + { + "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": 114, + "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", + " text1 = text.split()\n", + " for i in text1:\n", + " match = re.search(r'(e{1}\\w+)', i)\n", + " if match:\n", + " print('Found ' +'\"' +pattern +'\"')\n", + "\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": 115, + "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", + " for m in re.finditer(pattern, text):\n", + " print('Found ' +'\"' +pattern +'\"' +' at ' +str(m.start()) +' ' +str(m.end()))\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" + ] + }, + { + "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": 125, + "metadata": { + "id": "nUxR5Xvjos3z", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python_Exercises\n", + "Pytho Exercises\n" + ] + } + ], + "source": [ + "text = 'Python Exercises'\n", + "\n", + "# Your code\n", + "text1 = re.sub('\\s', '_', text)\n", + "print(text1) # Python_Exercises\n", + "\n", + "\n", + "# Your code\n", + "text2 = re.sub('\\w\\_', ' ', text1)\n", + "print(text2) # Python Exercises" + ] + }, + { + "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": 117, + "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", + " return re.findall(r'/(\\d{4})/(\\d{1,2})/(\\d{1,2})/', url)\n", + " \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": 118, + "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\n", + "def change_date_format(dt):\n", + " return re.sub(r'(\\d{4})-(\\d{1,2})-(\\d{1,2})', '\\\\3-\\\\2-\\\\1', dt)\n", + "\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\n" + ] + }, + { + "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": 127, + "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", + " for i in words:\n", + " match1 = re.match('(P\\w+)\\W(P\\w+)', i)\n", + " if match1:\n", + " print(match1.groups())\n", + "\n", + "words = [\"Python PHP\", \"Java JavaScript\", \"c c++\"]\n", + "print_words_starting_with_P(words) # ('Python', 'PHP')" + ] + }, + { + "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": 120, + "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", + " i = text.split()\n", + " for j in i:\n", + " match1 = re.search(r'\\d', j)\n", + " if match1:\n", + " print('# ' +j.replace(',', ''))\n", + " else:\n", + " pass\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": 121, + "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", + "None\n" + ] + } + ], + "source": [ + "# Solution\n", + "def get_all_words_containing_a_or_e(text):\n", + " list1 = []\n", + " list1 = re.findall(r'[ae]\\w+', text)\n", + " print(list1)\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']" + ] + }, + { + "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": 131, + "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", + " for i in re.finditer('(\\d{1,}\\s)', text):\n", + " print(i.group(0))\n", + " print('Index Position: ' +str(i.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": 123, + "metadata": { + "id": "VJE1Xm2Bos34", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "21 Ramkrishna Rd\n" + ] + } + ], + "source": [ + "# Solution\n", + "def abbreviate_road(street):\n", + " return re.sub('Road{1}', 'Rd', street)\n", + "\n", + "street = '21 Ramkrishna Road'\n", + "\n", + "print(abbreviate_road(street)) # 21 Ramkrishna Rd.\n" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "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.11.0" + }, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 18b6fc7aecce8e969233b524b0c3da2b29a8dff9 Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:50:43 -0500 Subject: [PATCH 06/22] Delete #Count_Positives.py --- #Count_Positives.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 #Count_Positives.py diff --git a/#Count_Positives.py b/#Count_Positives.py deleted file mode 100644 index 9e377b4..0000000 --- a/#Count_Positives.py +++ /dev/null @@ -1,13 +0,0 @@ -#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). -def count_positives(array): - count = 0 - for val in array: - if val > 0: - count += 1 - array[len(array)-1] = count - return array -print(count_positives([-1,1,1,1])) - - - - From 79e6184d216c2f10611f9ad32b26acd42f06900b Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:51:11 -0500 Subject: [PATCH 07/22] Delete Python_Practice2.py --- Python_Practice2.py | 109 -------------------------------------------- 1 file changed, 109 deletions(-) delete mode 100644 Python_Practice2.py diff --git a/Python_Practice2.py b/Python_Practice2.py deleted file mode 100644 index 194b36c..0000000 --- a/Python_Practice2.py +++ /dev/null @@ -1,109 +0,0 @@ - -#Without List Comprehension -my_list = [] -for e in range(1, 10, 2): - my_list.append(e**2) -print(my_list) -#With List Comprehension -my_list = [e**2 for e in range(1, 10, 2)] -print(my_list) - -#Without List Comprehension -my_list = [] -for e in range(10): - if e%2 == 1: - my_list.append(e**2) -print(my_list) -#With comprehension -my_list = [e**2 for e in range(10) if e%2 == 1] -print(my_list) - -#multiple conditional statements using ternary operators: -grades = [95, 55, 83, 75, 91] -grades_l = [('A' if g>=90 else ('B' if g>=75 else 'C')) for g in grades] -print(grades_l) -# OR - -grades_l = [] -for g in grades: - if g >= 90: - grades_l.append('A') - elif g >= 75: - grades_l.append('B') - else: - grades_l.append('C') -print(grades_l) - -#nested for loops -pairs = [] -for i in range(3): - for j in range(2): - pairs.append((i, j)) -print(pairs) - -#Generator Example1: -def powers_of_two(): - n = 1 - while True: - n *= 2 - yield n - -powers = powers_of_two() -for _ in range(20): - print(next(powers)) - squares = (i**2 for i in range(20)) -for s in squares: - print(s) - -#Generator Example2 -def even_n(max_n = 1): - n =1 - while n <= max_n: - yield n*2 - n +=1 - -i = even_n(3) -print(next(i)) -print(next(i)) -print(next(i)) - -#Prime_number Generator -def primes_gen(): - for n in range(2, 100): # n starts from 2 to end - for x in range(2, n): # check if x can be divided by n - if n % x == 0: # if true then n is not prime - break - else: # if x is found after exhausting all values of x - yield n # generate the prime - -gen = primes_gen() -for _ in range(10): - print(next(gen), end=' ') - -#Unique Letters generator -def unique_str(my_str): - char_seen = [] - for char in my_str: - if char not in char_seen: - char_seen.append(char) - yield (''.join(char_seen)) - - -# For loop to reverse the string -for char in unique_str("Hello"): - print(char, end = ' ') - -#Sort the list by each language's version in ascending order. -prog_lang = [('Python', 3.8), ('Java', 13), ('JavaScript', 2019), ('Scala', 2.13)] -new_list = sorted(prog_lang, key = lambda x : x[1], reverse = True) -print(new_list) - -#Sort the list by the length of the name of each language in descending order. -prog_lang = [('Python', 3.8), ('Java', 13), ('JavaScript', 2019), ('Scala', 2.13)] -new_list2 = sorted(prog_lang, key = lambda x : x[1], reverse = False) -print(new_list2) - -new_list4 = list(filter(lambda x: x[1] 'a', prog_lang)) -print(new_list4) - - From d0ecb55fa2270bf74bc7c0df9bf06546ff28399d Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:51:28 -0500 Subject: [PATCH 08/22] Delete Python_Practice_Assignment.py --- Python_Practice_Assignment.py | 149 ---------------------------------- 1 file changed, 149 deletions(-) delete mode 100644 Python_Practice_Assignment.py diff --git a/Python_Practice_Assignment.py b/Python_Practice_Assignment.py deleted file mode 100644 index 00b24d0..0000000 --- a/Python_Practice_Assignment.py +++ /dev/null @@ -1,149 +0,0 @@ - -#Python Practice Problems Assignment -#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(array): - for i in range(len(array)): - if array[i]>0: - array[i]= "big" - return array -print(make_it_big([-1, 3, 5, -5])) - -#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). -def count_positives(array): - count = 0 - for val in array: - if val > 0: - count += 1 - array[len(array)-1] = count - return array -print(count_positives([-1,1,1,1])) - -#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 sumlist(lista): - sumtotal = 0 - for i in lista: - sumtotal = sumtotal + i - return sumtotal -a = print(sumlist([1, 2, 3,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 avglist(lista): - sumvalue = 0 - avgvalue = 0 - for i in lista: - sumvalue = sumvalue + i - avgvalue = sumvalue/len(lista) - return avgvalue - -avg1 = print(avglist([1,2,3,4])) - -#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 -def lenlist(list1): - return len(list1) -len1 = print(lenlist([1,2,3,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. -def min_list(list1): - if list1 == []: - return False - else: - return min(list1) - -min1 = print(min_list([1,2,3,4])) -min2 = print(min_list([-1,-2,-3])) -min3 = print(min_list([])) - -#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(list1): - if list1 == []: - return False - else: - return max(list1) - -max1 = print(max_list([1,2,3,4])) -max2 = print(max_list([-1,-2,-3])) -max3 = print(max_list([])) - -#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. -def d_suavmima(list1): - sumlist = sum(list1) - avglist = sum(list1)/len(list1) - minlist = min(list1) - maxlist = max(list1) - lenlist = len(list1) - list2 = ['sumlist', 'avglist', 'minlist', 'maxlist', 'lenlist'] - list3 = [sum(list1), sum(list1)/len(list1), min(list1), max(list1), len(list1)] - i = iter(list2) - j = iter(list3) - dic1 = dict(zip(i, j)) - return dic1 - -dict1 = print(d_suavmima([1,2,3,4])) - -#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(list1): - left = 0 - right = len(list1) - 1 - while (left < right): - temp = list1[left] - list1[left] = list1[right] - list1[right] = temp - left+=1 - right-=1 - return list1 - -revlist1 = print(reverse_list([1,2,3,4,5])) - -#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 palindromchk(str1): - revstr1 = "" - for i in str1: - revstr1 = i + revstr1 - if (str1 == revstr1): - return 'Yes' - else: - return 'No' - -chk1 = print(palindromchk('12321')) - -#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(x): - i =1 - for i in range(x+1): - if i%3 == 0 and i%5 != 0: - i = 'Fizz' - print(i) - elif (i%5 == 0 and i%3 != 0): - i = 'Buzz' - print(i) - elif i%3 == 0 and i%5 == 0: - i = 'FizzBuzz' - print(i) - else: - i = i - print(i) - -fizzbuzz(100) - -#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. -def fibchk(n): - n1 = 0 - n2 = 1 - sum = 0 - for i in range(n): - sum = n1 + n2 - n1 = n2 - n2 = sum - i+=1 - print(sum) - - -fibchk(50) - - From d2e3ee0c1bb3f11e4adf09f5f3b1e0760da540dd Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:51:44 -0500 Subject: [PATCH 09/22] Delete Python_xml.py --- Python_xml.py | 104 -------------------------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 Python_xml.py diff --git a/Python_xml.py b/Python_xml.py deleted file mode 100644 index e32b227..0000000 --- a/Python_xml.py +++ /dev/null @@ -1,104 +0,0 @@ - -import xml.etree.ElementTree as ET -import re - -tree = ET.parse('movies.xml') -root = tree.getroot() - -print(tree) -print(root) -print(root.tag) -print(len(root)) - -#for child in root: -# print(child) - -print(len(root)) - -for child in root: - print(child.tag, child.attrib) - -print([i.tag for i in root.iter()]) - -print(ET.tostring(root, encoding = 'utf8').decode('utf8')) - -for movie in root.iter('movie'): - print(movie.attrib) - -for description in root.iter('description'): - print(description.text) - -for movie in root.findall("./genre/decade/movie/[year='1992']"): - print(movie.attrib) - -for movie in root.findall("./genre/decade/movie/format/[@multiple = 'Yes']"): - print(movie.attrib) - -#back = root.find("./genre/decade/movie[@title = 'Back to the Future']") -#print(back) - -#back.attrib['title'] = "Back 2 the Future" -#print(back.attrib) - -tree.write('movies.xml') -tree = ET.parse('movies.xml') -root = tree.getroot() - -for movie in root.iter('movie'): - print(movie.attrib) - -for form in root.findall("./genre/decade/movie/format"): - print(form.attrib, form.text) - -for form in root.findall("./genre/decade/movie/format"): - match = re.search(',', form.text) - if match: - form.set('multiple', 'Yes') - else: - form.set('multiple', 'No') - - print(form.attrib, form.text) - -for decade in root.findall("./genre/decade"): - print(decade.attrib) - for year in decade.findall("./movie/year"): - print(year.text, '\n') - -for movie in root.findall("./genre/decade/movie/[year = '2000']"): - print(movie.attrib) - -add = root.find("./genre[@category = 'Action']") -new_dec = ET.SubElement(add, 'decade') -print(new_dec) - -for genre in root.findall("./genre"): - print(genre.attrib) - -print(ET.tostring(root, encoding = 'utf8').decode('utf8')) -new_dec.attrib['years'] = '2000s' - -#xmen = root.find("./genre/decade/movie/[@title = 'X-Men']") -#dec2000 = root.find("./genre[@category = 'Action']/decade[@years = '2000s']") -#dec2000.append(xmen) -#dec1990 = root.find("./genre/[@category = 'Action']/decade[@years = '1990s']") -#dec1990.remove(xmen) -#tree.write('movies.xml') -#print(ET.tostring(root, encoding = 'utf8').decode('utf8')) - -new_anime1 = ET.SubElement(root, 'genre') -print(new_anime1) - -new_anime1.attrib['category'] = 'Anime' -print(ET.tostring(root, encoding='utf8').decode('utf8')) - -add1 = root.find("./genre[@category = 'Anime']") -new_dec2 = ET.SubElement(add1, 'decade') -print(new_dec2) - -new_dec2.attrib['years'] = '2000s' -btman = root.find("./genre/decade/movie/[@title = 'Batman Returns']") -dec_2000 = root.find("./genre[@category = 'Anime']/decade[@years = '2000s']") -dec_2000.append(btman) -dec_1990 = root.find("./genre/[@category = 'Action']/decade[@years = '1990s']") -dec_1990.remove(btman) - From e6334f1f0533cf7375bd18ff7919bfd8b201516f Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:52:09 -0500 Subject: [PATCH 10/22] Delete avg_list.py --- avg_list.py | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 avg_list.py diff --git a/avg_list.py b/avg_list.py deleted file mode 100644 index 1199b9a..0000000 --- a/avg_list.py +++ /dev/null @@ -1,11 +0,0 @@ - -#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 avglist(lista): - sumvalue = 0 - avgvalue = 0 - for i in lista: - sumvalue = sumvalue + i - avgvalue = sumvalue/len(lista) - return avgvalue - -avg1 = print(avglist([1,2,3,4])) \ No newline at end of file From 6e32f8e462562237709c8f1b2b70e7f20e13b5e6 Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:52:16 -0500 Subject: [PATCH 11/22] Delete fibchk.py --- fibchk.py | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 fibchk.py diff --git a/fibchk.py b/fibchk.py deleted file mode 100644 index 2483a22..0000000 --- a/fibchk.py +++ /dev/null @@ -1,19 +0,0 @@ - -#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. -def fibchk(n): - n1 = 0 - n2 = 1 - sum = 0 - for i in range(n): - sum = n1 + n2 - n1 = n2 - n2 = sum - i+=1 - print(sum) - - -fibchk(50) - From a29bba2fc05483a481f175a2ac09adb130cd29ce Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:52:24 -0500 Subject: [PATCH 12/22] Delete fizz_buzz.py --- fizz_buzz.py | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 fizz_buzz.py diff --git a/fizz_buzz.py b/fizz_buzz.py deleted file mode 100644 index d72b4af..0000000 --- a/fizz_buzz.py +++ /dev/null @@ -1,23 +0,0 @@ - -#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(x): - i =1 - for i in range(x+1): - if i%3 == 0 and i%5 != 0: - i = 'Fizz' - print(i) - elif (i%5 == 0 and i%3 != 0): - i = 'Buzz' - print(i) - elif i%3 == 0 and i%5 == 0: - i = 'FizzBuzz' - print(i) - else: - i = i - print(i) - -fizzbuzz(100) - \ No newline at end of file From b6ce40d53acee9c63f620179fadfd0f0e39d5c95 Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:52:34 -0500 Subject: [PATCH 13/22] Delete list_length.py --- list_length.py | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 list_length.py diff --git a/list_length.py b/list_length.py deleted file mode 100644 index ad2264f..0000000 --- a/list_length.py +++ /dev/null @@ -1,5 +0,0 @@ - -#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 -def lenlist(list1): - return len(list1) -len1 = print(lenlist([1,2,3,4])) From b20d34f2c5d016fcc69a4748368012f180497f41 Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:52:43 -0500 Subject: [PATCH 14/22] Delete list_suavmimax.py --- list_suavmimax.py | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 list_suavmimax.py diff --git a/list_suavmimax.py b/list_suavmimax.py deleted file mode 100644 index 34259fc..0000000 --- a/list_suavmimax.py +++ /dev/null @@ -1,23 +0,0 @@ - -#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. -def d_suavmima(list1): - sumlist = sum(list1) - avglist = sum(list1)/len(list1) - minlist = min(list1) - maxlist = max(list1) - lenlist = len(list1) - list2 = ['sumlist', 'avglist', 'minlist', 'maxlist', 'lenlist'] - list3 = [sum(list1), sum(list1)/len(list1), min(list1), max(list1), len(list1)] - i = iter(list2) - j = iter(list3) - dic1 = dict(zip(i, j)) - return dic1 - -dict1 = print(d_suavmima([1,2,3,4])) - - - - - - - \ No newline at end of file From ac4ec979cf712208d9e84f9d6a9dfef74e65138c Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:52:51 -0500 Subject: [PATCH 15/22] Delete make_it_big.py --- make_it_big.py | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 make_it_big.py diff --git a/make_it_big.py b/make_it_big.py deleted file mode 100644 index 9dba18b..0000000 --- a/make_it_big.py +++ /dev/null @@ -1,19 +0,0 @@ -#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(array): - for i in range(len(array)): - if array[i]>0: - array[i]= "big" - return array - -print(make_it_big([-1, 3, 5, -5])) - -def count_positives(array): - count = 0 - for val in array: - if val > 0: - count += 1 - array[len(array)-1] = count - return array - - -print(count_positives([-1,1,1,1])) \ No newline at end of file From 06b16cb27eca34d6daa01428a034f8b3e3f9c75f Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:52:58 -0500 Subject: [PATCH 16/22] Delete max_list.py --- max_list.py | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 max_list.py diff --git a/max_list.py b/max_list.py deleted file mode 100644 index 30debc2..0000000 --- a/max_list.py +++ /dev/null @@ -1,11 +0,0 @@ - -#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(list1): - if list1 == []: - return False - else: - return max(list1) - -max1 = print(max_list([1,2,3,4])) -max2 = print(max_list([-1,-2,-3])) -max3 = print(max_list([])) From ea3b33f27219cbe0d21098f09629dd67a54bb06b Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:53:12 -0500 Subject: [PATCH 17/22] Delete min_list.py --- min_list.py | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 min_list.py diff --git a/min_list.py b/min_list.py deleted file mode 100644 index ad0926a..0000000 --- a/min_list.py +++ /dev/null @@ -1,11 +0,0 @@ - -#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(list1): - if list1 == []: - return False - else: - return min(list1) - -min1 = print(min_list([1,2,3,4])) -min2 = print(min_list([-1,-2,-3])) -min3 = print(min_list([])) From 6f0601f6d2ca9f74dd89cfe57580de824558d4ff Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:53:20 -0500 Subject: [PATCH 18/22] Delete palindrome.py --- palindrome.py | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 palindrome.py diff --git a/palindrome.py b/palindrome.py deleted file mode 100644 index a9c48d6..0000000 --- a/palindrome.py +++ /dev/null @@ -1,12 +0,0 @@ -#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 palindromchk(str1): - revstr1 = "" - for i in str1: - revstr1 = i + revstr1 - if (str1 == revstr1): - return 'Yes' - else: - return 'No' - -chk1 = print(palindromchk('12321')) - From 0a48575e5a0d0f0c87d3abe6921a7127414f5e70 Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:53:28 -0500 Subject: [PATCH 19/22] Delete reverse_list.py --- reverse_list.py | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 reverse_list.py diff --git a/reverse_list.py b/reverse_list.py deleted file mode 100644 index ae8aaef..0000000 --- a/reverse_list.py +++ /dev/null @@ -1,14 +0,0 @@ - -#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(list1): - left = 0 - right = len(list1) - 1 - while (left < right): - temp = list1[left] - list1[left] = list1[right] - list1[right] = temp - left+=1 - right-=1 - return list1 - -revlist1 = print(reverse_list([1,2,3,4,5])) From 2eb7e8122cd3b6f45de91949bc92c07cf3bbb8f6 Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:53:36 -0500 Subject: [PATCH 20/22] Delete sum_list.py --- sum_list.py | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 sum_list.py diff --git a/sum_list.py b/sum_list.py deleted file mode 100644 index 6c36ad7..0000000 --- a/sum_list.py +++ /dev/null @@ -1,8 +0,0 @@ - -#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 sumlist(lista): - sumtotal = 0 - for i in lista: - sumtotal = sumtotal + i - return sumtotal -a = print(sumlist([1, 2, 3,4])) \ No newline at end of file From 2ffb1f5e8344cabe66d0fed6909d9e694096c25a Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:55:47 -0500 Subject: [PATCH 21/22] Create README.md Python Regex practice with Jupyter notebook --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..1073529 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# python-practice +Python_practice From 8bea540c03975e377fc6945668dde133f5fa1539 Mon Sep 17 00:00:00 2001 From: Suneetha-2022 <118309496+Suneetha-2022@users.noreply.github.com> Date: Wed, 21 Dec 2022 16:57:54 -0500 Subject: [PATCH 22/22] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 1073529..a1971c8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -# python-practice -Python_practice +#Regex Practice with Jupyter Notebook