[LeetCode]#2042. Check if Numbers Are Ascending in a Sentence
Environment: Python 3.8
Key technique: split, isdecimal, for, if, append
A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Every token is either a positive number consisting of digits
0-9 with no leading zeros, or a word consisting of lowercase English letters.
- For example,
"a puppy has 2 eyes 4 legs"is a sentence with seven tokens:
"4"are numbers and the other tokens such as
Given a string
s representing a sentence, you need to check if all the numbers in
s are strictly increasing from left to right (i.e., other than the last number, each number is strictly smaller than the number on its right in
true if so, or
Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
Explanation: The numbers in s are: 1, 3, 4, 6, 12.
They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
- Use split to get each word such as “1”, “box”….
- Use isdecimal to judge it is number or not.
- If yes, add it to list. If not, skip it.
- If list[t] ≥ list[t+1], return False. Else, return True.
def areNumbersAscending(self, s):ans=
for s in s.split():
if not s.isdecimal():
for i in range(len(ans)-1):