File: Tutorial_2.sor

package info (click to toggle)
libnumbertext 1.0.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,380 kB
  • sloc: python: 439; cpp: 395; java: 244; javascript: 108; makefile: 101; xml: 84; sh: 40
file content (193 lines) | stat: -rw-r--r-- 3,917 bytes parent folder | download | duplicates (4)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Soros Tutorial Two πŸ’ Fruitful Expressions
#
# We learn the basics of regular expressions,
# writing a program to convert even input
# numbers to apples, and odd numbers to pears.
#
# INTRODUCTION
#
# Regular expression (regexp or regex or RE) is
# a standard for pattern matching and replacement,
# supported by every modern programming languages.
#
# Why? Because it’s the easiest way to handle
# input texts.
#
# A Soros program line can be a regular
# expression pattern and replacement:
#
# regex_pattern regex replacement



# TASK 1
#
# The character . (dot) matches arbitrary
# characters in a regex pattern.
#
# Remove the # in the following program to add
# bananas to all 1-digit numbers (except one),
# and tangerines to all 2-digit numbers, and extend
# the third program line to convert all 3-digit
# numbers to pineapples (scroll the output
# window to the end to see 3-digit numbers)!

1 one
#. 🍌	(1-DIGIT)
#.. 🍊	(2-DIGIT)
🍍

# NOTE: Solutions are there in the end of this tutorial.

# TASK 2
#
# Dots can be combined with arbitrary other
# characters.
#
# Extend the previous program with a single
# program line to replace 10, 20, 30 ... 90
# with lemons instead of bananas!
# (Copy this lemon emoji: πŸ‹).
#



# TASK 3
#
# We can limit the previous pattern only for
# given characters, putting them between brackets.
#
# For example, [abcd] matches only the letter a, b, c and d.
#
# Add a *single* program line to the previous program to
# to replace every 2-digit *even* number with an apple, ie.
# compressing the following 45-line long program:
#
# 10 🍎	(EVEN)
# 12 🍎	(EVEN)
# ...
# 98 🍎	(EVEN)



# TASK 4
#
# The .+ (dot plus) matches 1 or more arbitrary characters.
#
# What will be the effect of the following program line, if
# you change the first line β€œ1 one” with it, and remove the
# sign of comment? Guess it, and check it!

#.+ 🍎🍎🍎



# TASK 5
#
# We can combine the different regex patterns, for example, the
# dot plus pattern with the bracket one.
#
# Write a program from scratch (removing the old program lines)
# to show apples and pears for every 2-digit or more input
# number, using .+ and bracket expressions!
#
# 10 🍎	(EVEN)
# 11 🍐	(ODD)
# 12 🍎	(EVEN)
# ...
# 100 🍎	(EVEN)
# 101 🍐	(ODD)
# etc.



# TASK 6
#
# The .* (dot asterisk) matches 0 or more arbitrary characters.
#
# Replace the first program line β€œ1 one” with a program line to
# convert all even input numbers (including the 1-letter ones)
# to apples, and insert a new program line to convert all
# odd numbers to pears.

SCROLL DOWN FOR SOLUTIONS
⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩











# SOLUTION – TASK 1
#
# 1 one
# . 🍌	(1-DIGIT)
# .. 🍊	(2-DIGIT)
# ... 🍍	(3-DIGIT)



# SOLUTION – TASK 2
#
# 1 one
# . 🍌	(1-DIGIT)
# .0 πŸ‹	(10Γ—n)
# .. 🍊	(2-DIGIT)
# ... 🍍	(3-DIGIT)



# SOLUTION – TASK 3
#
# 1 one
# . 🍌		(1-DIGIT)
# .[02468] 🍎	(EVEN 2-DIGIT)
# .0 πŸ‹		(10Γ—n)
# .. 🍊		(2-DIGIT)
# ... 🍍		(3-DIGIT)



# SOLUTION – TASK 4
#
# All numbers converted to "🍎🍎🍎", because .+ matches
# every input text, so the following program lines
# will never be executed, so we can remove them:
#
# .+ 🍎🍎🍎



# SOLUTION – TASK 5
#
# 2-digit or bigger numbers are converted to
# apples and pears by the following two lines:
#
# .+[02468] 🍎	(EVEN 2-OR-MORE-DIGIT)
# .+[13579] 🍐	(ODD 2-OR-MORE-DIGIT)
#
# or simply
#
# .+[02468] 🍎	(EVEN 2-OR-MORE-DIGIT)
# .+. 🍐		(ODD 2-OR-MORE-DIGIT)



# SOLUTION – TASK 6
#
# Change + with * in the previous program to convert
# every (1-digit or larger) number to apples and pears:
#
# .*[02468] 🍎	(EVEN NUMBER)
# .* 🍐		(ODD NUMBER)



######################################################
# CONGRATULATIONS – You have finished Tutorial Two! πŸ™‚
######################################################