假设我们有一个名为 nums 的数组。我们需要找到数组中所有元素的乘积结果的符号。因此,如果输入类似于 nums = [-2, 3, 6, -9, 2, -4],则输出将为负数,因为乘积结果为 -2592。为了解决这个问题,我们将遵循以下步骤 −zeroes := 0, negatives := 0for each i in nums, doif i is same as 0, thenzeroes := zeroes + 1if i < 0, thennegatives := negatives + 1if zeroes > 0 , thenreturn "Zero"otherwise when negatives mod 2 is same as 0, thenreturn "Positive"otherwise, ... 阅读更多
假设我们有一个句子 s,其中包含一些英文单词,这些单词由单个空格分隔,没有前导或尾随空格。我们还有另一个值 k。我们需要在截断后找到前 k 个单词。因此,如果输入类似于 s = "Coding challenges are really helpful for students" k = 5,则输出将为 True(参见图片)。为了解决这个问题,我们将遵循以下步骤 −words := split s by spacesjoin first k letters from words array by separating spaces and return让我们看看以下实现以更好地理解 ... 阅读更多
假设我们有一个棋盘坐标,它是一个字符串,表示棋盘的行和列的坐标。下面是一个供您参考的棋盘。我们需要检查给定单元格是否为白色,如果是白色则返回 true,否则返回 false。因此,如果输入类似于 coordinate = "f5",则输出将为 True(参见图片)。为了解决这个问题,我们将遵循以下步骤 −if ASCII of coordinate[0] mod 2 is same coordinate[1]) mod 2, thenreturn Falseotherwise, return True让我们看看以下实现以更好地理解 −示例实时演示def solve(coordinate): if (ord(coordinate[0]))%2 == ... 阅读更多
假设我们有一个字母数字字符串 s,我们需要找到出现在 s 中的第二大数字,如果没有这样的字符串,则返回 -1。因此,如果输入类似于 s = "p84t3ho1n",则输出将为 4,因为数字为 [1,3,4,8],所以第二大数字为 4。为了解决这个问题,我们将遵循以下步骤 −lst := a new setfor each let in s, doif let is not alphabetic, theninsert let as integer in lstif size of lst