Skip to content

Instantly share code, notes, and snippets.

@daroczig
Last active Nov 10, 2019
Embed
What would you like to do?
Math exercises for a 8 yrs old in Hungarian language
## TODO R package
## TODO internation via yaml that also provides the list of languages for the UI
## TODO cli --ui hu --control en --number 1:20 ...
library(logger)
library(crayon)
library(reticulate)
num2words <- import('num2words')
## rich-iannone/UnidecodeR
library(UnidecodeR)
logpath <- '/tmp'
logpath <- getwd()
logfile <- file.path(logpath, paste0(as.integer(Sys.time()), '.log'))
log_appender(appender_file(logfile))
log_threshold(INFO)
n <- 10
cat('Hány feladatot szeretnél megoldani? ')
n <- suppressWarnings(as.numeric(readLines("stdin", n = 1)))
if (is.na(n)) {
stop('HIBA!! Számot adj meg.')
}
log_info('Starting {n} exercises')
## TODO praise pkg
SUCCESS <- c(
'Ügyes vagy!',
"Szép munka!",
"Ez az, csak így tovább!",
"Juhúúú, ez is sikerült!",
"Nagyon megy ez!"
)
FAIL <- c(
"Ajaj, ez sajnos most nem sikerült.",
"Húha, sajnos nem jó választ adtál!",
"Hm, legközelebb jobban gondold át!"
)
number <- function(n = 1,
numbers = 0:20,
probs = c(1, 1:9, 2, 5:1, rep(1/2, 5))) {
sample(numbers, n, replace = TRUE, prob = probs)
}
lhs <- function(exercise) {
paste(exercise, '= ')
}
ask <- function(text) {
## readline
cat(text)
readLines("stdin", n = 1)
}
german <- function(number) {
if (missing(number) || is.na(number) || number == '') {
return(NA)
}
## sub(' Komma null', '', system(paste('num2words -l de', number), intern = TRUE))
num2words$num2words(number, lang = 'de')
}
scores <- 0
for (i in seq_len(n)) {
type <- sample(c('+', '-', '*', '/'), 1, prob = c(1, 2, 4, 1))
log_debug('Exercise type for #{i}: {type}')
if (type == '+') {
numbers <- number(sample(2:3, 1, prob = c(4, 1)))
question <- lhs(paste(numbers, collapse = ' + '))
answer <- sum(numbers)
}
if (type == '-') {
numbers <- number(2)
question <- lhs(paste(numbers[2], numbers[1], sep = ' - '))
answer <- diff(numbers)
}
if (type == '*') {
numbers <- number(2)
if (sum(nchar(numbers)) > 3) {
numbers <- number(2)
}
question <- lhs(paste(numbers[2], numbers[1], sep = ' × '))
answer <- numbers[1] * numbers[2]
}
if (type == '/') {
numbers <- number(2, numbers = 2:9, probs = c(2:9))
question <- lhs(paste(numbers[1] * numbers[2], numbers[1], sep = ' : '))
answer <- numbers[2]
}
log_debug('Exercise #{i}: {question}')
cat('[', i, '/', n, '] ', sep = '')
response <- ask(question)
pass <- response == answer | response == unidecode(data = german(answer), language = 'de')
log_debug('Response for #{i}: {response} ({pass})')
log_info('Exercise #{i}: {question}{response} ({pass})')
if (isTRUE(pass)) {
scores <- scores + 1 + (response == unidecode(data = german(answer), language = 'de'))
cat(green(sample(SUCCESS, 1), '\n'))
} else {
cat(red(sample(FAIL, 1), '\n'))
cat('A helyes megoldás: ', answer, "", shQuote(german(answer)), '\n')
}
}
log_info(paste('Score: ', scores, ' / ', n, ' (', round(scores / n * 100, 2), '%)', sep = ''))
cat('Pontszám: ', scores, ' / ', n, ' (', round(scores / n * 100, 2), '%)\n', sep = '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment