zoukankan      html  css  js  c++  java
  • Python 单词字母顺序不变且所有倒排

    翻出google測试project师的一道题目:

    设计一个函数,不论什么语言都能够,实现下面功能:

    一个句子,将句子中的单词所有倒排过来,但单词的字母顺序不变。eg.  this is a real world输出结果为:world real a is this

    笔者用Python实现例如以下:

    #! /usr/bin/env python

    # -* -coding:utf-8-*-

    def str_reverse(str):

             str_dst =str.split()[A1] 

             str_dst.reverse()[A2] 

             returnstr_dst

    if __name__ == ‘__main__’:

             forstr_out in str_reverse(raw_input(‘plz input ur sentence:’)):

                       printstr_out,

    Python is Python!


     [A1]以空格为分隔符,将单词取出放入list中。

    关于split():

    string.split(s[, sep[, maxsplit]])

    Return a list of the words of thestring s. If the optional second argument sep is absent or None, the words areseparated by arbitrary strings of whitespace characters (space, tab, newline,return, formfeed). If the second argument sep is present and not None, itspecifies a string to be used as the word separator. The returned list willthen have one more item than the number of non-overlapping occurrences of theseparator in the string. If maxsplit is given, at most maxsplit number ofsplits occur, and the remainder of the string is returned as the final elementof the list (thus, the list will have at most maxsplit+1 elements). If maxsplitis not specified or -1, then there is no limit on the number of splits (allpossible splits are made).

    The behavior of split on an emptystring depends on the value of sep. If sep is not specified, or specified asNone, the result will be an empty list. If sep is specified as any string, theresult will be a list containing one element which is an empty string

     [A2]反转list

    关于reverse():

    s.reverse() reverses the items of s inplace

    The reverse() methods modify the listin place for economy of space when reversing a large list. To remind you thatthey operate by side effect, they don’t return the reversed list.

  • 相关阅读:
    ASP.NET面试题(二)
    iBatis.Net系列(四) iBatisNet API基础
    ibatisnet系列(一) 总览
    iBatisnet系列(二) 配置运行环境和日志处理
    HDU 1575 Tr A (矩阵乘法)
    HDU 连连看
    1504: ZZ的橱柜 (优先队列)
    离散化思想
    POJ 2777 Count Color (线段树)
    POJ 1823 Hotel (线段树)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3829511.html
Copyright © 2011-2022 走看看