パスワードを変更する


17

一般的な単語は、パスワードとして使用するために依然として避ける必要があります。この課題は、mungesは、指定されたパスワード(という非常に簡単なプログラムをコーディングについてですM odify U ntil N OT Gは uessed Eを asily)。

入力

単語abcdefghijklmnopqrstuvwxyz。アルファベットで書かれた文字列です。文字が小文字でも大文字でも構いません。

ムング

  1. 同じ文字の繰り返しシーケンスを、その文字が繰り返された回数が前に付いたそれ自体に変更します(LLLLwith 4L
  2. 最初の変更a@
  3. 最初の変更b8
  4. 最初の変更c(
  5. 最初の変更d6
  6. 最初の変更e3
  7. 最初の変更f#
  8. 最初の変更g9
  9. 最初の変更h#
  10. 最初の変更i1
  11. で2番目iを変更する!
  12. 最初の変更k<
  13. 最初の変更l1
  14. で2番目lを変更するi
  15. 最初の変更o0
  16. 最初の変更q9
  17. 最初の変更s5
  18. で2番目sを変更する$
  19. 最初の変更t+
  20. 最初の変更v>
  21. で2番目vを変更する<
  22. 最初の変更wuu
  23. で2番目wを変更する2u
  24. 最初の変更x%
  25. 最初の変更y?

ルール1は、それ以上適用できなくなるまで、必要な回数だけ適用する必要があります。その後、残りのルールが適用されます。

出力変更された単語

  • codegolf -> (0639o1#
  • programming -> pr09r@2m1ng
  • puzzles -> pu2z135
  • passwords -> p@25uu0r6$
  • wwww -> 4uu
  • aaaaaaaaaaa -> 11a
  • lllolllolll -> 3103io3l
  • jjjmjjjj -> 3jm4j

これはなので、プログラムをできるだけ短くしてください!

この投稿の内容は、パスワードのアイデアとして、またはパスワードの実践の一部として使用しないでください。


18
このようなプログラムが可能であるという事実は、攻撃者が同じように簡単に(多くの場合、より優れたハードウェアにアクセスできるためさらに簡単に)それらを書いてパスワードを変更(およびさまざまな変更を試みます)できることを意味します。だから安全のため、私は言うだろう:何もこの記事では、パスワードのアイデアとして、またはパスワード慣行の一部として使用することはできません。
NH。

1
その免責事項を太字にして、上部に複製することをお勧めします。あなたがあまりにも慎重になることはありません
...-wizzwizz4

回答:


11

Java 8、237 321 319 280 247 241 240 237バイト

s->{for(int a[]=new int[26],i=0,l=s.length,t,x;i<l;i+=t){for(t=0;++t+i<l&&s[i]==s[t+i];);System.out.print((t>1?t+"":"")+(++a[x=s[i]-65]>2?s[i]:"@8(63#9#1J<1MN0P9R5+U>u%?ZABCDEFGH!JKiMNOPQR$TU<2XYZ".charAt(x+26*~-a[x])+(x==22?"u":"")));}}

84バイトのルールが変更..得たので(EDIT:最後に戻って私の最初の237のバイトを)交換するWWWW222W、Javaで簡単ですが、と4W...だけではなく、Javaは何かのために正規表現のキャプチャグループを使用する方法を持っていた場合。で長さを取得する"$1".length()、マッチ自体を置き換える、マッチを"$1".replace(...)整数に変換するnew Integer("$1")、またはRetina(ie s.replaceAll("(?=(.)\\1)(\\1)+","$#2$1"))またはJavaScript(ie s.replaceAll("(.)\\1+",m->m.length()+m.charAt(0)))のようなものを使用することは、Javaで見たい1番のことですcodegolfingに利益をもたらす将来..>。> Javaがキャプチャグループマッチで何もできないことを嫌うのは10回目だと思います。@OlivierGrégoire
により-78バイト。

I / Oは大文字です。

説明:

ここで試してみてください。

s->{                           // Method with String parameter and no return-type
  for(int a[]=new int[26],     //  Array with 26x 0
          i=0,                 //  Index-integer, starting at 0
          l=s.length,          //  Length
          t,x;                 //  Temp integers
      i<l;                     //  Loop (1) over the characters of the input
      i+=t){                   //    After every iteration: Increase `i` by `t`
    for(t=0;++                 //   Reset `t` to 1
        t+i<l                  //   Inner loop (2) from `t+i` to `l` (exclusive)
        &&s[i]==s[t+i];        //   as long as the `i`'th and `t+i`'th characters are equal
    );                         //   End of inner loop (2)
    System.out.print(          //   Print:
     (t>1?t+"":"")             //    If `t` is larger than 1: print `t`
     +(++a[x=s[i]-65]>2?       //    +If the current character occurs for the third time:
       s[i]                    //      Simply print the character
      :                        //     Else:
       "@8(63#9#1J<1MN0P9R5+U>u%?ZABCDEFGH!JKiMNOPQR$TU<2XYZ".charAt(x
                               //      Print the converted character at position `x`
        +26*~-a[x])            //       + 26 if it's the second time occurring
       +(x==22?"u":"")));      //      And also print an additional "u" if it's 'W'
  }                            //  End of loop (1)
}                              // End of method

10

JavaScript(ES6)、147バイト

s=>[[/(.)\1+/g,m=>m.length+m[0]],..."a@b8c(d6e3f#g9h#i1k<l1o0q9s5t+v>x%y?i!lis$v<".match(/../g),["w","uu"],["w","2u"]].map(r=>s=s.replace(...r))&&s

テストケース

説明

入力文字列の一連の置換をs、チャレンジで指定された順序で実行します。シリーズの各アイテムは、2つのアイテムを持つ配列または文字列であり、展開(...r)されてに渡されs.replace()ます。

s=>[
    [/(.)\1+/g, m=>m.length + m[0]],// first replacement: transform repeated letters
                                    // into run-length encoding

                                    // string split into length-2 partitions and
                                    // spread into the main array
    ..."a@b8c(d6e3f#g9h#i1k<l1o0q9s5t+v>x%y?i!lis$v<".match(/../g),
                                    // next replacements: all single-char replacements.
                                    // "second" versions are placed at the end so they
                                    //    replace the second instance of that char

    ["w","uu"],["w","2u"]           // last replacements: the two "w" replacements
]
.map(r=> s = s.replace(...r))       // run all replacements, updating s as we go
&& s                                // and return the final string

非常に良い答え
-mdahmoune


6

Perl 5、152 + 1(-p)= 153バイト

s/(.)\1+/(length$&).$1/ge;%k='a@b8c(d6e3f#g9h#i1j!k<l1mio0q9r5s$t+u>v<x%y?'=~/./g;for$i(sort keys%k){$r=$k{$i};$i=~y/jmru/ilsv/;s/$i/$r/}s/w/uu/;s/w/2u/

オンラインでお試しください!


Plz(-p)とはどういう意味ですか?
mdahmoune

1
@mdahmouneは、-p引数として使用されperl、自動的からの入力を読み取り、コマンドラインでSTDINprintの内容だ$_スクリプトの最後に。TIOではそのオプションが許可されており、perl -pe<code>1バイトのperl -e<code>追加バイトとしてカウントされるよりも1バイト多いためです。
ドムヘイスティングス

私はべきではない、あなたがタイプミスをしたと思う~とのj~kこと!代わりに?現在、の2番目のオカレンスiをの~代わりにに置き換えました!
ケビンCruijssen

@Xcali #testingonproduction
NieDzejkob

2
@NieDzejkobより良い場所はありません。それが本番環境で機能することを知っている唯一の方法です。
Xcali

4

おそらく最もゴルフが上手ではありませんが、うまくいきます。

ovsのおかげで-6バイト

NieDzejkobとJonathan Frenchに感謝-77バイト

Pythonの3329 323バイト 246のバイト

import re;n=input()
for a in re.finditer('(\w)\\1+',n):b=a.group();n=n.replace(b,str(len(b))+b[0],1)
for A,B,C in[('abcdefghikloqstvxyw','@8(63#9#1<1095+>%?','uu'),('ilsvw','!i$<','2u')]:
	for a,b in zip(A,list(B)+[C]):n=n.replace(a,b,1)
print(n)

オンラインでお試しください!


1
落とすと思う.lower()
-mdahmoune

大文字を処理する必要があるかどうかはわかりませんでした。
reffu



2
実際、あなたの答えはうまくいきません。jjjmjjjj出力する必要があります3jm4jが出力します3jm3jj。編集:この問題を修正した258バイト
-NieDzejkob

3

網膜166 124バイト

(.)\1+
$.&$1
([a-y])(?<!\1.+)
¶$&
¶w
uu
T`l¶`@8(63#9#1j<\1mn0\p9r5+u>\w%?_`¶.
([ilsvw])(?<!\1.+)
¶$&
¶w
2u
T`i\lsv¶`!i$<_`¶.

オンラインでお試しください!説明:

(.)\1+
$.&$1

繰り返される文字の連続を長さと文字に置き換えます。

([a-y])(?<!\1.+)
¶$&

最初に出現する文字aと一致yし、プレースホルダーでマークします。

¶w
uu

の最初の出現を修正しwます。

T`l¶`@8(63#9#1j<\1mn0\p9r5+u>\w%?_`¶.

ato からの他のすべての文字の最初の出現を修正しy、プレースホルダーを削除します。

([ilsvw])(?<!\1.+)
¶$&

文字の(当初)は、第2の発生をマークしilsv、またはwプレースホルダを有します。

¶w
2u

の2番目の出現を修正しwます。

T`i\lsv¶`!i$<_`¶.

他の4文字の2番目の出現を修正します。


さらにゴルフをすることは可能だと思いますか?
mdahmoune

@mdahmouneはい、33バイト節約できます。
ニール

私はあなたの答えをアップ投票しました:) 33バイトを保存することは素晴らしいことです;)
mdahmoune

@mdahmoune良いニュース、私は実際に42バイトを節約しました!
ニール

素晴らしい、あなたのコードは2番目に短い;)
mdahmoune

3

Haskell221 218 213バイト

($(f<$>words"w2u li i! s$ v< a@ b8 c( d6 e3 f# g9 h# i1 k< l1 o0 q9 s5 t+ v> wuu x% y?")++[r]).foldr($)
f(a:b)(h:t)|a==h=b++t|1>0=h:f(a:b)t
f _ s=s
r(a:b)|(p,q)<-span(==a)b=[c|c<-show$1+length p,p>[]]++a:r q
r s=s

オンラインでお試しください!

foldr一連の文字列変換を逆方向に実行する文字列の不正使用。シーケンスは「開始」し、文字列が先頭に等しくなくなったときに文字列の末尾を壊すためにr使用spanすることにより、繰り返しカウントの置換を行います。その最初の部分が空でない場合は繰り返しなので、長さ+1を出力します。次にf、文字の置換ごとに(逆)順序で引数をカリー化します。置換は単一の文字列としてエンコードされ、最初の文字は置換される文字で、残りは文字列として(w置換は複数の文字であるため)その場所に配置されます。これらのエンコードされた文字列を、スペースで区切られた1つの大きな文字列に入れwordsて、リストに分割できるようにします。

編集:5バイトを節約してくれてありがとう@Laikoni!それは$私が考えもしなかった賢い使い方でした。私もその<-トリックを知りませんでした。


詳細な説明を
ありがとう

1
(p,q)<-span(==a)b代わりにlet(p,q)=span(==a)bとのp>[] 代わりに使用できますp/=[]
ライコニ

2
mポイントフリーにすることで、さらに2バイト節約($(f<$>words"w2u ... y?")++[r]).foldr($) できます。オンラインで試してください!
ライコニ

2

Lua、173バイト

s=...for c,r in("uua@b8c(d6e3f#g9h#i1i!jjk<l1limmnno0ppq9rrs5s$t+v>v<wuuw2ux%y?zz"):gmatch"(.)(.u?)"do s=s:gsub(c..c.."+",function(p)return#p..c end):gsub(c,r,1)end print(s)

オンラインでお試しください!

ゴルフをしていないと説明:

s = ...


--This string contains every character to replace, followed by
--the character(s) it should be replaced with.
--
--It also contains all characters for which repeated sequences
--of them should be replaced by "<number><character>". That is,
--all letters in the alphabet. This way, a single loop can do
--both the "replace repeated characters" and "encode characters"
--operations, saving a for loop iterating over the alphabet.
--
--Characters that shouldn't be replaced will be replaced with
--themselves.
--
--In order to avoid matching half of the "replace u with u"
--command as the replace part of another command, "uu" is placed
--at the beginning of the string. This ensures that only the
--2-character replacements for "w" get an extra "u".

cmdstring = "uua@b8c(d6e3f#g9h#i1i!jjk<l1limmnno0ppq9rrs5s$t+v>v<wuuw2ux%y?zz"


--Iterate over all the search/replace commands.
--The character to replace is in the "c" variable, the string to
--replace it with is in "r".
--
--Due to the dummy search/replace commands (i.e. "mm") placed
--in the string, this loop will also iterate over all letters
--of the alphabet.

for c,r in cmdstring:gmatch("(.)(.u?)") do
	
	--First, replace any occurences of the current letter
	--multiple times in a row with "<number><letter>".
	s = s:gsub(c..c.."+", function(p)
		return #p .. c
	end)
	
	--Then, replace the first occurence of the letter
	--with the replacement from the command string.
	s = s:gsub(c, r, 1)
end

print(s)

Lol lua :)良い仕事
mdahmoune

2

C#(。NETコア)、317289、279バイト

p=>{string r="",l=r,h=r,c="a@b8c(d6e3f#g9h#i1i!k<l1lio0q9s5s$t+v>v<wuw2x%y?";int i=0,n=p.Length,d,a=1;for(;i<n;i++){h=p[i]+"";if(h==p[(i==n-1?i:i+1)]+""&&i!=n-1)a++;else{d=c.IndexOf(h);if(d>=0&&d%2<1){l=c[d+1]+"";h=l=="u"?"uu":l;c=c.Remove(d,2);}r+=a>1?a+""+h:h;a=1;}}return r;};

オンラインでお試しください!

文字列ではなく入力としてchar配列を受け取っても問題ないことを願っています。

アンゴルフド

string result = "", casesCharReplacement = result, currentChar = result, cases = "a@b8c(d6e3f#g9h#i1i!k<l1lio0q9s5s$t+v>v<wuw2x%y?";
int i = 0, n = pas.Length, casesIndex, charAmounts = 1;

// For every char in the pass.
for (; i < n; i++)
{
    currentChar = pas[i] + "";
    // if the next char is equal to the current and its not the end of the string then add a +1 to the repeated letter.
    if (currentChar == (pas[(i == n - 1 ? i : i + 1)] + "") && i != n - 1)
        charAmounts++;
    else
    {
        // Finished reading repeated chars (N+Char).
        casesIndex = cases.IndexOf(currentChar);
        // Look for the replacement character: only if the index is an even position, otherwise I could mess up with letters like 'i'.
        if (casesIndex >= 0 && casesIndex % 2 < 1)
        {
            casesCharReplacement = cases[casesIndex + 1]+"";
            // Add the **** +u
            currentChar = casesCharReplacement == "u"?"uu": casesCharReplacement;
            // Remove the 2 replacement characters (ex: a@) as I won't need them anymore.
            cases = cases.Remove(casesIndex, 2);
        }
        // if the amount of letters founded is =1 then only the letter, otherwise number and the letter already replaced with the cases.
        result += charAmounts > 1 ? charAmounts + ""+currentChar : currentChar;
        charAmounts = 1;
    }
}
return result;

1
はい、大丈夫です:)入力について
-mdahmoune

2

C ++、571の 495 478 444バイト

ザカリーのおかげで-127バイト

#include<string>
#define F r.find(
#define U(S,n)p=F s(S)+b[i]);if(p-size_t(-1)){b.replace(i,1,r.substr(p+n+1,F'/',n+p)-p-2));r.replace(p+1,F'/',p+1)-p,"");}
#define V(A)i<A.size();++i,c
using s=std::string;s m(s a){s b,r="/a@/b8/c(/d6/e3/f#/g9/h#/i1//i!/k</l1//li/o0/q9/s5//s$/t+/v>/wuu//w2u/x%/y?/";int c=1,i=0;for(;V(a)=1){for(;a[i]==a[i+1]&&1+V(a)++);b+=(c-1?std::to_string(c):"")+a[i];}for(i=0;V(b)){auto U("/",1)else{U("//",2)}}return b;}

"/a@/b8/c(/d6/e3/f#/g9/h#/i1//i!/k</l1//li/o0/q9/s5//s$/t+/v>/wuu//w2u/x%/y?/"文字列は、ある文字から別の文字に変換するために使用されます。1 /は最初の「次の文字」を次の文字に置き換える/ことを意味し、2は2番目の「次の文字」を次の文字に置き換えることを意味します。

オンラインで試す


素晴らしい、tio.runリンクを追加してもらえますか?
mdahmoune

@mdahmoune TIOリンクが追加され、テストケースをテストするためのコードが追加されます:)
HatsuPointerKun

494バイト、変更した場合はそれに応じてTIOリンクを更新します。
ザカリー

@Zacharýマクロ名とマクロコンテンツの間にスペースを入れる必要があります。そうしないと、C ++ 17でコンパイルするときにエラーがスローされます。また、TIOリンクを削除する方法を知っていますか?(古いものは無用であるので)
HatsuPointerKun


2

R224 219バイト

function(s,K=function(x)el(strsplit(x,"")),u=rle(K(s)))
Reduce(function(x,y)sub(K('abcdefghiiklloqsstvvwwxy')[y],c(K('@8(63#9#1!<1i095$+><'),'uu','2u',K('%?'))[y],x),1:24,paste0(gsub("1","",paste(u$l)),u$v,collapse=""))

オンラインでお試しください!

厄介ですが、主な部分はの反復置換Reduceです。sub一致の最初の発生のみを変更します。

素敵なゴルフを指摘してくれたJayCeに感謝します!


良い仕事:)))))
mdahmoune

引数を並べ替えて1バイト節約します。私が知っている大きな違いはありません;)
JayCe

@JayCeもう少しバイトを見つけました:
ジュゼッペ


1

パイソン2220の 216 194 190 188バイト

import re
S=re.sub(r'(.)\1+',lambda m:`len(m.group(0))`+m.group(1),input())
for a,b in zip('abcdefghiiklloqsstvvxyww',list('@8(63#9#1!<1i095$+><%?')+['uu','2u']):S=S.replace(a,b,1)
print S

オンラインでお試しください!

Python 3、187バイト

import re
S=re.sub(r'(.)\1+',lambda m:str(len(m.group(0)))+m.group(1),input())
for a,b in zip('abcdefghiiklloqsstvvxyww',[*'@8(63#9#1!<1i095$+><%?','uu','2u']):S=S.replace(a,b,1)
print(S)

オンラインでお試しください!


ありがとうTfeld 192バイトtio.run/...
mdahmoune

素晴らしいゴルフ;)
mdahmoune

186バイト。これを192バイトでPython 3に簡単に移植することもできますが、別の答えになるとは思いません。
-NieDzejkob

@NieDzejkobゴルフされたPython 2バージョンは、OPの現在のバージョンまたはPython 3バージョンとは異なる出力を生成するようです。
ジョナサンフレッチ

@JomathanFrech申し訳ありませんが、常に実稼働環境でテストしています。188バイト
-NieDzejkob

1

ピップ103 102バイト

aR:`(.)\1+`#_.B
Fm"abcdefghiiklloqsstvvwwxy"Z"@8(63#9#1!<1i095$+><WU%?"I#Ya@?@maRA:ym@1aR'W"uu"R'U"2u"

オンラインでお試しください!

説明

コードは変換の3つのステップを実行します。

aR:`(.)\1+`#_.B  Process runs of identical letters

a                1st cmdline argument
 R:              Do this replacement and assign back to a:
   `(.)\1+`       This regex (matches 2 or more of same character in a row)
           #_.B   Replace with callback function: concatenate (length of full match) and
                  (first capture group)
                  Note: #_.B is a shortcut form for {#a.b}

Fm"..."Z"..."I#Ya@?@maRA:ym@1  Do the bulk of rules 2-25

  "..."                        String of letters to replace
       Z"..."                  Zip with string of characters to replace with
Fm                             For each m in the zipped list:
                   @m           First item of m is letter to replace
                a@?             Find its index in a, or nil if it isn't in a
               Y                Yank that into y
             I#                 If len of that is truthy:*
                     aRA:        Replace character in a at...
                         y        index y...
                          m@1     with second item of m

aR'W"uu"R'U"2u"  Clean up substitution
                 In the previous step, the replacements each had to be a single character.
                 This doesn't work for uu and 2u, so we use W and U instead (safe, since
                 uppercase letters won't be in the input) and replace them here with the
                 correct substitutions.
aR'W"uu"         In a, replace W with uu
        R'U"2u"  and U with 2u
                 and print the result (implicit)

* a@?m@0nil かどうかをテストする必要があります。0は正当なインデックスで偽であるため、それが真実であることをテストするだけでは十分ではありません。Pipには値がnilであるかどうかをテストするための短い組み込みの方法はありませんが、この場合、その長さのテストは十分に機能します:任意の数字の長さは少なくとも1(真)であり、nilの長さはnil(偽)です。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.