|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 newbison 于 2019-12-23 15:56 编辑
不知道有没有小伙伴玩codewars。
题目如下
我做出来的答案跟最终答案顺序上总是差一点点~~~
有没有大神来解答一下的?谢谢了。
Given two strings s1 and s2, we want to visualize how different the two strings are. We will only take into account the lowercase letters (a to z). First let us count the frequency of each lowercase letters in s1 and s2.
s1 = "A aaaa bb c"
s2 = "& aaa bbb c d"
s1 has 4 'a', 2 'b', 1 'c'
s2 has 3 'a', 3 'b', 1 'c', 1 'd'
So the maximum for 'a' in s1 and s2 is 4 from s1; the maximum for 'b' is 3 from s2. In the following we will not consider letters when the maximum of their occurrences is less than or equal to 1.
We can resume the differences between s1 and s2 in the following string: "1:aaaa/2:bbb" where 1 in 1:aaaa stands for string s1 and aaaa because the maximum for a is 4. In the same manner 2:bbb stands for string s2 and bbb because the maximum for b is 3.
The task is to produce a string in which each lowercase letters of s1 or s2 appears as many times as its maximum if this maximum is strictly greater than 1; these letters will be prefixed by the number of the string where they appear with their maximum value and :. If the maximum is in s1 as well as in s2 the prefix is =:.
In the result, substrings (a substring is for example 2:nnnnn or 1:hhh; it contains the prefix) will be in decreasing order of their length and when they have the same length sorted in ascending lexicographic order (letters and digits - more precisely sorted by codepoint); the different groups will be separated by '/'. See examples and "Example Tests".
Hopefully other examples can make this clearer.
s1 = "my&friend&Paul has heavy hats! &"
s2 = "my friend John has many many friends &"
mix(s1, s2) --> "2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"
s1 = "mmmmm m nnnnn y&friend&Paul has heavy hats! &"
s2 = "my frie n d Joh n has ma n y ma n y frie n ds n&"
mix(s1, s2) --> "1:mmmmmm/=:nnnnnn/1:aaaa/1:hhh/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"
s1="Are the kids at home? aaaaa fffff"
s2="Yes they are here! aaaaa fffff"
mix(s1, s2) --> "=:aaaaaa/2:eeeee/=:fffff/1:tt/2:rr/=:hh"
- def islower(s): #将一串字符串中的小写字母摘出来
- length = len(s)
- res = ''
- for i in range(length):
- if s[i].islower(): res = res + s[i]
- return res
-
- def count_chr(s): #给定一串小写的字符串,给出每个字母的个数
- myset = set(list(s))
- res = {}
- for each in myset:
- res[each] = s.count(each)
- return res
- def compare(s1, s2):
- res = {}
- s1 = islower(s1)
- s2 = islower(s2)
- dict1 = count_chr(s1)
- dict2 = count_chr(s2)
- myset = set(list(s1+s2))
- for each in myset:
- if each not in dict1:
- res[each * dict2[each]] = '2'
- else: pass
- if each not in dict2:
- res[each * dict1[each]] = '1'
- else: pass
- if each in dict1 and each in dict2:
- if dict1[each] == dict2[each]:
- res[each * dict1[each]] = '='
- elif dict1[each] < dict2[each]:
- res[each * dict2[each]] = '2'
- else:
- res[each * dict1[each]] = '1'
-
- return res
- def mix(s1, s2):
- dict1 = compare(s1, s2)
- list1= sorted(dict1.items(),key=lambda x: len(x[0]))
- res = ''
- for each in list1:
- if len(each[0]) != 1:
- res = each[1] + ':' + each[0] + '/' + res
- return res
- s1 = 'asdfafafasdgadsg'
- s2 = 'yoiyioafdhsdfdfdfagasafdafsadf'
- s3 = 'aaaabbb'
- s4 = 'aaaabbbb'
- print(mix(s1, s2))
- print(mix(s3, s4))
- print(mix("Are they here", "yes, they are here"))
- print(mix("looping is fun but dangerous", "less dangerous than coding"))
- print(mix(" In many languages", " there's a pair of functions"))
- print(mix("Lords of the Fallen", "gamekult"))
复制代码
本帖最后由 TJBEST 于 2019-12-23 17:02 编辑
我已经给你改完了:1你没有考虑长度一样时是何种优先级 组号 和字母 你没考虑 2 你最后会多打出一个/应该去掉 python3的sorted函数的cmp参数没有了 我将比较函数曲线救国变成了key 代码如下:
- from functools import cmp_to_key
- def islower(s): #将一串字符串中的小写字母摘出来
- length = len(s)
- res = ''
- for i in range(length):
- if s[i].islower(): res = res + s[i]
- return res
- def count_chr(s): #给定一串小写的字符串,给出每个字母的个数
- myset = set(list(s))
- res = {}
- for each in myset:
- res[each] = s.count(each)
- return res
- def compare(s1, s2):
- res = {}
- s1 = islower(s1)
- s2 = islower(s2)
- dict1 = count_chr(s1)
- dict2 = count_chr(s2)
- myset = set(list(s1+s2))
- for each in myset:
- if each not in dict1:
- res[each * dict2[each]] = '2'
- else: pass
- if each not in dict2:
- res[each * dict1[each]] = '1'
- else: pass
- if each in dict1 and each in dict2:
- if dict1[each] == dict2[each]:
- res[each * dict1[each]] = '='
- elif dict1[each] < dict2[each]:
- res[each * dict2[each]] = '2'
- else:
- res[each * dict1[each]] = '1'
-
- return res
- def Mycmp(a,b):
- if len(a[0])> len(b[0]):
- return 1
- elif len(a[0]) < len(b[0]):
- return -1
- else:
- if a[1]< b[1]:
- return 1
- elif a[1] > b[1]:
- return -1
- else:
- if a[0][0] < b[0][0]:
- return 1
- else:
- return -1
-
- def mix(s1, s2):
- dict1 = compare(s1, s2)
- cmp2key = cmp_to_key(Mycmp)#cmp在python3弃用
- list1= sorted(dict1.items(),key = cmp2key)#从小到大排 结果是从大到小 所以倒过来是对的 但是会多出一个/
- res = ''
- for each in list1:
- if len(each[0]) != 1:
- res = each[1] + ':' + each[0] + '/' + res
- return res[0:(len(res)-1)]
复制代码
|
|