我想解析一个布尔表达式(在C++中).输入表格:
a and b xor (c and d or a and b);
Run Code Online (Sandbox Code Playgroud)
我只想将这个表达式解析为树,知道优先级规则(不是,和,xor,或).所以上面的表达式应该类似于:
(a and b) xor ((c and d) or (a and b));
Run Code Online (Sandbox Code Playgroud)
到解析器.
树将是以下形式:
a
and
b
or
c
and
d
xor
a
and
b
Run Code Online (Sandbox Code Playgroud)
输入将通过命令行或以字符串的形式.我只需要解析器.
有没有可以帮助我做到这一点的消息来源?
我正在尝试为我的简单类生成一个hashCode()方法,但我没有得到它的任何地方.我将不胜感激任何帮助.我已经实现了equals()方法,如下所示,并且还想知道我是否需要实现compareTo()方法.我已经导入java.lang.Character来使用character.hashCode()但它似乎不起作用.
private class Coord{
private char row;
private char col;
public Coord(char x, char y){
row = x;
col = y;
}
public Coord(){};
public char getX(){
return row;
}
public char getY(){
return col;
}
public boolean equals(Object copy){
if(copy == null){
throw new NullPointerException("Object entered is empty");
}
else if(copy.getClass()!=this.getClass()){
throw new IllegalArgumentException("Object entered is not Coord");
}
else{
Coord copy2 = (Coord)copy;
if(copy2.row==this.row && copy2.col==this.col)
return true;
else
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢...
comparTo()方法给了我java.lang.Comparable转换错误..
public int …Run Code Online (Sandbox Code Playgroud) 我有一些我正在尝试组合的大型数据集.我已经创建了一个我想做的玩具示例.我有三张桌子:
require(data.table)
set.seed(151)
x <- data.table(a=1:100000)
y <- data.table(b=letters[1:20],c=sample(LETTERS[1:4]))
proportion <- data.table(expand.grid(a=1:100000,c=LETTERS[1:4]))
proportion[,prop:=rgamma(4,shape = 1),by=a]
proportion[,prop:=prop/sum(prop),by=a]
Run Code Online (Sandbox Code Playgroud)
这三个表x,y和proportion.对于x我想要y使用表中的概率从整个表中进行采样proportion并将它们组合到另一个表中的每个元素.我想出的方法是:
temp <- setkey(setkey(x[,c(k=1,.SD)],k)[y[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL],a,c)
temp <- temp[setkey(proportion,a,c)][,prop:=prop/.N,by=.(a,c)] # Uniform distribution within the same 'c' column group
chosen_pairs <- temp[,.SD[sample(.N,5,replace=FALSE,prob = prop)],by=a]
Run Code Online (Sandbox Code Playgroud)
但是这种方法是内存密集型和缓慢的,因为它首先交叉连接两个表然后从中进行采样.有没有办法以有效(记忆和时间)的方式执行此任务?
我正在尝试为我的ggplot添加一个自定义的图例,但是图例框有一个角度的线条.我想将该角度更改为0度.有没有办法做到这一点?以下是示例图的代码.
ggplot()+geom_abline(aes(color="black",,slope=1,intercept = 0))+
geom_abline(aes(color="red",slope=0.5,intercept = 0))+
scale_color_manual(values=c("black"="black","red"="red"))
Run Code Online (Sandbox Code Playgroud)
我为这个问题创建了一个样本数据集
import pandas as pd
from pandas import DataFrame
import seaborn as sns
import numpy as np
sex = np.array(['Male','Female'])
marker1 = np.array(['Absent','Present'])
marker2 = np.array(['Absent','Present'])
sample1 = np.random.randint(0,2,100)
sample2 = np.random.randint(0,2,100)
sample3 = np.random.randint(0,2,100)
df=pd.concat([pd.Series(sex.take(sample1),dtype='category'),pd.Series(marker1.take(sample2),dtype='category'),pd.Series(marker2.take(sample3),dtype='category')],axis=1)
df.rename(columns={0:'Sex',1:'Marker1',2:'Marker2'},inplace=True)
fig =sns.FacetGrid(data=df,col='Sex',hue='Marker2',palette='Set1',size=4,aspect=1).map(sns.countplot,'Marker1',order=df.Marker1.unique()).add_legend()
Run Code Online (Sandbox Code Playgroud)
此代码创建的图是堆叠图
我想要创建的是一个闪避图(来自 R)。如何修改此代码以便我可以看到Marker2存在的并排比较?
我有一个嵌套的命名列表,如下所示:
> data <- list("1"=list(rating=-1,points=190,name="Fernando"),"2"=list(rating=3,points=532,name="Carlos"))
> data
$'1'
$'1'$rating
[1] -1
$'1'$points
[1] 190
$'1'$name
[1] "Fernando"
$'2'
$'2'$rating
[1] 3
$'2'$points
[1] 532
$'2'$name
[1] "Carlos"
Run Code Online (Sandbox Code Playgroud)
现在当我在"data"上使用融合函数时,它会将name的值转换为numeric而不是character.我可以理解为什么因为value的数据类型设置为数字.有没有办法将值的数据类型转换为整体字符.
这是我使用融合功能时得到的结果:
> melt(data)
value L2 L1
1 -1 rating 1
2 190 points 1
3 1 name 1
4 3 rating 2
5 532 points 2
6 1 name 2
Run Code Online (Sandbox Code Playgroud)
但我想要的是以下内容:
value L2 L1
1 -1 rating 1
2 190 points 1
3 Fernando name 1
4 3 rating 2 …Run Code Online (Sandbox Code Playgroud) 我遵循以下Vertex类,它实现了equals,hashCode和compareTo方法.即使这样,我的HashMap也会返回null.我不知道为什么?
public class Vertex implements Comparable<Vertex> {
int id;
public Vertex(int number) {
id = number;
}
public boolean equals(Object other) {
if (other == null)
return false;
else if (other.getClass() != this.getClass())
return false;
else {
Vertex copy = (Vertex) other;
if (copy.id == this.id)
return true;
else
return false;
}
}
public int hasCode() {
int prime = 31;
int smallPrime = 3;
int hashCode = this.id ^ smallPrime - prime * this.hasCode();
return hashCode;
}
public int …Run Code Online (Sandbox Code Playgroud)