Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Last active January 26, 2021 00:56
Show Gist options
  • Save ZechCodes/8a5e8e12b140c4dcc2cfc9015be19874 to your computer and use it in GitHub Desktop.
Save ZechCodes/8a5e8e12b140c4dcc2cfc9015be19874 to your computer and use it in GitHub Desktop.
Challenge 170 - Fruit Salad πŸ‡πŸ“πŸŽ

Challenge 170 - Fruit Salad πŸ‡πŸ“πŸŽ

Fruit salads are served best when the fruits are sliced and diced into small chunks!

For this challenge, slice each fruit in half and sort the chunks alphabetically. This recipe tastes best when the chunks are joined together to make a string.

Example

fruit_salad(['apple', 'pear', 'grapes']) ➞ 'apargrapepesple'

# Chunks: ['ap', 'ple', 'pe', 'ar', 'gra', 'pes']
# Sorted chunks: ['ap', 'ar', 'gra', 'pe', 'pes', 'ple']
# Final string: 'apargrapepesple'

fruit_salad(['apple', 'pear', 'grapes']) ➞ 'apargrapepesple'

fruit_salad(['raspberries', 'mango']) ➞ 'erriesmangoraspb'

fruit_salad(['banana']) ➞ 'anaban'

Notes

  • If a fruit has an odd number of letters, make the right side larger than the left. For example: 'apple' will be sliced into 'ap' and 'ple'.
  • All fruits will be given in lowercase.
from __future__ import annotations
import unittest
def fruit_salad(fruits: list[str]) -> str:
return "" # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEquals(fruit_salad(['apple', 'pear', 'grapes']), 'apargrapepesple')
def test_2(self):
self.assertEquals(fruit_salad(['banana', 'kiwi', 'strawberry', 'blueberries']), 'anabanberryblueberrieskistrawwi')
def test_3(self):
self.assertEquals(fruit_salad(['raspberries', 'mango']), 'erriesmangoraspb')
def test_4(self):
self.assertEquals(fruit_salad(['banana']), 'anaban')
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment