Stepwise Refinement

What is stepwise refinement?

The verb refine means to improve a method, plan, system etc., gradually making slight changes to it. The word stepwise means to do something in a series of distinct stages. So, stepwise refinement is a process of breaking down the steps of an outline solution into smaller steps and repeat the process for each smaller step until they are all detailed enough.

Using stepwise refinement, we improve our solution to a problem gradually by breaking down each step into smaller ones and adding more details, and we continue the breaking down until all the smaller step are detailed enough. It is also known as top-down design. The outline solution is the top-level solution and the much smaller steps are the low-level solution with details. The outline solution contains less or no details and it is unimplementable. While the much smaller steps are full of details, it can be implemented easily. So, this approach starts from the outline solution, the top-level, solution to detailed steps, the low-level. The low-level steps are so detailed that you can write program code from it directly.

Example

The problem to be solved: Take as input a chosen symbol and a number. Output a triangular flag which is made up of the symbol, with the number of lines matching the number input.

For example, the input symbol and number 5 result in the following output:

We can write an outline solution in pseudocode from the above example output easily:

INPUT Symbol, Number
Line <- 1
WHILE Line <= Number
   Output a line
   Line <- Line + 1
ENDWHILE

 

The steps are numbered so that they can be referred to later easier.

 

Line 01,02,03,05,06 are easy. We can translate them into python code without any effort. But line 04 is a bit more complicated. Each line of the flag contains different numbers of asterisks. Besides, a new line shall start after those asterisks.

 

We can see that the number of asterisks of each line is exactly the number of the line. So, we can write more pseudocode for the line 04:

Count <- 1
WHILE Count <= Line
   output Symbol
   Count <- Count + 1
ENDWHILE
Output NewLine

 

The above pseudocode gives more details about how to output a line. It can be translated into python code easily. Now, we can replace line 04 with the above segment:

INPUT Symbol, Number
Line <- 1
WHILE Line <= Number
    Count <- 1
    WHILE Count <= Line
       output Symbol
       Count <- Count + 1
    ENDWHILE
    Output NewLine
   
    Line <- Line + 1
ENDWHILE

 

and write the python code:

Symbol = input("Enter a Symbol:")
Number = int(input("Enter a number:"))
Line = 1
while Line <= Number:
    Count = 1
    while Count<= Line:
       print(Symbol,end='')
       Count = Count + 1
    print()

    Line = Line +1

Summary

Let's summarize the process:

  1. We write an outline solution to the problem
  2. We split the step output a line into 6 smaller steps
  3. Until now, the steps of the solution are all detailed enough
  4. We write python code directly from the pseudocode

 

Once again, stepwise refinement is a process of breaking down an outline solution into smaller steps, and repeat the process until all the smaller steps are detailed enough.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值