codecademy练习记录--Learn Python(70%)
Posted pandaboy1123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codecademy练习记录--Learn Python(70%)相关的知识,希望对你有一定的参考价值。
##############################################################################
# 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 are, 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 that string in reverse.
# For example: reverse("abcd") should return "dcba".
# You may not use reversed or [::-1] to help you with this.
# You may 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 are 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": 10, "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 two strings, text and word, as input.
# It should return the text with the word you 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 has two 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 that you can use for this, but you should do it the long way for practice.
# Your function should return an integer.
# The item you input may be an integer, string, float, or even another list!
# Be 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([1,2,3]) should return [2].
# Do not directly modify the list you are 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 all of the elements in the list.
# For example: product([4, 5, 5]) should return 100 (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 that are 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 you present your output does not matter.
# So returning [1, 2, 3] is the same as returning [3, 1, 2].
# Do not modify the list you 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 of any size and the numbers are not guaranteed to be 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 two.
# 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练习记录--Learn Python(70%)的主要内容,如果未能解决你的问题,请参考以下文章
理解 Python 中的逻辑(Learn Python the Hard Way 练习 27)