# 簡単な問があったのでPythonでやってみた

#### 問

２つの引数をもらうと次のReturnsが返る関数

##### 例
```#引き数
#引き数
#Returns:返り値

3
"123123123"
Returns: ["111","222","333"]

4
"123123123"
Returns: ["12","23","31","12"]

6
"012345012345012345"
Returns: ["000", "111", "222", "333", "444", "555" ]

4
"111122223333"
Returns: ["123", "123", "123", "123" ]

1
"012345012345012345"
Returns: ["012345012345012345" ]

6
"01234"
Returns: ["", "", "", "", "", "" ]

2
""
Returns: ["", "" ]
```

#### 自分的解答

スマンヌ(´･ω･`)
（thanks id:mincemakerさん）

```# *-# -*- coding: utf-8 -*-

def deal(member, data):
result = []
for i in range(member):
#result.append(data[i::member])                        　        修正前(´･ω･`)
result.append(data[i:len(data) - (len(data) % member):member])  #修正後
return result if not "" in result else len(result) * [""]

print deal(3, "123123123")
print deal(4, "123123123")
print deal(6, "012345012345012345")
print deal(4, "111122223333")
print deal(1, "012345012345012345")
print deal(6, "01234")
print deal(2, "")
```

```['111', '222', '333']
['12', '23', '31', '12']
['000', '111', '222', '333', '444', '555']
['123', '123', '123', '123']
['012345012345012345']
['', '', '', '', '', '']
['', '']```

#### 反省

:mincemakerさんのコードをパクって、doctestしてみた。

```#!/usr/bin/env python
# *-# -*- coding: utf-8 -*-

class Cards:
def deal(self, member, data):
"""
>>> a = Cards()
>>> a.deal(3, "123123123")
['111', '222', '333']
>>> a.deal(4, "123123123")
['12', '23', '31', '12']
>>> a.deal(6, "012345012345012345")
['000', '111', '222', '333', '444', '555']
>>> a.deal(4, '111122223333')
['123', '123', '123', '123']
>>> a.deal(1, '012345012345012345')
['012345012345012345']
>>> a.deal(6, '01234')
['', '', '', '', '', '']
>>> a.deal(2, '')
['', '']
"""

result = []
for i in range(member):
result.append(data[i:len(data) - (len(data) % member):member])
return result if not "" in result else len(result) * [""]

if __name__=='__main__':
import doctest
doctest.testmod()
```

#### また間違い見つけたので反省(´･ω･`)

っていうか必要ない行が混じってた(||ﾟДﾟ)

なんでいれたんだろ？

```#!/usr/bin/env python
# *-# -*- coding: utf-8 -*-

def deal(member, data):
"""
>>> deck(3, "123123123")
['111', '222', '333']
>>> deck(4, "123123123")
['12', '23', '31', '12']
>>> deck(6, "012345012345012345")
['000', '111', '222', '333', '444', '555']
>>> deck(4, '111122223333')
['123', '123', '123', '123']
>>> deck(1, '012345012345012345')
['012345012345012345']
>>> deck(6, '01234')
['', '', '', '', '', '']
>>> deck(2, '')
['', '']
"""
result = []
for i in range(member):
result.append(data[i:len(data) - (len(data) % member):member])
return result

if __name__ == '__main__':
import doctest
doctest.testmod()
```

これをリスト内包にすればよかったのかぁ。
とりあえず動けばいい的な発想の自分は、プログラミング向いてないなーと反省(´･ω･`)