由于python中没有显式输入,我希望能够使用命名约定区分序列和非序列.我已经用python编程了一段时间,我仍然没有找到任何逻辑/实用的方法来命名序列.当然,我经历了着名的PEP8,并对谷歌做了一些研究,似乎接受的惯例是在变量名的末尾添加字母"s".
假设我们有一系列"权重值",因此序列的变量名称应该是权重.到目前为止,这很好,但是有些情况下,某些单词以"s"结尾,恰好是命名变量的更合理的方式,而不是序列.或者,假设您将权重序列存储在序列中."s"命名约定将命名变量weightss,这很难看.我确信序列的命名约定更好.
您建议使用什么序列命名约定?
我正在寻找关于动态编程问题的一些指示.我找不到任何有关如何解决此类问题的相关信息.我知道如何使用动态编程解决的唯一问题是当我有两个序列并创建这些序列的矩阵时.但我不知道如何将其应用于以下问题......
如果我有一个集合A = {7,11,33,71,111}和一个数字B.那么作为A的子集的C包含来自A的元素,它构建了和B.
例:
A = {7,11,33,71,111}
If B = 18, then C = {7,11} (because 7+11 = 18)
If B = 3, then there is no solution
Run Code Online (Sandbox Code Playgroud)
感谢这里的任何帮助,我只是不知道在解决这些问题时如何思考.我也找不到任何一般方法,只有基因序列的一些例子和类似的东西.
报告二进制序列时,空间是多余的.这段代码
x <- '1 0 0 0 0 0 1 1 0 1 0 1 1 0 '
y<-gsub(' +', '', x)
Run Code Online (Sandbox Code Playgroud)
完成这项工作,以便我可以从R复制和粘贴.如何对其他格式的0-1序列(和其他一位数据)执行相同的操作,例如,
x <- c(1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0)
Run Code Online (Sandbox Code Playgroud)
要么
toString(x)
Run Code Online (Sandbox Code Playgroud)
或者其他什么(为了学习各种选择)?谢谢.
假设我有一个数据列表:{1,2,3,4,5,6,7,8,9,10}其中n = 10个元素
我想随机选择这个集合的k个元素来形成一个子列表,比如k = 5.
在那种情况下,我最终会得到一个看起来像{9,3,5,2,7}的子列表
我能做到这一点:
这个问题是,随着原始列表的增长,偏移量和删除时间也会增长,对于任何非常大的列表(例如超过1,000,000个元素),执行此算法需要相当长的时间.
有没有更快的方法从给定数据列表生成随机序列?应该为这个问题留出随机数发生器的实现,而是关注如何在提出的算法中使用RNG结果.
有什么想法吗?
现在我正在使用C++ STL列表
你会如何在Python中生成以下字符串序列?
00:00:00
00:00:07
00:00:14
00:00:21
...
00:00:49
00:00:56
00:01:03
Run Code Online (Sandbox Code Playgroud)
这一步是7秒.结束时间约为03:30:+/-
我会使用模块化算法的解决方案(首先1200小时,60小时有分钟,余数是秒,数字应转换为字符串,"一位"字符串应以"0"为前缀).
是否有一些更智能(pythonic)的解决方案,在标准库或列表理解中使用一些辅助生成器?
我想制作一组从x到20的序列,x = c(2:19).基本上我想要这个,但不必这样做:
a = seq(2, 20)
b = seq(3, 20)
...
q = seq(18, 20)
r = seq(19, 20)
> a
[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> b
[1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
...
> q
[1] 18 19 20
> r
[1] 19 20`
Run Code Online (Sandbox Code Playgroud)
我尝试使用for循环,但我无法让替换工作:
a = c(2:20)
b …Run Code Online (Sandbox Code Playgroud) 以下示例显示了我们遇到的问题Data.Sequence:
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.Sequence as S
import Data.Sequence ((|>), ViewL(..))
import Data.List (foldl')
import GHC.AssertNF
update !init !x = init |> x
main =
do let !seq = foldl' update S.empty [1..10]
assertNF seq
Run Code Online (Sandbox Code Playgroud)
它打印
Parameter not in normal form: 1 thunks found:
Deep (One (S# 1)) (_thunk (Deep (One ...) (_thunk ... ... ... ...) (Three ... ... ...) 93) (S# 95) (S# 96) (S# 97)) (Three (S# 98) (S# …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用递归定义来实现Hofstadter的Q序列:
Q(1) = 1
Q(2) = 1
Q(n) = Q(n - Q(n-2)) + Q(n - Q(n-1)) for n > 2
Run Code Online (Sandbox Code Playgroud)
我得到的错误结果n > 3。这是我到目前为止的内容:
: Q recursive
dup 3 <
if
drop 1
else
dup dup 2dup 2 - Q - Q -rot 1- Q - Q +
then ;
Run Code Online (Sandbox Code Playgroud)
在线尝试: http : //ideone.com/PmnJRO(编辑:现在具有固定,正确的实现)
我认为这是行不通的,因为每次调用Qwhere n大于之后,都会向堆栈中添加值2,从而使-rot行不通。
是否有一个简单的调整即可完成这项工作?还是我需要使用其他方法,也许将变量用于n?
OEIS:A005185
假设我有一串小写字母,例如
'ablccmdnneofffpg'
Run Code Online (Sandbox Code Playgroud)
我的目标是找到这个字符串中连续数字的最长序列,在这种情况下是:
'abcdefg'
Run Code Online (Sandbox Code Playgroud)
直观的尝试是在每个字母周围找到循环并从该字母开始获得最长的序列.一种可能的解决方案是
longest_length = 0
start = None
current_start = 0
while current_start < len(word) - longest_length:
current_length = 1
last_in_sequence = ord(word[current_start])
for i in range(current_start + 1, len(word)):
if ord(word[i]) - last_in_sequence == 1:
current_length += 1
last_in_sequence = ord(word[i])
if current_length > longest_length:
longest_length = current_length
start = current_start
while (current_start < len(word) - 1 and
ord(word[current_start + 1]) - ord(word[current_start]) == 1):
current_start += 1
current_start += 1
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以用更少的线来解决问题,甚至使用一些pythonic方法?
我的R课程有一个工作簿问题,我无法弄明白.我需要"编写一个使用rep()的R命令来创建一个带元素的向量1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7"
它似乎是1至4的重复序列,重复4次,并且在每次重复时将1加到起始元素上.我对R很新,所以我很难过.任何帮助,将不胜感激.