linux - 2 个字符串的所有排列和组合的列表

标签 linux bash shell combinations permutation

我们说句话

qwerty

我想要的是我需要在字符串之间插入句点(点。)。它也可以是任何其他字符。

例如,

q.werty
qw.erty
qwe.rty
qwer.ty
qwert.y

以上内容适用于 1 个句点或点。因此 5 个字母字符串的 1 个句点组合将生成 5 个输出。 (N-1)

现在 2 个句点(2 个点)(仅 2 个示例):

q.w.erty
q.we.rty
q.wer.ty
q.wert.y
qw.e.rty
qw.er.ty
qw.ert.y
qwe.r.ty
qwe.rt.y
qwer.t.y

等等..

注意:字符串中的 2 个字母之间不能有 2 个连续的点。此外,开始字符之前和/或结束字符之后不得有句点。

任何人都可以为上述内容提供一个 Shell 脚本(sh、bash)来列出所有可能的组合和排列。我尝试过谷歌搜索,但没有找到任何值得引用的内容。

编辑:任何有关如何在 bash shell 脚本上启动此脚本的帮助都会很棒...

最佳答案

你的谜题很有趣,所以这里有一个代码:

#!/bin/bash

t=qwerty

echo '---- one dot ----'

for (( i = 1; i < ${#t}; ++i )); do
    echo "${t:0:i}.${t:i}"
done

echo '---- two dots ----'

for (( i = 1; i < (${#t} - 1); ++i )); do
    for (( j = i + 1; j < ${#t}; ++j )); do
        echo "${t:0:i}.${t:i:j - i}.${t:j}"
    done
done

输出:

---- one dot ----
q.werty
qw.erty
qwe.rty
qwer.ty
qwert.y
---- two dots ----
q.w.erty
q.we.rty
q.wer.ty
q.wert.y
qw.e.rty
qw.er.ty
qw.ert.y
qwe.r.ty
qwe.rt.y
qwer.t.y

请参阅Bash Manual对于一切。

关于linux - 2 个字符串的所有排列和组合的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757486/

相关文章:

ruby - 当前行以 ^M 结尾时追加文件中的下一行

linux - git push 使用 crontab 每小时提示密码

linux - AT&T 汇编尝试将 "movb"一个字符常量注册,核心转储

c++ - 从 Linux 中的非子进程获取退出代码

linux - sed:替换特定部分

bash - 自动运行端口转发的脚本

php - linux xampp Visual Studio 代码配置

bash - kubectl bash 完成在 ubuntu docker 容器中不起作用

ruby - 隐藏敏感的 ruby​​ shell 命令

c - 为什么我的管道仅在我发送 SIGINT 时才结束