编写一个通用函数,可以迭代任何可迭代的返回现在,下一对.
def now_nxt(iterable):
iterator = iter(iterable)
nxt = iterator.__next__()
for x in iterator:
now = nxt
nxt = x
yield (now,nxt)
for i in now_nxt("hello world"):
print(i)
('h', 'e')
('e', 'l')
('l', 'l')
('l', 'o')
('o', ' ')
(' ', 'w')
('w', 'o')
('o', 'r')
('r', 'l')
('l', 'd')
Run Code Online (Sandbox Code Playgroud)
我一直在考虑编写函数的最佳方法,其中可以设置每个元组中的项目数.
例如,如果是的话
func("hello",n=3)
Run Code Online (Sandbox Code Playgroud)
结果将是:
('h','e','l')
('e','l','l')
('l','l','o')
Run Code Online (Sandbox Code Playgroud)
我是新手使用timeit,所以请指出我在这里做错了什么:
import timeit
def n1(iterable, n=1):
#now_nxt_deque
from collections import deque
deq = deque(maxlen=n)
for i in iterable:
deq.append(i)
if len(deq) == n:
yield …Run Code Online (Sandbox Code Playgroud) 特定
xs = [1,2,3,4,6,7,9,10,11]
我的目标是回归
[[1,2,3,4],[6,7],[9,10,11]]
我以为我能做到:
groupBy (\x y -> succ x == y) xs
但是这会返回:
[[1,2],[3,4],[6,7],[9,10],[11]]
一点点搜索从Haskell Data.List建议页面返回以下内容.
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy rel [] = []
groupBy rel (x:xs) = (x:ys) : groupBy rel zs
where (ys,zs) = groupByAux x xs
groupByAux x0 (x:xs) | rel x0 x = (x:ys, zs)
where (ys,zs) = groupByAux x xs
groupByAux y xs = ([], xs)
Run Code Online (Sandbox Code Playgroud)
他们给出的一个例子就是我正在寻找的东西:
groupBy (\a b -> …Run Code Online (Sandbox Code Playgroud) 让我们说我们有一棵树......
data Tree a = Node a [Tree a] deriving (Show)
Run Code Online (Sandbox Code Playgroud)
那棵树有一些节点
t = Node 1 [Node 2 [Node 3 []], Node 4 [], Node 5 [Node 6 []]]
Run Code Online (Sandbox Code Playgroud)
以下函数将collect是树中的路径.
paths :: Tree a -> [[a]]
paths (Node n []) = [[n]]
paths (Node n ns) = map ((:) n . concat . paths) ns
Run Code Online (Sandbox Code Playgroud)
像这样:
*Main> paths t
[[1,2,3],[1,4],[1,5,6]]
Run Code Online (Sandbox Code Playgroud)
但是现在我们怎么能fold这些路径呢?显然我们可以做到这一点.在找到路径后折叠.
wastefullFold :: (a -> b -> b) -> b -> Tree a -> [b]
wastefullFold …Run Code Online (Sandbox Code Playgroud) 给定一个通用接口:
public interface I<E> {
public int interfaceMethod(E s);
}
Run Code Online (Sandbox Code Playgroud)
以及实现接口的泛型类
public class A<T> implements I<T> {
private T val;
public A(T x) {
val = x;
}
public int interfaceMethod(T val) {
// looks like T should be of the same type as instance variable 'val'
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么以下工作?
public class Run {
public static void main(String[] args) {
A a = new A<String>("hello");
System.out.println(a.interfaceMethod(100)); \\ returns 0
}
}
Run Code Online (Sandbox Code Playgroud)
我期望定义T的方法的类型参数将方法约束为与提供给构造函数的类型相同的参数.(在这种情况下).interfaceMethodclass AA …
我解析,涉及到的字符,如文件æ ø å.如果我们假设我已经存储了一行文本文件,如下所示
#define MAXLINESIZE 1024
char* buffer = malloc(MAXLINESIZE)
...
fgets(buffer,MAXLINESIZE,handle)
...
Run Code Online (Sandbox Code Playgroud)
如果我想计算一行中的字符数.如果我尝试执行以下操作:
char* p = buffer
int count = 0;
while (*p != '\n') {
if (isgraph(*p)) {
count++;
}
p++;
}
Run Code Online (Sandbox Code Playgroud)
这忽略了任何的发生æ ø å
即:计算"aåeæioøu"将返回5而不是8
我是否需要以另一种方式阅读文件?我不应该使用char*但是int*?
我能看到的唯一好处是,它意味着一个人可以避免调用partial.
(defn foldl [f acc xs]
(loop [acc acc
xs xs]
(if (empty? xs)
acc
(recur (f (first xs) acc) (rest xs)))))
(defn $ [f x] (f x))
(defn thread-last [x & rest]
(foldl $ x rest))
Run Code Online (Sandbox Code Playgroud)
这使:
(thread-last (range 10)
(partial map inc)
(partial filter odd?)
(partial reduce +)) => 25
(->> (range 10)
(map inc)
(filter odd?)
(reduce +)) => 25
Run Code Online (Sandbox Code Playgroud)
在功能/显式版本失败的情况下是否有任何情况?
让我说我写了一个函数:
(defn foo [to x] (conj to x))
Run Code Online (Sandbox Code Playgroud)
并希望通过声明to必须实现某些协议来记录它(因为在结构/类型中to必须支持该调用conj).是否有包含此信息的网站或数据库?显然,我想将这个问题概括为"我在哪里可以找到所有clojure协议的完整参考?"
作为使用Sam Estep的建议的一个明确而具体的例子,它看起来像:
(defn invert-many-to-one
"returns a one-to-many mapping where vals are collections of type `(constructor-fn)`,
(defaults to `hash-set`). Note that `constructor-fn` is a function of 0 args.
`insert-fn` function can be passed. if only `constructor-fn` is passed
then `insert-fn` defaults to `conj` and `(constructor-fn)` must be an instance
of `clojure.lang.IPersistentCollection`"
([m] (invert-many-to-one hash-set conj m))
([constructor-fn m] {:pre [(instance? clojure.lang.IPersistentCollection (constructor-fn))]}
(invert-many-to-one constructor-fn conj m)) …Run Code Online (Sandbox Code Playgroud) python 3.x
>>> a = input()
hope
>>> a
'hope'
>>> b = input()
håpe
>>> b
'håpe'
>>> c = input()
start typing hå... delete using backspace... and change to hope
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 1: invalid continuation byte
>>>
Run Code Online (Sandbox Code Playgroud)
情况并不可怕,我正在解决它,但发现奇怪的是,删除时,字节搞砸了.还有其他人经历过这个吗?
终端历史表明它以为我进入了 h?ope
有任何想法吗?
在使用它的脚本中,我执行导入readline以提供命令行历史记录.
我想弄清楚如何计算以下内容.
给定根值,找到以该值的最后一个字符开头的所有值.显然,如果已经在路径中使用了元素,则不能重复该元素.找到最大深度(最长路线)
例如,种子"sip"和单词:
t1 = ["sour","piss","rune","profit","today","rat"]
Run Code Online (Sandbox Code Playgroud)
我们会看到最大路径是5.
siP 1 ---
| |
| |
pisS 2 profiT 2
| |
| |
| todaY 3
|
souR 3 ---
| |
| |
runE 4 raT 4
|
|
todaY 5
Run Code Online (Sandbox Code Playgroud)
我认为我在以下方面正确 - 但我无法弄清楚如何实际递归调用它.
type Depth = Int
type History = Set.Set String
type AllVals = Set.Set String
type NodeVal = Char
data Tree a h d = Empty | Node a h d [Tree a h d] …Run Code Online (Sandbox Code Playgroud) 当我从源代码管理中选择“提交...”时更新到 Xcode 10.1 后,出现错误
多个工作副本提交文件失败。
它以前一直有效。
推送到 Bitbucket 仍然有效。我怎样才能让 Commit 再次工作?