[Inside] Full Practice [assumption+Problem Solving+System Thinking]

问题1:已知一个字符串, 只含常见可打印ascii字符以及空格和换行, 要求进行如下过滤:
1, 过滤掉前导空白和后导空白;
2, 中间的连续空白字符, 只保留一个;
3, 删除换行前后的空白字符;

Assumption Questions:

1. 换行算空白吗

2. 是否支持Tab等其他空白字符,如果包含是否属于空白;

3. 是否可以修改原来字符串并作为结果返回;

4. 换行符是包含\r, \n和\r\n三种吗?

Problem Solving:

为了保证我们的处理考虑了各种情况,可以先Design一些Normal Cases:

//no change needed cases;

“assumption+problem”

“assumption+problem solving\n”

//spaces are pre and post postion

“  hello man   ”

“  hello man”

“hello man   “

//several spaces are in the middle + pre, post position

“hello    man"

“   hello    man   ”

// spaces around \n

“hello man   \n”

“hello man\n    ”

“hello man \n ”

“hello   man \n ”

“ hello man \n ”

“ hello \n    man ”

“ hello\nman"

“ \n hello \n man \n”

//special cases

“   ”

“ \n ”

“ ”

作为开始分析问题的一部分,可以不用设计的很完全,能够帮助分析问题就OK,不过要说一下,这不是完全的case list。需要注意不过上面的注释表明了设计case时要有结构化的思考方式,应该展现出来。

分析的结论是,因为需要过滤的空白字符可以很容易地发现其pattern(pre就是之前没有过非空格字符,post就是从某个点开始都是空白字符,中间的连续字符是之前出现过非空白字符, etc.),所以可以go through字符串,发现合适的pattern就执行相应的操作。

下一步是用一些test case来看看是否可以正常工作,这时我们也应该告诉面试官,我们go through的过程,如果发现错误,就修改一下,然后再选择一个做一下,2-3个就不错了。

问题2:

原文地址:https://www.cnblogs.com/whyandinside/p/2833784.html