Last active
February 18, 2020 01:01
-
-
Save lfalanga/ef75d9db60c9b67748a31069e5486690 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example 1: Verifies that the given number is an integer (not just the type). Works for floats too. | |
def is_int(x): | |
if x - round(x) == 0: | |
return True | |
else: | |
return False | |
# Example 2: Summarize all digits for a given number (non negative) | |
def digit_sum(n): | |
result = 0 | |
s = str(n) | |
for digit in s: | |
result += int(digit) | |
return result | |
# Example 3: Summarize all digits for a given number using modulo and floor division (non negative) | |
def digit_sum(n): | |
result = 0 | |
while (n % 10) > 0: | |
result += n % 10 | |
n = n // 10 | |
return result | |
# Example 4. Factorial for non negative integer values | |
def factorial(x): | |
result = 1 | |
while x > 0: | |
result *= x | |
x -= 1 | |
return result | |
# Example 5: Finds if a given number is prime or not (tired version) | |
def is_prime(x): | |
if x <= 1: | |
return False | |
elif x == 2: | |
return True | |
else: | |
result = True | |
for n in range(2, x): | |
if x % n == 0: | |
result = False | |
return result | |
# Example 6: Reverse string | |
def reverse(text): | |
lst = [] | |
for char in text: | |
lst.insert(0,char) | |
return "".join(lst) | |
# Example 7: Anti vowels funtion | |
def anti_vowel(text): | |
# Lowercase vowels | |
result = text.replace('a', '') | |
result = result.replace('e', '') | |
result = result.replace('i', '') | |
result = result.replace('o', '') | |
result = result.replace('u', '') | |
# Uppercase vowels | |
result = result.replace('A', '') | |
result = result.replace('E', '') | |
result = result.replace('I', '') | |
result = result.replace('O', '') | |
result = result.replace('U', '') | |
return result | |
# Example 8: Calculating Scrabble score | |
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, | |
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, | |
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, | |
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, | |
"x": 8, "z": 10} | |
def scrabble_score(word): | |
word_score = 0 | |
for char in word: | |
word_score += score[char.lower()] | |
return word_score | |
# Example 9: Couting appearences of an item in a list | |
def count(sequence, item): | |
hits = 0 | |
for x in sequence: | |
if x == item: | |
hits += 1 | |
return hits | |
# Example 10: Remove all odd numbers for a given list | |
def purify(arg): | |
even_numbers = [] | |
for n in arg: | |
if (n % 2 == 0): | |
even_numbers.append(n) | |
return even_numbers | |
# Example 11: Return the product of all items for a given list | |
def product(arg): | |
result = 1 | |
for i in range(0, len(arg)): | |
result *= 1 * arg[i] | |
return result | |
# Example 12: Remove duplicates from a list | |
def remove_duplicates(arg): | |
sorted_list = sorted(arg) | |
new_list = [] | |
for i in range(0, len(sorted_list)): | |
if (i == 0): | |
new_list.append(sorted_list[i]) | |
else: | |
if (sorted_list[i] != new_list[len(new_list)-1]): | |
new_list.append(sorted_list[i]) | |
return new_list | |
# Example 13: The median is the middle number in a sorted sequence of numbers | |
def median(arg): | |
sorted_list = sorted(arg) | |
if (len(sorted_list) % 2 != 0): | |
middle_item = len(sorted_list) / 2 | |
return sorted_list[middle_item] | |
else: | |
middle_item_1 = sorted_list[(len(sorted_list) / 2) - 1] | |
middle_item_2 = sorted_list[len(sorted_list) / 2] | |
return (middle_item_1 + middle_item_2) / 2.0 | |
# Example 14: Return all the reserved words in Python language | |
import builtins | |
for x in dir(builtins): | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment