Practice Makes Perfect!

I Fun With Numbers

i. Practice! Practice Practice! 1/15
>Save and Submit
ii. is_even 2/15

def is_even(x):
    if x % 2 == 0:
        return True
    else:
        return False

iii. is_int 3/15

def is_int(x):
    if x - round(x) == 0:
        return True
    else:
        return False
print is_int(7.5)
print is_int(7.0)

iv. digit_sum 4/15

def digit_sum(n):
    m = str(n)
    total = 0
    for x in m:
        total = total + int(x)
    return total
print digit_sum(567845837494362478562735675432568726923)

v. factorial 5/15

def factorial(x):
    result  = 1
    while x != 0:
        result *= x
        x = x - 1
    return result

vi. is_prime 6/15

def is_prime(x):
    count = 0
    x= int(x)
    if x = 1:
            return False
        else:
            return True

II. String it Together

i. reverse 7/15

def reverse(text):
    length = len(text)
    word = []
    while length > 0:
        word.append(text[length - 1])
        length -= 1
    return ''.join(word)

ii. anti_vowel 8/15

def anti_vowel(text):
    removed = text
    vowel = ["a","e","i","o","u","A","E","I","O","U"]
    for x in text:
        for y in vowel:
            if x == y:
                removed = removed.replace(x,"")
    return removed

iii. scrabble_score 9/15

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):
    scrabble = word.lower()
    total = 0
    for i in range(len(scrabble)):
        total = total + score[scrabble[i]]
    return total

iv. censor 10/15

def censor(text, word):
    fort = text
    if word in fort:
        fort = fort.replace(word,"*"*len(word))
    return fort
print censor("hey hey hello","hey")

III Listing Your Problems

i. count 11/15

def count(sequence, item):
    total = 0
    for i in sequence:
        if i == item:
            total += 1
    return total

ii. purify 12/15

def purify(numbers):
    new_numbers = []
    for n in numbers:
        if n % 2 == 0:
            new_numbers.append(n)
    return new_numbers

iii. product 13/15

def product(numbers):
    total = 1
    for n in numbers:
        total = total * n
    return total

iv. remove_duplicates 14/15

def remove_duplicates(numbers):
        new_list = []
        for i in numbers:
            if i not in new_list:
                new_list.append(i)
        return new_list

v. median 15/15

def median(numbers):
    n = sorted(numbers)
    if len(n)%2 == 0:
        return (n[(len(n)/2)] + n[((len(n)/2) - 1)]) / 2.0
    else: 
        return n[(len(n)/2)]
Advertisements

One thought on “Practice Makes Perfect!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s