การแปลงอีเมลอย่างมหัศจรรย์! หรือ: ช่วย NSA ดึงข้อมูลเมตาของคุณจากที่อยู่อีเมลของคุณ


17

เมื่อได้รับที่อยู่อีเมลผลลัพธ์ของการแปลงที่ใช้กับที่อยู่อีเมลนั้นและที่อยู่อีเมลที่สองจะส่งคืนผลลัพธ์ของการแปลงแบบเดียวกันที่ใช้กับที่อยู่อีเมลที่สอง

ที่อยู่อีเมลทั้งหมดจะมีโครงสร้างดังต่อไปนี้:

สตริงที่มีความยาวเป็นบวกที่มีอักขระตัวอักษรผสมตัวเลขและอย่างน้อยหนึ่งตัว.(ส่วนท้องถิ่น) ตามด้วย@สัญลักษณ์ตามด้วยสตริงที่มีความยาวเป็นบวกที่มีตัวอักษรและตัวเลข sumbols (โดเมน) ตามด้วย.สัญลักษณ์และสตริงสุดท้ายของความยาวเป็นบวก มีอักขระที่เป็นตัวอักษรและตัวเลข (TLD)

การแปลงที่อนุญาตมีสี่แบบ:

  • ตัวตน (ไม่มีการเปลี่ยนแปลง) ( a.b@c.d -> a.b@c.d)
  • ส่งคืนเฉพาะส่วนท้องที่ (ทุกอย่างก่อน@) ไม่ได้แก้ไข ( a.b@c.d -> a.b)
  • การคืนค่าส่วนท้องถิ่นจะแบ่งออก.หากมีพร้อมกับสัญลักษณ์แรกของแต่ละครึ่ง ( a.b@c.d -> A B)
  • ส่งคืนเฉพาะโดเมน (ทุกสิ่งระหว่าง@และถึงขั้นสุดท้าย.) ที่ไม่ได้แก้ไข ( a.b@c.d -> c)

เมื่อมีการแปลงมากกว่าหนึ่งครั้งคุณสามารถให้ผลลัพธ์ที่เป็นไปได้ ช่องว่างที่จุดเริ่มต้นและจุดสิ้นสุดของการส่งออกไม่สำคัญ แต่ในตรงกลางไม่ (เช่นถ้าคุณแยกa.bไปA Bควรจะมีเพียงหนึ่งช่องว่างตรงกลาง [และหมายเลขใด ๆ ที่จุดเริ่มต้นและจุดสิ้นสุดของการส่งออก] แต่ถ้าคุณแยกa.จากนั้นAด้วยช่องว่างจำนวนเท่าใดก็ได้ที่ยอมรับได้)

ตัวอย่าง ( input | output):

john.doe@gmail.com, John Doe, phillip.maini@gmail.com         | Phillip Maini
John.Doe@gmail.com, John Doe, Phillip.Maini@gmail.com         | Phillip Maini
foo.bar@hotmail.com, foo.bar, gee.whizz@outlook.com           | gee.whizz
foo.bar@hotmail.com, foo.bar, gEe.Whizz@outlook.com           | gEe.Whizz
rodney.dangerfield@comedy.net, comedy, michael.scott@office.0 | office
.jones@x.1, Jones, a.@3.z                                     | A
.jones@x.1, .jones@x.1, a.@3.z                                | a.@3.z
.jones@x.1, .jones, a.@3.z                                    | a.
.jones@x.1, x, a.@3.z                                         | 3
.@b.c, .@b.c, 1@2.3                                           | 1@2.3
john.jones@f.f, John Jones, 1in.thehand@2inthe.bush           | 1in Thehand
chicken.soup@q.z, Chicken Soup, fab@ulou.s                    | Fab
lange@haare.0, lange, fat.so@fat.net                          | fat.so
Lange@haare.0, Lange, fat.so@fat.net                          | {fat.so, Fat So} # either acceptable
chicken@chicken.chicken, chicken, horse@pig.farm              | {horse, pig} # either acceptable

ใช้กฎและช่องโหว่ตามปกติ


กรณีทดสอบสุดท้ายไม่ควรส่งคืน "ม้า" หรือไม่ ฉันไม่เห็นว่าทำไมจึงสามารถส่งคืน "หมู" แทน
Erik the Outgolfer

3
@EriktheOutgolfer เนื่องจากการแปลงที่ 4 คือการส่งคืนโดเมน (ส่วนระหว่าง@และสุดท้าย.) เนื่องจากส่วนและโดเมนในตัวเครื่องมีทั้งคู่chickenจึงคลุมเครือไม่ว่าจะเป็นการแปลงครั้งที่ 2 หรือครั้งที่ 4
LangeHaare

โอ้ฉันตีความผิดไป
Erik the Outgolfer

เราอาจต้องการให้อินพุตที่เกี่ยวข้องถูกจัดรูปแบบด้วยช่องว่างในทุกกรณี (เช่นในการทดสอบโดยที่เอาต์พุตเป็นA[ด้วยช่องว่างต่อท้าย] ที่อินพุตที่สองเป็นJones[ด้วยช่องว่างนำหน้า])?
Jonathan Allan

ผมไม่เข้าใจว่าทำไม.jones@x.1, Jones, a.@3.zเป็นA- ถ้าjonesเป็นคู่ที่หมายถึงส่วนที่ตรงกันเป็นส่วนหนึ่งระหว่างช่วงแรกและ @ สัญลักษณ์ แต่นั่นจะส่งผลให้สตริงว่างเพราะaเป็นก่อนช่วงเวลาแรกและไม่หลังจาก
Jerry Jeremiah

คำตอบ:


4

Java 8, 254 240 236 ไบต์

(a,b,c)->{String A[]=a.split("@"),C[]=c.split("@"),x="";for(String p:C[0].split("\\."))x+=(p.charAt(0)+"").toUpperCase()+p.substring(1)+" ";return a.equals(b)?c:A[0].equals(b)?C[0]:A[1].split("\\.")[0].equals(b)?C[1].split("\\.")[0]:x;}

-4 ไบต์ขอบคุณที่@LukeStevens

คำอธิบาย:

ลองที่นี่

(a,b,c)->{                  // Method with three String parameters and String return-type
  String A[]=a.split("@"),  //  Split `a` by "@" into two parts
         C[]=c.split("@"),  //  Split `c` by "@" into two parts
         x="";              //  Temp-String
  for(String p:C[0].split("\\.")) 
                            //  Loop over the first part of `c`, split by dots
    x+=                     //   Append String `x` with:
       (p.charAt(0)+"").toUpperCase()
                            //    The first character as uppercase
       +p.substring(1)      //    + the rest of the String
       +" ";                //    + a space
                            //  End of loop (implicit / single-line body)
  return a.equals(b)?       //  If input `a` and `b` are exactly the same:
    c                       //   Return `c`
   :A[0].equals(b)?         //  Else-if the first part of `a` equals `b`:
    C[0]                    //   Return the first part of `c`
   :A[1].split("\\.)[0].equals(b)?
                            //  Else-if the domain of `a` equals `b`
    C[1].split("\\.)[0]     //   Return the domain of `c`
   :                        //  Else:
    x;                      //   Return String `x`
}                           // End of method

1
คุณสามารถเราะ 4 ไบต์โดยใช้แทน(p.charAt(0)+"").toUpperCase() Character.toUpperCase(p.charAt(0))
ลุคสตีเวนส์

@ LukeStevens ขอบคุณ! (char)(p.charAt(0)&~32)ตอนแรกฉันมีแต่สิ่งนี้ไม่ได้ผลเนื่องจาก1in Thehandกรณีทดสอบ แต่การใช้ตัวพิมพ์ใหญ่ในฐานะ String นั้นสั้นกว่าจริงCharacter.toUpperCaseๆ ขอบคุณมาก!
Kevin Cruijssen

3

Haskell , 208 ไบต์

import Data.Char
s c""=[]
s c a=w:f t where
 (w,t)=span(/=c)a
 f(_:y)=s c y
 f _=[]
h=head
u""=""
u(x:y)=toUpper x:y
l=h.s '@'
f x y=h[t|t<-[id,l,unwords.filter(/="").map u.s '.'.l,h.s '.'.last.s '@'],t x==y]

ลองออนไลน์!

มันเป็นเรื่องน่าเศร้าที่ฉันต้องใช้เวลา 59 ไบต์ในการสร้างใหม่split( s)

โซลูชันสร้างรายการการแปลงและส่งกลับรายการแรกที่นำไปสู่ผลลัพธ์ที่คาดหวัง


ยินดีต้อนรับสู่เว็บไซต์! ฉันไม่รู้จัก Haskell แต่เป็นไปได้ไหมที่จะลบอักขระช่องว่างเช่นบรรทัดใหม่และช่องว่างออก?
caird coinheringaahing

คำตอบแรกที่ดี! คุณอาจจะสนใจในคอลเลกชันของเราเคล็ดลับสำหรับการเล่นกอล์ฟใน Haskellโดยเฉพาะอย่างยิ่งนี้และนี้ควรบันทึกไบต์บาง
Laikoni

อย่าลังเลที่จะเข้าร่วมกับเราในOf Monads and Menห้องแชทสำหรับเล่นกอล์ฟและพูดคุยทั่วไปของ Haskell
Laikoni

3

เยลลี่ขนาด 40 ไบต์

ต้องขอบคุณErik the Outgolferสำหรับการสังเกตความล้มเหลวในการใช้งานŒt(ชื่อเรื่อง) และด้วยเหตุนี้Œu1¦€Kมากกว่าŒtK

-1 ไบต์ด้วยErik the Outgolfer (การจัดเรียงใหม่⁵⁸ç⁹¤Ŀถึงçµ⁵⁸Ŀ)


ÑṪṣ”.Ḣ
ṣ”@
ÇḢ
Çṣ”.Œu1¦€K
⁹ĿðЀ5i
çµ⁵⁸Ŀ

สละโปรแกรมเต็มรูปแบบexampleEmail, exampleOutput, realEmailและการพิมพ์ผล

ลองออนไลน์!

อย่างไร?

ดำเนินการแปลงทั้งสี่ตัว (รวมถึงตัวตั้งต้น) ค้นหาอันแรกที่ให้ตัวอย่างจากอีเมลแรกจากนั้นนำไปใช้กับอีเมลที่สอง:

            - Link 1, do nothing: email
            - do nothing but return the input

ÑṪṣ”.Ḣ      - Link 2, the domain: email
Ñ           - call the next link (3) as a monad (split at "@")
 Ṫ          - tail
  ṣ”.       - split at "."
     Ḣ      - head

ṣ”@         - Link 3, split at @: email
ṣ”@         - split at "@"

ÇḢ          - Link 4, local part: email
Ç           - call the last link (3) as a monad (split at "@")
 Ḣ          - head

Çṣ”.Œu1¦€K  - Link 5, name-ified: email
Ç           - call the last link (4) as a monad (get the local part)
 ṣ”.        - split at "."
       ¦€   - for €ach sparsley apply:
      1     - ...to index: 1
    Œu      - ...action: uppercase
         K  - join with space(s)

⁹ĿðЀ5i     - Link 6, index of first correct link: exampleEmail; exampleOutput
   Ѐ5      - map across (implicit range of) 5 (i.e. for each n in [1,2,3,4,5]):
  ð         -   dyadicly (i.e. with n on the right and exampleEmail on the left):
 Ŀ          -     call referenced link as a monad:
⁹           -     ...reference: chain's right argument, n
      i     - first index of exampleOutput in the resulting list

çµ⁵⁸Ŀ       - Main link: exampleEmail; exampleOutput
ç           -   call the last link (6) as a dyad (get the first "correct" link index)
 µ          - monadic chain separation (call that L)
   ⁸        - chain's left argument, L
    Ŀ       - call the link at that reference as a monad with input:
  ⁵         -   program's third input, realEmail

หมายเหตุ:

  1. สมมติว่าอินพุทเอาท์พุทเอาท์พุทเป็นแบบเดียวกับเอาท์พุท

  2. "precursor" (ผลลัพธ์ของลิงค์ 3) ได้รับการทดสอบสำหรับการจับคู่exampleOutputแต่มันจะไม่ตรงกันเว้นแต่ว่าexampleOutputตัวเองจะเป็นรายการของตัวละคร ดังนั้นควรป้อนข้อมูลเช่นนี้ (อาจใช้การจัดรูปแบบ Python ที่นี่) เพื่อหลีกเลี่ยงความเป็นไปได้ของการตีความดังกล่าว




2

JavaScript (ES6), 145 ไบต์

เรียกใช้ด้วยไวยากรณ์การแก้ปัญหาเช่น f('chicken.soup@q.z')('Chicken Soup')('fab@ulou.s')

x=>y=>[x=>x,s=x=>x.split`@`[0],x=>s(x).split`.`.map(w=>w&&w[0].toUpperCase()+w.slice(1)).join` `.trim(),x=>/@(.+)\./.exec(x)[1]].find(f=>f(x)==y)




1

CJam, 42

q~@{[_\'@/~'./0=\_'.%{(eu\+}%S*]}:T~@a#\T=

ลองออนไลน์

คำอธิบาย:

q~        read and evaluate the input (given as 3 quoted strings)
@         bring the first string to the top of the stack
{…}:T     define a function T that calculates the 4 transformations of a string:
  [       begin array
  _\      duplicate the string, and swap with the other copy to bring it in the array
           (1st transformation)
  '@/~    split by '@' and put the 2 pieces on the stack
  './0=   split the 2nd piece by '.' and keep the first part
           (4th transformation)
  \_      swap with the piece before '@' and duplicate it
           (2nd transformation)
  '.%     split by '.', removing the empty pieces
  {…}%    transform the array of pieces
    (eu   take out the first character and capitalize it
    \+    prepend it back to the rest
  S*      join the pieces by space
           (3rd transformation)
  ]       end array
~         execute the function on the first string
@a        bring the 2nd string to the top of the stack, and wrap it in an array
#         find the position of this string in the array of transformations
\T        bring the 3rd string to the top and call function T
=         get the transformation from the array, at the position we found before

1

PHP 7.1, 176 ไบต์

<?$e=explode;[,$p,$q,$r]=$argv;echo$p==$q?$r:($e('@',$p)[0]==$q?$e('@',$r)[0]:($e('.',$e('@',$p)[1])[0]==$q?$e('.',$e('@',$r)[1])[0]:ucwords(join(' ',$e('.',$e('@',$r)[0])))));

ลองออนไลน์!

PHP <7.1, 180 ไบต์

รุ่นอายุต่ำกว่า 7.1 จะต้องมีการเปลี่ยนแปลง[,$p,$q,$r]=$argvเพื่อlist(,$p,$q,$r)=$argvเพิ่ม 4 ไบต์


1

GNU sed , 105 + 1 (แฟล็ก r) = 106 ไบต์

สามคนแรกsคำสั่งตรวจสอบบัตรประจำตัว , ส่วนท้องถิ่นและโดเมนการเปลี่ยนแปลงตามลำดับ หากการแปลงหนึ่งรายการตรงกันจะถูกนำไปใช้กับที่อยู่อีเมลที่สองและรายการต่อไปนี้sคำสั่งจะล้มเหลวเนื่องจากไม่มีรูปแบบอินพุต

s:^(.*),\1,::
s:(.*)@.*,\1,(.*)@.*:\2:
s:.*@(.*)\..*,\1,.*@(.*)\..*:\2:
s:.*,([^.]*)\.?(.*)@.*:\u\1 \u\2:

ลองออนไลน์!

การแยกส่วนโลคัล ( sคำสั่งสุดท้าย) มีราคาแพงที่สุดในการตรวจสอบในแง่ของไบต์ดังนั้นฉันวางไว้ที่ท้ายและถือว่ามันตรงกับ (ตั้งแต่คนอื่นล้มเหลวในเวลานั้น) ไปที่แอปพลิเคชันโดยตรง


1

เยลลี่ 43 ไบต์

ḢŒlṣ”.Œu1¦€K
ṣ”@Wẋ4j”@$ḷ/ÇṪṣ”.Ḣ$$4ƭ€
Çiị⁵Ǥ

ลองออนไลน์!


จะŒtKทำงานแทนŒu1¦€Kประหยัด 3 หรือไม่
Jonathan Allan

... และสิ่งที่ต้องการŒlคืออะไร?
Jonathan Allan

^ อาผมเห็นว่าจะไม่ทำงานร่วมกับ1in.thehand ŒtK
Jonathan Allan

@JanathanAllan ใช่นั่นคือเหตุผลที่ฉันไม่ได้ใช้และเหตุผลของคำตอบ (ลบแล้ว) ของ ovs นั้นไม่ถูกต้อง ( str.title)
Erik the Outgolfer
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.