当前位置:网站首页>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 count
Add :
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 += 1
Built 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],
边栏推荐
- Introduction and experience of wazuh open source host security solution
- [practical skills] how to do a good job in technical training?
- Drawing dynamic 3D circle with pure C language
- Implement a fixed capacity stack
- Implement an iterative stack
- Time of process
- 【云原生】微服务之Feign自定义配置的记录
- Typical use cases for knapsacks, queues, and stacks
- [cloud native] record of feign custom configuration of microservices
- 注解与反射
猜你喜欢
Web APIs DOM node
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Sword finger offer 05 Replace spaces
Little known skills of Task Manager
剑指 Offer 58 - II. 左旋转字符串
从Dijkstra的图灵奖演讲论科技创业者特点
Sword finger offer 09 Implementing queues with two stacks
sync. Interpretation of mutex source code
Introduction to tools in TF-A
Sword finger offer 53 - ii Missing numbers from 0 to n-1
随机推荐
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
A preliminary study of sdei - see the essence through transactions
AtCoder Grand Contest 013 E - Placing Squares
CF1637E Best Pair
The number of enclaves
挂起等待锁 vs 自旋锁(两者的使用场合)
软件测试 -- 0 序
On-off and on-off of quality system construction
Mysql database (I)
剑指 Offer 06.从头到尾打印链表
剑指 Offer 58 - II. 左旋转字符串
“磐云杯”中职网络安全技能大赛A模块新题
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Solution to the palindrome string (Luogu p5041 haoi2009)
In this indifferent world, light crying
Wazuh开源主机安全解决方案的简介与使用体验
Implement a fixed capacity stack
Add level control and logger level control of Solon logging plug-in
数仓项目的集群脚本
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade