MITx 6.00.1x Python Notes: Counting Letters

Problem 1: 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', the program should print:
Number of vowels: 5
Python code

Problem 2: Counting Bobs
Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then the program should print:

Number of times bob occurs is: 2

Python code

Problem 3: Alphabetical Substrings
Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then the program should print:

Longest substring in alphabetical order is: beggh

Python code



5 条评论:

  1. I get it a little but dummy[-1]
    isn't it referring to an array?
    why is the index negative? sorry i just started the course today and it is confusing.

    回复删除
    回复
    1. It is because when the loop starts it refers to the first element and when the loop starts for the second time the -1 index refers to the last added element and because it is checking the ascii value( most likely that is what it is checking ) of each alphabet, it checks for the last added element in dummy and next character that the 'for' loop outputs.

      删除
  2. i was just browsing along and came upon blog. just wanted to say good blog and this article really helped me.
    online word count

    回复删除
  3. can any one help me with the problem set 1 problem 3 for mit 6.00x 2016

    回复删除
  4. Am I right in saying that on line 7, the else statement shouldn't be indented, otherwise the code doesn't look for sub-strings?

    回复删除