-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactorial_question_answer_sequence.puml
42 lines (38 loc) · 1.56 KB
/
factorial_question_answer_sequence.puml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@startuml
/' computes the factorial of the given integer. '/
!function $factorial($n)
/' Return value of this function is memoized because it uses recursion.
PlantUML preprocessor doesn't provide dictionary or array data structure,
hence a "hack" simulates a dictionary. For every input integer, a variable
is created with the stringized integer as its name, and the return value is
assigned to it. Thus, the variable's name serves as the "key". '/
!if %variable_exists(%string($n))
!return %get_variable_value(%string($n))
!endif
!if $n == 0
!$value = 1
!else
!$value = $n * $factorial($n - 1)
!endif
%set_variable_value(%string($n), $value)
!return $value
!endfunction
!procedure $factorial_question_answer_sequence(\
$starting_number = 0,\
$ending_number = 12,\
$color_number_in_question = blue,\
$color_number_in_answer = green)
skinparam SequenceMessageAlignment direction
!$number = $starting_number
!while $number <= $ending_number
Question -> Answer : What is factorial of <color:$color_number_in_question>**$number**</color>?
Question <- Answer : Factorial of <color:$color_number_in_question>**$number**</color> is <color:$color_number_in_answer>**$factorial($number)**</color>.
|||
!$number = $number + 1
!endwhile
!endprocedure
scale 1.5 /' zooms the generated diagram per specified scaling factor '/
$factorial_question_answer_sequence(\
$color_number_in_answer = darkviolet, $color_number_in_question = red\
)
@enduml