MITx: 6.00.1x Problem Set1
来源:互联网 发布:淘宝联盟怎么做app 编辑:程序博客网 时间:2024/06/08 21:48
COUNTING VOWELS (10/10 分)
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.
# Paste your code into this box
numVowels = 0for char in s: if char.lower() in 'aeiou': numVowels +=1 else: None print('numVowels is: ' + str(numVowels))
COUNTING BOBS (15/15 分)
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.
# Paste your code into this box
countBob = 0for i in range(len(s)): if s[i:].startswith('bob'): countBob += 1print ("Number of times bob occurs is: ") + str(countBob)
PROBLEM 3: COUNTING AND GROUPING (15/15 分)
A catering company has hired you to help with organizing and preparing customer’s orders. You are given a list of each customer’s desired items, and must write a program that will count the number of each items needed for the chefs to prepare. The items that a customer can order are: salad, hamburger, and water.
Write a function called item_order that takes as input a string named order. The string contains only words for the items the customer can order separated by one space. The function returns a string that counts the number of each item and consolidates them in the following order: salad:[# salad] hamburger:[# hambruger] water:[# water]
If an order does not contain an item, then the count for that item is 0. Notice that each item is formatted as [name of the item][a colon symbol][count of the item] and all item groups are separated by a space.
For example:
If order = “salad water hamburger salad hamburger” then the function returns “salad:2 hamburger:2 water:1”
If order = “hamburger water hamburger” then the function returns “salad:0 hamburger:2 water:1”
Hint
Why is my code printing None?
This question asks you to return a value, not print it. Review the lectures on functions and this additional explanatory document to understand the difference.
# Paste your code into this box
def item_order(order): salads = order.count("salad") hamburgers = order.count("hamburger") waters = order.count("water") return "salad:{} hamburger:{} water:{}".format(salads, hamburgers, waters)
- MITx: 6.00.1x Problem Set1
- python 字符串 学堂在线 MITx: 6.00.1x 计算机科学和Python编程导论 problem set 2
- MITx: 6.00.1x Python Quiz
- MITx: 6.00.1x Alphabetical Substrings (python)
- MITx 6.00 Problem Set 3 hangma
- 学堂在线 MITx: 6.00.1x 计算机科学和Python编程导论 problemset3
- cs224d 作业 problem set1 (二) 简单的情感分析
- set1.cpp
- cs224d 作业 problem set1 (一) 主要是实现word2vector模型,SGD,CBOW,Softmax,算法
- 概率导论 MITx: 6.041x Introduction to Probability 视频网盘下载
- Problem X
- Problem-X
- CONVERT(c1,set1,set2)
- INTERVAL c1 set1
- 两个Set集合,set1,set2
- X-Y Problem
- X-Y Problem
- X-Y Problem
- OpenGL(1)概述
- 正则表达式 总结
- 常见 Java 字节码 指令 助记符
- hdoj 5458 Stability 【并查集 + 树链剖分】
- 淘宝Tprofiler工具实现分析
- MITx: 6.00.1x Problem Set1
- 【ABAP】根据表名和字段变量动态创建内表
- 芒果iOS开发之快速注释插件VVDocumenter-Xcode
- C++的sstream相关知识
- Android笔记---2016/1/28
- Android平台使用MediaCodec进行H264格式的视频编解码
- layer.alert文字换行
- js 字符串转换成数字的三种方法
- 字符串连接