小编use*_*455的帖子

使用xor解释在数组中找到两个非重复的整数

鉴于[1,1,4,5,5,6]我们可以找到46成为非重复整数.

有一个解决方案使用XOR.

这是作者提出的算法:

#include <stdio.h>
#include <stdlib.h>

/* This finction sets the values of *x and *y to nonr-epeating
 elements in an array arr[] of size n*/
void get2NonRepeatingNos(int arr[], int n, int *x, int *y)
{
  int xor = arr[0]; /* Will hold xor of all elements */
  int set_bit_no;  /* Will have only single set bit of xor */
  int i;
  *x = 0;
  *y = 0;

  /* …
Run Code Online (Sandbox Code Playgroud)

c arrays algorithm

21
推荐指数
2
解决办法
1万
查看次数

Django - 捕获异常

这可能是一个Python新手问题......

try:
   #do something
except:
   raise Exception('XYZ has gone wrong...')
Run Code Online (Sandbox Code Playgroud)

即使DEBUG = True,我也不希望这raise Exception给我那个黄页.我实际上想通过将用户重定向到错误页面来处理异常或显示错误(在页面顶部给出一条CSS错误消息......)

我该如何处理?有人可以指导我吗?如果我只是提高它,我将获得黄色调试页面(再次,我不希望某些例外通过在DEBUG = True时显示调试页面来阻止网站运行).

如何在views.py中处理这些异常?

谢谢.

python django exception-handling

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

在Fabric中执行sudo

我有一个命令service app start-demo要求我输入sudo service app start-demo命令行.

我用过sudo(service app start-demo),sudo(sudo service app start-demo)但我还是得到了

警告:sudo()在执行'sudo service app start-demo'时遇到错误(返回代码1)

我作为终端中的命令行执行它没有问题.

我不确定是否SADeprecationWarning:算不及面料?

谢谢.


user@box:/var/lib/app$ fab kickstart
You are installing prereqs..........
### Install Prereqs for Populate ###
No hosts found. Please specify (single) host string for connection: localhost
[localhost] Login password: 

### I am starting demo ###
[localhost] sudo: sudo service app start-demo
[localhost] out: Starting demo

Fatal error: sudo() encountered an error (return code …
Run Code Online (Sandbox Code Playgroud)

python fabric

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

Python装饰器可选参数

from functools import wraps
def logged(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print func.__name__ + " was called"
        return func(*args, **kwargs)
    return with_logging

@logged
def f(x):
   """does some math"""
   return x + x * x

print f.__name__  # prints 'f'
print f.__doc__   # prints 'does some math'
Run Code Online (Sandbox Code Playgroud)

鉴于此示例代码,我将如何做@logged(variable)

我试过这个

from functools import wraps
def logged(func):
    def outer(var):
        @wraps(func)
        def with_logging(*args, **kwargs):
            print func.__name__ + " was called"
            return func(*args, **kwargs)
        return with_logging
    return outer
Run Code Online (Sandbox Code Playgroud)

我希望这样执行:logged(func)(session_variable)

但是不起作用.任何的想法?我希望能够做@logged和@logged(var)(甚至@logged(var1,var2))谢谢.

python decorator

5
推荐指数
2
解决办法
2174
查看次数

知道忘记或忽视是否更好?

我有一些我不需要提交的二进制文件.我需要他们当地人.我该怎么做hg forget binary-folder/或添加这一行.hgignore

glob:binary-folder/*

哪一个更好?

谢谢.

mercurial

3
推荐指数
1
解决办法
3195
查看次数

从现有数组初始化向量

const int NUMB = 4;
int n[] = {5,6,7,8};

// create a vector of strings using the n[] array
vector<int> partnums(n, n + NUMB);
Run Code Online (Sandbox Code Playgroud)

类函数vector name(src.begin,src.end)

使用源自src.begin并以scr.end结尾的源容器中的元素创建初始化的向量

根据这本书,

向量partnums声明为向量类型int,并使用来自n数组的元素进行初始化,从第一个数组元素n [0]开始,以最后一个数组元素结束, 位于位置n + NUMB.

我还是不明白."位于位置n + NUMB,索引是否从0开始?或者编译器知道此src.end指的是位置1(scr.begin),并从数组n中的那个位置开始计数,并计数到第四位)?

谢谢

c++ vector

2
推荐指数
1
解决办法
1905
查看次数

修改参数的python函数

def add(a,b):
    for i in range(len(a)):
        a[i] = a[i] + b

def main():
    amounts = [100,200]
    rate = 1
    add(amounts,rate)
    print amounts

main()
Run Code Online (Sandbox Code Playgroud)

函数add没有返回.我读到只有像list这样的可变对象才能进行更改.但为什么这个人省略了回报呢?有或没有回报是好的.为什么?这与C++有很大的不同.

谢谢

python

0
推荐指数
1
解决办法
218
查看次数