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 / scrape.py
Created July 28, 2022 11:40
Creating a request and parsing a web page
import requests
from bs4 import BeautifulSoup
def scrape_page(url):
response = requests.get(url)
soup = BeautifulSoup(response.text,'lxml')
print(soup)
quotes = soup.find_all("span", class_="text")
authors = soup.find_all("small", class_="author")
tags = soup.find_all("div", class_="tags")
@Codehunter-py
Codehunter-py / fileIO.py
Created July 28, 2022 10:39
Read the inputFile.txt then write two output files if conditions are matched
def fileIO(inputFile='inputFile.txt'):
f = open(inputFile,'r')
output1 = "PassFile.txt"
output2 = "FailFile.txt"
passFile = open(output1,'w')
failFile = open(output2, 'w')
for line in f:
line_split = line.split()
if line_split[2] == "P":
@Codehunter-py
Codehunter-py / flatten_2d_list.py
Created July 25, 2022 14:26
Given a 2D list, write a Python program to convert the given list into a flattened list.
import numpy
list_2d = [['Volkswagen', 'Mercedes', 'BMW'], ['Honda', 'Toyota', 'Mazda']]
def convertList(list_2d):
return list(numpy.concatenate(list_2d).flat)
print(convertList(list_2d)) # Output ['Volkswagen', 'Mercedes', 'BMW', 'Honda', 'Toyota', 'Mazda']
@Codehunter-py
Codehunter-py / show_time_of_pid.py
Created March 27, 2022 17:30
We're using the same syslog, and we want to display the date, time, and process id that's inside the square brackets. We can read each line of the syslog and pass the contents to the show_time_of_pid function.
‎‎​
@Codehunter-py
Codehunter-py / convert_phone_number.py
Last active August 23, 2023 11:55
The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a dash, 3 more digits followed by a dash, and 4 digits), and converts it to a more formal format that looks like this: (XXX) XXX-XXXX.
import re
def convert_phone_number(phone):
result = re.sub(r"\b(\d{3})-(\d{3})-(\d{4})\b",r"(\1) \2-\3", phone)
return result
print(convert_phone_number("My number is 212-345-9999.")) # My number is (212) 345-9999.
print(convert_phone_number("Please call 888-555-1234")) # Please call (888) 555-1234
print(convert_phone_number("123-123-12345")) # 123-123-12345
print(convert_phone_number("Phone number of Buckingham Palace is +44 303 123 7300")) # Phone number of Buckingham Palace is +44 303 123 7300
@Codehunter-py
Codehunter-py / transform_comments.py
Created March 12, 2022 17:14
The transform_comments function converts comments in a Python script into those usable by a C compiler. This means looking for text that begins with a hash mark (#) and replacing it with double slashes (//), which is the C single-line comment indicator. For the purpose of this exercise, we'll ignore the possibility of a hash mark embedded inside…
‎‎​
@Codehunter-py
Codehunter-py / multi_vowels_words.py
Last active November 2, 2023 11:07
The multi_vowel_words function returns all words with 3 or more consecutive vowels (a, e, i, o, u).
import re
def multi_vowel_words(text):
pattern = r"(\w+[a,e,i,o,u]{3,}\w+)"
result = re.findall(pattern, text)
return result
print(multi_vowel_words("Life is beautiful"))
# ['beautiful']
print(multi_vowel_words("Obviously, the queen is courageous and gracious."))
@Codehunter-py
Codehunter-py / extract_pid.py
Created March 6, 2022 16:36
Regular expression used in the extract_pid function, to return the uppercase message in parenthesis, after the process id.
import re
def extract_pid(log_line):
regex = r"\[(\d+)\]\: (\w+)"
result = re.search(regex, log_line)
if result is None:
return None
return "{} ({})".format(result[1], result[2])
print(extract_pid("July 31 07:51:48 mycomputer bad_process[12345]: ERROR Performing package upgrade")) # 12345 (ERROR)
print(extract_pid("99 elephants in a [cage]")) # None
@Codehunter-py
Codehunter-py / long_words.py
Last active May 30, 2022 10:13
The long_words function returns all words that are at least 7 characters. Fill in the regular expression to complete this function.
import re
def long_words(text):
pattern = r"\w{7,}"
result = re.findall(pattern, text)
return result
print(long_words("I like to drink coffee in the morning.")) # ['morning']
print(long_words("I also have a taste for hot chocolate in the afternoon.")) # ['chocolate', 'afternoon']
print(long_words("I never drink tea late at night.")) # []
@Codehunter-py
Codehunter-py / rearrange_name.py
Created March 6, 2022 16:01
The regular expression used in the rearrange_name function so that it can match middle names, middle initials, as well as double surnames.
import re
def rearrange_name(name):
result = re.search(r"^([\w \.-]*), ([\w \.-]*)$", name)
if result == None:
return name
return "{} {}".format(result[2], result[1])
name=rearrange_name("Kennedy, John F.")
print(name)