地址:http://www.codewars.com/kata/5202ef17a402dd033c000009/train/python

题目:

A string is considered to be in title case if each word in the string is either (a) capitalised (that is, only the first letter of the word is in upper case) or (b) considered to be an exception and put entirely into lower case unless it is the first word, which is always capitalised.

Write a function that will convert a string into title case, given an optional list of exceptions (minor words). The list of minor words will be given as a string with each word separated by a space. Your function should ignore the case of the minor words string -- it should behave in the same way even if the case of the minor word string is changed.

Example:

title_case('a clash of KINGS', 'a an the of') # should return: 'A Clash of Kings'

title_case('THE WIND IN THE WILLOWS', 'The In') # should return: 'The Wind in the Willows'

title_case('the quick brown fox') # should return: 'The Quick Brown Fox'

代码:

def title_case(title,minor_words=""):
ans = title.title()
titleList = ans.split(" ")
words = minor_words.title()
wordsList = words.split(" ") if ans == "":
return ""
else:
for i in range(1,len(titleList)):
if titleList[i] in wordsList:
titleList[i] = titleList[i][0].lower() + titleList[i][1:] return " ".join(titleList)

  

Title Case的更多相关文章

  1. Title Case a Sentence-freecodecamp算法题目

    Title Case a Sentence(中单词首字母大写) 要求 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 思路 将句子小写化后用.split(& ...

  2. Title Case a Sentence

    解决思路 将字符串转换成小写 把字符串分割成字符串数组 循环数组将每一个单词首字母大写 把数组所有的元素转换成一个字符串 第一种方法 function titleCase(str) { str=str ...

  3. freeCodeCamp:Title Case a Sentence

    确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. /*思路 将字符串转为小写.toLowerCase() 分割字符串以单词形式组成数组myarr 确保数组中的 ...

  4. FreeCodeCamp:Title Case a Sentence

    要求: 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 结果: titleCase("I'm a little tea pot") 应该 ...

  5. FCC JS基础算法题(4):Title Case a Sentence(句中单词首字母大写)

    题目描述: 确保字符串的每个单词首字母都大写,其余部分小写.像'the'和'of'这样的连接符同理. 算法: function titleCase(str) { // 转小写及分割成数组 var st ...

  6. CASE WHEN 及 SELECT CASE WHEN的用法(转)

    Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END   ...

  7. [Upper case conversion ] 每个单词的首小写字母转换为对应的大写字母

    Given a string , write a program to title case every first letter of words in string. Input:The firs ...

  8. CASE WHEN 批量更新

    单个值: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHE ...

  9. Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例

    如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 复制代码 代码如下: UPDATE mytable SET myfield = CASE i ...

随机推荐

  1. 粘滞位(sticky bit)

    linux特殊权限:setUid, setGid, 粘着位(sticky) (1)目录的X权限(执行) 文件的可执行权限很简单,也就是可否执行它的意思,但目录的执行权限又代表什么意思呢? 当然不可能是 ...

  2. Quartz2D学习笔记(1)

    ********************************** 简介 *************************************** Quartz2D是⼀个二维绘图引擎,同时支持 ...

  3. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  4. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  5. PhoneGap 3 在 Mac 上安装使用

    1.下载安装 NodeJS . 2.安装 PhoneGap.打开终端执行: 1 $ sudo npm install -g phonegap 3.PhoneGap 3 不需要在Xcode中创建,而是在 ...

  6. caffe之(一)卷积层

    在caffe中,网络的结构由prototxt文件中给出,由一些列的Layer(层)组成,常用的层如:数据加载层.卷积操作层.pooling层.非线性变换层.内积运算层.归一化层.损失计算层等:本篇主要 ...

  7. python中的字典(dict),列表(list),元组(tuple)

    一,List:列表 python内置的一种数据类型是列表:list.list是一种有序的数据集合,可以随意的添加和删除其中的数据.比如列出班里所有的同学的名字,列出所有工厂员工的工号等都是可以用到列表 ...

  8. 练习--python中的Queue与多进程(multiprocessing)

    按官方说法: This module is OBSOLETE and is only provided on PyPI to support old projects that still use i ...

  9. 【UVA 10369】 Arctic Network (最小生成树)

    [题意] 南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连.任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远. 而安装有无线电设备的两个站 ...

  10. Highcharts实例

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...