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! π
######################################################
|