Skip to content

Instantly share code, notes, and snippets.

View Codehunter-py's full-sized avatar
🏠
Working from home

Ibrahim Musayev Codehunter-py

🏠
Working from home
View GitHub Profile
@Codehunter-py
Codehunter-py / check_zip_code.py
Created February 20, 2022 19:08
Check if the text passed includes a possible U.S. zip code, formatted as follows: exactly 5 digits, and sometimes, but not always, followed by a dash with 4 more digits. The zip code needs to be preceded by at least one space, and cannot be at the start of the text.
import re
def check_zip_code (text):
result = re.search(r"[ ]\d{5}|[ ]\d{5}-\d{4}", text)
return result != None
print(check_zip_code("The zip codes for New York are 10001 thru 11104.")) # True
print(check_zip_code("90210 is a TV show")) # False
print(check_zip_code("Their address is: 123 Main Street, Anytown, AZ 85258-0001.")) # True
print(check_zip_code("The Parliament of Canada is at 111 Wellington St, Ottawa, ON K1A0A9.")) # False
@Codehunter-py
Codehunter-py / contains_acronym.py
Created February 20, 2022 19:06
The contains_acronym function checks the text for the presence of 2 or more characters or digits surrounded by parentheses, with at least the first character in uppercase (if it's a letter), returning True if the condition is met, or False otherwise. For example, "Instant messaging (IM) is a set of communication technologies used for text-based …
import re
def contains_acronym(text):
pattern = r"\([A-Z1-9][a-zA-Z1-9]*\)"
result = re.search(pattern, text)
return result != None
print(contains_acronym("Instant messaging (IM) is a set of communication technologies used for text-based communication")) # True
print(contains_acronym("American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication")) # True
print(contains_acronym("Please do NOT enter without permission!")) # False
print(contains_acronym("PostScript is a fourth-generation programming language (4GL)")) # True
@Codehunter-py
Codehunter-py / check_time.py
Created February 20, 2022 19:06
The check_time function checks for the time format of a 12-hour clock, as follows: the hour is between 1 and 12, with no leading zero, followed by a colon, then minutes between 00 and 59, then an optional space, and then AM or PM, in upper or lower case.
import re
def check_time(text):
pattern = r"[1-9][0-2]?:[0-5][0-9] ?[am|pm|AM|PM]"
result = re.search(pattern, text)
return result != None
print(check_time("12:45pm")) # True
print(check_time("9:59 AM")) # True
print(check_time("6:60am")) # False
print(check_time("five o'clock")) # False
@Codehunter-py
Codehunter-py / check_web_address.py
Created February 20, 2022 19:05
The check_web_address function checks if the text passed qualifies as a top-level web address, meaning that it contains alphanumeric characters (which includes letters, numbers, and underscores), as well as periods, dashes, and a plus sign, followed by a period and a character-only top-level domain such as ".com", ".info", ".edu", etc.
import re
def check_web_address(text):
pattern = r"^\S+\.[a-zA-Z]+$"
result = re.search(pattern, text)
return result != None
print(check_web_address("gmail.com")) # True
print(check_web_address("www@google")) # False
print(check_web_address("www.Coursera.org")) # True
print(check_web_address("web-address.com/homepage")) # False
@Codehunter-py
Codehunter-py / check_sentence.py
Created February 20, 2022 18:32
Check if the text passed looks like a standard sentence, meaning that it starts with an uppercase letter, followed by at least some lowercase letters or a space, and ends with a period, question mark, or exclamation point.
import re
def check_sentence(text):
result = re.search(r"^[A-Z][a-z| ]*[.?\!]$", text)
return result != None
print(check_sentence("Is this is a sentence?")) # True
print(check_sentence("is this is a sentence?")) # False
print(check_sentence("Hello")) # False
print(check_sentence("1-2-3-GO!")) # False
print(check_sentence("A star is born.")) # True
@Codehunter-py
Codehunter-py / alphanumeric.py
Created February 20, 2022 18:11
Check if the text passed has at least 2 groups of alphanumeric characters (including letters, numbers, and underscores) separated by one or more whitespace characters.
import re
def check_character_groups(text):
result = re.search(r"[0-9]\w", text)
return result != None
print(check_character_groups("One")) # False
print(check_character_groups("123 Ready Set GO")) # True
print(check_character_groups("username user_01")) # True
print(check_character_groups("shopping_list: milk, bread, eggs.")) # False
@Codehunter-py
Codehunter-py / repeating_letter.py
Last active June 5, 2023 18:08
The repeating_letter_a function checks if the text passed includes the letter "a" (lowercase or uppercase) at least twice. For example, repeating_letter_a("banana") is True, while repeating_letter_a("pineapple") is False.
import re
def repeating_letter_a(text):
result = re.search(r"[Aa].*a", text)
return result != None
print(repeating_letter_a("banana")) # True
print(repeating_letter_a("pineapple")) # False
print(repeating_letter_a("Animal Kingdom")) # True
print(repeating_letter_a("A is for apple")) # True
@Codehunter-py
Codehunter-py / check_punctiation.py
Last active February 20, 2022 17:20
Check if the text passed contains punctuation symbols: commas, periods, colons, semicolons, question marks, and exclamation points.
import re
def check_punctuation (text):
result = re.search(r"[,.:;?!]", text)
return result != None
print(check_punctuation("This is a sentence that ends with a period.")) # True
print(check_punctuation("This is a sentence fragment without a period")) # False
print(check_punctuation("Aren't regular expressions awesome?")) # True
print(check_punctuation("Wow! We're really picking up some steam now!")) # True
print(check_punctuation("End of the line")) # False
@Codehunter-py
Codehunter-py / contents_of_file.py
Created February 20, 2022 14:31
Using the CSV file of flowers again, fill in the gaps of the contents_of_file function to process the data without turning it into a dictionary.
import os
import csv
# Create a file with data in it
def create_file(filename):
with open(filename, "w") as file:
file.write("name,color,type\n")
file.write("carnation,pink,annual\n")
file.write("daffodil,yellow,perennial\n")
file.write("iris,blue,perennial\n")
@Codehunter-py
Codehunter-py / create_file.py
Created February 20, 2022 14:08
The create_file function writes this information to a CSV file. The contents_of_file function reads this file into records and returns the information in a nicely formatted block. Fill in the gaps of the contents_of_file function to turn the data in the CSV file into a dictionary using DictReader.
import os
import csv
# Create a file with data in it
def create_file(filename):
with open(filename, "w") as file:
file.write("name,color,type\n")
file.write("carnation,pink,annual\n")
file.write("daffodil,yellow,perennial\n")
file.write("iris,blue,perennial\n")