python3学习每日一题(4)

python3每日一题


直接上题目

You have to test if a sentence complies with our rule.

Our rule : a sentence is valid if by successively rotating its words to the right we can find a form of the sentence whose words’ sizes make a increasing sequence of difference 1.

Example with the sentence “The grey Mo” and its two only possible rotated forms:
The grey Mo >>> Mo The grey >>> grey Mo The
It’s a valid sentence because “Mo The grey” forms a sequence of words of size 2 3 4, and 2<3<4 and 3-2=1 and 4-3=1
Input
Line 1: A sentence
Output
Line1 :The rotated valid sentence if one exists else ERROR
Constraints
Input contains only letters and spaces, two words are always separated by exactly one simple space.
There is no punctuation.
All sentences are at least 1 word long.
Example
Input
moon peaks to the
Output
to the moon peaks

解题代码如下:

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

sentence = input().split(" ")

for i in range(0,len(sentence)):
    print(sentence[i])
    list1=[]
    for j in range(0,len(sentence)-1):
    
        m = len(sentence[(i+j) % len(sentence)])
        n = len(sentence[(i + j + 1) % len(sentence)])
        if n-m != 1:
            break
        else:
            list1 = list1+[sentence[(i+j)%len(sentence)]]
        
    if len(list1) == len(sentence)-1:
        list1 = list1 + [sentence[(i + j +1) % len(sentence)]]
        print(" ".join(list1))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值