Codecademy Python Answers Practice Makes Perfect

codecademy-practice-makes-perfect

Practice Makes Perfect 1/13 – Practice! Practice! Practice!

Practice Makes Perfect 2/13 – is_even

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

Practice Makes Perfect 3/13 – is_int

def is_int(x):
    if x == int(x):
        return True
    else:
        return False

Practice Makes Perfect 4/13 – digit_sum

def digit_sum(num):
    return sum(int(c) for c in str(num))

Practice Makes Perfect 5/13 – factorial

def factorial(x):
    if x == 1:
        return 1
    else:
        return x*factorial(x-1)

Practice Makes Perfect 6/13 – is_prime

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2,x):
            if x % n == 0:
               return False
        return True

Practice Makes Perfect 7/13 – reverse

def reverse(text):
    y=""
    for i in text:
        y=i+y
    return y
p = "hello"
reverse(p)

Practice Makes Perfect 8/13 – anti_vowel

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): #Returns the scrabble score for a given word

    points = 0

    for letter in word.lower():
        points += score[letter]

    return points

print scrabble_score("Apple")

Practice Makes Perfect 9/13 – Scrabble_Score

def censor(text, word):
    censor=text.split(' ')
    frase=''
    for w in censor:
        if w== word:
            w='*'*len(word)
        if len(frase)!=0:
            espacio=' '
        else:
           espacio='' 
        frase = frase + espacio + ''.join(w)
    return frase
print censor('hola k ase', 'k')
print censor("hey hey hey","hey")
print censor("this hack is wack hack", "hack") 

Practice Makes Perfect 10/13 – Censor

def censor(text, word):
    censor=text.split(' ')
    frase=''
    for w in censor:
        if w== word:
            w='*'*len(word)
        if len(frase)!=0:
            espacio=' '
        else:
           espacio='' 
        frase = frase + espacio + ''.join(w)
    return frase
print censor('hola k ase', 'k')
print censor("hey hey hey","hey")
print censor("this hack is wack hack", "hack") 

Practice Makes Perfect 11/15 – Count

def count(sequence, item):
    rslt = 0
    for i in sequence:
        if i == item:
            rslt += 1
    return rslt
    print rslt
count([1,2,2,3,4,2],2)

Practice Makes Perfect 12/15 – Purify

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

Practice Makes Perfect 13/15 – Product

def product(lst):
    total = 1
    for i in lst:
        total *= i
    return total

Practice Makes Perfect 14/15 – Remove_duplicates

def remove_duplicates(lst):
    
    new_list = []

    for i in range(0, len(lst)):
        if lst[i] in new_list:
            pass
        else:
            new_list.append(lst[i])
    return new_list

Practice Makes Perfect 15/15 – Median

def median(list_num):
    s = sorted(list_num)
    if len(s)%2 == 0:
        return (s[len(s)/2] + s[(len(s)/2) - 1]) / 2.0
    else:
        return s[(len(s)-1)/2]
Latest Comments
  1. Python December 4, 2016
  2. Lukey May 5, 2017
  3. Manas Shrivastava June 24, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *