ใส่รหัสผ่านของฉัน


17

คำทั่วไปควรหลีกเลี่ยงเพื่อใช้เป็นรหัสผ่าน ความท้าทายนี้เป็นเรื่องเกี่ยวกับการเขียนโปรแกรมเป็นโปรแกรมที่ง่ายมากที่ munges รหัสผ่านที่กำหนด ( M odify U ntil N OT G uessed E asily)

อินพุต

abcdefghijklmnopqrstuvwxyzคำซึ่งเป็นสตริงที่เขียนในตัวอักษร ไม่สำคัญว่าตัวอักษรจะเป็นตัวพิมพ์เล็กหรือตัวพิมพ์ใหญ่

Munging

  1. เปลี่ยนลำดับของตัวอักษรเดียวกันซ้ำไปซ้ำมาด้วยตัวเองนำหน้าด้วยจำนวนครั้งที่จดหมายซ้ำ ( LLLLกับ4L)
  2. เปลี่ยนก่อนaด้วย@
  3. เปลี่ยนก่อนbด้วย8
  4. เปลี่ยนก่อนcด้วย(
  5. เปลี่ยนก่อนdด้วย6
  6. เปลี่ยนก่อนeด้วย3
  7. เปลี่ยนก่อนfด้วย#
  8. เปลี่ยนก่อนgด้วย9
  9. เปลี่ยนก่อนhด้วย#
  10. เปลี่ยนก่อนiด้วย1
  11. เปลี่ยนวินาทีiด้วย!
  12. เปลี่ยนก่อนkด้วย<
  13. เปลี่ยนก่อนlด้วย1
  14. เปลี่ยนวินาทีlด้วยi
  15. เปลี่ยนก่อนoด้วย0
  16. เปลี่ยนก่อนqด้วย9
  17. เปลี่ยนก่อนsด้วย5
  18. เปลี่ยนวินาทีsด้วย$
  19. เปลี่ยนก่อนtด้วย+
  20. เปลี่ยนก่อนvด้วย>
  21. เปลี่ยนวินาทีvด้วย<
  22. เปลี่ยนก่อนwด้วยuu
  23. เปลี่ยนวินาทีwด้วย2u
  24. เปลี่ยนก่อนxด้วย%
  25. เปลี่ยนก่อนyด้วย?

ต้องใช้กฎข้อ 1 ตามจำนวนครั้งที่ต้องการจนกว่าจะไม่สามารถใช้งานได้อีก หลังจากนั้นจะใช้กฎที่เหลือ

เอาท์พุทคำ munged

ตัวอย่าง

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

นี่คือดังนั้นโปรดทำให้โปรแกรมของคุณสั้นที่สุด!

ไม่ควรใช้สิ่งใดในโพสต์นี้เป็นแนวคิดรหัสผ่านหรือเป็นส่วนหนึ่งของแนวทางปฏิบัติเกี่ยวกับรหัสผ่าน


18
ความจริงที่ว่าโปรแกรมเช่นนี้เป็นไปได้หมายความว่าผู้โจมตีสามารถเขียนและใส่รหัสผ่าน (และลองใช้ munges ต่างๆ) ได้อย่างง่ายดาย (ง่ายยิ่งขึ้นเพราะพวกเขามักจะเข้าถึงฮาร์ดแวร์ที่ดีกว่า) ดังนั้นเพื่อความปลอดภัยฉันจะพูดว่า: ไม่มีสิ่งใดในโพสต์นี้ควรใช้เป็นแนวคิดรหัสผ่านหรือเป็นส่วนหนึ่งของการปฏิบัติรหัสผ่านใด ๆ
นิวแฮมป์เชียร์

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 ไบต์เนื่องจากกฎมีการเปลี่ยนแปลง .. ( แก้ไข: ในที่สุดก็กลับไปที่ 237 ไบต์แรกของฉัน ) การแทนที่WWWWด้วย222WJava เป็นเรื่องง่าย แต่4Wไม่ใช่ .. ถ้าเพียง Java มีวิธีใช้ regex capture-group สำหรับบางอย่าง รับความยาวด้วยการ"$1".length()แทนที่การจับคู่ตัวเองด้วย"$1".replace(...)การแปลงการแข่งขันเป็นจำนวนเต็มด้วยnew Integer("$1")หรือใช้สิ่งที่คล้ายกับ Retina (เช่นs.replaceAll("(?=(.)\\1)(\\1)+","$#2$1")) หรือ JavaScript (ie s.replaceAll("(.)\\1+",m->m.length()+m.charAt(0))) จะเป็นหมายเลข 1 ของฉันสิ่งที่ฉันต้องการเห็นใน Java ใน ในอนาคตที่จะได้รับประโยชน์ codegolfing .. >.> ฉันคิดว่านี่เป็นครั้งที่ 10 + เวลาที่ฉันเกลียด Java ไม่สามารถทำอะไรกับการจับคู่จับภาพกลุ่ม ..
-78 ไบต์ขอบคุณที่@ OlivierGrégoire

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ตามลำดับที่ระบุโดยการท้าทาย รายการในแต่ละชุดเป็นอาร์เรย์หรือสตริงที่มีสองรายการที่เป็นแล้วแพร่กระจาย ( ...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

05AB1E , 69 ไบต์

-9 ไบต์ขอบคุณ Emigna

γvygD≠×yÙ}J.•k®zĀÒĀ+ÎÍ=ëµι
•"@8(63#9#1<1095+>%?!i$<"ø'w„uu„2u‚â«vy`.;

ลองออนไลน์!


คุณสามารถใช้'w„uu„2u‚â
Emigna

ได้โปรดตรวจสอบผลลัพธ์สำหรับ wwww เป็นอินพุตได้ไหม
mdahmoune

@mdahmoune It outputs4uu
Okx

@Emigna ผลิตภัณฑ์คาร์ทีเซียนความคิดที่ดี
Okx

ส่วนแรกสามารถเป็นγvygD≠×yÙ}J
Emigna

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/

ลองออนไลน์!


กรุณาคุณหมายถึงอะไร (-p)
mdahmoune

1
@mdahmoune -pใช้เป็นอาร์กิวเมนต์perlในบรรทัดคำสั่งที่จะอ่านอินพุตSTDINและprintเนื้อหาของ$_ตอนท้ายสคริปต์โดยอัตโนมัติ TIO อนุญาตให้ใช้ตัวเลือกนั้นและเนื่องจากperl -pe<code>มีมากกว่า 1 ไบต์perl -e<code>จึงนับเป็นหนึ่งไบต์เพิ่มเติม
Dom Hastings

ฉันคิดว่าคุณได้ทำพิมพ์ผิดควรไม่~ระหว่างj~kเป็น!แทน? ปัจจุบันมันถูกแทนที่ด้วยการเกิดที่สองของการiมีแทน~ !
Kevin Cruijssen

@Xcali #testingonproduction
NieDzejkob

2
@NieDzejkob ไม่มีที่ไหนดีกว่านี้อีกแล้ว นั่นเป็นวิธีเดียวที่คุณรู้ว่ามันจะทำงานในการผลิต
Xcali

4

อาจไม่ใช่นักกอล์ฟที่มากที่สุด แต่เป็นไปได้

-6 ไบต์ขอบคุณ ovs

-77 ไบต์ขอบคุณ NieDzejkob และ Jonathan French

Python 3 , 329 323 bytes 246 bytes

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

321 ไบต์
Jonathan Frech

1
320 ไบต์
Jonathan Frech

2
จริงๆแล้วคำตอบของคุณไม่ได้ผล jjjmjjjjเอาท์พุทควรแต่เอาท์พุท3jm4j 3jm3jjแก้ไข: 258 bytesกับปัญหานี้ได้รับการแก้ไข
NieDzejkob

3

เรติน่า166 166ไบต์

(.)\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%?_`¶.

แก้ไขการเกิดขึ้นครั้งแรกของตัวอักษรอื่น ๆ ทั้งหมดจากaถึงyและลบตัวยึด

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

ทำเครื่องหมาย (เดิม) ที่สองเกิดขึ้นของตัวอักษรi,l , s, vหรือwกับตัวยึด

¶w
2u

wแก้ไขการเกิดที่สองของ

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

แก้ไขการเกิดขึ้นครั้งที่สองของตัวอักษรสี่ตัวอื่น ๆ


คุณคิดว่าเป็นไปได้ไหมที่จะตีกอล์ฟต่อไป?
mdahmoune

@mdahmoune ใช่ฉันคิดว่าฉันสามารถบันทึกได้ 33 ไบต์
Neil

ฉันโหวตคำตอบของคุณ :) มันจะดีถ้าคุณบันทึก 33 bytes;)
mdahmoune

@mdahmoune ข่าวดีฉันบันทึก 42 ไบต์จริง ๆ !
Neil

เยี่ยมมากรหัสของคุณสั้นที่สุด;)
mdahmoune

3

Haskell , 221 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 เป็นอักขระหลายตัว) เพื่อแทนที่ ฉันใส่สตริงที่เข้ารหัสเหล่านี้ในสตริงขนาดใหญ่หนึ่งเส้นคั่นด้วยช่องว่างเพื่อที่wordsสามารถแบ่งเป็นรายการสำหรับฉัน

แก้ไข: ขอบคุณ @Laikoni ที่ช่วยฉัน 5 ไบต์! นั่นเป็นการใช้ที่ฉลาดของ$ฉันไม่ได้คิด ฉันยังไม่รู้<-เคล็ดลับนั่น


Thanx สำหรับคำอธิบายอย่างละเอียด;)
mdahmoune

1
คุณสามารถใช้(p,q)<-span(==a)bแทนlet(p,q)=span(==a)bและแทนp>[] p/=[]
Laikoni

2
ประหยัดอีกสองไบต์ด้วยการทำให้mpointfree: ($(f<$>words"w2u ... y?")++[r]).foldr($) ลองออนไลน์!
Laikoni

2

ลัวะ 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)

ลองออนไลน์!

Ungolfed และอธิบาย:

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 Core), 317 , 289 , 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;};

ลองออนไลน์!

ฉันหวังว่ามันโอเคที่จะรับอาร์เรย์ถ่านเป็นอินพุตและไม่ใช่สตริง

Ungolfed :

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 ไบต์ขอบคุณZacharý

#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 หมายความว่าควรแทนที่ "ถ่านต่อไป" อันที่สองตามด้วยสิ่งต่อไปนี้

ลองออนไลน์


เยี่ยมมากคุณช่วยเพิ่มลิงค์ tio.run ได้ไหม?
mdahmoune

การเชื่อมโยง @mdahmoune TIO เพิ่มมีรหัสในการทดสอบสำหรับกรณีการทดสอบของคุณ :)
HatsuPointerKun

494 ไบต์และอัปเดตลิงค์ TIO ตามหากคุณเปลี่ยน
Zacharý

@ Zacharýคุณต้องใส่ช่องว่างระหว่างชื่อแมโครและเนื้อหามาโครมิฉะนั้นจะเกิดข้อผิดพลาดเมื่อคอมไพล์ด้วย C ++ 17. นอกจากนี้คุณรู้วิธีลบลิงค์ TIO หรือไม่ (ตั้งแต่รุ่นเก่าไม่มีประโยชน์)
HatsuPointerKun


2

R , 224 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 ไบต์โดยจัดเรียง args ใหม่ ไม่ได้สร้างความแตกต่างขนาดใหญ่ที่ฉันรู้;)
JayCe

@ JayCe ฉันพบไบต์เพิ่มเติม :-) อีกครั้ง
Giuseppe


1

Python 2 , 220 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)

ลองออนไลน์!


Thanx Tfeld 192 bytes tio.run/…
mdahmoune

การเล่นกอล์ฟที่ยอดเยี่ยม;)
mdahmoune

186 ไบต์ คุณสามารถนำพอร์ตนี้ไปยัง Python 3 ได้อย่างง่ายดายใน192 ไบต์แต่ฉันไม่คิดว่ามันควรจะเป็นคำตอบที่แยกต่างหาก
NieDzejkob

@NieDzejkob ดูเหมือนว่าเวอร์ชั่น Python 2 ของคุณที่เล่นกอล์ฟจะให้ผลลัพธ์ที่แตกต่างจากเวอร์ชั่นปัจจุบันของ OP หรือ Python 3 ของคุณ
Jonathan Frech

@JomathanFrech ขออภัยเช่นเคยทดสอบเกี่ยวกับการผลิต 188 ไบต์
NieDzejkob

1

Pip , 103 102 ไบต์

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

ลองออนไลน์!

คำอธิบาย

รหัสทำการแปลงสามขั้นตอน:

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@0เป็นศูนย์หรือไม่ ไม่เพียงพอที่จะทดสอบว่ามันเป็นความจริงเนื่องจาก 0 เป็นดัชนีที่ถูกต้องตามกฎหมายซึ่งเป็นเท็จ Pip ไม่มีวิธีการทดสอบระยะสั้นในตัวหากค่าเป็นศูนย์ แต่การทดสอบความยาวของมันทำงานได้ดีพอในกรณีนี้: หมายเลขใด ๆ จะมีความยาวอย่างน้อย 1 (จริง) และไม่มีศูนย์มีความยาวเท่ากับศูนย์

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.