我最近想到了这个:
func main() {
x, y := 0, 1
x, y = y, x+y
fmt.Println(y)
}
Run Code Online (Sandbox Code Playgroud)
我的想法是:
x, y = y, x+y
Run Code Online (Sandbox Code Playgroud)
与以下相同:
x = y
y = x+y
Run Code Online (Sandbox Code Playgroud)
这将导致最终值 x = 1, y = 2
然而,我得到的最终价值是 x = 1, y = 1
这是为什么?
谢谢.
我正在尝试转换传统地图:
1 -> "YES",
2 -> "NO",
3 -> "YES",
...
Run Code Online (Sandbox Code Playgroud)
具有固定键的地图列表,例如:
[
<number -> 1,
answer -> "YES">,
<number -> 2,
answer -> "NO">,
...
]
Run Code Online (Sandbox Code Playgroud)
现在我有一个看起来不太好的解决方案,并且没有真正利用 Kotlin 的功能特性。我想知道是否有更清晰的解决方案:
fun toListOfMaps(map: Map<Int, String>): List<Map<String, Any>> {
val listOfMaps = emptyList<Map<String, Any>>().toMutableList()
for (entry in map) {
val mapElement = mapOf(
"number" to entry.component1(),
"answer" to entry.component2()
)
listOfMaps.add(mapElement)
}
return listOfMaps
}
Run Code Online (Sandbox Code Playgroud) 我正在开发 Flask 应用程序。它仍然相对较小。我只有一个 app.py 文件,但因为我需要进行数据库迁移,我使用本指南将其分为 3 个:
https://realpython.com/blog/python/flask-by-example-part-2-postgres-sqlalchemy-and-alembic/
但是,我现在无法运行我的应用程序,因为应用程序和模型之间存在循环依赖关系。
应用程序.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import render_template, request, redirect, url_for
import os
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DB_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.debug = True
db = SQLAlchemy(app)
from models import User
... routes ...
if __name__ == "__main__":
app.run()
Run Code Online (Sandbox Code Playgroud)
模型.py:
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username …
Run Code Online (Sandbox Code Playgroud) 我有一个数组:
a = [1, 3, 5, 7, 29 ... 5030, 6000]
Run Code Online (Sandbox Code Playgroud)
此数组是从先前的进程创建的,并且数组的长度可能不同(它取决于用户输入).
我也有一个数组:
b = [3, 15, 67, 78, 138]
Run Code Online (Sandbox Code Playgroud)
(也可能完全不同)
我想使用该数组将数组b
切片a
为多个数组.
更具体地说,我希望结果数组为:
array1 = a[:3]
array2 = a[3:15]
...
arrayn = a[138:]
Run Code Online (Sandbox Code Playgroud)
哪里n = len(b)
.
我的第一个想法是创建一个slices
带维度的二维数组(len(b), something)
.但是我们something
事先并不知道这个,所以我给它分配了值,len(a)
因为它是它可以包含的最大数量.
我有这个代码:
slices = np.zeros((len(b), len(a)))
for i in range(1, len(b)):
slices[i] = a[b[i-1]:b[i]]
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
ValueError: could not broadcast input array from shape (518) into shape (2253412)
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种惯用的方式来返回Kotlin中的变量(如果不为null)。例如,我想要一些东西:
for (item in list) {
getNullableValue(item).? let {
return it
}
}
Run Code Online (Sandbox Code Playgroud)
但是不可能let
在Kotlin的一个街区中返回。
是否有一个好的方法可以做到这一点而不必这样做:
for (item in list) {
val nullableValue = getNullableValue(item)
if (nullableValue != null) {
return nullableValue
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个抽象类,我们称之为A.
abstract class A(private val name: String) {
fun read(key: String): Entity {
...
}
fun write(entity: Entity) {
...
}
abstract val mapper: Mapper<Any>
...
interface Mapper<T> {
fun toEntity(entry: T): Entity
fun fromEntity(entity: Entity): T
}
...
Run Code Online (Sandbox Code Playgroud)
它有一个抽象的映射器.关键是我可以将不同的对象映射到实体并使用read
和write
.
我的孩子班,我们称之为B,结构如下:
class B(private val name: String) : A(name) {
override val mapper = AnimalMapper
object AnimalMapper: Mapper<Animal> {
override fun fromEntity(entity: Entity): Animal {
TODO("not implemented")
}
override fun toEntity(animal: Animal): Entity {
TODO("not implemented")
} …
Run Code Online (Sandbox Code Playgroud) 我们有一个程序,它将一个文件作为输入,然后计算该文件中的行数,但不计算空行数。
Stack Overflow 中已经有一篇关于这个问题的帖子,但这个问题的答案并没有涵盖我。
我们举一个简单的例子。
文件:
I am John\n
I am 22 years old\n
I live in England\n
Run Code Online (Sandbox Code Playgroud)
如果最后一个 '\n' 不存在,那么计数会很容易。我们实际上已经有一个函数可以做到这一点:
/* Reads a file and returns the number of lines in this file. */
uint32_t countLines(FILE *file) {
uint32_t lines = 0;
int32_t c;
while (EOF != (c = fgetc(file))) {
if (c == '\n') {
++lines;
}
}
/* Reset the file pointer to the start of the file */
rewind(file);
return lines;
}
Run Code Online (Sandbox Code Playgroud)
这个函数,当把上面的文件作为输入时,计算了 4 行。但我只想要 3 …
我的界面中有这个方法:
public String getValue();
Run Code Online (Sandbox Code Playgroud)
字符串只是这里的例子,我不想返回一个字符串.
我希望将该方法放在将返回int,chars或bools的类中(取决于类).例如,我有一个实现我的界面的Animal类,并且有这个方法.我希望它在动物中调用此方法时返回一个int.我有另一个具有此方法的类人员,并且当在Person上调用此方法时,我希望它返回一个char.
public class Animal implements myInterface {
@Override
public int getValue() {
return 5;
}
}
public class Person implements myInterface {
@Override
public char getValue() {
return 'c';
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我必须用泛型来做这件事.但我究竟该怎么做呢?
如果我在接口中使用泛型类型替换String,例如:
public <T> T getValue();
Run Code Online (Sandbox Code Playgroud)
然后它说我不能从int(或char)转换为T.
我该怎么办?谢谢.
我正在学习ARM大会,现在我被困在了什么东西上.
我知道链接寄存器,如果我没有错,则保存地址以在函数调用完成时返回.
所以,如果我们有这样的东西(取自ARM文档):
0 | here
1 | B there
2 |
3 | CMP R1, #0
4 | BEQ anotherfunc
5 |
6 | BL sub+rom ; Call subroutine at computed address.
Run Code Online (Sandbox Code Playgroud)
那么,如果我们将左边的列视为每条指令的地址,那么在地址1的B之后,链接寄存器保持值为1对吗?
然后程序转到那里的方法然后它使用链接寄存器的值来知道返回的位置.
如果我们现在跳到地址6,我被卡住,我们知道BL将下一条指令的地址复制到lr(r14,链接寄存器).
所以现在它会复制sub的地址,这是一个子程序(什么是子程序?)+ rom(这是一个数字?)或sub + rom的地址(我不知道这可能是什么).
但总的来说,我们什么时候需要BL?为什么我们在上面的例子中想要它?有人能给我一个我们真正需要它的例子吗?
谢谢!
我正在制作一个小程序,将"你是一个成年人吗?"这个问题的答案作为输入.像这样的角色:
bool adult() {
char answer;
do {
printf("Are you an adult? [y/n]\n");
answer = getchar();
} while (!(answer == 'y' || answer == 'n'));
return (answer == 'y');
}
Run Code Online (Sandbox Code Playgroud)
我的目标是,如果答案既不是y,那么问题应该重复.但这似乎有一个错误:
当我回答其他内容(y或n)时,问题会被打印两次:
你是成年人吗?你是成年人吗?你是成年人吗?...
为什么会这样?另外,我尝试使用scanf而不是getchar的相同方法,但是存在相同的错误.对于这种程序,我应该使用scanf或getchar吗?为什么?
谢谢!