以下代码的最佳单线代替是什么?我确信这是一种更聪明的方式.
choices = ((1, 'ONE'), (2, 'TWO'), (3, 'THREE'))
some_int = 2
for choice in choices:
if choice[0] == some_int:
label = choice[1]
break;
# label == 'TWO'
Run Code Online (Sandbox Code Playgroud) 在解决方案属性中,我将Configuration设置为"release",用于我唯一的项目.
在主程序的开头,我有这个代码,它显示"Mode = Debug".我也在最顶端有这两行:
#define DEBUG
#define RELEASE
Run Code Online (Sandbox Code Playgroud)
我在测试正确的变量吗?
#if (DEBUG)
Console.WriteLine("Mode=Debug");
#elif (RELEASE)
Console.WriteLine("Mode=Release");
#endif
Run Code Online (Sandbox Code Playgroud)
我的目标是根据调试版本和发布模式为变量设置不同的默认值.
我目前正在使用symfony 1.4,并希望允许用户上传Microsoft Word docx文件.使用下面的sfWidgetFormInputFile小部件和sfValidatorFile,用户可以使用简单的Web表单选择并成功上传他们的docx文件.
$this->widgetSchema['file_name'] = new sfWidgetFormInputFile(array('label' => 'File'));
$this->validatorSchema['file_name'] = new sfValidatorFile(array(
'required' => true,
'path' => sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.sfConfig::get('app_dir_file_sharing').DIRECTORY_SEPARATOR,
'mime_types' => array('application/msword',
'application/vnd.ms-word',
'application/msword',
'application/msword; charset=binary')
), array(
'invalid' => 'Invalid file.',
'required' => 'Select a file to upload.',
'mime_types' => 'The file must be a supported type.'
));
Run Code Online (Sandbox Code Playgroud)
问题是文件上传后,扩展名更改为.zip,文件包含xml文件的文件树.我的理解是,这是因为Office 2007现在使用Open xml文件格式.有没有办法防止这种情况发生使用symfony或PHP?
我发现了很多关于如何使用JavaScript更改div的背景图像的信息,但我尝试使用JavaScript来确定正在显示哪个背景图像.设置图像的代码如下:
document.getElementById("widgetField").style.background="url(includes/images/datepicker_open.png)";
Run Code Online (Sandbox Code Playgroud)
我已经尝试了我能想到的每个组合来访问背景图片网址,但到目前为止还没有骰子:
alert(document.getElementById("widgetField").style.backgroundImage.url); - returns Undefined
alert(document.getElementById("widgetField").style.backgroundImage); - empty response
alert(document.getElementById("widgetField").style.background);
alert(document.getElementById("widgetField").style.background.image);
alert(document.getElementById("widgetField").style.background.url);
alert(document.getElementById("widgetField").style.background.image.url);
alert(document.getElementById("widgetField").style.background.value);
alert(document.getElementById("widgetField").style.background.image.value);
alert(document.getElementById("widgetField").style.background.image.value);
alert(document.getElementById("widgetField").style.backgroundImage.value);
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?可能吗?
顺便说一句,这是在开始时在CSS中设置图像的方式:
#widgetField {
width: 290px;
height: 26px;
background: url(../images/datepicker_closed.png);
overflow: hidden;
position: relative;
}
Run Code Online (Sandbox Code Playgroud)
更新:
如果我运行以下命令,它可以工作:
document.getElementById("widgetField").style.background="url(includes/images/datepicker_open.png)";
alert(document.getElementById("widgetField").style.background);
Run Code Online (Sandbox Code Playgroud)
但是,在JavaScript设置之前,我似乎无法访问URL属性,即使它已在CSS文件中定义.是否有原因无法访问原始CSS设置?
我有一个程序,有时用于使用逗号作为小数分隔符的语言环境.它很好C#处理所有这些(对UI有利),但是当我导出到文件时,我需要始终使用".",而不是特定于语言环境的数字.目前,我这样做:
String.Format("{0:0.####},{1:0.####}",x,y)
问题是在某些语言环境中,最终使用逗号而不是句点.问题是,是否有一个格式代码说"总是使用期限",或者是解决一个人的语言环境的唯一解决方案?
编辑:哇,很多很棒的回复.是的,我使用它作为一个适应度函数来判断遗传算法执行的排序质量.因此,评估成本很重要(即,必须快速,最好O(n).)
作为我正在使用的AI应用程序的一部分,我希望能够根据其单调性(也就是其"排序性")对整数的候选数组进行评级.目前,我正在使用一种计算最长排序运行的启发式算法,然后将其除以数组的长度:
public double monotonicity(int[] array) {
if (array.length == 0) return 1d;
int longestRun = longestSortedRun(array);
return (double) longestRun / (double) array.length;
}
public int longestSortedRun(int[] array) {
if (array.length == 0) return 0;
int longestRun = 1;
int currentRun = 1;
for (int i = 1; i < array.length; i++) {
if (array[i] >= array[i - 1]) {
currentRun++;
} else {
currentRun = 1;
}
if (currentRun > longestRun) longestRun = currentRun;
}
return …Run Code Online (Sandbox Code Playgroud) math artificial-intelligence information-theory genetic-algorithm
我有两个表,每个表都有一个我们称之为widgetid的字段.
我需要在两个表上运行查询,这将从两个表返回单个widgetid列表.
我不知道该怎么做.
我现在拥有的是:
$result = mysql_query("SELECT * FROM `inventory` WHERE find_in_set('$serial', items)") or die(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$widgetid = $row['widgetid'];
//Do Stuff For Each WidgetID
}
Run Code Online (Sandbox Code Playgroud)
现在我需要使用相同的$ serial,并在第二个表中搜索widgetid的列表.但我需要仍然能够在同一个地方"做事",两个小工具列表都是一个
我尽我所能地挖掘了这些互联网,我不能因为我没有找到任何方式轻松地用perforce在当地进行存储或分支.我知道perforce的git包装器,但是从我读过的所有内容来看,它似乎并没有太好开发或可靠.
我有一个字符串变量,其中有多个换行符,我想测试字符串的开头是否与正则表达式匹配.但是,当我使用该^字符时,它会匹配从每个换行符开始的文本.
我希望这匹配:
"foo\nbar" =~ /^foo/
Run Code Online (Sandbox Code Playgroud)
我希望这不匹配
"bar\nfoo" =~ /^foo/
Run Code Online (Sandbox Code Playgroud)
我找不到一个修饰符,使^(或任何其他)字符只匹配字符串的开头.任何帮助非常感谢.
任何人都可以告诉我为什么我在firebug 中检查的setTimeout中丢失内容而我正在丢失内容,尽管我的内容高度正在显示!欢迎所有回复!
function Tableslide_down(content,height)
{
var contentID = document.getElementById(content);
if(contentID.offsetHeight < height)
{
contentID.style.top = parseInt(slidingDiv.style.top) + 10 + "px";
setTimeout("Tableslide_down('"+content+"','"+height+"')",10);
}
contentID.style.display = "block";
}
Run Code Online (Sandbox Code Playgroud)