|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
## Background information
This question is inspired by `Answer Smash` from the BBC's game show `Richard Osman's House of Games`. Basically, participants have to "smash" the name of the picture and an answer to a question together to make a "word". For example, if the picture is `orlando bloom` (an English actor) and the answer to the question is `bloomingdale's` (a US department store):
<center><img src="figs/answer_smash.png" width="400"/></center> <p></p>
then the correct answer is `orlando bloomingdale's` with the part `bloom` appearing at the back of `orlando bloom` (first answer) and at the front of `bloomingdale's` (second answer).
---
## Instructions
1. In [src/answer_smash.py](src/answer_smash.py), write the function definition for `answer_smash()`, which takes 2 `str` as arguments and returns the smashed word (`str`) if two given words can be smashed together. If not, the function returns `None`.
**Note**
* About "argument" and "returning" values:
* You should not use `input()` or `print()` in your answer
* Remember:
* "Arguments" are the values that are passed to a function. Do not confuse "arguments" with the `input()` function, which is a function used to request users of a program to type in a value on a console
* "Return value" is the value to return when the function call is evaluated. Do not confuse "return value" with printout generated by `print()`
* If you are not sure how to write a function definition that takes "arguments" and "return" some value, please have a look at, for example, Exercise 3.2 of the exercises for self-study (at the back of the lecture notes)
* Remember to include an _abstract_ function docstring inside the function definition
* Some more specifications related to the question:
* The two words provided as arguments, and the smashed "words" do not have to be proper English words
* Two words can be smashed together only if there is any overlapping between the back of the first word and the beginning of the second word
* Always smash the words with the largest overlapping
2. Ensure your function works as intended by calling the functions with different arguments (like the ones in the `Examples` section). Please note down at least 5 function calls that you have tried, their expected return values, and the actual return values. You should consider different scenarios, for example, the two words cannot smash, the two words are the same, the words have some overlapping, etc
* Function calls that you have tried: _________________
* Expected return values: _________________
* Actual return values: _________________
**Note**
* When submitting the `answer_smash.py`, it should only contain the function definition of `answer_smash()`
* Please remove any function calls you tried from the `answer_smash.py` file before you submit your work
---
## Function argument assumptions
For simplicity, we assume:
* When calling the function, the given arguments are always string in lowercase, but they can have some non-letter characters
* For the overlapping part, it has to be matched exactly (including the whitespace, punctuation marks, numbers, etc.)
* Assume the given words do not start or end with whitespaces like spaces or newline characters
---
## Examples
Example function calls and the corresponding return values:
```
>>> answer_smash('orlando bloom', "bloomingdale's")
"orlando bloomingdale's"
>>> answer_smash('data', 'science')
>>> answer_smash('guacamole', 'leapfrog')
'guacamoleapfrog'
```
Below shows you some more examples of how words should be smashed together, and the corresponding return values:
| first argument | second argument |overlapping part| Return value |
|-----------------|------------------|----------------|--------------------|
| `'programming'` | `'ram'` | | `None` |
| `'data'` | `'science'` | | `None` |
| `'guacamole'` | `'leapfrog'` | `'le'` | `'guacamoleapfrog'`|
|`'orlando bloom'`|`"bloomingdale's"`| `'bloom'` |`"orlando bloomingdale's"` |
Some (even) more examples:
| first argument | second argument |overlapping part| Return value |
|----------------|-----------------|----------------|--------------|
| `'abcdef'` | `'efg'` | `'ef'` | `'abcdefg'` |
| `'abc!ef'` | `'!efg'` | `'!ef'` | `'abc!efg'` |
| `'abcd'` | `'abcd'` | `'abcd'` | `'abcd'` |
| `'abcab'` | `'bc'` | `'b'` | `'abcabc'` |
| `'cababa'` | `'babadd'` | `'baba'` | `'cababadd'` |
| `'abcab'` | `'ca'` | | `None` |
| `'abcd ef'` | `'def'` | | `None` |
| `"abcd'ef"` | `'d!ef'` | | `None` |
If there is nothing under the `overlapping part`, it means the corresponding two words cannot be smashed together. Notice "overlapping part" only includes the overlapping between the back of the first word and the beginning of the second word |
|