46 Simple Python Exercises-Very simple exercises
46 Simple Python Exercises-Very simple exercises
4、Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.
#编写一个函数,该函数接受一个字符(即长度为1的字符串),如果是元音,则返回true,否则返回false。
def if_vowel(a):
a=a.lower()
if a in('a','e','i','o','u'):
return True
else:
return False
print(if_vowel('A'))
5、Write a function translate() that will translate a text into "r�varspr�ket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".
编写一个函数translate(),将文本转换为“r varspr ket”(瑞典语表示“强盗的语言”)。也就是说,将每个辅音加倍,并在中间加上“o”。例如,translate(“this is fun”)应该返回字符串“totohohisos isos fofunon”。
def if_vowel(a):
if a in('a','e','i','o','u'):
return True
else:
return False
def translate(string):
char_list=[]
for char in string:
if if_vowel(char) or char==" ":
char_list.append(char)
else:
char_list.append(char+'o'+char)
return "".join(char_list)
print(translate("this is fun")) 6、Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.
定义一个函数sum()和一个函数multiple(),分别对数字列表中的所有数字求和和和相乘。例如,SUM([1,2,3,4])应返回10,乘法([1,2,3,4])应返回24。
def sum(sum_list):
sum1 = 0
for b in sum_list:
sum1 += b
return sum1
print(sum([1,2,3,4])) def mul(mul_list):
mul1=1
for a in mul_list:
mul1 = mul1*a
return mul1
print(mul([1,2,3,4,5])) 7、Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing") should return the string "gnitset ma I".
定义计算字符串反转的函数reverse()。例如,Reverse(“I am testing”)应该返回字符串“gnitset ma i”。
def reverse(string):
revStr = ''
for i in range(len(string)):
revStr += string[len(string) - i - 1] return revStra print(reverse('test')) #方法二
def reverse(string):
revStr=''
for i in range(len(string)-1, -1, -1):
revStr += string[i] return revStr print(reverse('test')) 8、Define a function is_palindrome() that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar") should return True.
定义一个函数是识别回文的_palindrome()(即向后写的单词)。例如,“回文”(“radar”)应该返回true。
def is_palindrome(string):
if string == ''.join(list(reversed(string))):#reversed输出的是一个迭代器
return True
else:
return False
print(is_palindrome('8radar8')) #迭代器:http://www.runoob.com/python3/python3-iterator-generator.html
#Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
#.join: http://www.runoob.com/python3/python3-string-join.html 9、Write a function is_member() that takes a value (i.e. a number, string, etc)
x and a list of values a, and returns True if x is a member of a, False otherwise. (Note that this is exactly
what the in operator does, but for the sake of the exercise you should pretend Python did not have this operator.)
写一个函数是获取一个值(即数字、字符串等)的_member()。
x和值a的列表,如果x是a的成员,则返回true,否则返回false。(注意,这正是
in操作符所做的,但是为了练习,您应该假装python没有这个操作符。)
def is_member(x,a_list):
for b in a_list:
if x==b:
return True
return False print(is_member(3,[2,3,4]))
10、Define a function overlapping() that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member() function, or the in operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.
定义一个函数overlapping(),该函数接受两个列表,如果它们至少有一个公共成员,则返回true,否则返回false。您可以使用is_member()函数或in运算符,但为了练习,您还应该(也)使用两个嵌套的for循环来编写它。
def overlapping(list1,list2):
for a in list1:
for b in list2:
if a==b:
return True
return False
print(overlapping([1,2,3],[11,5,6,7])) 11.Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of c:s.
For example, generate_n_chars(5,"x") should return the string "xxxxx". (Python is unusual in that you can actually write an expression 5 * "x"
that will evaluate to "xxxxx". For the sake of the exercise you should ignore that the problem can be solved in this manner.)
def generate_n_chars(n,c):
list1=[]
for i in range(n):
list1.append(c)#注意append的用法,之前写成了list1=list1.append() 这样就错了
return ''.join(list1)
print(generate_n_chars(10,'s2')) 12、Define a procedure histogram() that takes a list of integers and prints a histogram to the screen.
For example, histogram([4, 9, 7]) should print the following:
****
*********
*******
def histogram(list1):
list2=[]
for a in list1:
list2.append(a*'*')
return '\n'.join(list2) print(histogram([3,4,5]))
46 Simple Python Exercises-Very simple exercises的更多相关文章
- 46 Simple Python Exercises (前20道题)
46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises construct ...
- 46 Simple Python Exercises-Higher order functions and list comprehensions
26. Using the higher order function reduce(), write a function max_in_list() that takes a list of nu ...
- Simple python reverse shell
import base64,sys; import socket,struct s=socket.socket(2,socket.SOCK_STREAM) s.connect(('Attack's I ...
- Python Web Scraper - Simple Url Request
from urllib.request import urlopen html = urlopen("http://www.baidu.com") print(html.read( ...
- MITx: 6.00.1x Introduction to Computer Science and Programming Using Python Week 2: Simple Programs 4. Functions
ESTIMATED TIME TO COMPLETE: 18 minutes We can use the idea of bisection search to determine if a cha ...
- simple python code when @ simplnano
code: import serial,time,itertools try: ser=serial.Serial(2,115200,timeout=0) except: print 'Open CO ...
- PCI Express(六) - Simple transactions
原文地址:http://www.fpga4fun.com/PCI-Express6.html Let's try to control LEDs from the PCI Express bus. X ...
- 用于Simple.Data的ASP.NET Identity Provider
今天推举的这篇文章,本意不是要推举文章的内容,而是据此介绍一下Simple.Data这个很有意思的类ORM工具. 现在大家在.NET开发中如果需要进行数据访问,那么基本都会使用一些ORM工具,比如微软 ...
- 学习simple.data之基础篇
simple.data是一个轻量级的.动态的数据访问组件,支持.net4.0. 1.必须条件和依赖性: v4.0 or greater of the .NET framework, or v2.10 ...
随机推荐
- 9.利用msfvenom生成木马
这篇文章来介绍一下msf中一个生成木马的msfvenom模块. msfvenom命令行选项如下: 英文原版: 中文版: Options: -p, --payload <payload> 指 ...
- Torando 入门
1. 前言 Tornado 是使用 Python 编写的一个强大的.可拓展性的 Web 服务器/框架.与其他主流 Web 服务器框架有着明显区别:Tornado 支持异步非阻塞框架.同时它处理速度非常 ...
- SQL中的union,except,intersect用法
限制:所有查询中的列数和列的数序必须相同 union all:完全整合两个结果集查出所有数据 union:查出两个表的数据并且去除重复的数据 except:去重之后只会保留第一个表中的数据,查询a表在 ...
- 滴水穿石 C#中多线程 委托的使用
什么是多线程?我们在建立以个C#项目时,往往会在Form1上添加控件,然后写代码,初 学者都是在重复这个过程,其实这个过程是单线程的,可以理解为只有“main”主线程,有 的时候往往需要同时测量多个东 ...
- sqlserver2012——游标
游标:一种数据访问机制,允许用户访问单独的数据行而不是对整个行集进行操作.用户可以通过单独处理每一行逐条收集信息并对数据逐行进行操作,这样可以将降低系统开销. 游标主要有以下两部分: 游标结果集:由定 ...
- 安全测试 + 渗透测试 Xmind 要点梳理
从事测试工作多年,一直对安全测试充满神秘感.买了本书,闲来无事时翻看了解.发现书的开头提供的Xmind脑图挺有参考价值,所以做了次“搬运工”,提供给想接触了解安全测试/渗透测试的小伙伴. 安全测试要点 ...
- ABC118D(DP,完全背包,贪心)
#include<bits/stdc++.h>using namespace std;int cnt[10]={0,2,5,5,4,5,6,3,7,6};int dp[10007];int ...
- Spark Streaming 的容错
Spark Streaming 为了实现容错特性,接收到的数据需要在集群的多个Worker 节点上的 executors 之间保存副本(默认2份).当故障发生时,有两种数据需要恢复: 1. 已接收并且 ...
- npm install 权限问题
npm ERR! Error: EACCES: permission denied, access '/Users/Lobin/work/note-vue/node_modules/@babel/hi ...
- POJ1032 Parliament
题目来源:http://poj.org/problem?id=1032 题目大意:给定一个正整数N(5<=N<=1000),将N拆为若干个不同的数使得它们的乘积最大(找到一组互不相等,和为 ...