当前位置:网站首页>One question per day 2047 Number of valid words in the sentence
One question per day 2047 Number of valid words in the sentence
2022-07-05 05:42:00 【A big pigeon】
Judge the number of effective words in the sentence .
The topic is relatively simple , Split sentences into words , Then judge whether the word is valid .
however “ It works ” The conditions are a little cumbersome :
If one token At the same time, the following conditions are met , I think this is token Is a valid word :
1. No numbers
2. At most one A hyphen '-' . If there is , There should be lowercase letters on both sides of the hyphen ("a-b" Is a valid word , but "-ab" and "ab-" Not a valid word ).
3. At most one Punctuation . If there is , Punctuation marks should be located at token Of At the end of .
Official explanation :
class Solution:
def countValidWords(self, sentence: str) -> int:
def valid(s):
hasHyphens = False
for i, ch in enumerate(s):
if ch.isdigit() or ch in "!.," and i < len(s)-1:
return False
if ch == '-':
if hasHyphens or i==0 or i== len(s)-1 or not s[i-1].islower() or not s[i+1].islower():
return False
hasHyphens = True
return True
return sum(valid(s) for s in sentence.split())
His writing :
I haven't seen it before , Easy to leak conditions .
class Solution:
def countValidWords(self, sentence: str) -> int:
tokens = sentence.split()
count = 0
print(tokens)
for token in tokens:
istoken = True
hasHyphens = False
for i,x in enumerate(token):
if x.isdigit():
istoken = False
break
if x in "!.," and i != len(token)-1:
istoken = False
break
if x == '-':
if hasHyphens or i==0 or i== len(token)-1 or not token[i-1].islower() or not token[i+1].islower():
istoken = False
break
hasHyphens = True
if istoken and token:
count += 1
return countAdd :
Three ways to simplify code learned :
One 、enumerate(iterable,start=0) Returns an enumeration object , It contains a count value and iteration object iterable Generated value .
This method is equivalent to :
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1Built in functions — Python 3.10.2 file
Two 、sum(iterable, /, start=0) Also can be [True,True,False] So boolean type sums , Return to it True Number .
from start Start from left to right iterable And returns the total value . iterable The term of is usually a number , and start Value is not allowed to be a string .
3、 ... and 、 use in Instead of multiple == or == or ==
Such as : ch in "!.,"
x in [1,3,5],
边栏推荐
- Sword finger offer 04 Search in two-dimensional array
- Sword finger offer 58 - ii Rotate string left
- SSH password free login settings and use scripts to SSH login and execute instructions
- 【实战技能】如何做好技术培训?
- Developing desktop applications with electron
- Haut OJ 2021 freshmen week II reflection summary
- Wazuh開源主機安全解决方案的簡介與使用體驗
- 卷积神经网络——卷积层
- SAP-修改系统表数据的方法
- 软件测试 -- 0 序
猜你喜欢

Sword finger offer 58 - ii Rotate string left

CF1634E Fair Share

Solution to game 10 of the personal field
![[practical skills] how to do a good job in technical training?](/img/a3/7a1564cd9eb564abfd716fef08a9e7.jpg)
[practical skills] how to do a good job in technical training?

AtCoder Grand Contest 013 E - Placing Squares

游戏商城毕业设计

Brief introduction to tcp/ip protocol stack

Some common problems in the assessment of network engineers: WLAN, BGP, switch

lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
随机推荐
Alu logic operation unit
注解与反射
[jailhouse article] look mum, no VM exits
Sword finger offer 09 Implementing queues with two stacks
A misunderstanding about the console window
Haut OJ 2021 freshmen week II reflection summary
Transform optimization problems into decision-making problems
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
卷积神经网络——卷积层
Detailed explanation of expression (csp-j 2021 expr) topic
Over fitting and regularization
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
On-off and on-off of quality system construction
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
Daily question - Search two-dimensional matrix PS two-dimensional array search
【Jailhouse 文章】Look Mum, no VM Exits
Full Permutation Code (recursive writing)
Solution to the palindrome string (Luogu p5041 haoi2009)
sync.Mutex源码解读