Skip to content

Instantly share code, notes, and snippets.

View jamesblakefan's full-sized avatar

jamesblakefan

View GitHub Profile
@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 / 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
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings_Lists_Dictionaries_7.py
Created March 23, 2021 23:13
Question 7 Use a dictionary to count the frequency of letters in the input string. Only letters should be counted, not blank spaces, numbers, or punctuation. Upper case should be considered the same as lower case. For example, count_letters("This is a sentence.") should return {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}.
def count_letters(text):
result = {}
text = text.lower()
# Go through each letter in the text
for letter in text:
# Check if the letter needs to be counted or not
if letter .isalpha() and letter not in result:
result[letter] = text.lower().count(letter)
# Add or increment the value in the dictionary
return result
@AnisahTiaraPratiwi
AnisahTiaraPratiwi / Strings1.py
Created March 22, 2021 08:15
The is_palindrome function checks if a string is a palindrome. A palindrome is a string that can be equally read from left to right or right to left, omitting blank spaces, and ignoring capitalization. Examples of palindromes are words like kayak and radar, and phrases like "Never Odd or Even". Fill in the blanks in this function to return True …
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for string in input_string.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if string.replace(" ",""):
@RabeyaMuna
RabeyaMuna / Q3.py
Created May 7, 2020 02:40
he 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. Fill in the gaps to create a file called "newfile.txt" and check the date that it was modified.
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)
c=datetime.datetime.fromtimestamp(timestamp)
# Convert the timestamp into a readable format, then into a string