[LeetCode]#2042. Check if Numbers Are Ascending in a Sentence

  • For example, "a puppy has 2 eyes 4 legs" is a sentence with seven tokens: "2" and "4" are numbers and the other tokens such as "puppy" are words.
Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
Output: true
Explanation: The numbers in s are: 1, 3, 4, 6, 12.
They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
  1. Use split to get each word such as “1”, “box”….
  2. Use isdecimal to judge it is number or not.
  3. If yes, add it to list. If not, skip it.
  4. If list[t] ≥ list[t+1], return False. Else, return True.
class Solution:
def areNumbersAscending(self, s):
ans=[]
for s in s.split():
if not s.isdecimal():
continue
else:
ans.append(int(s))
#sans=sorted(ans)
for i in range(len(ans)-1):
if ans[i]>=ans[i+1]:
return False

return True

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Power BI — many files from data lake

Setting Up Masscan Web UI — Version for Muggles

Control Tasmota with Google Assistant/ Home and Alexa

How to Choose the Right Cloud Migration Strategy for a Data Platform

What is mesh networking?

Another unowned/weak article

How To Install and Use Composer on Debian 9

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2114. Maximum Number of Words Found in Sentences

LeetCode Patterns Adventure 17 — Binary Search

Leetcode 534 Game Play Analysis III without sum window function

Leetcode problem: Solving Questions With Brainpower