Home > Enterprise >  CS50 Week 6 Python Readability
CS50 Week 6 Python Readability

Time:09-22

The problem set number 6 in cs50 requires to rebuild the Readability project from week 2 in Python
But there is a problem I haven't encountered first when writing the code in C
The programm uses for loop for each letter in input to check if its a period, exclamation point, or question mark which all indicate the end of a sentence
But the input that check50 throws at the program has multiple periods back to back each of which count as its own sentence
So the question is:
How can I improve this sentence counting function to only concider one of periods in case it encounters them back to back? Should I for example add a condition that sees next symbol in a for loop and if its also a period it just ignores it?

def countSentences(input):
    counterS = 0
    for symbol in input:
        if symbol in ['.', '!', '?']:
            counterS  = 1
    return counterS

CodePudding user response:

You can save the previous symbol in another variable. Then if it's the same as the current symbol, don't count it.

def countSentences(input):
    counterS = 0
    previous_symbol = None
    for symbol in input:
        if symbol in ['.', '!', '?']:
            if previous_symbol != symbol:
                counterS  = 1
        previous_symbol = symbol
    return counterS

CodePudding user response:

If you only care about counting the sentences and not tracking which punctuation was used, you can use re.sub to squash duplicate punctuation into a single period (.). The function would look like the following

Documentation here

import re

def countSentences(input):
  counterS = 0
  for symbol in re.sub('[.!?] ', '.', input):
    if symbol in ['.', '!', '?']:
      counterS  = 1
  return counterS
  • Related