Common Words

Common Words

Let‘s continue examining words. You are given two string with words separated by commas. Try to find what is common between these strings. The words are not repeated in the same string.

Your function should find all of the words that appear in both strings. The result must be represented as a string of words separated by commas in alphabetic order.

Tips: You can easily solve this task with several useful functions:str.splitstr.join and sorted. Also try using the built-in type -- set.

Input: Two arguments as strings.

Output: The common words as a string.

题目大义:给出两个字符串,找出在两个字符串中同时出现的单词,并按字典序输出

当然使用了str.split方法,然后想到set有in方法,于是乎得到这么一段代码

 1 def checkio(first, second):
 2     first_str = first.split(‘,‘)
 3     words_set = set(first_str)
 4
 5     second_str = second.split(‘,‘)
 6
 7     result = []
 8
 9     for each in second_str:
10         if each in words_set:
11             result.append(each)
12         else:
13             words_set.add(each)
14
15     result.sort()
16
17
18     return ‘,‘.join(result);

接下来看了别人的解答,发现其实可以把两个字符串split后,转化为set,再使用set的intersection方法即可得到相同单词

Common Words

时间: 2024-10-03 23:31:31

Common Words的相关文章

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

ANSI Common Lisp Chapter 2

Chapter 2 总结 (Summary) Lisp 是一种交互式语言.如果你在顶层输入一个表达式, Lisp 会显示它的值. Lisp 程序由表达式组成.表达式可以是原子,或一个由操作符跟着零个或多个实参的列表.前序表示法代表操作符可以有任意数量的实参. Common Lisp 函数调用的求值规则: 依序对实参从左至右求值,接着把它们的值传入由操作符表示的函数. quote 操作符有自己的求值规则,它完封不动地返回实参. 除了一般的数据类型, Lisp 还有符号跟列表.由于 Lisp 程序是

14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个问题是关于如何寻找字符串数组的公共前缀的,从第一个字母开始,只要共有的都要输出. 思路: 1.首先,如何得到字符串共有前缀,既然共有,那么很容易先想到最短字符串. 2.最短字符串和其余进行比较,这样就省下了大于最短字符串长度的比较. 3.关于字符串的操作,length和null是很必要的判断输入操作,千万别忘记! 4.关

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

bzoj1968【AHOI2005】COMMON 约数研究

1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1492  Solved: 1139 [Submit][Status][Discuss] Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input 3 Sample Output 5 HINT Source Day2

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on

Common LISP自带单步跟踪功能

Common LISP自带单步跟踪功能,执行 (step 要跟踪的命令)即可.以sdraw为例,跟踪其执行. [1]启动单步跟踪 SDRAW[60]> (step (sdraw '(a (b c d) c))) step 1 --> (SDRAW '(A (B C D) C))     显示下一个要执行的语句 Step 1 SDRAW[61]>                         等待用户输入调试指令 [2]输入help查看帮助,帮助的内容很多,前面一大段和Debug是一样的

Longest Common Prefix

https://leetcode.com/problems/longest-common-prefix/ Write a function to find the longest common prefix string amongst an array of strings 1 public class Solution { 2 public static String longestCommonPrefix(String[] strs) { 3 if(strs.length==0){retu

LintCode-Longest Common Substring

Given two strings, find the longest common substring. Return the length of it. Note The characters in substring should occur continiously in original string. This is different with subsequnce. Solution: 1 public class Solution { 2 /** 3 * @param A, B