鉴于:
- 一组项目,每个项目都有放入给定容器类型的成本.
- 一组容器类型,每个容器类型都有许多可用容器.
例:
金额*集装箱类型:5*A,3*B,2*C.
物品(成本):
3*X(A = 2,B = 3,C = 1)
2*Y(A = 5,B = 2,C = 2)
1*Z(A = 3,B = 3,C = 1)
问题:
找到物品放入容器的最佳位置,以便降低成本.为简单起见,仅将项目放入单一类型的容器中.
我尝试使用匈牙利方法来解决问题,但是运行时间为O(n³),对于大问题(例如,100000项)来说,这是非常令人望而却步的.
我目前的解决方案是一种贪婪的方法,只需按成本(asc)对项目 - 容器组合进行排序,并在O(n log n)中为第一个容器分配足够的数量.
有更好的解决方案吗?
>>> a=range(5)
>>> [a[i] for i in range(0,len(a),2)] ## list comprehension for side effects
[0, 2, 4]
>>> a
[0, 1, 2, 3, 4]
>>> [a[i]=3 for i in range(0,len(a),2)] ## try to do assignment
SyntaxError: invalid syntax
>>> def setitem(listtochange,n,value): ## function to overcome limitation
listtochange[n]=value
return value
>>> [setitem(a,i,'x') for i in range(0,len(a),2)] ## proving the function
['x', 'x', 'x']
>>> a
['x', 1, 'x', 3, 'x'] # We did assignment anyway
Run Code Online (Sandbox Code Playgroud) 我正在编辑一个脚本,该脚本对openstack keystone进行身份验证以获取令牌.API-Call有效,但我想使用变量而不是直接值来使其更具可读性和可重用性.但问题是值必须在引号(")中,我无法弄清楚如何做到这一点.我发现一些代码示例[1],[2]一般在字符串中使用变量但是我需要使用某种转义序列将值放在引号中.
目前我的字符串赋值如下:
params = '{"auth":{"passwordCredentials":{"username":"nodermatt", "password":"feelfree"}, "tenantId":"4"}}'
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,用户名,密码和tenantID的值都在引号中,我希望它用变量替换它们:
osuser = "nodermatt"
ospassword = "feelfree"
ostenant = "4"
params = '{"auth":{"passwordCredentials":{"username":osuser, "password":ospassword}, "tenantId":ostenant}}'
Run Code Online (Sandbox Code Playgroud)
如果可以解决这个"问题",我会很高兴.
PS:如果我在Google上错过了与我的问题匹配的帖子或搜索结果,我会非常感谢这个链接.
提前致谢!最好的问候,尼古拉斯
[1] Python中的子进程添加变量
以下两个版本的相同功能(基本上试图通过强力恢复密码)不会提供相同的性能:
private static final char[] CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
private static final int N_CHARS = CHARS.length;
private static final int MAX_LENGTH = 8;
private static char[] recoverPassword()
{
char word[];
int refi, i, indexes[];
for (int length = 1; length <= MAX_LENGTH; length++)
{
refi = length - 1;
word = new char[length];
indexes = new int[length];
indexes[length - 1] = 1;
while(true)
{
i = length - 1;
while ((++indexes[i]) == N_CHARS)
{
word[i] = CHARS[indexes[i] = 0];
if …Run Code Online (Sandbox Code Playgroud) 在Coursera 的"反应式编程原理"课程的第二周讲座中,有一个例子包含了这段有趣的代码:
val notIn1, notIn2, notOut = new Wire
Run Code Online (Sandbox Code Playgroud)
这里使用的语言功能是什么?
这会创建三个唯一的实例(向构造函数添加println会打印3行),因此它不会被转换为类似的东西
val notIn1 = notIn2 = notOut = new Wire
Run Code Online (Sandbox Code Playgroud)
调用的结果new Wire不会返回元组,所以我不认为它是模式匹配.有任何想法吗?
我可以在python中分配这样的序列:
a,b,c="ABC"
Run Code Online (Sandbox Code Playgroud)
但我无法将此序列作为参数传递给函数.即
def function2(a,b,c):
print a
print b
print c
function2("ABC")
Run Code Online (Sandbox Code Playgroud)
上述声明引发了错误.
任何人都可以告诉我在python中传递赋值和参数之间的区别吗?
标题说明了一切.但是,请string作为任何课程的占位符.
std::string s1("hello"); // construct from arguments
std::string s2 = "hello"; // ???
std::string s3; // construct with default values
s3 = "hello"; // assign
Run Code Online (Sandbox Code Playgroud)
我想知道声明s2是否与for s1或for 相同s3.
c++ initialization variable-assignment copy-constructor assignment-operator
这工作:
foo, bar = 1, 2
foo == 1 #true
bar == 2 #true
Run Code Online (Sandbox Code Playgroud)
这也有效:
baz = true
foo = baz ? 1 : 2
foo == 1 #true
Run Code Online (Sandbox Code Playgroud)
这不起作用:
foo, bar = baz ? 1, 2 : 3, 4
# SyntaxError: (irb):4: syntax error, unexpected ',', expecting ':'
# foo, bar = baz ? 1, 2 : 3, 4
# ^
Run Code Online (Sandbox Code Playgroud)
应如何格式化以使其有效?
_item = new OutTYonetimOzet();
_item.Banka = Convert.ToDecimal(" ");
liste.Add(_item);
Run Code Online (Sandbox Code Playgroud)
有一个名单liste.在列表项中,Banka命名元素是十进制值.我想在屏幕上显示空字符串时显示空字符串.但是这段代码得到了一个无法投射的错误.问题是什么.
错误信息是:
输入字符串的格式不正确.