zltzlt 发表于 2020-4-2 17:50:20

Python:每日一题 367

本帖最后由 zltzlt 于 2020-4-2 17:54 编辑

今天的题目:

给定字符串 S 和 T,它们只包含小写字符。在 S 中,所有字符只会出现一次。

S 已经根据某种规则进行了排序。我们要根据 S 中的字符顺序对 T 进行排序。

更具体地说,如果 S 中 x 在 y 之前出现,那么返回的字符串中 x 也应出现在 y 之前。

返回任意一种符合条件的对 T 排序后的字符串。

示例:

输入:S = "cba", T = "abcd"
输出:"cbad"
解释:S 中出现了字符 "a", "b", "c",所以 "a", "b", "c" 的顺序应该是 "c", "b", "a" 。
由于 "d" 没有在 S 中出现,它可以放在 T 的任意位置。"dcba", "cdba", "cbda" 都是正确的答案。

{:10_298:}欢迎大家一起答题!{:10_298:}

TJBEST 发表于 2020-4-2 17:56:47

本帖最后由 TJBEST 于 2020-4-2 20:03 编辑

先来一个
def fun367(S,T):
    def inner(element):
      try:
            return S.index(element)
      except Exception:
            return 100
    T_arr = list(T)
    T_arr.sort(key = inner)
    result = ''
    for each in T_arr:
      result += each
    return result
再来一个 ,您测测 不知道哪个更快,如果您测完了 请写两个测试时间 我最近想研究一下 python的内置函数算法 以便优化。
def fun367(S,T):
    def inner(element):
      try:
            return S.index(element)
      except Exception:
            return 100
    dic = {}
    for each in T:
      try:
            dic += 1
      except Exception:
            dic = 1
    item = list(dic.items())
    item.sort(key = inner)
    result = ''
    for each in item:
      result += each*each
    return result

wcshds 发表于 2020-4-2 18:16:59

def test(S, T):
    def idx(x):
      try:
            index = S.index(x)
      except:
            index = 99
      return index
    return ''.join(sorted(T, key=idx))
奇奇怪怪的

永恒的蓝色梦想 发表于 2020-4-2 18:30:24

本帖最后由 永恒的蓝色梦想 于 2020-4-2 18:40 编辑

垃圾方法class Solution:
    def customSortString(self, S: str, T: str) -> str:
      d={j:i for i,j in enumerate(S)}
      return ''.join(sorted(T,key=lambda v:d.get(v,-1)))

NAMELESSONE 发表于 2020-4-2 18:39:21

def solve(S,T):
    string = ''
    for i in S:
      string += i*T.count(i
                            )
      T=T.replace(i,'')
    return string+T

kinkon 发表于 2020-4-2 18:47:10

本帖最后由 kinkon 于 2020-4-2 20:21 编辑

试试这样行不行
def f367(S, T):
    tmp_S = set(S) & set(T)
    s = ''
    for i in S:
      if i in tmp_S:
            s += i * T.count(i)
            T = T.replace(i,'')
    return s + ''.join(T)

永恒的蓝色梦想 发表于 2020-4-2 18:48:09

TJBEST 发表于 2020-4-2 17:56
先来一个

再来一个 ,您测测 不知道哪个更快,如果您测完了 请写两个测试时间 我最近想研究一下 python ...

冒昧的提个意见

如果追求效率的话    def inner(element):
      if element in S:
            return S.index(element)
      else:
            return 100这段代码每执行一次,就会遍历两次(不一定完全遍历)S,拉低效率。

旅途Z 发表于 2020-4-2 19:31:30

def str_sort(s, t):
    sort_dict = {}
    sorted_str = ""
    for letter in t:
      if letter not in sort_dict:
            sort_dict = 0
      sort_dict += 1
    for each_letter in s:
      if each_letter in sort_dict:
            for i in range(sort_dict):
                sorted_str += each_letter
            del sort_dict
    for item in sort_dict:
      for j in range(sort_dict):
            sorted_str += item
    return sorted_str

TJBEST 发表于 2020-4-2 20:01:05

本帖最后由 TJBEST 于 2020-4-2 20:04 编辑

永恒的蓝色梦想 发表于 2020-4-2 18:48
冒昧的提个意见

如果追求效率的话这段代码每执行一次,就会遍历两次(不一定完全遍历)S,拉低效率。

有道理,谢谢了

chen971130 发表于 2020-4-2 20:26:00

def a367(s,t):
    a, b = list(s), list(t)
    for i in a:
      if i in b:
            b.remove(i)
            b.append(i)
    print(''.join(b))
a367('cba','abcd')

字符串的方法掌握的不好先,转化成列表做吧

风魔孤行者 发表于 2020-4-2 20:50:33

def f(s,t):
    d = {}
    for each in t:
      if each in d.keys():
            d += 1
      else:
            d.setdefault(each,1)
    t1 = ''
    for each in s:
      if each in d.keys():
            t1 += each * d
            del d
    for n in d.keys():
      t1 += n * d
    return t1

sYMMetrY 发表于 2020-4-2 21:05:10

def q367(S,T):
    dict_S = dict()
    for each in S:
      dict_S = 0

    new_T = ''
    for each in T:
      if each in dict_S:
            dict_S += 1
      else:
            new_T += each

    new_S = ''
    for each in S:
      new_S += each*dict_S

    return new_S + new_T

March2615 发表于 2020-4-2 21:20:36

def customSortString_2(S: str, T: str) -> str:
    dict_T = {}
    for each in T:
      dict_T = dict_T.setdefault(each, 0) + 1
    result = []
    for ch in S:
      if ch in T:
            result.append(ch * dict_T)
    for ch in T:
      if ch not in S:
            result.append(ch)
    return ''.join(result)

fan1993423 发表于 2020-4-2 21:50:20

那问一下如果S='cba',T='abcdac'则答案是什么?

ouyunfu 发表于 2020-4-2 21:59:22

能一行代码解决的问题坚决不用两行,但效率可能会低点{:5_91:}def f367(S, T):
    return ''.join()+''.join()

fan1993423 发表于 2020-4-2 22:02:02

def fun367(S,T):
    dic={i:T.count(i) for i in set(T)}
    result=''
    for i in S:
      if i in dic:
            result+=i*dic
            dic.pop(i)
    for k,v in dic.items():
      result+=k*v
    return result

whosyourdaddy 发表于 2020-4-2 22:25:46

def func367(S,T):
    res = ''
    for each in S:
       if each in T:
         res += each * T.count(each)
         T = T.replace(each,'')
    return res+T

l0stparadise 发表于 2020-4-2 22:56:08

def f367(s,t):
    dict_s_index = {}
    for index,string in enumerate(s):
      dict_s_index = index
    dict_t={}
    for i in t:
      dict_t=dict_t.get(i,0)+1
    t_str=''
    for i in dict_s_index:
      if i in dict_t.keys():
            t_str+=dict_t*i
      else:
            continue
    for j in dict_t:
      if j not in dict_s_index.keys():
            t_str+=dict_t*j
    return t_str

在东边 发表于 2020-4-2 23:44:56

def fun367(S, T):
    return ''.join(sorted(T, key=S.find))

十月故里 发表于 2020-4-2 23:48:51

本帖最后由 十月故里 于 2020-4-2 23:56 编辑

s=input()
t=input()
a=s
for i in a:
    if i not in t:
      s=s.replace(i,'')
    else:
      num=t.count(i)
      t=t.replace(i,'')
      s=s.replace(i,i*num)
      
print(s+t)
页: [1] 2 3
查看完整版本: Python:每日一题 367