我发现自己遇到了一个面试问题,其目标是编写一个排序算法,对一系列未排序的int值进行排序:
int[] unsortedArray = { 9, 6, 3, 1, 5, 8, 4, 2, 7, 0 };
Run Code Online (Sandbox Code Playgroud)
现在我用Google搜索并发现那里有很多排序算法!最后,我可以激励自己深入研究Bubble Sort,因为它看起来很简单.
我阅读了示例代码并找到了一个如下所示的解决方案:
static int[] BubbleSort(ref int[] array)
{
long lastItemLocation = array.Length - 1;
int temp;
bool swapped;
do
{
swapped = false;
for (int itemLocationCounter = 0; itemLocationCounter < lastItemLocation; itemLocationCounter++)
{
if (array[itemLocationCounter] > array[itemLocationCounter + 1])
{
temp = array[itemLocationCounter];
array[itemLocationCounter] = array[itemLocationCounter + 1];
array[itemLocationCounter + 1] = temp;
swapped = true;
} …Run Code Online (Sandbox Code Playgroud) programming-languages while-loop conditional-statements c#-4.0
我正在尝试使用app引擎模板和html表生成一个板(10X10).所以这意味着在循环的10次迭代之后基本上是休息.如何使用app引擎的内置模板引擎(django 0.96)来解决这个问题?
卢克斯的回答解决了我自动插入休息的问题.
但是我仍然需要找到一种方法来检查每个数字是否存在于列表中并给它一个特定的类.有没有办法实现这样的事情:
{% for number in list }
<td {% if number in another_list %}class="special"{% endif %}>{{number}}</td>
{% endfor }
Run Code Online (Sandbox Code Playgroud) python google-app-engine loops django-templates conditional-statements
我正在尝试检索上传到我网站的最新图片,但是当他们在私人图库,活动或用户被删除时却没有.我需要对0或null进行这些检查.
我环顾四周,发现了一些做多个和多个或多个但不在一起的方法.它似乎看起来正确但根本不起作用.
$conditions = array(
'AND' => array(
array(
'OR' => array(
'Event.private' => null,
'Event.private' => 0
)
),
array(
'OR' => array(
'Gallery.private' => null,
'Gallery.private' => 0
)
),
array(
'OR' => array(
'User.deleted' => null,
'User.deleted' => 0
)
)
)
);
$images = $this->Image->find('all',array(
'order'=>array('Image.id'=>'desc'),
'limit'=>$limit,
'group'=>'Image.id',
'offset'=>$offset,
'conditions'=>$conditions)
);
Run Code Online (Sandbox Code Playgroud) 此代码应在O(n)线性时间内找到列表的模式.我想把它变成一个列表理解,因为我在教自己Python,并且我正在努力提高我的列表理解能力.
这些都是提供信息,但没有真正回答我的问题:
我遇到的问题是嵌套if和try/except.我确信这是一个简单的问题,所以初级Python程序员可能很快得到答案.
def mode(L):
# your code here
d = {}; mode = 0; freq = 0
for j in L:
try:
d[j] += 1
if d[j] > freq:
mode = j; freq = d[j]
except(KeyError): d[j] = 1
return mode
Run Code Online (Sandbox Code Playgroud)
请注意,L参数是这样的int列表:
L = [3,4,1,20,102,3,5,67,39,10,1,4,34,1,6,107,99]
Run Code Online (Sandbox Code Playgroud)
我想的是:
[try (d[j] += 1) if d[j] > freq (mode = j; freq = d[j]) except(KeyError): d[j] = 1 for j in L]
Run Code Online (Sandbox Code Playgroud)
但我没有足够的胶带来修复语法错误的程度.
python list-comprehension try-catch conditional-statements python-2.7
我有这样的脚本
$number = range(0, 9);
Run Code Online (Sandbox Code Playgroud)
当我有这样的条件
if (in_array('@', $number) === true) {
echo "true";
}else "false";
Run Code Online (Sandbox Code Playgroud)
并输出:
true
Run Code Online (Sandbox Code Playgroud)
而我的问题是为什么这些符号与数组$ number中的任何数字相同?我希望符号只是符号而不是数字.
例子我想要这样
if (in_array('@', $number) === true) {
echo "true";
}else "false";
Run Code Online (Sandbox Code Playgroud)
输出:
false
Run Code Online (Sandbox Code Playgroud) 在阅读一本面向初学者的Java书籍时,我偶然发现了一个练习:
编写一个程序,该程序
int从命令行获取三个值并按升序打印它们。使用Math.min()和Math.max()。
问题是if尚未考虑条件条件,因此从逻辑上讲我不能使用它们。
我试图对C中的类似问题使用答案,但遇到not a statement错误。
public class three_sort
{
public static void main(String[] args)
{
int a = Integer.parseInt( args[0] );
int b = Integer.parseInt( args[1] );
int c = Integer.parseInt( args[2] );
int min = a;
(min > b) && (min = b); //finding minimum
(min > c) && (min = c);
System.out.println(min);
int i = a;
(b < max) && (b > min) && (i …Run Code Online (Sandbox Code Playgroud) 为什么这个当前的多条件if语句不能创建没有元音的新字符串?
public class Disemvowel {
public static void main(String[] args) {
System.out.println("Please enter a word: ");
Scanner stdin = new Scanner(System.in);
String word = stdin.next();
int wordLength = word.length();
String disemvoweledWord = "";
for (int i=0;i<=(wordLength-1);i++){
char currentLetter = word.charAt(i);
System.out.println(currentLetter);
if (currentLetter!='a' || currentLetter!='e' || currentLetter!='i' || currentLetter!='o' || currentLetter!='u'){
disemvoweledWord += currentLetter;
}
}
System.out.println(disemvoweledWord);
}
}
Run Code Online (Sandbox Code Playgroud) 我是python的新手.在C/C++中,在while或if语句中,我经常进行变量赋值.以下是C++中的一个简单示例代码:
#include <iostream>
int Increment(const int x) {
return (x + 1);
}
int main(void) {
int x = 2, y;
while ((y = Increment(x)) > 2) {
std::cout << "y is larger than 2" << std::endl;
}
return (0);
}
Run Code Online (Sandbox Code Playgroud)
但是以下python代码不起作用:
#!/usr/bin/python
def Increment(x):
return x + 1
def main():
x= 2
while (y = Increment(x)) > 2:
print "y is larger than 2"
if __name__ == "__main__"
main()
Run Code Online (Sandbox Code Playgroud)
错误消息如下:
while (y = Increment(x)) > 2:
^
SyntaxError: invalid …Run Code Online (Sandbox Code Playgroud) 我有这个代码
if(beforeModify.get(i).equals("a")|beforeModify.get(i).equals("e")|beforeModify.get(i).equals("i")|
beforeModify.get(i).equals("o")|beforeModify.get(i).equals("u")|beforeModify.get(i).equals("á")|
beforeModify.get(i).equals("é")|beforeModify.get(i).equals("í")|beforeModify.get(i).equals("?")|
beforeModify.get(i).equals("y")|beforeModify.get(i).equals("ý")|beforeModify.get(i).equals("?")|
beforeModify.get(i).equals("ú"))
Run Code Online (Sandbox Code Playgroud)
我可以做得更好吗?
好的,大家好!这个switch语句永远注定是行不通的。
最初的想法是创建一个提示变量x,用户必须选择输入任何数字,而该数字就是x的值。
然后,在第一种情况下,如果x小于0.5,则它将只是console.log“ less”。如果x大于0.5,它将只是console.log“ more”。如果由于某种原因程序无法按预期运行,则默认值为console.log“这是默认值”
然后我最后添加了一个x的console.log,只是想知道用户输入了什么数字。
让我们尝试一下!
我试了又试,无论我输入什么数字,总是打印“这是默认值”。然后打印x的值。
我最终去了Rambo并删除了提示,并声明x为0.6。它应该打印“更多”,但仍然不会。
var x = 0.6;
switch (x) {
case x < 0.5:
console.log("less");
break;
case x > 0.5:
console.log("more");
break;
default:
console.log("its the dflt");
};
console.log(x);Run Code Online (Sandbox Code Playgroud)
所以我想知道这段代码有什么问题。救命
java ×3
python ×3
if-statement ×2
php ×2
algorithm ×1
arrays ×1
c#-4.0 ×1
cakephp ×1
javascript ×1
loops ×1
python-2.7 ×1
try-catch ×1
while-loop ×1