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%)的更多相关文章
- 《Learn python the hard way》Exercise 48: Advanced User Input
这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 记录:python读取excel文件
由于最近老是用到python读取excel文件,所以特意记录一下python读取excel文件的大体框架. 库:xlrd(读),直接pip安装即可.想要写excel文件的话,安装xlwd库即可,也是直 ...
- 学 Python (Learn Python The Hard Way)
学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...
- 工作记录之 [ python请求url ] v s [ java请求url ]
背景: 模拟浏览器访问web,发送https请求url,为了实验需求需要获取ipv4数据包 由于不做后续的内容整理(有内部平台分析),故只要写几行代码请求发送https请求url列表中的url即可 开 ...
- 快速入门:十分钟学会PythonTutorial - Learn Python in 10 minutes
This tutorial is available as a short ebook. The e-book features extra content from follow-up posts ...
- Python basic (from learn python the hard the way)
1. How to run the python file? python ...py 2. UTF-8 is a character encoding, just like ASCII. 3. ro ...
- 记录一些python内置函数
整理一些内置函数,平时用得比较少,但是时不时遇上,记录一下吧(嘻嘻(●'◡'●)) 1.help() 查看模块or函数的帮助文档 help(pandas) #模块 Help on package pa ...
随机推荐
- python之组合与继承的使用场景
1.什么时候使用类的组合?当类之间有显著的不同,并且较小的类是组成较大类所需要的组件,此时用类的组合较合理:场景:医院是由多个科室组成的,此时我们可以定义不同科室的类,这样医院的类我们可以直接使用各个 ...
- JavaScript学习笔记(第一天)
javascript个人笔记 JavaScript的组成 JavaScript是一种运行在客户端的脚本语言 ECMAScript 标准----js的基本的语法 DOM------Document ...
- BFS与DFS模板
搜索问题的解法 DFS(深度优先搜索) BFS(广度优先搜索) backtracking(回溯) DFS模板 void dfs(...) { // 结束递归的条件 if (...) { ..... / ...
- 训练1-A
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入中含有一些数据,分别是成对出现的花布条和 ...
- nmcli 静态方式添加IP地址
[root@ansible02:/root] > ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state U ...
- VLC编译
http://blog.csdn.net/hdh4638/article/details/7602321 1 下载代码 ki.videolan.org/VLC_Source_code git colo ...
- OOP 面向对象 七大原则 (一)
OOP 面向对象 七大原则 (一) 大家众所周知,面向对象有三大特征继承封装多态的同时,还具有这七大原则,三大特征上一篇已经详细说明,这一篇就为大家详解一下七大原则: 单一职责原则,开闭原则,里氏 ...
- Linux线程资源限制
- Microsoft Dynamics CRM 2013 for Outlook 的硬件要求
当仅联机或脱机模式下执行 Microsoft Dynamics CRM 2013 for Microsoft Office Outlook 时,下表列出了建议的最低硬件要求 watermark/2/t ...
- linux下測试硬盘读写速度
买了个ssd硬盘,就想着跟普通的机械盘做个比較.由于桌面装的是ubuntu系统,所以就想用linux的命令简单測一下好了 以下是ssd的性能数据: 測试写: xxx@WaitFish:~ > t ...