Generating a quiz in Emacs Lisp? - elisp

Forgive the "duplicate" question. I'd like to see this solved in Emacs Lisp too, and if I just tagged it for both topics, I probably would have only gotten one answer. The Emacs answer should be sufficiently different that it's probably worthwhile to have it.
I want to teach myself Spanish and I've got several word lists like
the data show below. How can I generate a quiz from the data that
looks like this?
amarillo? [ ] blue [ ] yellow [ ] gray [ ] pink
azul? [ ] red [ ] blue [ ] green [ ] orange
.
.
.
verde? [ ] purple [ ] gold [ ] green [ ] black
The idea is to randomly include the answer with 3 randomly chosen
incorrect answers. Ideally, the incorrect answers would not be too
repetitive.
amarillo|yellow
azul|blue
blanco|white
dorado|golden
gris|gray
marrón|brown
naranja|orange
negro|black
oro|gold
púrpura|purple
rojo|red
rosa|pink
verde|green

Ok, so I'm assuming that you have the input in a file opened in an Emacs buffer.
(defun insert-quiz (a-buffer)
(interactive "bBuffer name: ")
(let* ((question-pairs (split-string (with-current-buffer a-buffer (buffer-string))))
(quiz-answers (mapcar (lambda (x) (cadr (split-string x "|"))) question-pairs)))
(insert
(apply #'concat
(mapcar
(lambda (x)
(let ((q-pair (split-string x "|")))
(make-question (car q-pair) (answers-list quiz-answers (cadr q-pair)))))
question-pairs)))))
insert-quiz is an interactive function that takes a buffer name, and uses the stuff in that buffer to generate a quiz for you, then insert that quiz at point as a side-effect. It calls some smaller functions which I'll explain below.
(defun make-question (question answers)
(apply #'format
"%-16s[ ] %-16s[ ] %-16s[ ] %-16s[ ] %s \n"
(append
(list (concat question "?"))
answers)))
make-question takes a question and a list of answers, and formats them as one line of the quiz.
(defun answers-list (quiz-answers right-answer)
(replace (n-wrong-answers quiz-answers right-answer)
(list right-answer)
:start1 (random 3)))
answers-list takes a list of all possible answers in the quiz, and the right answer and uses n-wrong-answers to create a list of four answers, one of which is the correct one.
(defun n-wrong-answers (answer-list right-answer &optional answers)
(if (= 4 (list-length answers))
answers
(n-wrong-answers
answer-list
right-answer
(add-to-list 'answers (random-wrong-answer answer-list right-answer)))))
n-wrong-answers takes a list of all possible answers in the quiz, and the right answer, then uses random-wrong-answer to return a list of four unique incorrect answers.
(defun random-wrong-answer (answer-list right-answer)
(let ((gen-answer (nth (random (list-length answer-list)) answer-list)))
(if (and gen-answer (not (string= gen-answer right-answer)))
gen-answer
(random-wrong-answer answer-list right-answer))))
Finally, at the lowest level, random-wrong-answer takes a list of all possible answers in the quiz, and returns a single wrong answer.
After you load the above functions into Emacs, use M-x insert-quiz and type the name of the buffer you have your input loaded into (you'll get tab completion). It wouldn't be too difficult to change the insert-quiz function so that it takes a filename rather than an open buffer-name.
The input you list above will yield:
amarillo? [ ] yellow [ ] orange [ ] gray [ ] red
azul? [ ] gold [ ] purple [ ] blue [ ] orange
blanco? [ ] pink [ ] red [ ] white [ ] black
dorado? [ ] yellow [ ] golden [ ] red [ ] orange
gris? [ ] red [ ] pink [ ] gray [ ] green
marrón? [ ] brown [ ] yellow [ ] white [ ] golden
naranja? [ ] orange [ ] gold [ ] black [ ] golden
negro? [ ] pink [ ] black [ ] blue [ ] white
oro? [ ] red [ ] gold [ ] purple [ ] brown
púrpura? [ ] purple [ ] orange [ ] gray [ ] black
rojo? [ ] gray [ ] red [ ] black [ ] pink
rosa? [ ] red [ ] green [ ] pink [ ] yellow
verde? [ ] green [ ] purple [ ] red [ ] brown
Hope that helps.

Related

Grok pattern for data separated by pipe with whitespaces and optional values in it

I have a textfile/logfile in which the values are separated by a pipe symbol. "|" with multiple whitespaces.
Also I just wanted to try it without gsub.
An example is below,
Does anyone know how to write a GROK pattern to extract it for logstash? as I am very new to it. Thanks in advance
5000| | |applicationLog |ClientLog |SystemLog |Green | |2014-01-07 11:58:48.76948 |12345 (0x1224)|1) Error 2)Sample Log | Configuration Manager
Since the number of | are inconsistent between different words, you can match it with .*? and extract rest of the data as predefined grok pattern
%{NUMBER:num}.*?%{WORD:2nd}.*?%{WORD:3rd}.*?%{WORD:4th}.*?%{WORD:5th}.*?%{TIMESTAMP_ISO8601}
which will give you,
{
"num": [
[
"5000"
]
],
"BASE10NUM": [
[
"5000"
]
],
"2nd": [
[
"applicationLog"
]
],
"3rd": [
[
"ClientLog"
]
],
"4th": [
[
"SystemLog"
]
],
"5th": [
[
"Green"
]
],
"TIMESTAMP_ISO8601": [
[
"2014-01-07 11:58:48.76948"
]
],
"YEAR": [
[
"2014"
]
],
"MONTHNUM": [
[
"01"
]
],
"MONTHDAY": [
[
"07"
]
],
"HOUR": [
[
"11",
null
]
],
"MINUTE": [
[
"58",
null
]
],
"SECOND": [
[
"48.76948"
]
],
"ISO8601_TIMEZONE": [
[
null
]
]
}
You can test it at online grok debugger.
Since you are new to grok you might want to read, grok filter plugin basics
If you can, I'd suggest you to also have a look in dissect filter which is faster and efficient than grok,
The Dissect filter is a kind of split operation. Unlike a regular
split operation where one delimiter is applied to the whole string,
this operation applies a set of delimiters to a string value. Dissect
does not use regular expressions and is very fast. However, if the
structure of your text varies from line to line then Grok is more
suitable. There is a hybrid case where Dissect can be used to
de-structure the section of the line that is reliably repeated and
then Grok can be used on the remaining field values with more regex
predictability and less overall work to do.

tegrahost_v2: Stat for tegra186-quill-p3310-1000-c03-00-base.dtb failed

I've built an image for Jetson TX2 module using yocto. Everything when fine for few days but now I get this error when I try to flash the device.
Welcome to Tegra Flash
version 1.0.0
Type ? or help for help and q or quit to exit
Use ! to execute system commands
[ 0.0008 ] tegrasign_v2 --key None --getmode mode.txt
[ 0.0016 ] Assuming zero filled SBK key
[ 0.0016 ]
[ 0.0016 ] Generating RCM messages
[ 0.0023 ] tegrarcm_v2 --listrcm rcm_list.xml --chip 0x18 --download rcm mb1_recovery_prod.bin 0 0
[ 0.0030 ] RCM 0 is saved as rcm_0.rcm
[ 0.0033 ] RCM 1 is saved as rcm_1.rcm
[ 0.0033 ] List of rcm files are saved in rcm_list.xml
[ 0.0033 ]
[ 0.0033 ] Signing RCM messages
[ 0.0040 ] tegrasign_v2 --key None --list rcm_list.xml --pubkeyhash pub_key.key
[ 0.0046 ] Assuming zero filled SBK key
[ 0.0076 ]
[ 0.0076 ] Copying signature to RCM mesages
[ 0.0083 ] tegrarcm_v2 --chip 0x18 --updatesig rcm_list_signed.xml
[ 0.0093 ]
[ 0.0093 ] Parsing partition layout
[ 0.0100 ] tegraparser_v2 --pt flash.xml.tmp
[ 0.0109 ]
[ 0.0109 ] Creating list of images to be signed
[ 0.0116 ] tegrahost_v2 --chip 0x18 --partitionlayout flash.xml.bin --list images_list.xml zerosbk
[ 0.0124 ] Stat for tegra186-quill-p3310-1000-c03-00-base.dtb failed
[ 0.0161 ]
Error: Return value 4
Command tegrahost_v2 --chip 0x18 --partitionlayout flash.xml.bin --list images_list.xml zerosbk
Does this error ring a bell to anyone?
I am able to flash the board with JetPack.
Thanks,
-Damien
Just in case you never figured this out, it looks like
[ 0.0124 ] Stat for tegra186-quill-p3310-1000-c03-00-base.dtb failed
is the real error. Fix that and you should be good.

parsing command line output on Windows command line batch file

I have this command that scan a file and returns a summary.
For example on running this command
omsCmdLineUtil.exe process C:\test.exe Default
the result output is:
Ticket:[ 2214271306 ]
Process Details
---------------
File: [ C:\test.exe ]
MD5: [ D41D8CD98F00B204E9800998ECF8427E ]
SHA1: [ DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ]
SHA256: [ E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 ]
File Size: [ 0 bytes ]
File Type Category: [ O ]
File Type: [ - ]
File Type Description: [ empty ]
[ Clean ] Ahnlab scan engine [ 1 ms ]
[ Clean ] ClamAV scan engine [ 1 ms ]
[ Clean ] BitDefender scan engine [ 1 ms ]
[ Clean ] Avira scan engine [ 1 ms ]
[ Clean ] Quick Heal scan engine [ 1 ms ]
[ Clean ] ThreatTrack scan engine [ 1 ms ]
[ Clean ] ESET scan engine [ 1 ms ]
[ Clean ] Total Defense scan engine [ 1 ms ]
Scan Completion
---------------
[ Clean ]
Ticket: [ 2214271306 ]
File path: C:\test.exe
Scan time: 1 ms [12/20/2015 13:00:06:791]
Process Completion
------------------
Ticket: [ 2214271306 ]
User agent: Default
Profile: Default
Result: [ Allowed ]
File processed: C:\test.exe
I want to create a batch file that parses this result by searching for the output line Result:, check if it's [ Allowed ] or [ Blocked ] and return 0 for allowed and 1 for blocked.
I tried something like this, but its not really working:
omsCmdLineUtil.exe process C:\test.exe Default | set ts = findstr /C:"Result: [ Allowed ]"
if %ts% == "Result: [ Allowed ]" return 0
else return 1
Which modification on code is necessary to get the expected result?
there is no return in Batch. I think, you want exit /b <errorlevel>
omsCmdLineUtil.exe process C:\test.exe Default | find "Result: [ Allowed ]" >nul && Exit /b 0 || Exit /b 1
Instead of Exit 0 you can of Course also use set ts=0 and use that. Or use echo instead.
Some explanations:
>nul redirects the output to nirvana, keeping your screen clean.
&& acts as "If previous command was successfull, then..." (string was found)
|| acts as "if previous command was not successfull, then...` (string was not found)
I prefer using find when possible because of it's simpler syntax, but of course findstr /C:"Result: [ Allowed ]"will also work

Behat Configuration: how to set output_style option without specifying fg and bg colors

I am one of the 10% of males who is colorblind, so I'm trying to configure my Behat CLI output to better distinguish between the yellow and green lines. This is generally supported, but judging from the docs, if I want to add an option such as underscore, I must also specify both foreground and background colors.
http://docs.behat.org/guides/7.config.html#colors
I wouldn't even mind if I had to redundantly define the fg color, but the default style does not apply a bg color, and I can't discover if the arguments can be configured so that they don't override the default.
Here is the example given:
default:
formatter:
name: pretty
parameters:
output_styles:
comment: [ black, white, [ underscore ] ]
This is roughly what I'm after:
default:
formatter:
name: pretty
parameters:
output_styles:
undefined: [ , , [ underscore ] ]
However, if I leave the first two arguments (or just the second argument) blank, I get an error like this:
[InvalidArgumentException]
Invalid foreground color specified: "Array". Expected one of (black, red, green, yellow, blue, magenta, cyan, white)
It's not the end of the world, because I can simply specify both fg and bg colors, but since the defaults don't use bg colors, it ends up looking ugly.
I stumbled on the answer by trial and error:
default:
formatter:
name: pretty
parameters:
output_styles:
undefined: [ null, null, [ underscore ] ]
After working with the styles and keeping them in sync with the documented color meanings (see http://docs.behat.org/guides/2.definitions.html#step-execution-result-types), I came up with the following settings which work well on dark and light terminal color schemes, and which also use non-color styles to differentiate result types:
default:
formatter:
name: pretty
parameters:
output_styles:
passed: [ white, green ]
passed_param: [ white, green, [ bold ]]
undefined: [ yellow, null, [ underscore, bold ]]
pending: [ yellow, null, [ underscore ]]
pending_param: [ yellow, null, [ underscore, bold ]]
failed: [ white, red ]
failed_param: [ white, red, [ bold ]]
skipped: [ cyan ]
skipped_param: [ cyan, null, [ bold ]]
comment: [ cyan ]
tag: [ cyan ]
Here's a screenshot of these output_styles on both a light and dark terminal:

Handling 4-block oriented matrix product and inversion in Maxima

I am concerned in finding symbolic solutions and expansion to matrix products and inversions. Actually, it is something I would like to define by myself. I will explain myself.
I want to create a "mathematical" object that i will call B4MAT which represents a square matrix whose elements are 4 square half-sized matrices. So I want to define the product between two B4MAT giving me back another B4MAT whose components are calculated by applying product rules, but among matrices, not scalars.
Furthermore, and this is a very important point, consider Blockwise Inversion of a matrix. I want to define inversion of a B4MAT as an operation returning me another B4MAT whose elements are calculated using the blockwise inversion algorithm in the link.
How to achieve this in Maxima?
Thankyou
For the first half of your question, you just need to change matrix_element_mult to non-commutative multiplication and then use a matrix whose elements are the blocks you want. For example:
Maxima branch_5_27_base_248_ge261c5e http://maxima.sourceforge.net
using Lisp SBCL 1.0.57.0.debian
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) A: matrix([1,2],[3,4])$ B: matrix([2,1],[3,4])$
(%i3) matrix([A,B], [B,A]);
*** output flushed ***
(%i4) C: matrix([A,B], [B,A]);
[ [ 1 2 ] [ 2 1 ] ]
[ [ ] [ ] ]
[ [ 3 4 ] [ 3 4 ] ]
(%o4) [ ]
[ [ 2 1 ] [ 1 2 ] ]
[ [ ] [ ] ]
[ [ 3 4 ] [ 3 4 ] ]
(%i5) C . C;
[ [ 5 5 ] [ 4 4 ] ]
[ [ ] [ ] ]
[ [ 18 32 ] [ 18 32 ] ]
(%o5) [ ]
[ [ 4 4 ] [ 5 5 ] ]
[ [ ] [ ] ]
[ [ 18 32 ] [ 18 32 ] ]
(%i6) matrix_element_mult: ".";
(%o6) .
(%i7) C . C;
[ [ 14 16 ] [ 13 17 ] ]
[ [ ] [ ] ]
[ [ 33 41 ] [ 33 41 ] ]
(%o7) [ ]
[ [ 13 17 ] [ 14 16 ] ]
[ [ ] [ ] ]
[ [ 33 41 ] [ 33 41 ] ]
I think you have to code up the inversion formula yourself though (don't forget you can get at the blocks with expressions like "C[1][2]" (for the top right corner) etc.

Resources