Monday, September 9, 2019

IslandT: Turn string into the score

You are working at a lower league football stadium and you’ve been asked to automate the scoreboard.

The referee will shout out the score, you have already set up the voice recognition module which turns the ref’s voice into a string, but the spoken score needs to be converted into a pair for the scoreboard!

e.g. “The score is four nil” should return [4,0]

Either teams score has a range of 0-9, and the ref won’t say the same string every time e.g.

“new score: two three”

“two two”

“Arsenal just conceded another goal, two nil”

Create a function which will return the array of scores.

def scoreboard(string):
    
    score = ['nil', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    score_arr = []
    string_list = string.split(' ')
    
    for word in string_list:
        
        if word in score:
            
            score_arr.append(score.index(word))
    
    return score_arr

These are the steps to create the score array.

  1. Split the string into a list.
  2. Iterate through the list.
  3. Find the scores and append them to the array then return.


from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...