我有以下 Excel 文件:
在 Python 中,它看起来像这样:
如何转换数据,以便名称列中的数字形成另一列?
或者使用正则表达式进行排序,以便对于每个名称都满足第 8、9 和 10 个月内有访问的条件?
我发现了这个实现循环队列的例子,但我不太明白 的含义 if((self.tail + 1) % self.size == self.head),如果你可以使用队列大小:(self.tail == self.size - 1)或者也可以在增加变量的(self.tail < self.size)情况下更改它。也许有一些关于数组溢出的主题,我只是不太明白如何进行调试,因为我使用ide并且不明白如何以常规方式进行调试。elsetailAtomcmd
import random
class MyCircularQueue():
def __init__(self, size):
self.size = size
self.queue = [None] * size
self.head = self.tail = -1
def enqueue(self, data):
if((self.tail + 1) % self.size == self.head):
print("The queue is full\n")
elif (self.head) == -1:
self.head = 0
self.tail = 0
self.queue[self.tail] = data
else:
self.tail = (self.tail +1) % self.size
self.queue[self.tail] = data
def dequeue(self):
if(self.head == -1):
print("The queue is empty\n")
elif(self.head == self.tail):
temp = self.queue[self.head]
self.head = -1
self.tail = -1
return temp
else:
temp = self.queue[self.head]
self.head = (self.head + 1) % self.size
return temp
def printqueue(self):
if(self.head == -1):
print("Is empty, i cant print\n")
elif(self.tail >= self.head):
for i in range(self.head, self.tail + 1):
print(self.queue[i], end = " ")
print()
else:
for i in range(self.head, self.size):
print(self.queue[i], end = " ")
for i in range(0, self.tail + 1):
print(self.queue[i], end = " ")
print()
que = MyCircularQueue(5)
for x in range(que.size):
que.enqueue(random.randint(1, 10))
que.printqueue()
UPD:我将条件更改为self.tail == self.size - 1,并将增量设置为self.tail。显示队列后,发现没有添加新元素,并显示队列已满的消息。我决定更改输出,不显示元素本身,而是显示其在队列中的索引。事实证明,删除是从左到右进行的,添加是从右侧进行的。
Before Delete: 0 1 2 3 4
After Delete: 1 2 3 4
After additional number: 1 2 3 4 0
事实证明,如果您设置增量,则它tail不会重置,并且始终具有适合新条件的最后一个值。
UPD2:在本文中找到了方法实现的示例isEmpty()
我正在尝试在底部导航上制作一个简单的动画
这是代码:
@Composable
private fun BottomNavImpl() {
Scaffold(
bottomBar = {
var isBottomBarVisible: Boolean by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
delay(2000)
isBottomBarVisible = true
}
AnimatedVisibility(
visible = isBottomBarVisible,
enter = slideInVertically(
initialOffsetY = { it },
animationSpec = tween(
durationMillis = 500,
easing = FastOutSlowInEasing
)
),
exit = slideOutVertically(
targetOffsetY = { it },
animationSpec = tween(
durationMillis = 300,
easing = FastOutSlowInEasing
)
)
) {
NavigationBar(
containerColor = Color.Red,
) {
repeat(3) {
NavigationBarItem(
icon = {
Icon(
modifier = Modifier,
painter = painterResource(android.R.drawable.ic_menu_add),
contentDescription = null,
)
},
selected = false,
onClick = { }
)
}
}
}
},
content = { paddingValues ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.background(Color.Green)
) {
}
}
)
}
这是结果
问题是在动画过程中,后面出现白色背景
这应该不会太困难,但由于某种原因,重组经历了两次,这使得添加仅第一次执行动画的条件变得困难。
一般来说,我想像这个例子那样做,但我改变了一点,但本质是一样的 - https://yasincamaz.medium.com/simple-item-animation-with-jetpack-composes-lazygrid -78316992af22
网格元素出现气泡效果是必要的
这是代码
private val dataSet: List<String> = listOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")
private val data: List<String> = List(5) { dataSet }.flatten()
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
Test_delete_itTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Gallery(
paddingValues = innerPadding,
uiConfig = { data }
)
}
}
}
}
}
@Composable
private fun Gallery(
paddingValues: PaddingValues,
uiConfig: () -> List<String>
) {
val config: List<String> = uiConfig()
val columns = 2
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
LazyVerticalGrid(
columns = GridCells.Fixed(columns),
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
content = {
items(config.size) { idx ->
val item: String = config[idx]
val (scale, alpha) = scaleAndAlpha(idx, columns)
MyItem(
modifier = Modifier.graphicsLayer(alpha = alpha, scaleX = scale, scaleY = scale),
text = item
)
}
}
)
}
}
@Composable
private fun MyItem(
modifier: Modifier = Modifier,
text: String
) {
Card(
modifier = modifier.height(150.dp),
shape = RoundedCornerShape(16.dp),
elevation = CardDefaults.cardElevation(8.dp),
colors = CardDefaults.cardColors(
containerColor = Color.Blue,
)
) {
Box(
modifier = Modifier
.weight(1f)
.height(150.dp)
.clip(RoundedCornerShape(16.dp))
) {
Text(
text = text,
color = Color.White
)
}
}
}
@Immutable
private enum class State { PLACING, PLACED }
@Immutable
data class ScaleAndAlphaArgs(
val fromScale: Float,
val toScale: Float,
val fromAlpha: Float,
val toAlpha: Float
)
@OptIn(ExperimentalTransitionApi::class)
@Composable
fun scaleAndAlpha(
args: ScaleAndAlphaArgs,
animation: FiniteAnimationSpec<Float>
): Pair<Float, Float> {
val transitionState = remember { MutableTransitionState(State.PLACING).apply { targetState = State.PLACED } }
val transition = rememberTransition(transitionState, label = "")
val alpha by transition.animateFloat(transitionSpec = { animation }, label = "") {
if (it == State.PLACING) args.fromAlpha else args.toAlpha
}
val scale by transition.animateFloat(transitionSpec = { animation }, label = "") {
if (it == State.PLACING) args.fromScale else args.toScale
}
return alpha to scale
}
val scaleAndAlpha: @Composable (idx: Int, columns: Int) -> Pair<Float, Float> = { idx, columns ->
scaleAndAlpha(
args = ScaleAndAlphaArgs(2f, 1f, 0f, 1f),
animation = tween(300, delayMillis = (idx / columns) * 100)
)
}
您可以尝试将Gallery其更改为此以跟踪该元素是否已向用户显示
@Composable
private fun Gallery(
paddingValues: PaddingValues,
uiConfig: () -> List<String>
) {
val config: List<String> = uiConfig()
val columns = 2
// Remember a set of already animated indices
val animatedIndices = remember { mutableSetOf<Int>() }
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
LazyVerticalGrid(
columns = GridCells.Fixed(columns),
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
content = {
items(config.size) { idx ->
val item: String = config[idx]
// Determine if the item should animate
val shouldAnimate = !animatedIndices.contains(idx)
// If it should animate, mark it as animated
if (shouldAnimate) {
animatedIndices.add(idx)
}
val (scale, alpha) = if (shouldAnimate) {
scaleAndAlpha(idx, columns)
} else {
1f to 1f // No animation
}
MyItem(
modifier = Modifier.graphicsLayer(alpha = alpha, scaleX = scale, scaleY = scale),
text = item
)
}
}
)
}
}
但问题是,这里每个元素都会调用两次重组 -items(config.size) { idx ->因此,该元素出现在屏幕上就好像没有动画一样
我在这里缺少什么?
我决定训练自己解决各种面试问题的能力,并且全面地对待一切:composer、PHPUnit...
看起来一切都做得正确,无论是结构还是嵌套。但是当我运行测试时,消息是:调用未定义的函数。
同时:在IDE中编写代码时,用函数代替它们,参数的描述取自PHPDoc结构。当通过 required 连接时 - 一切正常,但这是“错误的”。
现在我们来具体说明一下。
“项目”结构
PHPInterviewTasks/
├── src/
│ ├── phpZoneTasks/
│ │ └── LargestPossibleNumberFromOthers.php
├── tests/
│ ├── phpZoneTasksTests/
│ │ └── LargestPossibleNumberFromOthersTest.php
├── vendor/
├── composer.json
└── phpunit.xml
LargestPossibleNumberFromOthers.php:
<?php
namespace PHPInterviewTasks\phpZoneTasks;
function getLargeNumberFromOthers (string $line): string
{
$tmp_array = explode(" ", $line);
usort($tmp_array, function($p1, $p2) {
$order1 = $p1 . $p2;
$order2 = $p2 . $p1;
return $order2 <=> $order1;
});
return implode('', $tmp_array);
}
包含测试 LargestPossibleNumberFromOthersTest.php 的文件:
<?php
use PHPUnit\Framework\TestCase;
//use function InterviewTasks\phpZoneTasks\getLargeNumberFromOthers;
use function \PHPInterviewTasks\phpZoneTasks\getLargeNumberFromOthers;
use \PHPInterviewTasks\phpZoneTasks;
//require_once __DIR__ . '/../../src/phpZoneTasks/LargestPossibleNumberFromOthers.php';
class LargestPossibleNumberFromOthersTest extends TestCase {
public function testGetLargeNumberFromOthers()
{
$this->assertEquals("9958142211100", getLargeNumberFromOthersAlias("100 95 9 2 42 11 81"));
}
}
PS 注释的使用选项显示了一些实验。
好吧,composer.json
{
"name": "php-projects/interview-tasks",
"description": "Examples, Solutions for interview",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "vitaly_root",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
"PHPInterviewTasks\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PHPInterviewTasks\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit"
}
}
2 人工智能系统已经在愚蠢地原地踏步了。我将很感激能有一个工作版本能够将这种耻辱转化为正确的形式。