Skip to content

Instantly share code, notes, and snippets.

View RabeyaMuna's full-sized avatar

Rabeya Khatun Muna RabeyaMuna

View GitHub Profile
@RabeyaMuna
RabeyaMuna / Q4.py
Created May 7, 2020 02:43
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". Fill in the gaps to complete this function.
import os
def parent_directory():
# Create a relative path to the parent
# of the current working directory
relative_parent = os.path.join(os.getcwd(), os.pardir)
# Return the absolute path of the parent directory
return os.path.abspath(relative_parent)
print(parent_directory())
@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
@RabeyaMuna
RabeyaMuna / Q2.py
Created May 7, 2020 02:31
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. Complete the function to create a file "script.py" in the directory "PythonPrograms".
import os
def new_directory(directory, filename):
# Before creating a new directory, check to see if it already exists
if not os.path.exists(directory):
os.mkdir(directory)
name=os.path.join(directory, filename)
file=open(name,'w')
file.close()
return os.listdir(directory)
@RabeyaMuna
RabeyaMuna / gist:2b3c5a0f6aeea831d564ee121b077352
Last active March 27, 2023 13:08
The permissions of a file in a Linux system are split into three sets of three permissions: read, write, and execute for the owner, group, and others. Each of the three values can be expressed as an octal number summing each permission, with 4 corresponding to read, 2 to write, and 1 to execute. Or it can be written with a string using the lette…
def octal_to_string(octal):
result = ""
value_letters = [(4,"r"),(2,"w"),(1,"x")]
#Iterating over each digit in octal
for digit in [int(n) for n in str(octal)]:
#Checking for each of permission values
for value, letter in value_letters:
if digit >= value:
result += letter
@RabeyaMuna
RabeyaMuna / gist:39805ccf914940b32a0b27be052e5b66
Created April 7, 2020 13:57
The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end is replaced, not all of them. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxy…
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence
if sentence.endswith(old):
# Using i as the slicing index, combine the part
# of the sentence up to the matched string at the
# end with the new string
i = sentence.split()
k =i[-1].replace(old,new)
new_sentence=sentence[0:-len(old)]+k
return new_sentence
@RabeyaMuna
RabeyaMuna / string.py
Last active October 6, 2022 08:35
Want to try some string methods yourself? Give it a go! Fill in the gaps in the initials function so that it returns the initials of the words contained in the phrase received, in upper case. For example: "Universal Serial Bus" should return "USB"; "local area network" should return "LAN”.
def initials(phrase):
words = phrase.split()
result = ""
for word in words:
x=list(word)
if x[0].isupper():
result +=x[0]
elif x[0].islower():
result +=x[0].upper()
import math
import os
import random
import re
import sys
# Complete the compareTriplets function below.
def compareTriplets(a, b):
c=0
d=0