完成对称图


12

您将获得四分之一的地图,该地图在x轴和y轴上对称,作为输入。该程序应打印完整的地图。

该地图可以包含以下字符:-+/\|.,并且应该按预期的方式将其打开。输入数据始终为矩形且较小。

$ cat in
+---
|./.
|/..

$ ./solution < in
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+

最短的代码获胜。


对于输入数据到底意味着什么?量化有点模糊。
Joey

@Joey假设输入最多16行40个字符。

我意识到我没有时间限制,但是如果有人需要一秒钟以上的解决方案,我真的很想看到它。

Answers:


1

Golfscript-32个字符

n%{{.-1%{.3%2=115*^}%+}%zip}2*n*

由于问题的对称性,我们重复两次{水平翻转,转置(zip)}。另外,您可以将值更改2为更大的数字以重复更多图像。

x^=155 if x%3==2由于字符空间的限制,字符转置按进行。{.5^3%(45+}长度也一样。


3

画布5 4 字节

║Q↷↷

首先是Canvas的答案,所以让我们从一个简单的答案开始。:)

-1个字节感谢@dzaima

在Canvas中镜像或旋转时,斜线会自动转换。
可以是1个字节在线尝试),但是不幸的是,当水平镜像时,它还会将点转换.为单引号'

在线尝试。

说明:

         # (Take the multi-line input implicitly as canvas object)
        # Palindromize the canvas object (without overlap)
       # Output it with a trailing newline (without popping)
  ↷↷    # Rotated the canvas object that's still on the stack by 90 degrees twice
         # (and output it implicitly as well at the end)

2

Windows PowerShell中,99 103 117 126 129

filter x{$_-split'/'-replace'\\','/'-join'\'}$input|%{$_+-join($_[40..0]|x)}|%{$_
$s=,($_|x)+$s}
$s

笔记:

  • 不幸的是,这需要PowerShell在打高尔夫球时不擅长的两件事:反转字符串(或值序列)和音译字符串中的内容。我相当确定这至少是Perl Ruby解决方案时间的两倍。

测试:

> gc in| .\map.ps1
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+

> gc in2
+\/
/\/
> gc in2| .\map.ps1
+\/\/+
/\/\/\
\/\/\/
+/\/\+

历史

  • 2011-02-09 11:10(129)–第一次尝试。
  • 2011-02-09 11:27(126)– OFS保存-join和存储99..0在变量中。
  • 2011-02-09 11:31(117) - -replace工作对数组,所以我并不需要三个-replace秒,但可以做一个-split-replace-join来代替。
  • 2011-02-09 15:03(105)–不要一次做同样的事情,而要一次做一次然后扭转它。将赋值放在括号中会使它向管道中吐出它的值:-)
  • 2011-02-09 15:08(103)–我不再需要$a了,因为99..0现在已经很少使用了。
  • 2011-02-09 15:17(99)– filter定义后不需要空格。删除$x并且取而代之的是在第一次运行中收集每一行,然后在第二部分中输出。

2

红宝石-88 87个字符

t=->s{s.tr'/\\\\','\\\\/'}
puts a=$<.map{|l|l.chop!+t[l.reverse]}
puts a.reverse.map &t

测试运行

D:\tmp>ruby cg_sym_map.rb < sym_map.in.
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+

1
很好,我喜欢推杆分配。

啊,这是我的灭亡;-)我知道最终会灭亡
Joey


2

木炭5 4 字节

S‖M⌈

-1个字节感谢@Neil

木炭会自动处理正确的斜线。

在线尝试(详细)在线尝试(纯)

说明:

将输入作为字符串:

InputString()
S

向右和向下反射(这:⌈是的内置功能:Right, :Down):

ReflectMirror(:⌈)
‖M⌈

此后,仅@ ASCII-only添加了更多的多向功能,其中包括在单个字节中向右和向下的功能。
尼尔,

@Neil您如何在详细代码中使用?:RightDown当然不会是我们想要的结果。
凯文·克鲁伊森

多向仅:在详细模式下带有前缀。
尼尔,

@Neil那么:Right:Down,还是::RightDown,还是其他?不过,这两个结果都不会在带有-vl参数的编码版本中给出a 。S‖M⌈使用-vlarg 时,必须得到什么冗长的代码?
凯文·克鲁伊森

ReflectMirror(:⌈)
尼尔,

1

Perl,80个字符

print reverse map{s@.*@($b=$&)=~y:/\\:\\/:,$&.reverse$b@e;print;y@/\\@\\/@;$_}<>

1

Shell脚本!!

#!/bin/sh

rm temp
touch temp
file=$1
for STRING in `cat $1`
do
   printf $STRING >> temp
   for ((COUNT=0; COUNT<${#STRING}; COUNT++))
   do
      RECORD[$COUNT]=${STRING:$COUNT:1}
   done
   for ((REV_COUNT=${#STRING}; REV_COUNT>=0; REV_COUNT--))
      do
        if [ "${RECORD[$REV_COUNT]}" = "\\" ]; then
            printf "/" >> temp
        elif [ "${RECORD[$REV_COUNT]}" = "/" ]; then
            printf "\\" >> temp
        else
           printf "${RECORD[$REV_COUNT]}" >> temp
        fi
      done
   echo >> temp
done
cat temp
tac temp > temp2
for STRING in `cat temp2`
do
   for ((COUNT=0; COUNT<${#STRING}; COUNT++))
   do
      RECORD[$COUNT]=${STRING:$COUNT:1}
   if [ "${RECORD[$COUNT]}" = "\\" ]; then
            printf "/"
   elif [ "${RECORD[$COUNT]}" = "/" ]; then
            printf "\\"
   else
           printf "${RECORD[$COUNT]}"
   fi
   done
echo
done

输入输出

./solution in

+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+

1

CJam,26个字节

CJam比此挑战要新,因此此答案不符合绿色复选标记的要求,但无论如何这都是一个有趣的练习

qN/{{_W%"\/"_W%er+}%z}2*N*

在这里测试。

说明

qN/{{_W%"\/"_W%er+}%z}2*N*
qN/                        "Read STDIN and split on newlines.";
   {                 }2*   "Execute this block twice.";
    {             }%       "Map this block onto each line.";
     _W%                   "Duplicate and reverse.";
        "\/"               "Push the string '\/'.";
            _W%            "Duplicate and reverse.";
               er          "Character transliteration, swaps slashes and backslashes.";
                 +         "Append to first half of the line.";
                    z      "Zip, i.e. transpose the map.";
                        N* "Join with newlines.";

末端的移置导致第二次翻转沿列进行。最后,我们再次对地图进行转置,因此我们以原始方向结束。


1

Powershell,95个字节

受到乔伊的答案的启发。

filter x{$_;$_[40..0]|%{$_-split'/'-replace'\\','/'-join'\'}},($args|%{-join(,($_|% t*y)|x)})|x

注意:40因为作者发表了评论Let's say the input is at most 16 rows and 40 characters

测试脚本:

$f = {

filter x{$_;$_[40..0]|%{$_-split'/'-replace'\\','/'-join'\'}}
,($args|%{-join(,($_|% t*y)|x)})|x

}

@(
    ,( ("+---",
        "|./.",
        "|/.."),
        "+------+",
        "|./..\.|",
        "|/....\|",
        "|\..../|",
        "|.\../.|",
        "+------+")
    ,( ("+\/",
        "/\/"),
        "+\/\/+",
        "/\/\/\",
        "\/\/\/",
        "+/\/\+")
    ,( ("+---",
        "|...",
        "|..\"),
        "+------+",
        "|......|",
        "|..\/..|",
        "|../\..|",
        "|......|",
        "+------+")
) | % {
    $m,$expected = $_
    $result = &$f @m
    "$result"-eq"$expected"
    $result
}

输出:

True
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
True
+\/\/+
/\/\/\
\/\/\/
+/\/\+
True
+------+
|......|
|..\/..|
|../\..|
|......|
+------+

0

红宝石-105

t=->s{s.tr '/\\\\','\\\\/'}
$<.read.split.map{|l|print l+=t[l.reverse]+"
"
l}.reverse.map{|l|print t[l]}

0

Golfscript-44个字符

n%{.-1%'/'/{'\\'/'/'*}%'\\'*+}%.-1%{-1%}%+n*

结果

$ cat in2
+-/|/\
/\|//-
$ cat in2 | golfscript codegolf-761.gs 
+-/|/\/\|\-+
/\|//--\\|/\
\/|\\--//|\/
+-\|\/\/|/-+

例如,另一个仅工作且不会翻转为'\'的脚本-32个字符

n%{.-1%'/'/'\\'*+}%.-1%{-1%}%+n*

结果

$ cat in
+---
|./.
|/..
$ cat in | golfscript codegolf-761.gs 
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
$ 

`\`也需要翻转。
Nabb 2011年

@Nabb,谢谢,这使我的代码非常庞大:P

标记:如果变量变短,则对重复的字符串使用变量。尽管您可以尝试一些其他技巧,然后再稍后发布答案。
Nabb 2011年

@Nabb,谢谢,我会尽力让我有30分钟的时间:D
YOU11

@Nabb,我仍然不知道,您可以发表您的文章。

0

Haskell,76个字节

c '/'='\\';c '\\'='/';c x=x;i=(c<$>)
q#x=x++q(reverse x)
f=((i<$>)#).map(i#)

在线尝试!

-- Only / and \ get converted, all other chars are passed as is
c '/'='\\';c '\\'='/';c x=x

-- "Invert" the string (that is switch all / and \ in it)
-- Just map our conversion function over the string
i = (c<$>)

-- Helper: Concatenate a list with its reversed copy (with the given function applied to the copy)
q # x = x ++ q (reverse x)

-- the resulting function:
f = ((i<$>)#) . -- produce the lower half of the image by reversing the upper half and inverting slashes in each line
    map (i#) -- produce the upper half or the image (by concating each input line with its reversed, inverted version)

0

MS-SQL 2017,243字节

输入

DECLARE @s VARCHAR(100)='+ ---'+ CHAR(10)+'| ...'+ CHAR(10)+'| .. \';

压缩

声明@t TABLE(l INT IDENTITY(1,1),s CHAR(40));插入@t(s)SELECT值+ TRANSLATE(REVERSE(value),'\ /','/ \')FROM STRING_SPLIT (@ s,char(10)); SELECT s FROM(SELECT l,s FROM @t UNION ALL SELECT 1e3-l,TRANSLATE(s,'\ /','/ \')FROM @t)b ORDER BY l

可读性

声明@t TABLE(l INT IDENTITY(1,1),s CHAR(40));
插入@t(s)
  选择值+ TRANSLATE(REVERSE(值),'\ /','/ \')
  从STRING_SPLIT(@ s,char(10));

选择 
从(
   从@t选择l,s 
   全联盟 
   SELECT 1e3-l,TRANSLATE(s,'\ /','/ \')FROM @t
  )b 
按l订购

输出(如ex.management studio中的文本):

+ ------ +                                
| ...... |                                
| .. \ / .. |                                
| ../ \ .. |                                
| ...... |                                
+ ------ +                                

(受影响的6行)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.