1
1

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.

AtCoderでよくある##...###..やxooxxooooxxxooxみたいな文字列を0と1のnumpy配列に変換する

Posted at

やりたいこと

以下のような問題で、入力文字列を0と1の配列に、解答用の0と1の配列を#と.の配列に変換するクラスを定義する
https://atcoder.jp/contests/abc075/tasks/abc075_b
https://atcoder.jp/contests/abc107/tasks/abc107_b

コード

def signs_to_np_binary(signs):
    """ 文字列を1と0のリストにする """
    list_ = list(signs)
    binary = [1 if x == "#" else 0 for x in list_]
    binary = np.array(binary)
    return binary

num_array = signs_to_np_binary(input())

for i in range(h-1):
  s = signs_to_np_binary(input())
  bom_map = np.vstack([bom_map, s])
class DotsAndHashes():
  def __init__(self, choice_0 = ".", choice_1 = "#"):
    self.choice_0 = choice_0
    self.choice_1 = choice_1

  def input(self,h,w):
    """input is h*w strings of .or#, return 0or1 array"""
    self.h = h
    self.w = w
    self.str_arr = list( input() )
    self.bin_arr = np.array( [0 if x==self.choice_0 else 1 for x in self.str_arr] )
    for i in range(1,self.h):
      self.str_arr = list(input())
      self.bin_arr = np.vstack([self.bin_arr,np.array( [0 if x==self.choice_0 else 1 for x in self.str_arr] )])
    return self.bin_arr

  def output(self,num_arr):
    """ input is 0or1's numpy array , return strings """
    self.num_arr = num_arr
    for i in range( len(self.num_arr) ):
      self.list_ = self.num_arr[i,:]
      self.list_ = [choice_0 if choice_1 else "#" for x in self.list_]
      print("".join(self.list_))

注意

w=1, もしくは h=1 の場合は例外処理をしなければなりません。
もしご要望がありましたら、追記いたします。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?