简体   繁体   中英

How to remove brackets in a reticulate output in R/exams?

I have the following single-choice exercise RadicalPrimo.Rmd , which is intended to simplify square roots:

```{r data generation, echo = FALSE, results = "hide"}
library("exams")
library("reticulate")

# use_python('/usr/bin/python3', required = TRUE)
use_python('/home/rstudio/.local/share/r-miniconda/envs/r-reticulate/bin/python', required = TRUE)
options(scipen=999)
typ <- match_exams_device()
#--------------------------------------------------------
```

```{python, include=FALSE}
from sympy import *   
init_printing()     ##### Facilita la impresión en un formato legible
```

```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand1 = random.randint(2, 200)
nram = nrand1*2
sol = sympy.sqrt(nrand1)
pp = print('$' + sympy.printing.latex(sol) + '$')
```

Question
========

Simplify: 

$$\sqrt{`r py$nrand1`}$$


Answerlist
----------

*   
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
print('$' + sympy.printing.latex(sol) + '$')
```

*   
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random 
nrand2 = random.randint(1, 200)
if (nrand2 != nrand1) & (nrand2<nrand1):
  inc2 = sympy.sqrt(nrand2)
  inc2b = sympy.root(nrand2,2)
  inc =random.sample((inc2,inc2b),1)
print('$' + sympy.printing.latex(inc) + '$')
```

*  
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand3 = random.randint(2, 200)
if (nrand2 != nrand1) & (nrand3 != nrand2) & (nrand3<nrand1):
  inc3 = sympy.sqrt(nrand3)
pp3 = print('$' + sympy.printing.latex(inc3) + '$')
```

*  
```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
import random
nrand4 = random.randint(2, 200)
if (nrand2 != nrand1) & (nrand3 != nrand2) & (nrand4 != nrand3) & (nrand4<nrand1):
  inc4 = sympy.sqrt(nrand4)
pp4 = print('$' + sympy.printing.latex(inc4) + '$')
```

Solution
========

```{python, eval = TRUE, echo = FALSE,results='asis'}
import sympy
print('$' + sympy.printing.latex(sol) + '$')
``` 

Meta-information
================
exname:RadicalPrimo(single-choice)
extype:schoice
exsolution: 1000
exshuffle: TRUE

However, some answers in the choice list have inelegant square brackets:

在此处输入图像描述

I have applied various methods to remove brackets in Python output, but to no avail. How can I permanently remove those brackets?

In your example inc , the output of random.sample((inc2,inc2b),1) is a list in Python. Hence, when printed (both within Python or as LaTeX) the brackets are added:

>>> type(inc)
## <class 'list'>
>>> inc
## [sqrt(67)]
>>> sympy.printing.latex(inc)
## '\\left[ \\sqrt{67}\\right]'

You can simply extract the first element and print that:

>>> inc = inc[0]
>>> type(inc)
## <class 'sympy.core.power.Pow'>
>>> inc
## sqrt(67)
>>> sympy.printing.latex(inc)
## '\\sqrt{67}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM