String Zip และเรียงลำดับ


14

กำหนดรายการของสตริงเอาท์พุทสตริงเดียวที่เกิดขึ้นโดยการใช้อักขระจากแต่ละสตริงที่แต่ละตำแหน่งเรียงลำดับตามลำดับ ASCII และต่อท้ายพวกเขาเพื่อสตริงออก กล่าวอีกนัยหนึ่งสำหรับnสตริงอินพุตอักขระnตัวแรกของเอาต์พุตจะเป็นอักขระตัวแรกของแต่ละอินพุตเรียงตามลำดับnตัวอักขระที่สองของเอาต์พุตจะเป็นอักขระตัวที่สองของอินพุตที่เรียงลำดับตามลำดับและดังนั้น บน. คุณอาจคิดว่าสตริงนั้นมีความยาวเท่ากันและจะมีสตริงอย่างน้อยหนึ่งสตริง สตริงทั้งหมดจะประกอบด้วยอักขระที่พิมพ์ได้ ASCII เท่านั้น (เลขที่ 32-127)

การใช้งานอ้างอิงใน Python ( ลองออนไลน์ ):

def stringshuffle(strings):
  res = ''
  for i in range(len(strings[0])):
    res += ''.join(sorted([s[i] for s in strings],key=ord))
  return res

ตัวอย่าง:

"abc","cba" -> "acbbac"
"HELLO","world","!!!!!" -> "!Hw!Eo!Lr!Ll!Od"

กฎระเบียบ

  • ช่องโหว่มาตรฐานเป็นสิ่งต้องห้าม
  • นี่คือดังนั้นคำตอบที่สั้นที่สุดในจำนวนไบต์ชนะ

ลีดเดอร์บอร์ด

สแต็คส่วนย่อยที่ด้านล่างของโพสต์นี้สร้างกระดานแต้มนำจากคำตอบ a) เป็นรายการคำตอบสั้นที่สุดต่อภาษาและ b) เป็นกระดานแต้มนำโดยรวม

เพื่อให้แน่ใจว่าคำตอบของคุณปรากฏขึ้นโปรดเริ่มคำตอบด้วยหัวข้อโดยใช้เทมเพลต Markdown ต่อไปนี้:

## Language Name, N bytes

ที่Nมีขนาดของส่งของคุณ หากคุณปรับปรุงคะแนนของคุณคุณสามารถเก็บคะแนนเก่าไว้ในบรรทัดแรกโดยการตีพวกเขาผ่าน ตัวอย่างเช่น

## Ruby, <s>104</s> <s>101</s> 96 bytes

หากคุณต้องการรวมหลายตัวเลขไว้ในส่วนหัวของคุณ (เช่นเนื่องจากคะแนนของคุณคือผลรวมของสองไฟล์หรือคุณต้องการแสดงรายการการลงโทษการตั้งค่าสถานะของล่ามแยกต่างหาก) ตรวจสอบให้แน่ใจว่าคะแนนจริงเป็นตัวเลขสุดท้ายในส่วนหัว:

## Perl, 43 + 2 (-p flag) = 45 bytes

คุณยังสามารถตั้งชื่อภาษาให้เป็นลิงค์ซึ่งจะปรากฏในตัวอย่าง:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

คำตอบ:


11

GS2 , 4 ไบต์

*Ü■/

สิ่งนี้อ่านสตริงจาก STDIN โดยคั่นด้วย linefeeds

ซอร์สโค้ดใช้การเข้ารหัสCP437 ลองออนไลน์!

ทดสอบการทำงาน

$ xxd -r -ps <<< '2a 9a fe 2f' > zip-sort.gs2
$ echo -e 'HELLO\nworld\n!!!!!' | gs2 zip-sort.gs2 
!Hw!Eo!Lr!Ll!Od

มันทำงานอย่างไร

*       Split the input into the array of its lines.
 Ü      Zip the resulting array.
  ■     Map the rest of the program over the resulting array.
   /        Sort.

6

Haskell, 39 36 ไบต์

import Data.List
(>>=sort).transpose

ตัวอย่างการใช้งาน: ->((>>=sort).transpose) ["HELLO","world","!!!!!"]"!Hw!Eo!Lr!Ll!Od"

ย้ายรายการสตริงคัดลอกเหนือสตริงsortและเชื่อมโยงรายการผลลัพธ์ของสตริง ( >>=ในบริบทรายการคือconcatMap)


ฉันมากับสิ่งนี้!
ภูมิใจ haskeller

ฉันไม่ได้; ฉันลืมที่จะใช้ประโยชน์จากอินสแตนซ์ Monad สำหรับสิ่งต่าง ๆ เช่นรายการ (+1)
ballesta25


5

TeaScript ขนาด 9 ไบต์

_t¡ßlp¡)µ

TeaScript มีการติดตั้งบิวด์อินที่ถูกต้องในรูปแบบที่ผิด

ลองออนไลน์

Ungolfed

_t()m(#lp())j``

คำอธิบาย

_t()        // Transposes input array
    m(#     // Loops through inputs
       lp() // Sorts characters by char code
     )
j``         // Joins back into string

@intrepidcoder ใช้งานได้ดีสำหรับฉัน บางทีเบราว์เซอร์ของคุณแคชไฟล์บางไฟล์? บางทีการล้างแคชของคุณอาจใช้งานได้ ฉันใช้ Safari อยู่ ฉันจะลองรีเฟรชไฟล์
Downgoat




3

อ็อกเทฟ 15 ไบต์

@(a)sort(a)(:)'

ตัวอย่าง:

octave:1> (@(a)sort(a)(:)')(["abc";"cba"])
ans = acbbac
octave:2> (@(a)sort(a)(:)')(["HELLO";"world";"!!!!!"])
ans = !Hw!Eo!Lr!Ll!Od

2

จูเลีย 46 ไบต์

x->(j=join)(map(i->j(sort([i...])),zip(x...)))

สิ่งนี้จะสร้างฟังก์ชันที่ไม่มีชื่อที่ยอมรับอาร์เรย์ของสตริงและส่งคืนสตริง หากต้องการเรียกใช้ให้ตั้งชื่อเช่นf=x->...เรียกว่าให้มันชื่อเช่น

Ungolfed:

function zipsort{T<:AbstractString}(x::Array{T,1})
    # Splat the input array and zip into an iterable
    z = zip(x...)

    # For each tuple consisting of corresponding characters
    # in the input array's elements, splat into an array,
    # sort the array, and join it into a string
    m = map(i -> join(sort([i...])), z)

    # Take the resulting string array and join it
    return join(m)
end

1

Minkolang 0.13 , 46 bytes

$od0Z2:$zIz:$xd0G2-[i1+z[di0c*+c$r]xz$(sr$Ok].

ลองที่นี่ ต้องการอินพุตเหมือน"HELLO""world""!!!!!"(ดังนั้นไม่มีเครื่องหมายจุลภาค)

คำอธิบาย

$o     Read in whole input as characters
d      Duplicate top of stack (the ")
0Z     Count how often this appears in the stack
2:     Divide by two
$z     Store this in the register (z)
Iz:    Length of stack divided by z (k)
$x     Dump one element from the front/bottom of stack
d      Duplicate top of stack (which is k)
0G     Insert it at the front/bottom of stack
2-     k-2

  [                              Open for loop that repeats k-2 times
   i1+                           Loop counter + 1 (i)
      z[                         Open for loop that repeats z times
        d                        Duplicate top of stack (which is i)
         i                       Loop counter (j)
          0c                     Copy k from front of stack
            *                    Multiply (j*k)
             +                   Add (j*k + i)
              c                  Copy character at position j*k+i to the top
               $r                Swap top two elements of stack (so i is on top)
                 ]               Close for loop
                  x              Dump the top of stack (dump i)
                   z$(           Start a new loop with the top z elements
                      s          Sort
                       r$O       Reverse and output the whole (loop) stack as characters
                          k      Break - exits while loop
                           ].    Close for loop and stop


1

K, 10 ไบต์

,/{x@<x}'+

เข้าร่วม ( ,/) การจัดเรียงของ ( {x@<x}) แต่ละรายการ ( ') ของการย้าย (+ ) ของรายการของสตริง

ในการดำเนินการ:

  ,/{x@<x}'+("HELLO";"world";"!!!!!")
"!Hw!Eo!Lr!Ll!Od"

เรียบง่าย แต่ K นั้นเจ็บนิดนี่โดยไม่ได้มีตัวเดียวฟังก์ชั่นการจัดเรียงและแทนที่จะแบ่งการดำเนินงานเป็นกระจายรวบรวมผู้ประกอบการดัชนีและดั้งเดิมที่ทำให้เวกเตอร์การเปลี่ยนแปลงซึ่งจะเรียงลำดับรายการ@<


1

C ++ 14, 152 ไบต์

#include<iostream>
#include<regex>
[](auto s){for(int i=0;i<s[0].size();++i){auto r=""s;for(auto k:s)r+=k[i];std::sort(begin(r),end(r));std::cout<<r;}};

ไม่ได้ใช้ประโยชน์จากแผนที่ + zip (คาดเดาว่าทำไม)

Ungolfed + การใช้งาน

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

int main()
{
    auto lambda = [](auto s)
    {
        for (int i = 0; i < s[0].size(); ++i)
        {
            auto r = ""s;
            for (auto k : s)
                r += k[i];
            std::sort(begin(r), end(r));
            std::cout << r;
        }
    };

    std::vector<std::string> data = { "HELLO", "world", "!!!!!" };
    lambda(data);
}



1

PHP ,92 91 ไบต์

for($argv[0]='';$a=array_column(array_map(str_split,$argv),$i++|0);print join($a))sort($a);

ลองออนไลน์!

ฉันมั่นใจว่าสิ่งนี้จะสั้นลงได้โดยไม่พยายามใช้ฟังก์ชันอาร์เรย์ในตัวของ PHP แต่ต้องลอง!

หรือ 85 ไบต์

การแกว่งของ @ Night2 สั้นลงโดยไม่พยายามใช้ฟังก์ชั่นอาร์เรย์ในตัวของ PHP:

for(;''<$argv[1][$i++];print join($a))for($a=[];''<$a[]=$argv[++$$i][$i-1];sort($a));

ลองออนไลน์!


@ Night2 ทำได้ดีมาก! คุณควรโพสต์สิ่งนั้นเป็นของคุณเอง มันแย่มากที่array_columnจะไม่ทำงานกับอาเรย์ของสตริงมิฉะนั้นมันจะมีประโยชน์มากกว่าสำหรับ CG และแน่นอนว่าการต้องข้าม$argv[0]ก็มักจะเจ็บปวดเช่นกัน ...
640KB

0

Clojure / ClojureScript, 43 ไบต์

#(apply str(mapcat sort(apply map list %)))

สร้างฟังก์ชั่นที่ไม่ระบุชื่อ เขียนใน ClojueScript REPL ก็ควรเป็น Clojure ที่ถูกต้อง

ใส่มันนี่(*1 ["HELLO" "world" "!!!!!"])แล้วโทรผ่าน หรือทำแล้วใช้(def f *1)(f ["abc" "cba"])


0

Ceylon, 166

String z(String+l)=>String(expand(t(l).map(sort)));[T+]n<T>(T?+i)=>[for(e in i)e else nothing];{[X+]*}t<X>([{X*}+]l)=>l[0].empty then{}else{n(*l*.first),*t(l*.rest)};

ในขณะที่ Ceylon มีzipฟังก์ชั่นมันใช้เพียงสอง iterables แทน iterable ของพวกเขา unzipในอีกทางหนึ่งใช้ tuples ซ้ำได้และฉันไม่ต้องการแปลงสตริงของฉันเป็น tuples ดังนั้นผมจึงนำมาใช้ฟังก์ชั่น transpose ของตัวเองแรงบันดาลใจจากการดำเนินงาน Haskell ที่ Google พบสำหรับฉันอยู่ที่ไหนสักแห่ง

// zip-sort
//
// Question:  http://codegolf.stackexchange.com/q/64526/2338
// My answer: ...

// Takes a list of strings (same length), and produces
// a string made by concatenating the results of sorting
// the characters at each position.
String z(String+ l) =>
        String(expand(t(l).map(sort)));

// Narrow an iterable of potential optionals to their non-optional values,
// throwing an AssertionError if a null is in there.
[T+] n<T>(T?+ i) =>
        [for (e in i) e else nothing];

// Transpose a nonempty sequence of iterables, producing an iterable of
// sequences.
// If the iterables don't have the same size, either too long ones are
// cut off or too short ones cause an AssertionError while iterating.
{[X+]*} t<X>([{X*}+] l) =>
        l[0].empty
        then {}
        else { n(*l*.first), *t(l*.rest) };

ประเภทnและtสามารถกำหนดได้ทั่วไปมากขึ้น แต่นี่คือ Codegolf ;-) ( nเป็นกรณีพิเศษของสิ่งที่ฉันเสนอเมื่อassertNarrowสองสัปดาห์ก่อน )


0

Perl 6 , 33 ไบต์

{[~] flat ([Z] @_».comb)».sort}

ตัวอย่างการใช้งาน:

say {[~] flat ([Z] @_».comb)».sort}(< abc cba >) # acbbca

my &code = my $code = {[~] flat ([Z] @_».comb)».sort}

say code "HELLO","world","!!!!!"; # !Hw!Eo!Lr!Ll!Od

say ((<cba abc>),(<testing gnitset gttseni>)).map($code);
# (acbbac ggtentiststteisenngit)


0

เยลลี่ 3 ไบต์

ZṢ€

ลองออนไลน์!

เท่านั้นที่ถูกต้องถ้าถือว่าเป็นโปรแกรมเต็มรูปแบบ : ค่าที่เกิดขึ้นเป็นรายการของสตริง แต่เมื่อมันพิมพ์วุ้นโดยปริยาย flattens มัน

  €    Map
 Ṣ     sort
Z      over the columns of the input.

0

Stax , 5 ไบต์

LMFop

ใกล้กับ LMNOP :(

เรียกใช้และแก้ไขปัญหาได้ที่ staxlang.xyz!

ใส่อินพุตทั้งหมดลงในหนึ่งรายการของสตริง ( L) และย้ายรายการนี้ ( M) สำหรับแต่ละสตริงผลลัพธ์ ( F) ให้เรียง ( o) และพิมพ์ ( p) มัน

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