티스토리 뷰

python lecture/algorism

[edu] Colorful fruits

burningrizen 2018. 10. 24. 20:08

Little Lena's book of puzzles contains the following challenge. The goal is to connect each fruit with the correct color.

kleurrijk fruit

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 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
댓글