티스토리 뷰
Little Lena's book of puzzles contains the following challenge. The goal is to connect each fruit with the correct color.
Because little Lena is a bit stubborn (or because she's too young), she takes pleasure in randomly connecting the fruits with the colors. However, she always makes sure that each fruit is connected with at most one color and that each color is connected with at most one fruit.
Assignment
Write a function
combine(colors, fruits[, number])
that takes two sequences (lists or tuples) of words (strings). The function also has a third optional parameter amount that may take an integer argument. The function must return a list of strings, whose elements are formatted as a word1 word2 with word1 a randomly chosen word from the first sequence and word2 a randomly chosen word from the second sequence. The function must make different combinations until all words from one of the given sequences have been used, taking into account that each word from a sequence can be used at most once. If a third argument is given, no more than the given amount of combinations should be included in the returned list.
Tip: Take a look at the functions choice and sample in the random module, which is included in The Python Standard Library.
Example
>>> colors = ['purple', 'yellow', 'green']
>>> fruits = ('grape', 'banana', 'apple')
>>> combine(colors, fruits)
['a purple grape', 'a green banana', 'a yellow apple']
>>> combine(colors, fruits)
['a purple grape', 'a green apple', 'a yellow banana']
>>> combine(colors, fruits)
['a purple apple', 'a yellow grape', 'a green banana']
>>> combine(colors, fruits, amount=1)
['a purple grape']
>>> combine(colors, fruits, amount=2)
['a yellow apple', 'a green banana']
>>> combine(colors, fruits, amount=4)
['a yellow banana', 'a green grape', 'a purple apple']
ver. latest
import random
def add_idx(item, items):
if item not in items:
items.append(item)
def create_randoms(size):
items = list()
while len(items) < size:
add_idx(random.randrange(size), items)
return items
def get_size(colors, fruits, amount):
size_min = len(colors) if len(colors) <= len(fruits) else len(fruits)
if amount == 0:
return size_min
return amount if amount < size_min else size_min
def generate(idxes_random, size, colors, fruits):
result = list()
for i in range(size):
idx_random = idxes_random[i]
result.append("a {} {}".format(colors[idx_random], fruits[idx_random]))
return result
def combine(colors, fruits, amount=0):
size = get_size(colors, fruits, amount)
idxes_random = create_randoms(size)
return generate(idxes_random, size, colors, fruits)
colors = ['purple', 'yellow', 'green']
fruits = ('grape', 'banana', 'apple')
print(combine(colors, fruits, amount=10))
import random
def add_idx(item, items):
if item not in items:
items.append(item)
def add_idxes(items, size):
while len(items) < size:
add_idx(random.randrange(size), items)
def get_size(colors, fruits, amount):
size_min = len(colors) if len(colors) <= len(fruits) else len(fruits)
if amount == 0:
return size_min
return amount if amount < size_min else size_min
def create(idxes_color, idxes_fruit, size, colors, fruits):
result = list()
for i in range(size):
idx_color = idxes_color[i]
idx_fruit = idxes_fruit[i]
result.append("a {} {}".format(colors[idx_color], fruits[idx_fruit]))
return result
def combine(colors, fruits, amount=0):
size = get_size(colors, fruits, amount)
idxes_color = list()
idxes_fruit = list()
add_idxes(idxes_color, size)
add_idxes(idxes_fruit, size)
return create(idxes_color, idxes_fruit, size, colors, fruits)
colors = ['purple', 'yellow', 'green']
fruits = ('grape', 'banana', 'apple')
print(combine(colors, fruits, amount=10))
'python lecture > algorism' 카테고리의 다른 글
[edu] Applied chemistry (0) | 2018.11.02 |
---|---|
[edu] Friday the 13th (0) | 2018.10.24 |
[eud] bubble sorting (0) | 2018.10.15 |
[edu] Zigzag (0) | 2018.10.15 |
[edu] Kaprekar series (0) | 2018.10.14 |
- Total
- Today
- Yesterday
- 파이썬
- Tistory
- 플러스친구 자동응답
- virtualenv
- admin.py
- wsgi
- 파이썬 프로그래밍
- gitlab
- 파이썬 강좌
- 문과 코딩
- GIT
- chatbot
- 면접정답
- 문서 비교
- 파이썬 독학
- 모바일 테마 적용
- 장고 카톡 자동응답
- 장고
- 모바일 스킨 적용
- 이미지 비교
- django
- gitignore
- django chatbot
- 장고 플러스친구 자동응답
- PuTTYGen
- pycrypto
- 엑셀 비교
- Python
- 파이썬 입문
- 면접답변
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |