Last active
November 30, 2017 13:57
-
-
Save Sh-ui/51ddf7218d37696c9009736dd1106527 to your computer and use it in GitHub Desktop.
This file contains 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
# some useful functions based on practice activities on codeacademy |
This file contains 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
# censors the 'word' out of the 'text' | |
def censor(text, word): | |
words = text.split() | |
result = '' | |
stars = '*' * len(word) | |
count = 0 | |
for i in words: | |
if i == word: | |
words[count] = stars | |
count += 1 | |
result =' '.join(words) | |
return result |
This file contains 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
def remove_duplicates(inputlist): | |
if inputlist == []: | |
return [] | |
# Sort the input list from low to high | |
inputlist = sorted(inputlist) | |
# Initialize the output list, and give it the first value of the now-sorted input list | |
outputlist = [inputlist[0]] | |
# Go through the values of the sorted list and append to the output list | |
# ...any values that are greater than the last value of the output list | |
for i in inputlist: | |
if i > outputlist[-1]: | |
outputlist.append(i) | |
return outputlist |
This file contains 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
# remove vowels from string | |
def anti_vowel(text): | |
text = text.translate(None,'eiouaEIOUA') | |
return text |
This file contains 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
def count(sequence, item): | |
count = 0 | |
for i in sequence: | |
if i == item: | |
count += 1 | |
return count |
This file contains 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
# factorial a number | |
def factorial(x): | |
total = 1 | |
while x > 0: | |
total *= x | |
x -= 1 | |
return total |
This file contains 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
def median(lst): | |
sorted_list = sorted(lst) | |
if len(sorted_list) % 2 != 0: | |
#odd number of elements | |
index = len(sorted_list)//2 | |
return sorted_list[index] | |
elif len(sorted_list) % 2 == 0: | |
#even no. of elements | |
index_1 = len(sorted_list)/2 - 1 | |
index_2 = len(sorted_list)/2 | |
mean = (sorted_list[index_1] + sorted_list[index_2])/2.0 | |
return mean |
This file contains 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
# is it even? | |
def is_even(x): | |
return x % 2 == 0 |
This file contains 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
# is it an integer? | |
def is_int(x): | |
return abs(x) - round(abs(x)) == 0 |
This file contains 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
# is a number prime? | |
def is_prime(x): | |
if x < 2: | |
return False | |
else: | |
for n in range(2, x-1): | |
if x % n == 0: | |
return False | |
return True |
This file contains 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
def product(list): | |
total = 1 | |
for num in list: | |
total = total * num | |
return total |
This file contains 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
def purify(lst): | |
res = [] | |
for ele in lst: | |
if ele % 2 == 0: | |
res.append(ele) | |
return res |
This file contains 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
# reverse a string | |
def reverse(text): | |
word = "" | |
l = len(text) - 1 | |
while l >= 0: | |
word = word + text[l] | |
l -= 1 | |
return word |
This file contains 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
# get a score from a word | |
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 = word.lower() | |
total = 0 | |
for letter in word: | |
for leter in score: | |
if letter == leter: | |
total = total + score[leter] | |
return total |
This file contains 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
# turn a multidigit number into a summed number | |
def digit_sum(x): | |
total = 0 | |
while x > 0: | |
total += x % 10 | |
x = x // 10 | |
print(x) | |
return total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment