我有一个包含物品的商店.每个项目都是一个组件(它是一个组件)或一个由各种组件组成的产品(但从不包含2个或更多相同的组件).
现在,当我想要从商店购买产品时,有各种各样的场景:
下面你可以看到我的代码到目前为止(getAssemblyPath
).如果可能的话,它确实找到了组装所需项目的方法,但它没有优化组装路径.
我想以两种方式优化路径:
现在,我完全失去了如何完成这项优化(我甚至不确定这是SO或数学的问题).
如何更改getAssemblyPath
以满足我的优化要求?
我的代码到目前为止:
#! /usr/bin/python
class Component:
def __init__ (self, name): self.__name = name
def __repr__ (self): return 'Component {}'.format (self.__name)
class Product:
def __init__ (self, name, components):
self.__name = name
self.__components = components
@property
def components (self): return self.__components
def __repr__ (self): return 'Product {}'.format (self.__name)
class Store:
def __init__ (self): self.__items = {}
def __iadd__ (self, item):
item, count = item
if not item …
Run Code Online (Sandbox Code Playgroud) 我刚刚开始,pyparsing
我遇到了换行问题.
我的语法是:
from pyparsing import *
newline = LineEnd () #Literal ('\n').leaveWhitespace ()
minus = Literal ('-')
plus = Literal ('+')
lparen = Literal ('(')
rparen = Literal (')')
ident = Word (alphas)
integer = Word (nums)
arith = Forward ()
parenthized = Group (lparen + arith + rparen)
atom = ident | integer | parenthized
factor = ZeroOrMore (minus | plus) + atom
arith << (ZeroOrMore (factor + (minus | plus) ) + factor)
statement = arith …
Run Code Online (Sandbox Code Playgroud) Pyparsing对于非常小的语法工作得很好,但随着语法的增长,性能下降并且通过屋顶使用内存.
我目前的gramar是:
newline = LineEnd ()
minus = Literal ('-')
plus = Literal ('+')
star = Literal ('*')
dash = Literal ('/')
dashdash = Literal ('//')
percent = Literal ('%')
starstar = Literal ('**')
lparen = Literal ('(')
rparen = Literal (')')
dot = Literal ('.')
comma = Literal (',')
eq = Literal ('=')
eqeq = Literal ('==')
lt = Literal ('<')
gt = Literal ('>')
le = Literal ('<=')
ge = Literal ('>=')
not_ = Keyword ('not')
and_ = …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个Haskell函数,它返回给定正则表达式的所有匹配的捕获组.
我一直在看Text.Regex,但在那里找不到任何东西.
现在我使用这个似乎有效的解决方法:
import Text.Regex
findNext :: String -> Maybe (String, String, String, [String] ) -> [ [String] ]
findNext pattern Nothing = []
findNext pattern (Just (_, _, rest, matches) ) =
case matches of
[] -> (findNext pattern res)
_ -> [matches] ++ (findNext pattern res)
where res = matchRegexAll (mkRegex pattern) rest
findAll :: String -> String -> [ [String] ]
findAll pattern str = findNext pattern (Just ("", "", str, [] ) )
Run Code Online (Sandbox Code Playgroud)
结果: …
我想和我的调制解调器通话。它作为/ dev / ttyUSB挂载,可以完美理解AT命令。
我可以从带有标准文件模块的设备上读写吗?
波特率,字节大小,奇偶校验,RTS / CTS,DSR / DTR等如何?
您对tonyg-erlang-serial-1.0有任何经验吗?(我对这个程序包不太满意,因为它在自述文件中说:“这是一个带有erlang驱动程序的用于串行通信的端口程序,最初由Johan Bevemyr在1996年编写,并由Tony Garnock-Jones从2007年开始偶尔维护。”
erlang中串行I / O的常见做法是什么?
如何将函数作为参数传递?
基本的想法是这样的(这是行不通的):
?R ? double a
R ? 2 × a
?
?R ? a applytwice f
R ? f f a
?
5 applytwice double
Run Code Online (Sandbox Code Playgroud)
\fun
在C中是否有类似erlang或函数指针的东西?
我想创建一个程序,从文件中读取文本,并在"a"和"an"使用不正确时指出.据我所知,一般规则是当下一个单词以元音开头时使用"an".但是还应该考虑到也应该从文件中读取异常.
有人可以给我一些关于如何开始这个的提示和技巧.可以帮助的功能.
我会很高兴的:-)
我是Python的新手.
在模块级别声明的函数永远不会有闭包并通过访问非局部变量LOAD_GLOBAL
.
声明的函数不能在模块级可以通过具有封闭和访问非本地,变量LOAD_DEREF
如果这些变量不是全局性的.
所以基本上我们有三种方式来存储和加载变量GLOBAL
(全局),FAST
(本地)和DEREF
(非本地,封闭,覆盖).
为什么GLOBAL
?会不会FAST
和DEREF
足够的,如果你让所有的功能都有其封闭?非局部变量和全局变量之间是否存在一些重要区别?这可能是由于性能问题,因为全局变量(如模块级别定义的所有函数和类(包括它们的方法)加上内置函数)通常比非局部变量更常见吗?
玩弄树木,我偶然发现了这种行为:
def descendants (self):
return #or "pass" or "42"
Run Code Online (Sandbox Code Playgroud)
明显回归None
.
另一方面:
def descendants (self):
return
yield 42
Run Code Online (Sandbox Code Playgroud)
返回一个不产生任何东西的生成器(实际上是叶节点所需的行为).
有人可以向我解释一下这里发生了什么吗?
不应该yield 42
是无法访问的代码吗?(我猜测函数是生成器还是"普通"函数的决定是在编译时根据它是否包含一个或多个yield
语句来做出的,无论它们是否可达.但这只是一个黑暗的镜头.)
上下文如下:我有树,每个节点都是树或叶子.现在我想生成一个节点的所有后代:
class Leaf (Node):
@property
def descendants (self):
return
yield 42
class Tree (Node):
@property
def descendants (self):
for child in self.children:
yield child
yield from child.descendants
Run Code Online (Sandbox Code Playgroud) 我试图按照1998年Bruce Schneider的论文中的描述逐步实现Twofish cypher .尽管如此,我在关键扩张方面已经失败了.
我试图将纸张1对1的细节复制到python中,结果如下:
#! /usr/bin/python3.2
def expandKey256 (key):
m = [0] * (32)
for i in range (32):
m [i] = (key >> (i * 8) ) & 0xff
#m [31 - i] = (key >> (i * 8) ) & 0xff
print ('m = {}\n'.format ( [hex (b) for b in m] ) )
M = [0] * 8
for i in range (8):
for j in range (4):
M [i] += m [4 * …
Run Code Online (Sandbox Code Playgroud) python ×7
pyparsing ×2
apl ×1
at-command ×1
closures ×1
cryptography ×1
erlang ×1
function ×1
generator ×1
grammar ×1
haskell ×1
newline ×1
optimization ×1
performance ×1
python-3.2 ×1
python-3.x ×1
regex ×1
serial-port ×1
twofish ×1