新手·ing 发表于 2017-8-10 08:38:52

Python:每日一题 75(答题领鱼币)

本帖最后由 新手·ing 于 2017-8-10 08:49 编辑

&名字问题&



My friend wants a new band name for her band. She like bands that use the formula: 'The' + a noun with first letter capitalized.

dolphin -> The Dolphin

However, when a noun STARTS and ENDS with the same letter, she likes to repeat the noun twice and connect them together with the first and last letter, combined into one word like so (WITHOUT a 'The' in front):

alaska -> Alaskalaska

europe -> Europeurope

Can you write a function that takes in a noun as a string, and returns her preferred band name written as a string?

中文版:
**** Hidden Message *****

答案:
**** Hidden Message *****

shinemic 发表于 2017-8-10 09:00:54

本帖最后由 shinemic 于 2017-8-10 09:07 编辑

由于不清楚输入的字符是否全部小写以及输出是否要求严格「仅首字母大写」,试写了两个版本:
def preferred_band_name_raw(rawstr):
    if rawstr.lower() == rawstr[-1].lower():
      tmp = rawstr * 2
      return rawstr.upper() + tmp
    else:
      return 'The ' + rawstr.upper() + rawstr

def preferred_band_name(rawstr):
    if rawstr.lower() == rawstr[-1].lower():
      return (rawstr + rawstr * 2).title()
    else:
      return 'The ' + rawstr.title()

print('preferred_band_name_raw:')
print(preferred_band_name_raw('Dolphin'))
print(preferred_band_name_raw('DolphiN'))
print(preferred_band_name_raw('Dolphid'))
print(preferred_band_name_raw('DolphiD'))
print('preferred_band_name:')
print(preferred_band_name('Dolphin'))
print(preferred_band_name('DolphiN'))
print(preferred_band_name('Dolphid'))
print(preferred_band_name('DolphiD'))输出:preferred_band_name_raw:
The Dolphin
The DolphiN
Dolphidolphid
DolphiDolphiD
preferred_band_name:
The Dolphin
The Dolphin
Dolphidolphid
Dolphidolphid手法低劣,还望大Lao指正!{:10_270:}

小Q学Python 发表于 2017-8-10 09:26:21

本帖最后由 小Q学Python 于 2017-8-10 09:40 编辑

def test(n):
    n = n.lower()
    if n==n[-1]:
      return n.upper()+n*2
    else:
      return 'The '+ n.upper()+n

MSK 发表于 2017-8-10 10:08:26

def test(string):
        return 'The %s' % string.capitalize() if string != string[-1] else (string*2).capitalize()

MSK 发表于 2017-8-10 10:09:19

支持楼主{:10_270:}

Mrzhou1201 发表于 2017-8-10 10:23:10

#def name():
    usrinput = input('plz input a world:')
    usrinput.lower()

    if usrinput==usrinput[-1]:
      usrinput = usrinput+usrinput
    return('The '+usrinput.title())
#

woigh 发表于 2017-8-10 11:43:10

def bandName(str):
    if str == str[-1]:
      band_name = str.capitalize() + str
      return band_name
    else:
      band_name = 'The ' +str.capitalize()
      return band_name

name = input("input your band name: ")

print("your band name is " + bandName(name.lower()))


ktjack2009 发表于 2017-8-11 14:57:21

word = input('please input a word:')
if word == word[-1]:
    band_name = (word + word).capitalize()
else:
    band_name = 'The ' + word.capitalize()
print(band_name)

左手十字 发表于 2017-8-11 15:07:34

中文版

冬雪雪冬 发表于 2017-8-11 16:01:57

def fun(s):
    return (s[:-1] + s).capitalize() if s == s[-1] else ('The ' + s)

stefanie0511 发表于 2017-8-11 17:00:04

呐呢{:5_94:}

chunchun2017 发表于 2017-8-11 17:28:14

本帖最后由 chunchun2017 于 2017-8-12 10:20 编辑

def NewBand(str):
    str0=str.capitalize()
    if(str==str[-1]):
      str0+=str
    else:
      str0='The '+str0
    print(str0)
name = input("Please input The band name:")
NewBand(name)


运行结果:
====================== RESTART: F:\Python3\code\名字问题.py ======================
Please input The band name:dolphin
The Dolphin
>>>
====================== RESTART: F:\Python3\code\名字问题.py ======================
Please input The band name:alaska
Alaskalaska
>>>
====================== RESTART: F:\Python3\code\名字问题.py ======================
Please input The band name:europe
Europeurope
>>>

上面的代码可以继续精简为:
def NewBrandName(s):
    print (s.capitalize()+s if s==s[-1] else "The "+s.capitalize())
s = input("请输入名字:")
NewBrandName(s)

撒法空间 发表于 2017-8-11 19:02:26

看看看

九九八十一 发表于 2017-8-11 19:45:01

果断中文

lalalaoh 发表于 2017-8-11 21:08:21


def pre_tit():
      origin = input("Please input title: ")
      new=''
      if not origin == origin[-1]:
                new= origin.upper()+origin
                new='The '+new
      else:
                new= origin+origin
                new=new.upper()+new
      print(new)

sdsd 发表于 2017-8-12 10:24:06

while True:
      band=input("Please Input:")

      if band!=band[-1]:            #首尾字母不相等
                print('Result: The '+band.title()+'\n')
      else:                                    #首尾字母相等
                print('Result: '+band.title()[:-1]+band+'\n')

swordlj 发表于 2017-8-12 14:40:04

def fun(name):
    return (name[:-1]+name).title() if name == name[-1] else ('The ' + name.title())

魔王浮士德 发表于 2017-8-12 16:50:34

谢谢

1141429506 发表于 2017-8-12 22:21:23

str1 = input('请输入一个乐队的名字')
str2 = 'The '+ str1.capitalize()
print(str2)

if str1==str1:
    str3 = str1*2
    str4=str3[:len(str1)]+str3
    str5=str4.capitalize()
    print(str5)

l1993520 发表于 2017-8-12 23:29:25

英文不咋的,过来看看
页: [1] 2 3 4 5
查看完整版本: Python:每日一题 75(答题领鱼币)