Codecademy Practice Notes--learn Python (70%)

Source: Internet
Author: User
Tags mul

##############################################################################
# Codecademy Python 5.5
# Define A function factorial that takes an integer x as input.
# Calculate and return the factorial of that number.
# def digit_sum (x):
# mul = 1
# for I in Range (1,x+1):
# mul = I*mul
# Print (MUL)
# digit_sum (6)
##############################################################################
# Codecademy Python 5.6
# Define A function called Is_prime that takes a number x as input.
# for each number n from 2 to x-1, test if x is evenly divisible by N.
# If It is, return False.
# If None of them is, then return True.
# def is_prime (x):
# for I in Range (2,x):
# if x%i ==0:
# return True
# Else:
# return False
# is_prime (100)
##############################################################################
# Codecademy Python 5.7
# Define A function called reverse that takes a string Textand returns this string in reverse.
# for Example:reverse ("ABCD") should return "DCBA".
# You could not use the reversed or [:: -1] To help you with this.
# you could get a string containing special characters (for example,!, @, or #).
# def Read (str):
# return STR[::-1]
# Print (Read (' abc '))
##############################################################################
# Codecademy Python 5.8
# Define A function called Anti_vowel that takes one string, text,
# as input and returns the text with all of the vowels removed.
# for Example:anti_vowel ("Hey you!") should return "Hy y!".
# Don ' t count Y as a vowel. Make sure to remove lowercase and uppercase vowels.
# import RE
# def Anti_vowel (str):
# Print (Re.sub (' [Aeiou] ', ', str))
# Anti_vowel (' Hey you! ')
##############################################################################
# Codecademy Python 5.9
# Define A function Scrabble_score that takes a string word as input
# and returns the equivalent Scrabble score for that word.
# assume your input is only one word containing no spaces or punctuation.
# As mentioned, no need to worry about score multipliers!
# Your function should work even if the letters you get is uppercase, lowercase, or a mix.
# Assume that you ' re only given non-empty strings.
# 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": Ten, "P": 3, "S": 1,
# "R": 1, "U": 1, "T": 1, "W": 4, "V": 4, "Y": 4,
# "X": 8, "Z": 10}
# word = str (input ()). Lower ()
# def scrabble_score (word):
# sum = 0
# for item in Word:
# sum = score[item]+sum
# Print (SUM)
# Scrabble_score (Word)
##############################################################################
# Codecademy Python 5.10
# Write A function called Censor that takes the strings, text and Word, as input.
# It should return the text with the word to chose replaced with asterisks. For example:
# censor ("This hack is wack hack", "hack")
# should return:
# "This * * * * is wack * * * * *"
# assume your input strings won ' t contain punctuation or upper case letters.
# The number of asterisks you put should correspond to the number of letters in the censored word.
# text = ' This hack is wack hack '
# word = ' hack '
# def censor (Text,word):
# num = 0
# for I in Word:
# num +=1
# Print (Text.replace (Word, ' * ' *num))
# censor (Text,word)
##############################################################################
# Codecademy Python 5.11
# Define A function called count that have both arguments called sequence and item.
# Return the number of times the item occurs in the list.
# for Example:count ([1, 2, 1, 1], 1) should return 3 (because 1 appears 3 times in the list).
# There is a list method in Python so you can use for this, but you should does it the long method for practice.
# Your function should return an integer.
# The item you have input May is an integer, string, float, or even another list!
# is careful not to use list as a variable name in your code-it ' s a reserved word in python!
# def COUNT (Sequence,item):
# sum = 0
# for I in sequence:
# If I = = Item:
# sum+=1
# return sum
##############################################################################
# Codecademy Python 5.12
# Define A function called purify that takes in a list of numbers,
# removes all odd numbers in the list, and returns the result.
# For example, purify ([]) should return [2].
# do not directly modify the list is given as input;
# instead, return a new list with only the even numbers.
# def purify (x):
# li = []
# for I in x:
# If I%2 ==0:
# li.append (i)
# return Li
# Print (Purify ([1,2,3,4]))
##############################################################################
# Codecademy Python 5.13
# Define A function called product that takes a list of integers as input and
# returns the product of the elements in the list.
# for Example:product ([4, 5, 5]) should return (because 4 * 5 * 5 is 100).
# Don ' t worry about the list being empty.
# Your function should return an integer.
# Def product (x):
# mul = 1
# for I in x:
# mul = I*mul
# return Mul
# Print (Product ([12,4,3]))
##############################################################################
# Codecademy Python 5.14
# Write A function remove_duplicates that takes in a list and removes elements of the list is the same.
# for Example:remove_duplicates ([1, 1, 2, 2]) should return [1, 2].
# Don ' t remove every occurrence, since you need to keep a single occurrence of a number.
# The order in which your present your output does not matter.
# so returning [1, 2, 3] are the same as returning [3, 1, 2].
# do not modify the list to take as input! Instead, return a new list.
# def Remove_duplicates (LI):
# li1 = []
# for I in Li:
# If I not in Li1:
# li1.append (i)
# return LI1
# Print (Remove_duplicates ([1,1,2,2,3,3,4]))
##############################################################################
# Codecademy Python 5.15
# Write A function called Median that takes a list as an input
# and returns the median value of the list. For Example:median ([1, 1, 2]) should return 1.
# The list can be is of any size and the numbers is not guaranteed to is in any particular order. Make sure to sort it!
# If The list contains an even number of elements, your function should return the average of the middle.
# def median (LI):
# li = sorted (list (LI))
# Print (LI)
# num = 0
# for I in Li:
# num +=1
# if (num-1)%2==0:
# return Li[int (NUM/2)]
# Else:
# Return ((Li[int ((num/2-0.5)-1)]+li[int ((num/2-0.5) +1)])/2
# print (median ([6,2,4,8,9,1,2,3,3,7]))

Codecademy Practice Notes--learn Python (70%)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.