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 / parts-of-string.py
Created November 29, 2021 00:11
Modify the first_and_last function so that it returns True if the first letter of the string is the same as the last letter of the string, False if they’re different.
def first_and_last(message):
if not message or message[0] == message[len(message)-1]:
return True
else:
return False
@Codehunter-py
Codehunter-py / split-string.py
Created November 29, 2021 00:52
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.
def initials(phrase):
words = phrase.split()
result = ""
for word in words:
result += word[0].upper()
return result
@Codehunter-py
Codehunter-py / format-string.py
Last active November 29, 2021 01:04
Modify the student_grade function using the format method, so that it returns the phrase "X received Y% on the exam". For example, student_grade("Reed", 80) should return "Reed received 80% on the exam".
def student_grade(name, grade):
return "{} received {}% on the exam".format(name, grade)
print(student_grade("Reed", 80))
print(student_grade("Paige", 92))
print(student_grade("Jesse", 85))
@Codehunter-py
Codehunter-py / string-reference-cheat-sheet.txt
Last active December 1, 2021 18:20
String operations and methods
String operations
len(string) Returns the length of the string
for character in string Iterates over each character in the string
if substring in string Checks whether the substring is part of the string
string[i] Accesses the character at index i of the string, starting at zero
string[i:j] Accesses the substring starting at index i, ending at index j-1.
@Codehunter-py
Codehunter-py / replace-string.py
Created December 1, 2021 19:13
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.
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 = len(old)
new_sentence = sentence[:-i]+new
return new_sentence
# Return the original sentence if there is no match
@Codehunter-py
Codehunter-py / is-palindrome.py
Last active December 1, 2021 19:18
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.
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 letter in input_string.strip():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
new_string = new_string+letter.replace(" ","")
@Codehunter-py
Codehunter-py / skip-elemets.py
Created December 1, 2021 21:45
The skip_elements function returns a list containing every other element from an input list, starting with the first element. Complete this function to do that, using the for loop to iterate through the input list.
def skip_elements(elements):
# Initialize variables
new_list = []
i = 0
# Iterate through the list by value and not index
for element in elements:
# Does this element belong in the resulting list? Check on index
if i % 2 == 0:
# Add this element to the resulting list
@Codehunter-py
Codehunter-py / skip-elements-enumerate.py
Created December 1, 2021 22:28
Completing the skip_elements function to return every other element from the list, this time using the enumerate function to check if an element is on an even position or an odd position.
def skip_elements(elements):
# code goes here
new_list = []
for index, element in enumerate(elements):
if index % 2 == 0:
new_list.append(element)
return new_list
@Codehunter-py
Codehunter-py / lists-operations-cheat-sheet.txt
Last active December 1, 2021 23:11
Lists and Tuples Operations Cheat Sheet
Common sequence operations
len(sequence) Returns the length of the sequence
for element in sequence Iterates over each element in the sequence
if element in sequence Checks whether the element is part of the sequence
sequence[i] Accesses the element at index i of the sequence, starting at zero
@Codehunter-py
Codehunter-py / group-list.py
Created December 2, 2021 00:25
The group_list function accepts a group name and a list of members, and returns a string with the format: group_name: member1, member2, … For example, group_list("g", ["a","b","c"]) returns "g: a, b, c". Fill in the gaps in this function to do that.
def group_list(group, users):
members = [ user for user in users]
return "{}: {}".format(group, members)
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"