Python:每日一题 75

2017-11-27 14:32:00
六月
来源:
http://bbs.fishc.com/thread-94131-1-1.html
转贴 522
&名字问题&





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?


中文版:

您是VIP用户,您可免回复查看本帖隐藏的内容

我的朋友想要一个乐队的新乐队名。 她喜欢使用以下公式的乐队:'The'+具有第一个字母大写的名词。

dolphin - >The Dolphin

然而,当一个名词STARTS和ENDS具有相同的字母时,她喜欢重复该名词两次,并将它们与第一个和最后一个字母连接在一起,并合并成一个如下所示的单词(前面没有“The”):

alaska -> Alaskalaska

europe -> Europeurope

你可以写一个将一个名词作为一个字符串的函数,并返回一个写成字符串的首选频带名称吗?



答案:

您是VIP用户,您可免回复查看本帖隐藏的内容

  1. def xxx(name):
  2.     if name[0] != name[-1]:
  3.         return 'The ' + name[0].upper() + name[1:]
  4.     else:
  5.         return name[0].upper() + name[1:] * 2
复制代码
&名字问题&





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?


中文版:

您是VIP用户,您可免回复查看本帖隐藏的内容

我的朋友想要一个乐队的新乐队名。 她喜欢使用以下公式的乐队:'The'+具有第一个字母大写的名词。

dolphin - >The Dolphin

然而,当一个名词STARTS和ENDS具有相同的字母时,她喜欢重复该名词两次,并将它们与第一个和最后一个字母连接在一起,并合并成一个如下所示的单词(前面没有“The”):

alaska -> Alaskalaska

europe -> Europeurope

你可以写一个将一个名词作为一个字符串的函数,并返回一个写成字符串的首选频带名称吗?



答案:

您是VIP用户,您可免回复查看本帖隐藏的内容

  1. def xxx(name):
  2.     if name[0] != name[-1]:
  3.         return 'The ' + name[0].upper() + name[1:]
  4.     else:
  5.         return name[0].upper() + name[1:] * 2
复制代码
发表评论
评论通过审核后显示。