0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

移植やってます

deque (Python)

cleavage_sites = deque([0], maxlen=ml)

デックは分かるのですが、maxlenは曲者

長さが制限された deque がいっぱいになると、新しい要素を追加するときに追加した要素数分だけ追加したのと反対側から要素が捨てられます

どうする? (Ruby)

class Deque
  attr_accessor :que
  def initialize(*args)
    @que = args[0]
    @maxlen = args[1]
  end

  def push(args)
    @que.push args
    while @maxlen.nil?.! && @que.size > @maxlen
      @que.shift
    end
  end

  def unshift(args)
    @que.unshift args
    while @maxlen.nil?.! && @que.size > @maxlen
      @que.pop
    end
  end
end

deque = Deque.new([1, 2], 3)
deque.push 3
deque.push 4
deque.push 5
p deque.que
deque.unshift 6
deque.unshift 7
deque.unshift 8
p deque.que
deque.que.clear
p deque.que

# [3, 4, 5]
# [8, 7, 6]
# []

このような感じでクラスを作成する。

メモ

  • Python の deque を学習した
  • 道のりは遠そう
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?