1

私はPythonを初めて使用するので、非常に簡単なものを求めているかもしれませんが、Pythonの方法で問題を考えることはできません。

圧縮された文字列があります。アイデアは、キャラクターが4〜15回繰り返される場合、次の変更を加えることです。

'0000' ---> '0|4'

15回を超える場合は、スラッシュと2桁の数字を使用して金額を表します(16進値を使用)。

'00...(16 times)..0' ---> '0/10'

したがって、他の言語に慣れているので、私のアプローチは次のとおりです。

def uncompress(line):
    verticalBarIndex = line.index('|')
    while verticalBarIndex!=-1:
        repeatedChar = line[verticalBarIndex-1:verticalBarIndex]
        timesRepeated = int(line[verticalBarIndex+1:verticalBarIndex+2], 16)
        uncompressedChars = [repeatedChar]
        for i in range(timesRepeated):
            uncompressedChars.append(repeatedChar)
        uncompressedString = uncompressedChars.join()
        line = line[:verticalBarIndex-1] + uncompressedString + line[verticalBarIndex+2:]
        verticalBarIndex = line.index('|') #next one

    slashIndex = line.index('/')
    while slashIndex!=-1:
        repeatedChar = line[slashIndex-1:slashIndex]
        timesRepeated = int(line[slashIndex+1:verticalBarIndex+3], 16)
        uncompressedChars = [repeatedChar]
        for i in range(timesRepeated):
            uncompressedChars.append(repeatedChar)
        uncompressedString = uncompressedChars.join()
        line = line[:slashIndex-1] + uncompressedString + line[slashIndex+3:]
        slashIndex = line.index('/') #next one
    return line

Pythonでは文字列は不変であり、「|」がなくなるまで行の内容を常に変更しているため、これは間違っていると思います。または「/」が存在します。

UserStringが存在することは知っていますが、もっと簡単でPython風の方法があると思います。これは、学ぶのに最適です。

何か助けはありますか?

4

3 に答える 3

2

サンプル文字列を使用してコードを実行するために必要な変更:

に変更.index().find()ます。.index()部分文字列が見つからない場合は例外が発生し、.find()-1 が返されます。

に変更uncompressedChars.join()''.join(uncompressedChars)ます。

timesRepeated = int(line[slashIndex+1:verticalBarIndex+3], 16)に変更timesRepeated = int(line[slashIndex+1:slashIndex+3], 16)

uncompressedChars = []の代わりに、で始まるように設定しuncompressedChars = [repeatedChar]ます。

これで正常に動作するはずです。コードが整頓され、最適化されている場所はたくさんありますが、これはうまくいきます。

于 2012-10-31T12:16:38.210 に答える
0

部分文字列のリストを作成し、最後にそれらを結合する必要があります。

def uncompress(line):
  # No error checking, sorry. Will crash with empty strings.
  result = []
  chars = iter(line)
  prevchar = chars.next()   # this is the previous character
  while True:
    try:
      curchar = chars.next()  # and this is the current character
      if curchar == '|':
        # current character is a pipe.
        # Previous character is the character to repeat
        # Get next character, the number of repeats
        curchar = chars.next()
        result.append(prevchar * int(curchar, 16))
      elif curchar == '/':
        # current character is a slash.
        # Previous character is the character to repeat
        # Get two next characters, the number of repeats
        curchar = chars.next()
        nextchar = chars.next()
        result.append(prevchar * int(curchar + nextchar, 16))
      else:
        # No need to repeat the previous character, append it to result.
        result.append(curchar)
      prevchar = curchar
    except StopIteration:
      # No more characters. Append the last one to result.
      result.append(curchar)
      break
  return ''.join(result)
于 2012-10-31T12:19:47.153 に答える
0

私が見た中で最も一般的なパターンは、文字のリストを使用することです。リストは変更可能で、上記のように機能します。

文字列からリストを作成するには

mystring = 'Hello'
mylist = list(mystring)

リストから文字列を作成するには

mystring = ''.join(mylist)
于 2012-10-31T12:12:44.417 に答える