我是Python的初学者,自学Google代码大学.我把这个问题作为练习,并且能够使用下面显示的解决方案解决它:
# F. front_back
# Consider dividing a string into two halves.
# If the length is even, the front and back halves are the same length.
# If the length is odd, we'll say that the extra char goes in the front half.
# e.g. 'abcde', the front half is 'abc', the back half 'de'.
# Given 2 strings, a and b, return a string of the form
# a-front + b-front + a-back + b-back
def front_back(a, b): …Run Code Online (Sandbox Code Playgroud) 我想知道是否有更简单的方法来编写if语句的条件.有点像item1, item2 == "wood":.我目前有这个代码:
item1 = input("item1: ")
item2 = input("item2: ")
if item1 == "wood" and item2 == "wood":
print("You created a stick")
Run Code Online (Sandbox Code Playgroud) 我试图简化代码:
header = []
header.append(header1)
header.append(header2)
header.append(header3)
header.append(header4)
header.append(header5)
header.append(header6)
Run Code Online (Sandbox Code Playgroud)
哪里:
header1 = str(input.headerOut1)
header2 = str(input.headerOut2)
header3 = str(input.headerOut3)
header4 = str(input.headerOut4)
header5 = str(input.headerOut5)
header6 = str(input.headerOut6)
Run Code Online (Sandbox Code Playgroud)
我本来想使用for循环,比如:
headerList = []
for i in range(6)
headerList.append(header+i)
Run Code Online (Sandbox Code Playgroud)
但是,python不会识别标头+ i代表字符串header1.有没有办法简化这个代码或让for循环工作?非常感谢!
我有多个div,我必须为每个div编写这样的代码,并让php通过循环$ NBR ++来改变id; 在末尾.有没有更快的方法来写这一次,而不是每个div都有这个独特的?
// when $NBR = 1
<div class="playbutton<?php echo $NBR?>">
<script>
$(".playbutton<?php echo $NBR?>").click(function()
{ $(".playbutton<?php echo $NBR?>").hide();
$('#songpicture').attr("src", "<?php echo $thumb[$NBR]?>");
});
</script>
</div>
// when $NBR = 2
<div class="playbutton<?php echo $NBR?>">
<script>
$(".playbutton<?php echo $NBR?>").click(function()
{ $(".playbutton<?php echo $NBR?>").hide();
$('#songpicture').attr("src", "<?php echo $thumb[$NBR]?>");
});
</script>
</div>
Run Code Online (Sandbox Code Playgroud)
这持续15次......等.....所有的PHP变量都是用PHP设置在每个div的开头.
那么有没有一种方法可以将其"压缩"为一个可以写一次的代码?或者必须在每个div之后写出?
谢谢你的帮助!
我想简化一个布尔表达式.
表达式是这样的
X1 xor (X2 || X3 && X4 || x5)
Run Code Online (Sandbox Code Playgroud)
如何使用布尔代数规则简化此表达式.
此外,我想将上面的布尔表达式转换为CNF格式,所以我该怎么做.
我是编程和尝试简化一些jquery/javascript代码的新手,但我没有取得多大成功.我试图调整这里找到的解决方案:jQuery Simplify
......但又一次,没有成功.
这是我的jquery代码:
<script>
$(document).ready(function(){
$("#hide01").click(function(){
$(".content01").hide();
});
$("#show01").click(function(){
$("p").hide();
$(".content01").show();
});
$("#hide02").click(function(){
$(".content02").hide();
});
$("#show02").click(function(){
$("p").hide();
$(".content02").show();
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
和HTML:
<button id="hide01">Hide</button>
<button id="show01">Show</button>
<button id="hide02">Hide</button>
<button id="show02">Show</button>
<p class="content01">Content 01</p>
<p class="content02">Content 02</p>
Run Code Online (Sandbox Code Playgroud)
这个解决方案正在运行,但我需要40个按钮/内容块......
有人可以帮忙吗?谢谢!
我一直在我的程序中使用以下代码来设置轴的范围,以便图形看起来更美观.
plot.set_ylim([0,a+(a*15/100)])
Run Code Online (Sandbox Code Playgroud)
具体是这样的:
a+(a*15/100)
Run Code Online (Sandbox Code Playgroud)
我很感兴趣
是否存在简化此功能的功能?
原因是当我的图形在for循环中创建,并且a的值是列表的最大值(等等)时,整个事情开始变得混乱.例如:
a+(a*15/100)
max(listA[x])+(max(listA[x]))*15/100
Run Code Online (Sandbox Code Playgroud)
有人知道简化吗?
如何简化这三个简单的条件?
if(v.x < 0)
{
v.x *= -1;
}
if(v.y < 0)
{
v.y *= -1;
}
if(v.z < 0)
{
v.z *= -1;
}
Run Code Online (Sandbox Code Playgroud) 考虑以下示例,对于指定奇数N = 51 的用户,应看到以下输出:
A = [1 4 2 5 3];
为了N = 7: A = [1 5 2 6 3 7 4];
奇数的更一般形式N:
A = [1 ceil(N/2+1) 2 ceil(N/2+2) ... N ceil(N/2)];
我是这样解决这个问题的
A = zeros(1, N);
a1 = 1:ceil(N/2): a2 = ceil(N/2+1):N;
j = 1; k = 1;
for i = 1:N
if rem(i, 2) ~= 0
A(i) = a1(j);
j = j+1;
else
A(i) = a2(k);
k = k+1;
end
end …Run Code Online (Sandbox Code Playgroud) 我正在制作一个简单的 C# 控制台应用程序,用户必须输入 1 或 2 来选择他们的选项,显然,由于用户可以输入任何内容,我需要进行检查以返回他们的输入,如果不是如果不是 1 或 2,它将返回 null。
这是我做的
bool? getResponse = null;
if (read == "1")
{
getResponse = true;
}
else if (read == "2")
{
getResponse = false;
}
else
{
getResponse = null;
}
Run Code Online (Sandbox Code Playgroud)
了解 C#,肯定有一种方法可以简化此操作,但我似乎找不到在线方法。有什么指点吗?
simplify ×10
python ×4
if-statement ×2
jquery ×2
arrays ×1
boolean ×1
c ×1
c# ×1
expression ×1
for-loop ×1
html ×1
javascript ×1
loops ×1
math ×1
matlab ×1
optimization ×1
percentage ×1
python-3.x ×1
string ×1