F#-从字符数组中删除后续重复项(无序)
我正在努力学习法语。我需要一个简单的soundex表达式的som帮助。 我对简化(也称为美国)soundex使用以下规则集: 现在我被第二条规则难住了。我在考虑使用递归表达式。 由于我目前是F#上的n00b,我将尝试向您寻求一个优雅的解决方案。也许我将文本翻译成soundex的整个方法都偏离了目标 如有任何建议,将不胜感激:) 这是我的密码:F#-从字符数组中删除后续重复项(无序),f#,soundex,F#,Soundex,我正在努力学习法语。我需要一个简单的soundex表达式的som帮助。 我对简化(也称为美国)soundex使用以下规则集: 现在我被第二条规则难住了。我在考虑使用递归表达式。 由于我目前是F#上的n00b,我将尝试向您寻求一个优雅的解决方案。也许我将文本翻译成soundex的整个方法都偏离了目标 如有任何建议,将不胜感激:) 这是我的密码: let Simplified (name:string) = let ca = name.ToLower().ToCharArray() new stri
let Simplified (name:string) =
let ca = name.ToLower().ToCharArray()
new string(
Array.map(
fun e ->
match e with
| 'a' | 'e' | 'i' | 'o' | 'u' | 'y' | 'w' | 'h' -> '0'
| 'b' | 'f' | 'p' | 'v' -> '1'
| 'c' | 's' | 'k' | 'g' | 'j' | 'q' | 'x' | 'z' -> '2'
| 'd' | 't' -> '3'
| 'l' -> '4'
| 'm' | 'n' -> '5'
| 'r' -> '6'
| _ -> ' '
) ca
//|> fun s -> TODO: Remove duplicates here
|> fun s -> Array.set s 0 (ca.[0])
Array.choose(fun e -> if e <> '0' then Some(e) else None) s
)
|> fun s -> (
match s.Length with
| x when x < 3 -> s.PadRight(4, '0')
| _ -> s.Substring(0, 4)
).ToUpper()
let Simplified(名称:string)=
设ca=name.ToLower().ToCharArray()
新字符串(
Array.map(
乐趣e->
匹配
|“a”|“e”|“i”|“o”|“u”|“y”|“w”|“h”->“0”
|“b”|“f”|“p”|“v”->“1”
|“c”|“s”|“k”|“g”|“j”|“q”|“x”|“z”->“2”
|“d”|“t”->“3”
|“l”->“4”
|“m”|“n”->“5”
|‘r’->‘6’
| _ -> ' '
)ca
//|>乐趣->待办事项:在此处删除重复项
|>fun s->Array.set s 0(ca.[0])
数组。选择(乐趣e->如果e为“0”,则选择一些(e)其他无
)
|>乐趣s->(
将s.长度与
|当x<3->s.PadRight(4,'0')时的x
|_u->s.子串(0,4)
).ToUpper()
如果要从阵列中删除所有重复项(保留唯一元素),请执行以下操作:
arr |> Seq.distinct |> Seq.toArray
如果要删除连续的重复项,则解决方案更难。这是我能想到的最简单的一个:
let unique list =
list
|> List.fold (fun acc e ->
match acc with
| x::xs when x = e -> acc
| _ -> e::acc) []
|> List.rev
您可以通过Array.toList
和Array.ofList
或使用Array.fold
更改匹配表达式和列表结构来使用数组;代码可读性较差,因此我将发布列表版本
替代解决方案包括成对的序列,即:
let unique arr =
if Array.isEmpty arr then
arr
else
Array.append [|arr.[0]|] (
arr
|> Seq.pairwise
|> Seq.toArray
|> Array.choose (fun (p, n) -> if p = n then None else Some n))
如果要从阵列中删除所有重复项(保留唯一元素),请执行以下操作:
arr |> Seq.distinct |> Seq.toArray
如果要删除连续的重复项,则解决方案更难。这是我能想到的最简单的一个:
let unique list =
list
|> List.fold (fun acc e ->
match acc with
| x::xs when x = e -> acc
| _ -> e::acc) []
|> List.rev
您可以通过Array.toList
和Array.ofList
或使用Array.fold
更改匹配表达式和列表结构来使用数组;代码可读性较差,因此我将发布列表版本
替代解决方案包括成对的序列,即:
let unique arr =
if Array.isEmpty arr then
arr
else
Array.append [|arr.[0]|] (
arr
|> Seq.pairwise
|> Seq.toArray
|> Array.choose (fun (p, n) -> if p = n then None else Some n))
如果希望删除后续重复项(zeuxcg解决方案中的第二个选项),那么还可以将其直接实现为递归函数(使用累加器参数)。这很好地演示了模式匹配,因此在学习F#时尝试是一件好事:
此版本适用于列表,但由于您使用的是阵列,因此可能需要命令式版本。可以使用如下顺序表达式:
let removeConsequentDuplicates (arr:_[]) =
let rec loop last i = seq {
if i < arr.Length - 1 && last = arr.[i] then
yield! loop last (i+1)
elif i < arr.Length - 1 then
yield arr.[i]
yield! loop (arr.[i]) (i + 1) }
[| if arr.Length > 0 then
yield arr.[0]
yield! loop arr.[0] 0 |]
let removeDuplicates (xs: _ []) =
[|if xs.Length > 0 then yield xs.[0]
for i=1 to xs.Length-1 do
if xs.[i] <> xs.[i-1] then
yield xs.[i]|]
如果希望删除后续重复项(zeuxcg解决方案中的第二个选项),那么还可以将其直接实现为递归函数(使用累加器参数)。这很好地演示了模式匹配,因此在学习F#时尝试是一件好事:
此版本适用于列表,但由于您使用的是阵列,因此可能需要命令式版本。可以使用如下顺序表达式:
let removeConsequentDuplicates (arr:_[]) =
let rec loop last i = seq {
if i < arr.Length - 1 && last = arr.[i] then
yield! loop last (i+1)
elif i < arr.Length - 1 then
yield arr.[i]
yield! loop (arr.[i]) (i + 1) }
[| if arr.Length > 0 then
yield arr.[0]
yield! loop arr.[0] 0 |]
let removeDuplicates (xs: _ []) =
[|if xs.Length > 0 then yield xs.[0]
for i=1 to xs.Length-1 do
if xs.[i] <> xs.[i-1] then
yield xs.[i]|]
使用使用循环而不是递归的数组删除连续重复项,最简单的方法是使用如下序列表达式:
let removeConsequentDuplicates (arr:_[]) =
let rec loop last i = seq {
if i < arr.Length - 1 && last = arr.[i] then
yield! loop last (i+1)
elif i < arr.Length - 1 then
yield arr.[i]
yield! loop (arr.[i]) (i + 1) }
[| if arr.Length > 0 then
yield arr.[0]
yield! loop arr.[0] 0 |]
let removeDuplicates (xs: _ []) =
[|if xs.Length > 0 then yield xs.[0]
for i=1 to xs.Length-1 do
if xs.[i] <> xs.[i-1] then
yield xs.[i]|]
let removeDuplicates(xs:[])=
[|如果xs.Length>0,则产生xs。[0]
对于i=1到xs.Length-1 do
如果xs[i]xs[i-1],那么
收益率xs。[i]|]
使用循环而不是递归的数组删除连续重复项,最简单的方法是使用如下序列表达式:
let removeConsequentDuplicates (arr:_[]) =
let rec loop last i = seq {
if i < arr.Length - 1 && last = arr.[i] then
yield! loop last (i+1)
elif i < arr.Length - 1 then
yield arr.[i]
yield! loop (arr.[i]) (i + 1) }
[| if arr.Length > 0 then
yield arr.[0]
yield! loop arr.[0] 0 |]
let removeDuplicates (xs: _ []) =
[|if xs.Length > 0 then yield xs.[0]
for i=1 to xs.Length-1 do
if xs.[i] <> xs.[i-1] then
yield xs.[i]|]
let removeDuplicates(xs:[])=
[|如果xs.Length>0,则产生xs。[0]
对于i=1到xs.Length-1 do
如果xs[i]xs[i-1],那么
收益率xs。[i]|]
Seq.fold是你的朋友
let soundex (text : string) =
let choose =
function
| 'b' | 'f' | 'p' | 'v' -> Some "1"
| 'c' | 'g' | 'j' | 'k' | 'q' | 's' | 'x' | 'z' -> Some "2"
| 'd' | 't' -> Some "3"
| 'l' -> Some"4"
| 'm' | 'n' -> Some "5"
| 'r' -> Some "6"
| _ -> None
let fold state value =
match state with
| i :: _ when i = value -> state
| _ -> value :: state
let t = text.Substring(1).ToLower() |> Seq.choose choose |> Seq.fold fold [] |> Seq.toList |> List.rev |> String.concat ""
text.Substring(0,1) + t.PadRight(3, '0').Substring(0, 3)
这是基于维基百科上关于soundex的文章。fold是你的朋友
let soundex (text : string) =
let choose =
function
| 'b' | 'f' | 'p' | 'v' -> Some "1"
| 'c' | 'g' | 'j' | 'k' | 'q' | 's' | 'x' | 'z' -> Some "2"
| 'd' | 't' -> Some "3"
| 'l' -> Some"4"
| 'm' | 'n' -> Some "5"
| 'r' -> Some "6"
| _ -> None
let fold state value =
match state with
| i :: _ when i = value -> state
| _ -> value :: state
let t = text.Substring(1).ToLower() |> Seq.choose choose |> Seq.fold fold [] |> Seq.toList |> List.rev |> String.concat ""
text.Substring(0,1) + t.PadRight(3, '0').Substring(0, 3)
这是基于wikipedia关于soundex的文章。是的,我假设任务是删除连续的重复项;否则,解决方案就是Seq.distinct |>Seq.toArray-我将更新帖子。不要忘记糟糕的被忽略的折叠。是的,我假设任务是删除连续的重复项;否则,解决方案就是Seq.distinct |>Seq.toArray-我会更新帖子。别忘了可怜的被忽略的折叠。