LaTeX真值表


11

编写一个程序或函数,该程序或函数接受逻辑函数的输出列表,并输出其真值表的LaTeX代码。

输入应标记为小写字母a-z,输出应标记为F。输入列表的长度将始终小于2^25,这意味着输入数量将始终小于25,因此您可以将小写字母的字母用作输入名称。

输入值

n输入数量和2^n代表逻辑功能输出的二进制数长度列表。

输出量

产生该功能真值表的LaTeX代码。输入和输出值应以行为中心。表标题及其值之间以及输入和输出之间必须有一条线,因此代码应类似于下面的代码。

\begin{tabular}{c * <NUMBER OF INPUTS>|c}
<INPUTS>&F\\
\hline
<INPUT VECTOR i>&<OUTPUT>\\
\end{tabular}

输入:

2
[0, 0, 0, 1]

输出:

\begin{tabular}{cc|c}
a & b & F \\
\hline
0 & 0 & 0 \\
0 & 1 & 0 \\
1 & 0 & 0 \\
1 & 1 & 1 \\
\end{tabular}

在LaTeX中显示时,哪个显示以下真值表

真相表

一般规则


3
这个挑战是否需要完全相同的输出或在TeX中可能产生相同结果的任何输出?
tsh

2
在TeX中产生相同结果的任何输出
drobilc

2
在这里,我发现有些棘手的事情是对TeX不太了解,那就是可能还有其他更短的方法来编写格式化TeX代码的表,甚至还有其他方法(打包?)来生成表。无论我使用哪种语言,TeX golf都是挑战的一部分。为了方便起见,是否有TeX的在线解释器,也许为了使确切的实现模棱两可?
xnor

1
提示:TeX代码似乎可以删除所有空格和换行符。
xnor

1
任何不知道如何在LaTeX中执行此操作的人,请遵循上面的示例输出。如果n = 5,则简单放置ccccc而不是cc,但不要|c理会...是的,在此表中,所有空格和换行符都是可选的,但我会避免使用空行。
Heimdall

Answers:


10

木炭,70字节

≔tabularζ\ζ{*θc|c}⸿⪫✂β⁰Iθ¹&⁰&F\\⸿\hline⸿Eη⁺⪫⁺⮌EIθI﹪÷κX²λ²⟦ι⟧&¦\\⁰\endζ

在线尝试!链接是详细版本的代码。说明:

≔tabularζ

将此字符串保存在变量中,以避免重复。

\ζ{*θc|c}⸿

打印初始\tabular{*2c|c}行(2或第一个输入q具有的任何值)。

⪫✂β⁰Iθ¹&⁰&F\\⸿\hline⸿

q从预定义的变量中获取第一个字母,b&在它们之间插入,然后附加&F\\和并\hline在下一行上打印。

Eη⁺⪫⁺⮌EIθI﹪÷κX²λ²⟦ι⟧&¦\\

循环输入第二个输入中的字符。对于每个索引,将其索引转换为具有length的二进制数,q并连接字符,将结果与&s \\附加在一起。结果字符串隐式打印在单独的行上。

⁰\endζ

打印\endtabular。(The 只是一个分隔符,因为deverbosifier忘记插入¦。)


2
鉴于这项挑战并非真正针对其设计的,木炭目前是赢家,这令人印象深刻。
Erik the Outgolfer

6

Python 2,153字节

lambda n,l:r'\tabular{*%dc|c}%s&F\\\hline%s\endtabular'%(n,q(map(chr,range(97,97+n))),r'\\'.join(q(bin(2**n+i)[3:]+x)for i,x in enumerate(l)))
q='&'.join

在线尝试!

输出像

\tabular{*2c|c}a&b&F\\\hline0&0&0\\0&1&0\\1&0&0\\1&1&1\endtabular

\tabular\endtabular被用作短\begin{tabular}\end{tabular},按照该胶乳高尔夫球尖端。的*2c是一个速记定义2列。


5

Haskell,164155字节

s%f=((:"&")=<<s)++f:"\\\\"
n#r=unlines$("\\tabular{"++('c'<$[1..n])++"|c}"):take n['a'..]%'F':"\\hline":zipWith(%)(mapM id$"01"<$[1..n])r++["\\endtabular"]

在线尝试!

unlines                               -- take a list of strings and join it with NL.
                                      -- the strings are:
   "\\tabular{"++('c'<$[1..n])++"|c}" -- tabular definition with n times 'c'
   take n['a'..]%'F'                  -- table header
   "\\hline"                          -- hline
   zipWith(%)(mapM id$"01"<$[1..n])r  -- table content
   ["\\endtabular"]                   -- end of tabular definition

Table header and content are built via function '%'

s%f=                                  -- take a string 's' and a char 'f'
    ((:"&")=<<s)                      -- append a "&" to each char in 's'
    ++f:"\\\\"                        -- and append 'f' and two backslashes

Table header:

take n['a'..] % 'F'                   -- s: the first n letters from the alphabet
                                      -- f: char 'F'
Table content:

zipWith(%)                            -- apply '%' pairwise to
    mapM id$"01"<$[1..n]              -- all combinations of '0' and '1' of length n
    r                                 -- and the string 'r' 

编辑:使用\tabular而不是\begin{tabular}(从@xnor的答案中窃取)。


3

Python 2中192个 168 166字节

lambda n,l:r'\begin{tabular}{*%dc|c}%s\end{tabular}'%(n,r'\\'.join(map('&'.join,[map(chr,range(97,97+n))+[r'F\\\hline']]+[bin(2**n+i)[3:]+l[n]for i in range(2**n)])))

在线尝试!

漂亮的印刷版:

Python 2中234个 229 218 209 205 203字节

n,l=input()
print'\\begin{tabular}{'+'c'*n+'|c}\n'+' & '.join(chr(i+97)for i in range(n)+[-27]),'\\\\\n\hline'
i=0
for r in l:print' & '.join(bin(i)[2:].rjust(n,'0')+`r`),r'\\';i+=1
print'\\end{tabular}'

在线尝试!


2

质子,142字节

n=>x=>"\\tabular*#{n}c|c#{j(map(chr,97..97+n))}&F\\\\\hline"+'\\\\'.join(j(bin(i)[2to].zfill(n)+x[i])for i:0..len(x))+"\\endtabular"j="&".join

在线尝试!

输出为高尔夫球乳胶形式;感谢xnor的把戏!

这应该可以比xnor的Python答案短一些,因为Proton从理论上讲应该永远不会输给Python大声笑(实际上,我对xD不好)。我可能会从xnor那里窃取一些技巧; P

现在通过将一些东西变成变量来使其变得更短,我刚刚注意到xnor也:P

然后,我们通过使用一些Proton高尔夫技巧来获得-6个字节。


1

R196187171字节

function(m,n){cat("\\tabular{*",n,"c|c}")
write(c(letters[1:n],"F\\\\\\hline",rbind(t(rev(expand.grid(rep(list(0:1),n)))),paste0(m,"\\\\")),"\\endtabular"),1,n+1,sep="&")}

在线尝试!

输出类似于木炭的答案expand.grid这个答案

根据记录,使用xtable从名字命名包不是太大短,因为一个具有指定很多选项,以满足规范,另外还包括包装:

R,187字节

function(m,n){u=rbind(apply(expand.grid(rep(list(0:1),n)),1,rev),m)
rownames(u)=c(letters[1:n],"F")
print(xtable(t(u),dig=0,align=c(rep("c",n+1),"|c}")),hl=0,include.r=F)}
library(xtable)

在线尝试!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.