重新排序相同的字符,使得字符彼此之间的距离至少为d

时间:2016-04-22 23:01:43

标签: python python-2.7 python-3.x

请帮我编写以下问题的python代码:

  

给定一个小写字符串,重新排序它们   相同的字符至少相距d。

     

输入: { a, b, b }, distance = 2

     

输出: { b, a, b }

1 个答案:

答案 0 :(得分:0)

我尝试过解决这个问题的方法。它尝试创建一个字符列表,并按照频率的顺序用原始字符串中的字符填充它。例如,对于一个字符串' babac'给定距离2,它按顺序填写:

most common:  ('b', 3)  # character b with frequency 3
['-', '-', '-', '-', '-', '-']
updated o:  ['b', '-', '-', '-', '-', '-']
['b', '-', '-', '-', '-', '-']
updated o:  ['b', '-', 'b', '-', '-', '-']
['b', '-', 'b', '-', '-', '-']
updated o:  ['b', '-', 'b', '-', 'b', '-']
most common:  ('c', 2)
['b', '-', 'b', '-', 'b', '-']
updated o:  ['b', 'c', 'b', '-', 'b', '-']
['b', 'c', 'b', '-', 'b', '-']
updated o:  ['b', 'c', 'b', 'c', 'b', '-']
most common:  ('a', 1)
['b', 'c', 'b', 'c', 'b', '-']
updated o:  ['b', 'c', 'b', 'c', 'b', 'a']

给出最终字符串bcbcba。

我已粘贴以下代码。它包含许多关于每条线的作用和原因的评论。尝试运行它。如果要获取详细输出以了解上面的列表是如何填充的,那么取消注释包含print语句的行。

这是代码,希望它有用:

import collections
import math

def printMyString():
  # get inputs
  myStr = raw_input("enter string: ")
  dist = int(raw_input("enter dist: "))

  #create a dict, where each key is a character from myStr and corresponding value is its frequency
  counter = collections.Counter(list(myStr))

  # create an empty list where we will fill our characters to get final string
  o = ['-']*len(myStr)

  # get the most common character
  most_common_char_freq = counter.most_common(1)[0][1]

  # sep is the maximum distance at which repeated instances of the most frequent character m can be located from each other in the final string.
  sep = int(math.ceil(len(myStr)*1.0/most_common_char_freq))

  # if sep is less than given distance, then it is not possible to have such a string.

  if(sep < dist):
    print "such a string is not possible"
    return
  #print "sep", sep


  j = 0 # this marks index at which we can write into the list

  # while we still have characters left, we will continue to fill our output list o 
  while len(counter) > 0:
   current_most_common_char = counter.most_common(1)[0][0]       # get the most common character left in counter        
   current_most_common_char_freq = counter.most_common(1)[0][1]   

   #print "most common: ", current_most_common_char
   while o[j] != '-':  # Go to the next position in the output list where a character is yet to be written.
     j += 1  
     if(j == len(o)):  # We are out of places to write, this is bad!
      # print "breaking, o = ", o
      return

   for i in range(current_most_common_char_freq): # For multiple occurences of the current most freq char, we write them one after the other, a distance of 'sep' apart
    #print o
    if (j+i*sep) >= len(o): # If we have to go beyond the length of the output list/string to write a character, then such a string is not possible
      #print "not possible, o, char is ", o, current_most_common_char
      print "such a string is not possible"
      return
    o[j+i*sep] = current_most_common_char # Write to the output list
    #print "updated o: ", o

   del counter[current_most_common_char] # remove the most common character. lets move on to next one in the loop.
   j += 1 # update before moving on

  print ''.join(o) # merge the characters in the output list to get final string

printMyString()