Debian Codegraph @

doc/Tutorial_2.sor

raw
1 # Soros Tutorial Two πŸ’ Fruitful Expressions
2 #
3 # We learn the basics of regular expressions,
4 # writing a program to convert even input
5 # numbers to apples, and odd numbers to pears.
6 #
7 # INTRODUCTION
8 #
9 # Regular expression (regexp or regex or RE) is
10 # a standard for pattern matching and replacement,
11 # supported by every modern programming languages.
12 #
13 # Why? Because it’s the easiest way to handle
14 # input texts.
15 #
16 # A Soros program line can be a regular
17 # expression pattern and replacement:
18 #
19 # regex_pattern regex replacement
20
21
22
23 # TASK 1
24 #
25 # The character . (dot) matches arbitrary
26 # characters in a regex pattern.
27 #
28 # Remove the # in the following program to add
29 # bananas to all 1-digit numbers (except one),
30 # and tangerines to all 2-digit numbers, and extend
31 # the third program line to convert all 3-digit
32 # numbers to pineapples (scroll the output
33 # window to the end to see 3-digit numbers)!
34
35 1 one
36 #. 🍌 (1-DIGIT)
37 #.. 🍊 (2-DIGIT)
38 🍍
39
40 # NOTE: Solutions are there in the end of this tutorial.
41
42 # TASK 2
43 #
44 # Dots can be combined with arbitrary other
45 # characters.
46 #
47 # Extend the previous program with a single
48 # program line to replace 10, 20, 30 ... 90
49 # with lemons instead of bananas!
50 # (Copy this lemon emoji: πŸ‹).
51 #
52
53
54
55 # TASK 3
56 #
57 # We can limit the previous pattern only for
58 # given characters, putting them between brackets.
59 #
60 # For example, [abcd] matches only the letter a, b, c and d.
61 #
62 # Add a *single* program line to the previous program to
63 # to replace every 2-digit *even* number with an apple, ie.
64 # compressing the following 45-line long program:
65 #
66 # 10 🍎 (EVEN)
67 # 12 🍎 (EVEN)
68 # ...
69 # 98 🍎 (EVEN)
70
71
72
73 # TASK 4
74 #
75 # The .+ (dot plus) matches 1 or more arbitrary characters.
76 #
77 # What will be the effect of the following program line, if
78 # you change the first line β€œ1 one” with it, and remove the
79 # sign of comment? Guess it, and check it!
80
81 #.+ 🍎🍎🍎
82
83
84
85 # TASK 5
86 #
87 # We can combine the different regex patterns, for example, the
88 # dot plus pattern with the bracket one.
89 #
90 # Write a program from scratch (removing the old program lines)
91 # to show apples and pears for every 2-digit or more input
92 # number, using .+ and bracket expressions!
93 #
94 # 10 🍎 (EVEN)
95 # 11 🍐 (ODD)
96 # 12 🍎 (EVEN)
97 # ...
98 # 100 🍎 (EVEN)
99 # 101 🍐 (ODD)
100 # etc.
101
102
103
104 # TASK 6
105 #
106 # The .* (dot asterisk) matches 0 or more arbitrary characters.
107 #
108 # Replace the first program line β€œ1 one” with a program line to
109 # convert all even input numbers (including the 1-letter ones)
110 # to apples, and insert a new program line to convert all
111 # odd numbers to pears.
112
113 SCROLL DOWN FOR SOLUTIONS
114 ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩
115
116
117
118
119
120
121
122
123
124
125
126 # SOLUTION – TASK 1
127 #
128 # 1 one
129 # . 🍌 (1-DIGIT)
130 # .. 🍊 (2-DIGIT)
131 # ... 🍍 (3-DIGIT)
132
133
134
135 # SOLUTION – TASK 2
136 #
137 # 1 one
138 # . 🍌 (1-DIGIT)
139 # .0 πŸ‹ (10Γ—n)
140 # .. 🍊 (2-DIGIT)
141 # ... 🍍 (3-DIGIT)
142
143
144
145 # SOLUTION – TASK 3
146 #
147 # 1 one
148 # . 🍌 (1-DIGIT)
149 # .[02468] 🍎 (EVEN 2-DIGIT)
150 # .0 πŸ‹ (10Γ—n)
151 # .. 🍊 (2-DIGIT)
152 # ... 🍍 (3-DIGIT)
153
154
155
156 # SOLUTION – TASK 4
157 #
158 # All numbers converted to "🍎🍎🍎", because .+ matches
159 # every input text, so the following program lines
160 # will never be executed, so we can remove them:
161 #
162 # .+ 🍎🍎🍎
163
164
165
166 # SOLUTION – TASK 5
167 #
168 # 2-digit or bigger numbers are converted to
169 # apples and pears by the following two lines:
170 #
171 # .+[02468] 🍎 (EVEN 2-OR-MORE-DIGIT)
172 # .+[13579] 🍐 (ODD 2-OR-MORE-DIGIT)
173 #
174 # or simply
175 #
176 # .+[02468] 🍎 (EVEN 2-OR-MORE-DIGIT)
177 # .+. 🍐 (ODD 2-OR-MORE-DIGIT)
178
179
180
181 # SOLUTION – TASK 6
182 #
183 # Change + with * in the previous program to convert
184 # every (1-digit or larger) number to apples and pears:
185 #
186 # .*[02468] 🍎 (EVEN NUMBER)
187 # .* 🍐 (ODD NUMBER)
188
189
190
191 ######################################################
192 # CONGRATULATIONS – You have finished Tutorial Two! πŸ™‚
193 ######################################################