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 / 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")
@Codehunter-py
Codehunter-py / parent_dir.py
Last active January 27, 2022 00:57
The parent_directory function returns the name of the directory that's located just above the current working directory. Remember that '..' is a relative path alias that means "go up to the parent directory".
import os
def parent_directory():
# Create a relative path to the parent
# of the current working directory
dir = os.getcwd()
relative_parent = os.path.join(dir)
os.chdir("..")
# Return the absolute path of the parent directory
return os.path.abspath(relative_parent)
@Codehunter-py
Codehunter-py / file_date.py
Last active January 27, 2022 00:47
The file_date function creates a new file in the current working directory, checks the date that the file was modified, and returns just the date portion of the timestamp in the format of yyyy-mm-dd.
import os
import datetime
def file_date(filename):
# Create the file in the current directory
with open(filename, 'w') as file:
pass
timestamp = os.path.getmtime(filename)
# Convert the timestamp into a readable format, then into a string
date = datetime.datetime.fromtimestamp(timestamp).date()
@Codehunter-py
Codehunter-py / script.py
Last active January 27, 2022 00:22
The new_directory function creates a new directory inside the current working directory, then creates a new empty file inside the new directory, and returns the list of files in that directory.
import os
def new_directory(directory, filename):
# Before creating a new directory, check to see if it already exists
if os.path.isdir(directory) == False:
os.makedirs(directory, exist_ok=True)
# Create the new file inside of the new directory
os.chdir(directory)
with open (filename, "w") as file:
@Codehunter-py
Codehunter-py / create_py_script.py
Created January 27, 2022 00:00
function creates a new python script in the current working directory, adds the line of comments to it declared by the 'comments' variable, and returns the size of the new file.
import os
def create_python_script(filename):
comments = "# Start of a new Python program"
with open("filename", 'w') as f:
filesize = f.write(comments)
return(filesize)
print(create_python_script("program.py"))
@Codehunter-py
Codehunter-py / highlight_word.py
Last active January 9, 2022 21:16
The highlight_word function changes the given word in a sentence to its upper-case version.
def highlight_word(sentence, word):
return(sentence.replace(word,word.upper()))
@Codehunter-py
Codehunter-py / format_address.py
Last active December 22, 2021 00:49
The format_address function separates out parts of the address string into new strings: house_number and street_name, and returns: "house number X on street named Y".
def format_address(address_string):
# Declare variables
house_number = 0
street_name = []
# Separate the address string into parts
address = address_string.split()
# Traverse through the address parts
for item in address:
if item.isnumeric():
house_number = item