MITx: 6.00.1x Problem Set1

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 = 0
for 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 = 0
for i in range(len(s)):
    if s[i:].startswith('bob'):
        countBob += 1
print ("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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值