- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的一个 friend 参加 Volley 联赛,向我提出了一个有趣的问题:
以下每个字母代表一对玩家
; player pairs (24)
'(a b c d e f g h i j k l m n o p q r s t u v w x)
; teams
'((a b c) (d e f) (g h i) (j k l) (m n o) (p q r) (s t u) (v w x))
; matches
'(((a b c) (d e f))
((g h i) (j k l))
((m n o) (p q r))
((s t u) (v w x)))
'a
将与
'b
配对太多次等等。
'(a b c)
的场景中组队,理想情况下
'a
不会在有
'b
的团队中踢球或
'c
再次。同样,
'b
不会再玩
'c
, 如果可能的话。
'((a b c)
(a b d)
(a b e)
...)`
'(a b c) ; team1
'(a w x) ; team2
'a
不可能同时在两支球队比赛。因此,这些团队组合决不应该在单个比赛解决方案中提供。
; player pairs (26)
'(a b c d e f g h i j k l m n o p q r s t u v w x y z)
; teams
'((a b c) (d e f) (g h i) (j k l) (m n o) (p q r) (s t u) (v w x))
; matches
'(((a b c) (d e f))
((g h i) (j k l))
((m n o) (p q r))
((s t u) (v w x)))
; sit-out this game
'(y z)
X choose Y
这样我就有了所有可能的团队组合的列表。我在过滤掉非完全唯一的组合时遇到了麻烦。即使我成功了,我也不知道如何将完全独特的组合组合成匹配。
最佳答案
您可以使用 combinations
构建问题 1 的解决方案。功能和一些额外的过滤。首先是一些数据定义:
;; A Player-Pair is a Symbol.
;; A Team is a (List Player-Pair Player-Pair Player-Pair).
;; A Match is a (List Team Team) where the teams are disjoint.
;; Player-Pairs : (Listof Player-Pair)
(define player-pairs '(a b c d e f g h i j k l m n o p q r s t u v w x))
filter-team-combinations
的辅助函数.
;; find-teams : (Listof Player-Pair) -> (Listof Team)
(define (find-teams player-pairs)
(filter-team-combinations (combinations player-pairs 3)))
;; filter-team-combinations : (Listof Team) -> (Listof Team)
;; Filters out teams where a player-pair would on a team with another
;; player-pair for the second time.
(define (filter-team-combinations teams) ....)
Team
有两对已经一起加入球队的球员,我也需要跟踪这一点。它可能会是递归的,并且已经在一起的玩家对将从一个递归调用更改为下一个。当我们添加到结果列表中时,pair-pairs 的列表会变大。所以我们需要将它作为一个参数添加,它以一个空列表开始。
;; find-teams : (Listof Player-Pair) -> (Listof Team)
(define (find-teams player-pairs)
(filter-team-combinations (combinations player-pairs 3) (list)))
;; A Pair-Pair is a (List Player-Pair Player-Pair)
;; filter-team-combinations : (Listof Team) (Listof Pair-Pair) -> (Listof Team)
;; Filters out teams where a player-pair would on a team with another
;; player-pair for the second time.
;; pair-pairs is an accumulator that stores the pair-pairs that we've
;; seen so far.
(define (filter-team-combinations teams pair-pairs) ....)
filter-team-combinations
函数处理一个团队列表,一个列表可以是空的,也可以是第一个团队让给其余团队:
(define (filter-team-combinations teams pair-pairs)
(cond [(empty? teams) ....]
[else .... (first teams) .... (rest teams) ....]))
pair-pairs
冲突。 ,并以此为基础:
(define (filter-team-combinations teams pair-pairs)
(cond [(empty? teams) (list)]
[else
(define new-pair-pairs (combinations (first teams) 2))
(cond [(pair-pairs-conflict? new-pair-pairs pair-pairs)
.... (first teams) .... (rest teams) ....]
[else
.... (first teams) .... (rest teams) ....])]))
;; pair-pairs-conflict? : (Listof Pair-Pair) (Listof Pair-Pair) -> Boolean
(define (pair-pairs-conflict? as bs) ....)
pair-pairs-conflict?
做正确的事,我们将填写
....
s 完成
filter-team-combinations
.在它们发生冲突的情况下,我们应该放弃第一支队伍并在其余的队伍上重新开始。在他们不冲突的情况下,我们应该让第一支球队去做一些事情。
(define (filter-team-combinations teams pair-pairs)
(cond [(empty? teams) (list)]
[else
(define new-pair-pairs (combinations (first teams) 2))
(cond [(pair-pairs-conflict? new-pair-pairs pair-pairs)
;; This team has a pair-pair that a previous team already had,
;; so don't include this team in the result; recur on the rest.
(filter-team-combinations (rest teams) pair-pairs)]
[else
;; Cons this team onto something.
(cons (first teams)
....)])]))
....
,我们需要对其余部分进行递归,但我们还需要确保递归调用知道第一支球队中的球员对不应该再次出现在同一支球队中。为此,我们可以将它们附加到
pair-pairs
争论。
;; filter-team-combinations : (Listof Team) (Listof Pair-Pair) -> (Listof Team)
;; Filters out teams where a player-pair would on a team with another
;; player-pair for the second time.
;; pair-pairs is an accumulator that stores the pair-pairs that we've
;; seen so far.
(define (filter-team-combinations teams pair-pairs)
(cond [(empty? teams) (list)]
[else
(define new-pair-pairs (combinations (first teams) 2))
(cond [(pair-pairs-conflict? new-pair-pairs pair-pairs)
;; This team has a pair-pair that a previous team already had,
;; so don't include this team in the result; recur on the rest.
(filter-team-combinations (rest teams) pair-pairs)]
[else
;; Add this team and add the new pair-pairs.
(cons (first teams)
(filter-team-combinations (rest teams)
(append new-pair-pairs pair-pairs)))])]))
pair-pairs-conflict?
谓词。
;; pair-pairs-conflict? : (Listof Pair-Pair) (Listof Pair-Pair) -> Boolean
;; A team must be made up of sets of player-pairs that haven't been on the
;; same team yet. This function takes two lists of player-pair pairs.
;; Each pair-pair in the first list has two player-pairs that would now be
;; on the same team.
;; Each pair-pair in the second list has two player-pairs that have been
;; on the same team already.
;; This function returns true iff any player-pair would be on the same
;; team with anyone they have already been on the same team with.
(define (pair-pairs-conflict? as bs) ....)
as
中的每一对pair并检查它是否在
bs
,如果有冲突,
a
在
bs
.一种方法是使用
ormap
,另一种方法是使用
for/or
.
(define (pair-pairs-conflict? as bs)
(for/or ([a (in-list as)])
(member a bs)))
(list 'a 'b)
应视为与pair-pair
(list 'b 'a)
相同.所以我们需要一个
member
不关心这个排序的函数。幸运的是,
member
可以将第三个参数用作相等谓词。
(define (pair-pairs-conflict? as bs)
(for/or ([a (in-list as)])
(member a bs pair-pair=?)))
;; pair-pair=? : Pair-Pair Pair-Pair -> Boolean
(define (pair-pair=? a b)
(match-define (list a1 a2) a)
(match-define (list b1 b2) b)
(or (and (equal? a1 b1) (equal? a2 b2))
(and (equal? a1 b2) (equal? a2 b1))))
(define teams (find-teams player-pairs))
;; find-matches : (Listof Team) -> (Listof Match)
(define (find-matches teams)
(filter match-has-disjoint-teams? (combinations teams 2)))
;; match-has-disjoint-teams? : Match -> Boolean
(define (match-has-disjoint-teams? match)
(teams-disjoint? (first match) (second match)))
;; teams-disjoint? : Team Team -> Boolean
(define (teams-disjoint? team-1 team-2) ....)
teams-disjoint?
,我们需要匹配
team-1
中的每个玩家对对抗
team-2
中的每个玩家对并确保它们都不相等。一种方法是使用
cartesian-product
和
andmap
,但另一种方法是使用
for*/and
.
;; teams-disjoint? : Team Team -> Boolean
(define (teams-disjoint? team-1 team-2)
(for*/and ([p1 (in-list team-1)]
[p2 (in-list team-2)])
(not (equal? p1 p2))))
find-matches
:
> (find-matches (list (list 'a 'b) (list 'b 'c) (list 'c 'd) (list 'd 'a)))
(list (list (list 'a 'b) (list 'c 'd))
(list (list 'b 'c) (list 'd 'a)))
> (find-matches (list (list 'a 'b 'c)
(list 'c 'd 'e)
(list 'e 'f 'g)
(list 'g 'h 'i)))
(list (list (list 'a 'b 'c) (list 'e 'f 'g))
(list (list 'a 'b 'c) (list 'g 'h 'i))
(list (list 'c 'd 'e) (list 'g 'h 'i)))
find-matches
与
find-teams
:
(find-matches (find-teams player-pairs))
关于algorithm - 如何计算列表的特定组合? (对接会),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402954/
如标题所示,ans_list是一个答案列表,ans_index是一个数字(答案在词汇表中的索引,但与atm无关) 这里生成的 tree.anslist 是什么? (例如,仅针对第一个),忽略迭代。 f
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我目前将用户的输入存储在逗号分隔的列表中,如下所示: Userid | Options 1 | 1,2,5 用户在一个数组形式中勾选一组选项,然后用逗号连接起来 1,2,5 然后 MySQ
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将具有一个元素的东西拿走。 这与 How do I “flatte
我想知道如何完全展平列表和包含它们的东西。除其他外,我想出了一个解决方案,它可以将具有多个元素的东西滑倒并将它们放回原处,或者在滑倒后将带有一个元素的东西拿走。 这与 How do I “flatte
这个问题已经有答案了: Convert nested list to 2d array (3 个回答) 已关闭 7 年前。 java中有没有快捷方式可以转换 List> 到 String[][] ?
我在排序时遇到问题 List> 。我创建了一个自定义比较器,在其中编写了对数据进行排序的代码。 public class CustomComparator implements Comparator
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Java Generics: Cannot cast List to List? 我只是想知道为什么下面的java代
试图想出一个 LINQy 方法来做到这一点,但我什么也没想到。 我有一个对象列表<>,其中包含一个属性,该属性是逗号分隔的字母代码列表: lst[0].codes = "AA,BB,DD" lst[1
假设我有这些任务: points = [] point = (1, 2) 我怎么会这样做: points += point 它工作得很好,并且给了我点 = [1, 2]。但是,如果我这样做: poin
如何在 scala 中将 List[Task[List[Header]]] 类型转换为 Task[List[Header]]。 我有一个方法返回 Task[List[Header]] 并多次调用 do
如何在 Java 中查找二维列表的元素? 我有一个参数为 List> 的函数我想知道如何找到这个列表的行和列。 最佳答案 如果你喜欢 List> obj 然后你就可以像这样访问 obj.get(cur
分配 List到 List工作正常。 分配 List>到 List>不编译。 代码 public class Main { public static void main(String[] a
我正在用 Java 编写一个方法,该方法必须接收并迭代 Serializable 的 List。 有什么区别: public void myMethod(List list) { } 和 public
我看到很多人想用 mvvm 更新网格/列表/树的一部分,但他们不想刷新整个列表。 对于所有遇到此问题的人,我做了以下示例。 希望这对你有用。 最佳答案 这是一个简单的例子。整个代码中最重要的是: Bi
我正在为现有的 C++ 库编写包装器,该库使用列表,其中 T 是自定义结构。我被建议使用 vector 而不是列表,但我试图避免修改库。 为了更好地理解这个场景,我做了一个简单的应用程序,使用一个列表
List list List list 这两种声明有什么区别吗? 谢谢, 最佳答案 是的。 List可以包含所有派生自 Base 的不同事物的混合物. List包含同质项(从某种意义上说,它们必须全部
有人可以尽可能详细地解释以下类型之间的区别吗? List List List 让我更具体一点。我什么时候想使用 // 1 public void CanYouGiveMeAnAnswer(List l
我有一个元组列表,每个元组都是一对列表。所以我的数据看起来像: mylist = [(['foo', 'bar'], ['bar', 'bar']),(['bar', 'bar'],['bar', '
也许是一个时髦的标题,但我遇到了以下问题: 给定一个类型为 (a * b) list 的列表,我想创建一个类型为 (a * b list) list 的新列表。一个例子: 给定列表 let testL
我是一名优秀的程序员,十分优秀!