MIT 6.00.1x 计算机科学和Python编程导论 Set 1
来源:互联网 发布:java项目开发全程过程 编辑:程序博客网 时间:2024/05/29 02:30
Counting Vowels
Assume s is a string of lower case characters.
Write a program that counts up the number of vowels contained in the string s. Valid vowels are: ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. For example, if s = ‘azcbobobegghakl’, your program should print:
Number of vowels: 5
For problems such as these, do not include raw_input statements or define the variable s in any way. Our automated testing will provide a value of s for you - so the code you submit in the following box should assume s is already defined. If you are confused by this instruction, please review L4 Problems 10 and 11 before you begin this problem set.
print ('Number of vowel: '+str(s.count('a')+s.count('e')+s.count('i')+s.count('o')+s.count('u')))
Counting bobs
Assume s is a string of lower case characters.
Write a program that prints the number of times the string ‘bob’ occurs in s. For example, if s = ‘azcbobobegghakl’, then your program should print:
Number of times bob occurs is: 2
For problems such as these, do not include raw_input statements or define the variable s in any way. Our automated testing will provide a value of s for you - so the code you submit in the following box should assume s is already defined. If you are confused by this instruction, please review L4 Problems 10 and 11 before you begin this problem set.
num=0i=0for i in range(len(s)-2): if (s[i:i+3]=='bob'): num+=1print ('Number of times bob occurs is: '+str(num))
Alphabetical Substrings
Assume s is a string of lower case characters.
Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = ‘azcbobobegghakl’, then your program should print:
Longest substring in alphabetical order is: beggh
In the case of ties, print the first substring. For example, if s = ‘abcbcd’, then your program should print:
Longest substring in alphabetical order is: abc
For problems such as these, do not include raw_input statements or define the variable s in any way. Our automated testing will provide a value of s for you - so the code you submit in the following box should assume s is already defined. If you are confused by this instruction, please review L4 Problems 10 and 11 before you begin this problem set.
'''若未按照字母顺序,便分割字符串,得到字符子串,然后打印长度最长的字符子串'''newL=[]beg=0remain=s for i in range(len(s)-1): if (s[i+1] < s[i]): end=i+1 newL.append(s[beg:end]) beg=end remain=s[beg:]newL.append(remain)longest=''for e in newL: if len(e) > len(longest): longest=eprint ('Longest substring in alphabetical order is: '+str(longest))
I’m gonna make him an offer he can’t refuse. ——《Godfather》
- MIT 6.00.1x 计算机科学和Python编程导论 Set 1
- MIT 6.00.1x 计算机科学和Python编程导论 Set 2
- MIT 6.00.1x 计算机科学和Python编程导论 Set 3
- MIT 6.00.1x 计算机科学和Python编程导论 Set 4
- MIT 6.00.1x 计算机科学和Python编程导论 Set 6
- python 字符串 学堂在线 MITx: 6.00.1x 计算机科学和Python编程导论 problem set 2
- MIT 计算机科学和Python 编程导论
- mit 计算机科学及编程导论(1)
- mit 计算机科学及编程导论(1)
- 学堂在线 MITx: 6.00.1x 计算机科学和Python编程导论 problemset3
- MIT麻省理工学院公开课:计算机科学及编程导论 Python 笔记1-3
- MIT《计算机科学与编程导论》课堂笔记
- MIT《计算机科学与编程导论》第六讲
- MIT《计算机科学与编程导论》第七讲
- mit 计算机科学及编程导论(2)
- mit 计算机科学及编程导论(3)
- mit 计算机科学及编程导论(4)
- mit 计算机科学及编程导论(5)
- C. Alyona and Spreadsheet
- 响应封装器(使用HttpServletResponseWrapper实现)
- 数字金字塔
- Java中泛型方法的使用
- 博为峰JavaEE技术文章 ——MyBatis @one使用方法
- MIT 6.00.1x 计算机科学和Python编程导论 Set 1
- 第六章 注解式控制器详解--Spring MVC 3.1新特性 生产者、消费者请求限定
- 杨辉三角形(记忆化递归)
- C++之 继承考点篇
- 算法相关——Java排序算法之希尔排序(五)
- 较简单的修改和添加功能(链接数据库)
- js实现七天免登陆
- mfc:工具栏中添加组合框
- PAT 1103