小编Pph*_*nix的帖子

Haskell logbase错误

我试图使用长度等于的事实来计算Haskell中Integer的长度truncate (log10(x)+1).

使用我创建的整数:

len :: Integer -> Integer
len i = toInteger (truncate (logBase 10 (fromIntegral i)) + 1)
Run Code Online (Sandbox Code Playgroud)

不幸的是,并非所有数字都得到正确的长度.我尝试了几个不同的案例,发现:

logBase 10 10         = 1.0
logBase 10 100        = 2.0
logBase 10 1000       = 2.9999..6
logBase 10 10000      = 4.0
logBase 10 100000     = 5.0
logBase 10 1000000    = 5.9999999
Run Code Online (Sandbox Code Playgroud)

有没有理由logBase 10 1000不返回3.0?如何在基数10中获得1000的正确日志值?

haskell logarithm rounding-error

7
推荐指数
1
解决办法
825
查看次数

多行if语句Haskell

我在Haskell中编写一个简单的程序,并具有以下功能:

tryMove board player die = do
  let moves = getMoves board player die
  putStrLn ("Possible columns to move: " ++ (show $ moves))

  if not $ null moves then
    let col = getOkInput $ moves
    putStrLn " "
    return $ step board (getMarker player) col firstDie
  else
    putStrLn ("No possible move using "++(show die)++"!")
    return board
Run Code Online (Sandbox Code Playgroud)

它需要一块板,如果玩家可以根据掷骰子进行移动,则返回新板,否则返回旧板.

但是,haskell不允许我在if语句中使用多行.是否可以使用某种限制器,以便我可以使用letif中的东西?

haskell if-statement

7
推荐指数
1
解决办法
2583
查看次数

在 vs re.search python 中

我有一个 python 2.7 脚本,通常运行几个小时,现在我正在尝试优化它。它有大量的字符串搜索,这代表了计算的大部分。目前我正在使用re.search('stringToFind', haystack)在较长字符串中查找子字符串。我正在考虑重写所有不包含正则表达式并使用的表达式in。几乎所有搜索的字符串都是普通字符串,即不包含正则表达式。

我知道in使用一种contains方法来检查子字符串,但我不知道 re.search 如何处理非 re 字符串。研究实际上是如何运作的?在字符串比较中使用 in 而不是 re.search 来查找子字符串真的会更有效吗?

编辑:为了澄清我可以给出当前运行代码的示例:

if re.search('extern', variable):
    # Do something...
Run Code Online (Sandbox Code Playgroud)

可以替换为:

if 'extern' in variable:
    # Do  something...
Run Code Online (Sandbox Code Playgroud)

python regex

5
推荐指数
1
解决办法
1957
查看次数

带有标题栏和 Windows 任务栏的 Tkinter 窗口

我已经广泛搜索这个问题,但似乎没有人知道。我在 python 2.7 中创建了一个简单的 tkinter 窗口 (tcl 8.5) 并希望它最大化,就像我点击右上角的最大化按钮一样。使用该-fullscreen选项不是一个选项,因为它会删除标题栏。

我尝试了以下方法:

import Tkinter

root = Tkinter.Tk()
root.overrideredirect(True)
# Set window to be size of screen
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))  
Run Code Online (Sandbox Code Playgroud)

问题是该窗口现在位于 Windows 任务栏下方,因此未显示我的一些元素。一个简单的技巧是将高度设置为 screenheight-some_constant,或根据操作系统的数据计算 some_constant。然而,这似乎是一个极其丑陋的黑客行为。

有没有办法以干净的方式最大化 tkinter 中的窗口,其中窗口位于(Windows)任务栏上方并且仍然有标题栏?

python taskbar window tkinter fullscreen

3
推荐指数
1
解决办法
6116
查看次数