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 / permission-in-octal.py
Last active December 2, 2021 00:34
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
@Codehunter-py
Codehunter-py / pig-latin.py
Last active December 2, 2021 00:45
Creating a function that turns text into pig latin: a simple text transformation that modifies each word moving the first character to the end and appending "ay" to the end. For example, python ends up as ythonpay.
def pig_latin(text):
words = text.split()
pigged_text = []
for word in words:
word = word[1:] + word[0] + 'ay'
pigged_text.append(word)
return ' '.join(pigged_text)
@Codehunter-py
Codehunter-py / replace-suffix.py
Last active December 2, 2021 01:00
Given a list of filenames, we want to rename all the files with extension hpp to the extension h. To do this, we would like to generate a new list called newfilenames, consisting of the new filenames. Fill in the blanks in the code using any of the methods you’ve learned thus far, like a for loop or a list comprehension.
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.
newfilenames = [x.replace('.hpp','.h') for x in filenames]
print(newfilenames)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]
@Codehunter-py
Codehunter-py / guests-list.py
Created December 2, 2021 00:58
The guest_list function reads in a list of tuples with the name, age, and profession of each party guest, and prints the sentence "Guest is X years old and works as __." for each one.
def guest_list(guests):
for guest in guests:
name, age, job = guest
print("{} is {} years old and works as {}".format(name, age, job))
guest_list([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")])
#Click Run to submit code
"""
Output should match:
@Codehunter-py
Codehunter-py / count-logs.py
Created December 4, 2021 00:12
Count how many times each log error appears on the server
def count_letters(text):
result = {}
for char in text:
if char not in result:
result[char] = 0
result[char] += 1
return result
@Codehunter-py
Codehunter-py / dict-methods-sheet.txt
Last active December 7, 2021 08:22
Dictionary Methods Cheat Sheet
Definition
x = {key1:value1, key2:value2}
Operations
len(dictionary) - Returns the number of items in the dictionary
for key in dictionary - Iterates over each key in the dictionary
@Codehunter-py
Codehunter-py / email_list.py
Created December 8, 2021 00:37
The email_list function receives a dictionary, which contains domain names as keys, and a list of users as values. Filling in the blanks to generate a list that contains complete email addresses (e.g. diana.prince@gmail.com).
def email_list(domains):
emails = []
for domain, users in domains.items():
for user in users:
emails.append(user+'@'+domain)
return(emails)
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], \
"yahoo.com": ["barbara.gordon", "jean.grey"], "hotmail.com": ["bruce.wayne"]}))
@Codehunter-py
Codehunter-py / groups_per_user.py
Last active May 24, 2023 08:21
The groups_per_user function receives a dictionary, which contains group names with the list of users. Users can belong to multiple groups. Filling in the blanks to return a dictionary with the users as keys and a list of their groups as values.
def groups_per_user(group_dictionary):
user_groups = {}
for group, users in group_dictionary.items():
for user in users:
if user not in user_groups:
user_groups[user] = []
user_groups[user].append(group)
return user_groups
print(groups_per_user({"local": ["admin", "userA"],
@Codehunter-py
Codehunter-py / add_prices.py
Last active December 8, 2021 01:07
The add_prices function returns the total price of all of the groceries in the dictionary. Filling in the blanks to complete this function.
def add_prices(basket):
total = 0
for price in basket.values():
total += price
return round(total, 2)
@Codehunter-py
Codehunter-py / counter.py
Created December 9, 2021 20:30
The counter function counts down from start to stop when start is bigger than stop, and counts up from start to stop otherwise.
def counter(start, stop):
x = start
if x > stop:
return_string = "Counting down: "
while x >= stop:
return_string += str(x)+","
x -= 1
else:
return_string = "Counting up: "
while x < stop: