昨天我有这个面试问题,我无法完全回答:
给定f() = 0 or 1具有完美1:1分布的函数,创建f(n) = 0, 1, 2, ..., n-1每个概率为1/n的函数
我可以想出一个解决方案,如果n是2的自然幂,即用于f()生成二进制数的位k=ln_2 n.但这显然不适用于n = 5,因为这会产生f(5) = 5,6,7我们不想要的东西.
有谁知道解决方案?
我有一组有序的数据点存储为TreeSet<DataPoint>.每个数据点具有一position和一Set的Event对象(HashSet<Event>).
有4个可能的Event对象A,B,C,和D.每个DataPoint具有这些,例如,2 A和C,除了第一个和最后DataPoint在对象集,其具有T尺寸1的.
我的算法是要找到一个新的可能性DataPoint Q的位置x有Event q在这个集合.
我这样做是通过计算S此数据集的值,然后添加Q到集合并S再次计算.然后我将第二个S除以第一个来隔离新概率DataPoint Q.
计算公式为S:
http://mathbin.net/equations/105225_0.png
哪里
http://mathbin.net/equations/105225_1.png
http://mathbin.net/equations/105225_2.png
对于 http://mathbin.net/equations/105225_3.png
和
http://mathbin.net/equations/105225_4.png
http://mathbin.net/equations/105225_5.png是一种昂贵的概率函数只取决于它的参数,并没有别的(和http://mathbin.net/equations/105225_6.png),HTTP:// mathbin.净/方程/ 105225_7.png是最后DataPoint在该组(右侧节点),http://mathbin.net/equations/105225_8.png是第一DataPoint(左侧节点), …
我使用psych包的fa命令进行因子分析,因此有一个类的对象fa.我可以查询加载fac$loadings,但我想只提取包含加载的表,所以我可以使用xtable(或类似)将其转换为LaTeX格式.
示例代码:
library(psych)
library(xtable)
data(bfi)
fac <- fa(r=cor(bfi, use="complete.obs"), nfactors=5, fm="ml", rotate="none")
fac$loadings
ld <- someMagicalFunction(fac$loadings)
xtable(ld)
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我能用什么someMagicalFunction?
我有一个包含包装器的TreeSet,它将Foo对象存储在某个对象中position,定义如下:
class Wrapper implements Comparable<Wrapper> {
private final Foo foo;
private final Double position;
...
@Override boolean equals(Object o) {
...
if(o instanceof Wrapper)
return o.getFoo().equals(this.foo);
if(o instanceof Foo)
return o.equals(this.foo);
}
@Override public int compareTo(MarkerWithPosition o) {
return position.compareTo(o.getPosition());
}
}
NavigableSet<Wrapper> fooWrappers = new TreeSet<Wrapper>();
Run Code Online (Sandbox Code Playgroud)
因为我希望我TreeSet被命令position但可以搜索到foo.但是当我执行这些操作时:
Foo foo = new Foo(bar);
Wrapper fooWrapper = new Wrapper(foo, 1.0);
fooWrappers.add(fooWrapper);
fooWrapper.equals(new Wrapper(new Foo(bar), 1.0));
fooWrapper.equals(new Foo(bar));
fooWrappers.contains(fooWrapper);
fooWrappers.contains(new Wrapper(foo, 1.0)); …Run Code Online (Sandbox Code Playgroud) 我有一个抽象的课
public abstract class Integrator {
public abstract Point integrate();
public abstract Point function(Double x, Point y);
}
Run Code Online (Sandbox Code Playgroud)
延长了
public abstract class Euler extends Integrator {
public Point integrate() {
... // this calls function(x, y)
}
}
public abstract class Central extends Integrator {
public Point integrate() {
... // this calls function(x, y)
}
}
Run Code Online (Sandbox Code Playgroud)
两者的实施方式integrate()不同.现在,我实例化的具体类是这样定义的
public class EulerIVP extends Euler {
public EulerIVP(...) { ... }
public Point function(Double x, Point y) {
...
} …Run Code Online (Sandbox Code Playgroud) 我有需要的方法Collection<Object>,其中Object可以是一个String或CustomClass.然后它接受集合的每个元素并将其传递给具有如下参数的方法Object:
public void foo(Collection<Object> c) {
for(Object o : c)
bar(o);
}
public void bar(Object o) {
if(o instanceof String || o instanceof CustomClass) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
bar当我通过它正常工作String或CustomClass,但是当我试图通过一个NavigableSet<String>给foo我弄cannot find symbol; symbol : method foo(java.util.NavigableSet<java.lang.String>).
但是,如果我更改参数类型foo以Collection<String>它工作正常,但是这意味着我需要一个新的foo(Collection<CustomClass>),这将涉及到重复代码的方法.有没有解决的办法?
java ×4
algorithm ×2
comparable ×1
equals ×1
generics ×1
inheritance ×1
latex ×1
overloading ×1
polymorphism ×1
psych ×1
r ×1
random ×1
set ×1