mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 01:18:02 +00:00
Add sentence screen fit challenge
This commit is contained in:
@@ -0,0 +1,239 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Challenge Notebook"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Find how many times a sentence can fit on a screen.\n",
|
||||
"\n",
|
||||
"See the [LeetCode](https://leetcode.com/problems/sentence-screen-fitting/) problem page.\n",
|
||||
"\n",
|
||||
"<pre>\n",
|
||||
"Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.\n",
|
||||
"\n",
|
||||
"Note:\n",
|
||||
"\n",
|
||||
"A word cannot be split into two lines.\n",
|
||||
"The order of words in the sentence must remain unchanged.\n",
|
||||
"Two consecutive words in a line must be separated by a single space.\n",
|
||||
"Total words in the sentence won't exceed 100.\n",
|
||||
"Length of each word is greater than 0 and won't exceed 10.\n",
|
||||
"1 ≤ rows, cols ≤ 20,000.\n",
|
||||
"Example 1:\n",
|
||||
"\n",
|
||||
"Input:\n",
|
||||
"rows = 2, cols = 8, sentence = [\"hello\", \"world\"]\n",
|
||||
"\n",
|
||||
"Output: \n",
|
||||
"1\n",
|
||||
"\n",
|
||||
"Explanation:\n",
|
||||
"hello---\n",
|
||||
"world---\n",
|
||||
"\n",
|
||||
"The character '-' signifies an empty space on the screen.\n",
|
||||
"Example 2:\n",
|
||||
"\n",
|
||||
"Input:\n",
|
||||
"rows = 3, cols = 6, sentence = [\"a\", \"bcd\", \"e\"]\n",
|
||||
"\n",
|
||||
"Output: \n",
|
||||
"2\n",
|
||||
"\n",
|
||||
"Explanation:\n",
|
||||
"a-bcd- \n",
|
||||
"e-a---\n",
|
||||
"bcd-e-\n",
|
||||
"\n",
|
||||
"The character '-' signifies an empty space on the screen.\n",
|
||||
"Example 3:\n",
|
||||
"\n",
|
||||
"Input:\n",
|
||||
"rows = 4, cols = 5, sentence = [\"I\", \"had\", \"apple\", \"pie\"]\n",
|
||||
"\n",
|
||||
"Output: \n",
|
||||
"1\n",
|
||||
"\n",
|
||||
"Explanation:\n",
|
||||
"I-had\n",
|
||||
"apple\n",
|
||||
"pie-I\n",
|
||||
"had--\n",
|
||||
"\n",
|
||||
"The character '-' signifies an empty space on the screen.\n",
|
||||
"</pre>\n",
|
||||
"\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Unit Test](#Unit-Test)\n",
|
||||
"* [Solution Notebook](#Solution-Notebook)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Can we assume sentence is ASCII?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume the inputs are valid?\n",
|
||||
" * No\n",
|
||||
"* Is the output an integer?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume this fits memory?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> TypeError\n",
|
||||
"* rows < 0 or cols < 0 -> ValueError\n",
|
||||
"* cols = 0 -> 0\n",
|
||||
"* sentence = '' -> 0\n",
|
||||
"* rows = 2, cols = 8, sentence = [\"hello\", \"world\"] -> 1\n",
|
||||
"* rows = 3, cols = 6, sentence = [\"a\", \"bcd\", \"e\"] -> 2\n",
|
||||
"* rows = 4, cols = 5, sentence = [\"I\", \"had\", \"apple\", \"pie\"] -> 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Solution(object):\n",
|
||||
"\n",
|
||||
" def count_sentence_fit(self, sentence, rows, cols):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_count_sentence_fit.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestSolution(object):\n",
|
||||
"\n",
|
||||
" def test_count_sentence_fit(self):\n",
|
||||
" solution = Solution()\n",
|
||||
" assert_raises(TypeError, solution.count_sentence_fit, \n",
|
||||
" None, None, None)\n",
|
||||
" assert_raises(ValueError, solution.count_sentence_fit, \n",
|
||||
" 'abc', rows=-1, cols=-1)\n",
|
||||
" sentence = [\"hello\", \"world\"]\n",
|
||||
" expected = 1\n",
|
||||
" assert_equal(solution.count_sentence_fit(sentence, rows=2, cols=8),\n",
|
||||
" expected)\n",
|
||||
" sentence = [\"a\", \"bcd\", \"e\"]\n",
|
||||
" expected = 2\n",
|
||||
" assert_equal(solution.count_sentence_fit(sentence, rows=3, cols=6),\n",
|
||||
" expected)\n",
|
||||
" sentence = [\"I\", \"had\", \"apple\", \"pie\"]\n",
|
||||
" expected = 1\n",
|
||||
" assert_equal(solution.count_sentence_fit(sentence, rows=4, cols=5),\n",
|
||||
" expected)\n",
|
||||
" print('Success: test_count_sentence_fit')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestSolution()\n",
|
||||
" test.test_count_sentence_fit()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Solution Notebook\n",
|
||||
"\n",
|
||||
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
Reference in New Issue
Block a user