Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #597 from iKostanOrg/master #598

Merged
merged 26 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/kyu_7/kyu_7.password_validator.module.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kyu\_7.password_validator.module package
========================================
kyu\_7.password\_validator.module package
=========================================

Subpackages
-----------
Expand Down
11 changes: 11 additions & 0 deletions docs/kyu_7/kyu_7.pointless_farmer.module.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
kyu\_7.pointless\_farmer.module package
=======================================

Subpackages
-----------

.. toctree::
:maxdepth: 4

kyu_7.pointless_farmer.readme
kyu_7.pointless_farmer
5 changes: 5 additions & 0 deletions docs/kyu_7/kyu_7.pointless_farmer.readme.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
README
======

.. include:: ../../kyu_7/pointless_farmer/README.md
:parser: myst_parser.sphinx_
32 changes: 32 additions & 0 deletions docs/kyu_7/kyu_7.pointless_farmer.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
kyu\_7.pointless\_farmer package
================================

Submodules
----------

kyu\_7.pointless\_farmer.solution module
----------------------------------------

.. automodule:: kyu_7.pointless_farmer.solution
:members:
:undoc-members:
:show-inheritance:
:private-members:

kyu\_7.pointless\_farmer.test\_buy\_or\_sell module
---------------------------------------------------

.. automodule:: kyu_7.pointless_farmer.test_buy_or_sell
:members:
:undoc-members:
:show-inheritance:
:private-members:

Module contents
---------------

.. automodule:: kyu_7.pointless_farmer
:members:
:undoc-members:
:show-inheritance:
:private-members:
1 change: 1 addition & 0 deletions docs/kyu_7/kyu_7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Subpackages
kyu_7.make_class.module
kyu_7.maximum_multiple.module
kyu_7.password_validator.module
kyu_7.pointless_farmer.module
kyu_7.powers_of_3.module
kyu_7.pull_your_words_together_man.module
kyu_7.remove_the_minimum.module
Expand Down
74 changes: 37 additions & 37 deletions kyu_7/README.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions kyu_7/pointless_farmer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Pointless Farmer

You've harvested a fruit. But the Internal Raisin Service (IRS) doesn't
allow you to eat your own produce, you have to launder it on the market first.
When you visit the market, you are given three conversion offers, and for
each conversion offer you must decide which direction to trade. A conversion
offer is a pair of fruits, and to buy the second fruit for the first fruit,
the action is "buy". The opposite direction is "sell".

Given the offer `("apple", "orange")`, if you have an `apple`, then you may `buy
an orange`, or, if you have an `orange`, you may `sell` it for the apple.

## Example

```bash
pairs: [("apple", "orange"), ("orange", "pear"), ("apple", "pear")]
harvested fruit: "apple"

currently holding: apple
("apple", "orange")
buy
currently holding: orange
("orange", "pear")
buy
currently holding: pear
("apple", "pear")
sell
currently holding: apple (successfully ended up with the same fruit again)
```

As input you receive three conversion offers together with your harvested fruit,
and your output is a list of three actions of buy/sell, for the above example the
output is: `["buy", "buy", "sell"]`.

If it is not possible to end up with the original kind of fruit again after the
three conversions, return `"ERROR"` instead of the list of actions.

[Source](https://www.codewars.com/kata/597ab747d1ba5b843f0000ca)
1 change: 1 addition & 0 deletions kyu_7/pointless_farmer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Pointless Farmer."""
35 changes: 35 additions & 0 deletions kyu_7/pointless_farmer/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Solution for -> Pointless Farmer.

Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""

from typing import Any


def buy_or_sell(pairs: list, harvested_fruit: str) -> list[Any] | str:
ikostan marked this conversation as resolved.
Show resolved Hide resolved
ikostan marked this conversation as resolved.
Show resolved Hide resolved
"""
Decide which direction to trade.

:param pairs: list
:param harvested_fruit: str
:return: list
"""
currently_holding: str = harvested_fruit
results: list = []

for pair in pairs:
# First item in pairs is for selling, second is for buying.
if pair[0] == currently_holding == pair[-1] or pair[-1] == currently_holding:

Check notice on line 24 in kyu_7/pointless_farmer/solution.py

View check run for this annotation

codefactor.io / CodeFactor

kyu_7/pointless_farmer/solution.py#L24

Unnecessary "elif" after "continue", remove the leading "el" from "elif" (no-else-continue)

Check notice on line 24 in kyu_7/pointless_farmer/solution.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

kyu_7/pointless_farmer/solution.py#L24

Unnecessary "elif" after "continue", remove the leading "el" from "elif"
results.append('sell')
currently_holding = pair[0]
continue
elif pair[0] == currently_holding:
results.append('buy')
currently_holding = pair[-1]
continue

return "ERROR"

return results if currently_holding == harvested_fruit else "ERROR"
127 changes: 127 additions & 0 deletions kyu_7/pointless_farmer/test_buy_or_sell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
Test for -> Pointless Farmer.

Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""

# ALGORITHMS

import unittest
import allure
from parameterized import parameterized
from utils.log_func import print_log
from kyu_7.pointless_farmer.solution import buy_or_sell


# pylint: disable-msg=R0801
@allure.epic('7 kyu')
@allure.parent_suite('Beginner')
@allure.suite("Algorithms")
@allure.sub_suite("Unit Tests")
@allure.feature("Lista")
@allure.story('Password validator')
@allure.tag('FUNDAMENTALS',
'ALGORITHMS')
@allure.link(
url='https://www.codewars.com/kata/597ab747d1ba5b843f0000ca',
name='Source/Kata')
# pylint: enable-msg=R0801
class PointlessFarmerTestCase(unittest.TestCase):
"""Test 'buy_or_sell' function."""

@parameterized.expand([
([("apple", "orange"), ("orange", "pear"), ("apple", "pear")],
"apple", ["buy", "buy", "sell"]),
([("orange", "apple"), ("orange", "pear"), ("pear", "apple")],
"apple", ["sell", "buy", "buy"]),
([("apple", "orange"), ("pear", "orange"), ("apple", "pear")],
"apple", ["buy", "sell", "sell"]),
([("orange", "apple"), ("pear", "orange"), ("pear", "apple")],
"apple", ["sell", "sell", "buy"]),
([("orange", "apple"), ("orange", "pear"), ("apple", "pear")],
"apple", ["sell", "buy", "sell"]),
([("apple", "orange"), ("pear", "orange"), ("pear", "apple")],
"apple", ["buy", "sell", "buy"]),
([("apple", "orange"), ("orange", "pear"), ("pear", "apple")],
"apple", ["buy", "buy", "buy"]),
([("orange", "apple"), ("pear", "orange"), ("apple", "pear")],
"apple", ["sell", "sell", "sell"]),
([('Raspberry', 'Raspberry'), ('Jabuticaba', 'Raspberry'),
('Jabuticaba', 'Raspberry')], 'Raspberry', ['sell', 'sell', 'buy'])])
def test_buy_or_sell_positive(self, market, harvested_fruit, expected):
"""
Testing 'buy_or_sell' function, positive test case..

:return:
"""
# pylint: disable-msg=R0801
allure.dynamic.title("Testing 'buy_or_sell' function -> positive.")
allure.dynamic.severity(allure.severity_level.NORMAL)
allure.dynamic.description_html(
'<h3>Codewars badge:</h3>'
'<img src="'
'https://www.codewars.com/users/myFirstCode/badges/large'
'">'
'<h3>Test Description:</h3>'
"<p>"
"When you visit the market, you are given three conversion "
"offers, and for each conversion offer you must decide which "
"direction to trade. A conversion offer is a pair of fruits, "
"and to buy the second fruit for the first fruit, the action is 'buy'."
"<br>"
"The opposite direction is 'sell'."
"</p>")
# pylint: enable-msg=R0801
with allure.step(f"Enter test data: {market} "
f"and verify the expected result: {expected}."):
result: list = buy_or_sell(market, harvested_fruit)
print_log(market=market,
harvested_fruit=harvested_fruit,
expected=expected,
result=result)
self.assertEqual(expected, result)

@parameterized.expand([
([("orange", "apple"), ("pear", "orange"), ("apple", "paer")],
"apple", "ERROR"),
([('Jackfruit', 'Physalis'), ('Physalis', 'Prune'),
('Prune', 'Tamarind')],
'Tamarind', 'ERROR'),
([('Mulberry', 'Strawberry'), ('Passionfruit', 'Mulberry'),
('Strawberry', 'Mulberry')],
'Strawberry', 'ERROR'),
([('Cherry', 'Cucumber'), ('Cherry', 'Cherry'), ('Cucumber', 'Ugli fruit')],
'Boysenberry', 'ERROR'),
([('Jackfruit', 'Purple mangosteen'), ('Purple mangosteen', 'Jackfruit'),
('Purple mangosteen', 'Jackfruit')],
'Purple mangosteen', 'ERROR')])
def test_buy_or_sell_negative(self, market, harvested_fruit, expected):
"""
Testing 'buy_or_sell' function, negative test case.

:return:
"""
# pylint: disable-msg=R0801
allure.dynamic.title("Testing 'buy_or_sell' function -> negative.")
allure.dynamic.severity(allure.severity_level.NORMAL)
allure.dynamic.description_html(
'<h3>Codewars badge:</h3>'
'<img src="'
'https://www.codewars.com/users/myFirstCode/badges/large'
'">'
'<h3>Test Description:</h3>'
"<p>"
"If it is not possible to end up with the original kind "
"of fruit again after the three conversions, return \"ERROR\" "
"instead of the list of actions."
"</p>")
# pylint: enable-msg=R0801
with allure.step(f"Enter test data: {market} "
f"and verify the expected result: {expected}."):
result: list = buy_or_sell(market, harvested_fruit)
print_log(market=market,
harvested_fruit=harvested_fruit,
expected=expected,
result=result)
self.assertEqual(expected, result)
Loading