小编Vik*_*ren的帖子

前10000个素数最有效的代码?

我想打印前10000个素数.任何人都可以给我最有效的代码吗?澄清:

  1. 如果你的代码在n> 10000时效率低下并不重要.
  2. 代码的大小无关紧要.
  3. 您不能以任何方式对值进行硬编码.

algorithm performance primes

55
推荐指数
9
解决办法
6万
查看次数

for - else vs for elif

我认为elif:是简写

else:
     if:
Run Code Online (Sandbox Code Playgroud)

但是不可能使用

for - elif:

只要

for - else:if:if:

在这段代码中:

for line in source:
    change_next = False
    for dataset,artnr,revision in datasets:
        if dataset in line:
            change_next = True
            print "  **  " + dataset + " found"
            datasets.remove((dataset,artnr,revision))
            break
    else:
        if line.startswith("DstID:"):
            print line.replace("DstID:","").rstrip()

    if change_next and "Partno:" in line:
        destination.write("Partno: " + artnr + "\n")
        print "Partno: " + artnr
    elif change_next and "Revno:" in line:
        destination.write("Revno:" + revision + "\n")
        print "Revno:" + revision
    else:
        destination.write(line) …
Run Code Online (Sandbox Code Playgroud)

python for-loop if-statement python-2.7

9
推荐指数
4
解决办法
6326
查看次数

基元的 @NotNull 约束,为什么?

刚刚遇到一个错误,问题是我遇到的:

@Column(name = "ACTIVE")
@NotNull
private boolean active;
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我忘记设置该值,但它仍然“有效”,因为布尔值的默认值是 false。我现在已将其更改为Boolean,如果未主动设置,则验证失败。

为什么我可以对@NotNull显然不能的事情施加限制null?这是重构的原因,所以如果我像现在所做的那样更改为布尔值,我仍然保留预期的约束吗?

是否有任何好的想法可以解决这些问题(除了为此目的进行更多测试)?或者我应该避免使用基元?

java constraints notnull

8
推荐指数
1
解决办法
7974
查看次数

c#short if语句不能与int一起使用?(INT = NULL)

我试图通过使用short-if缩短我的代码:

int? myInt=myTextBox.Text == "" ? null : 
    Convert.ToInt32(myTextBox.Text);
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:无法确定条件表达式的类型,因为''和'int'之间没有隐式转换

以下作品:

int? myInt;
if (myTextBox.Text == "") //if no text in the box
   myInt=null;
else
   myInt=Convert.ToInt32(myTextBox.Text);
Run Code Online (Sandbox Code Playgroud)

如果我在整数中替换'null'(比如'4')它也有效:

int? myInt=myTextBox.Text == "" ? 4: 
    Convert.ToInt32(myTextBox.Text);
Run Code Online (Sandbox Code Playgroud)

c# expression if-statement

7
推荐指数
1
解决办法
5242
查看次数

"没有操作"已经开始了

如果我从学校正确记得,有一个函数或关键字用于"尚未实现",但代码编译.我试图搜索它,但找不到.谁知道我在找什么?

就是这样的

isDivisor :: Integer -> Integer -> Bool
isDivisor x y = None
--isDivisor x y = (rem x y) == 0
Run Code Online (Sandbox Code Playgroud)

debugging haskell no-op

7
推荐指数
1
解决办法
579
查看次数

Java:将byte []转换为base36 String

我有点迷茫.对于一个项目,我需要使用base 36将哈希函数(SHA256)的输出(字节数组)转换为String.

所以最后,我想转换哈希的(Hex-String表示),即

43A718774C572BD8A25ADBEB1BFCD5C0256AE11CECF9F9C3F925D0E52BEAF89
Run Code Online (Sandbox Code Playgroud)

到base36,所以上面的示例String将是:

3SKVHQTXPXTEINB0AT1P0G45M4KI8U0HR8PGB96DVXSTDJKI1
Run Code Online (Sandbox Code Playgroud)

对于实际转换为base36,我在StackOverflow上找到了一些代码:

public static String toBase36(byte[] bytes) {
    //can provide a (byte[], offset, length) method too
    StringBuffer sb = new StringBuffer();
    int bitsUsed = 0; //will point how many bits from the int are to be encoded
    int temp = 0;
    int tempBits = 0;
    long swap;
    int position = 0;

    while((position < bytes.length) || (bitsUsed != 0)) {
        swap = 0;
        if(tempBits > 0) {
            //there are bits left over from previous iteration
            swap …
Run Code Online (Sandbox Code Playgroud)

java converter base36

7
推荐指数
1
解决办法
2323
查看次数

保持cmd.exe打开

我有一个python脚本,我使用注册表项可删除,但它似乎不起作用.cmd.exe窗口只是闪烁,我可以以某种方式使窗口保持,或保存输出?

编辑:问题是它给了整个路径不仅文件名.

python registry cmd

6
推荐指数
1
解决办法
4892
查看次数

在字典<string,<IEnumerable string >>中连接键和值以生成一个列表

我有

Dictionary<string,IEnumerable<string>> pathAndItems = new Dictionary<string,IEnumerable<String>>();
Run Code Online (Sandbox Code Playgroud)

this/is/path/: {hey, ho, lets, go}
another/path/: {hey, hello}
Run Code Online (Sandbox Code Playgroud)

我想要做的是使用所有连接的值制作一个IEnumerable.

this/is/path/hey, this/is/path/ho, this/is/path/lets, this/is/path/go, another/path/hey, another/path/hello
Run Code Online (Sandbox Code Playgroud)

我可以把所有这些都集中在一起,但我怎样才能将密钥添加到每个密钥中?

var SL_requirements = SL_requirementsDict.SelectMany(kvp => kvp.Value);
Run Code Online (Sandbox Code Playgroud)

编辑:我想将它作为LINQ表达式而不是循环

c# linq dictionary

5
推荐指数
1
解决办法
2045
查看次数

调整窗口大小时Bootstrap <select>宽度,内容太宽

如何使下拉大小适合内容(在我的情况下,它发生在缩小浏览器到小于某个特定大小,然后内容开始消失?我最好不要任何自定义CSS,任何内置到bootstrap到支持这个?

 <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form class="form-horizontal" role="form" enctype="multipart/form-data" id="inputForm" name="inputForm" novalidate>
<div class="well">
<div class="form-group">
    <label class="col-xs-3 control-label"><strong>4.</strong>&nbsp;Select thing</label>
    <div class="col-xs-2">
        <select class="form-control">
            <option>Short
            </option>
            <option>Medium lenght
            </option>
            <option>Much much much longer text not fitting when resizing
            </option>
        </select>
    </div>
</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)

html css twitter-bootstrap

5
推荐指数
3
解决办法
6万
查看次数

这个Java for循环终止条件是什么意思?

我想知道这里是否有人知道以下for循环的终止条件应该是什么意思.

for (int i = 0; i < 1 << Level; i++) {
...
}
Run Code Online (Sandbox Code Playgroud)

java loops for-loop

5
推荐指数
1
解决办法
167
查看次数