Difficulty:
Questions
Problem 1:
Create methods for the Calculator
class that can do the following:
- Add two numbers.
- Subtract two numbers.
- Multiply two numbers.
- Divide two numbers.
Examples
calculator = Calculator()
calculator.add(10, 5) ➞ 15
calculator.subtract(10, 5) ➞ 5
calculator.multiply(10, 5) ➞ 50
calculator.divide(10, 5) ➞ 2
Problem 2
The caret ^
, when found at the start of a character set, is the equivalent to “not” in RegEx. The regular expression [^a-c]
matches any characters except “a”, “b” and “c”. Write the regular expression that matches any characters except letters, digits and spaces. You must use a negated character set in your expression.
Examples
txt = " alice15@gmail.com "
pattern = "yourregularexpressionhere"
re.findall(pattern, txt) ➞ ["@", "."]
Simply post your code and a screenshot of your results.
Please format your Python code and blur it or place it in a hidden section.
This workout will be released on Monday May 29, 2023, and the author’s solution will be posted on Sunday June 4, 2023.
Summary
class Calculator:
def __init__(self):
pass
def add(self,a,b):
return a + b
def subtract(self,a,b):
return a - b
def multiply(self,a,b):
return a * b
def divide(self,a,b):
return a // b
import re
txt = " alice15@gmail.com "
pattern = “[^a-z0-9 ]”
re.findall(pattern,txt)
Problem 1:
class Calculator:
def add(self, num1, num2):
return num1 + num2
def subtract(self, num1, num2):
return num1 - num2
def multiply(self, num1, num2):
return num1 * num2
def divide(self, num1, num2):
if num2 == 0:
return "Error: Division by zero is not allowed."
return num1 / num2
Problem 2
import re
text = "Hello, 123 World!"
pattern = r"[^a-zA-Z0-9\s]"
matches = re.findall(pattern, text)
print(matches) # Output: [',', '!']
Answer:
Problem-1
"""
Calculator Class
=============================
Author: Udit Kumar Chatterjee
Email: quantumudit@gmail.com
=============================
This module defines the Calculator class for basic arithmetic operations.
The operation involves: addition, subtraction, multiplication and division
"""
class Calculator:
"""Class representing a calculator"""
def __init__(self, num1: int, num2: int):
"""
Initialize a Calculator object with two numbers.
Args:
num1 (int): The first integer for calculations.
num2 (int): The second integer for calculations.
Raises:
ValueError: If num1 or num2 is not an integer
"""
if not isinstance(num1, int) or not isinstance(num2, int):
raise ValueError("Both num1 and num2 must be integers.")
self.num1 = num1
self.num2 = num2
def add(self) -> int:
"""
Add two numbers and return the result.
Returns:
int: The sum of num1 and num2.
"""
return self.num1 + self.num2
def subtract(self) -> int:
"""
Subtract the second number from the first and return the result.
Returns:
int: The result of num1 - num2.
"""
return self.num1 - self.num2
def multiply(self) -> int:
"""
Multiply two numbers and return the result.
Returns:
int: The product of num1 and num2.
"""
return self.num1 * self.num2
def divide(self) -> float:
"""
Divide the first number by the second and return the result as a float.
Returns:
float: The result of num1 / num2.
Raises:
ValueError: If num2 is zero.
"""
if self.num2 == 0:
return ValueError("Division by zero is not allowed.")
return self.num1 / self.num2
if __name__ == '__main__':
calculator = Calculator(num1=0, num2=0)
print(f"Given 2 numbers: {calculator.num1} and {calculator.num2}")
print("The results of basic arithmetic operations over the 2 numbers are:")
print(f"Addition: {calculator.add()}")
print(f"Subtraction: {calculator.subtract()}")
print(f"Multiplication: {calculator.multiply()}")
print(f"Division: {calculator.divide()}")
Problem-2
"""
The Caret Matcher
=============================
Author: Udit Kumar Chatterjee
Email: quantumudit@gmail.com
=============================
This script demonstrates the use of regular expressions to find matches in a text.
The script defines a regular expression pattern to match non-word, non-whitespace characters
and searches for such matches in the given text. The matches are printed to the console.
"""
import re
TEXT = " alice15@gmail.com "
PATTERN = r"[^\w\s]"
matches = re.findall(PATTERN, TEXT)
print(f"Matches: {matches}")