再次可视化欧几里得算法


10

任务

给定两个正整数:

  1. 绘制由两个整数指定的尺寸的矩形。
  2. 重复步骤3,直到没有更多空间。
  3. 绘制并填充与(剩余)矩形的三个边接触的最大正方形。
  4. 输出结果矩形。

例如,我们的输入是610

我们绘制大小为6 x 10的空心矩形:

xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx

重复填充正方形后,我们将获得以下结果:

aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaaccdd
aaaaaaccdd

有4个方格位置(abcd),每个边长6422分别。

规则与自由

  1. 您必须为每个正方形使用不同的字母。
  2. 您可以选择要支持的字母,只要支持的字母都是可打印字符,并且至少10支持字符即可。
  3. 在上面的步骤3的每个迭代中,您都有两个选择(在上一个迭代中,只有一个选择)。两种选择均有效。
  4. 所需的平方数不会超过您支持的字母数。
  5. 您可以按任何顺序用支持的字母填写方块。

测试用例

输入: 6, 10

输出:

aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaaccdd
aaaaaaccdd

要么

aaaaaaccdd
aaaaaaccdd
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb
aaaaaabbbb

要么

bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa
ccddaaaaaa
ccddaaaaaa

要么

ccddaaaaaa
ccddaaaaaa
bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa
bbbbaaaaaa

要么

ddddddaaaa
ddddddaaaa
ddddddaaaa
ddddddaaaa
ddddddbbcc
ddddddbbcc

输入: 1,1

输出:

a

输入: 1,10

输出:

abcdefghij

输入: 10,1

输出:

a
b
c
d
e
f
g
h
i
j

请注意,以上测试用例包含的可能性比我能提供的更多。

计分

这是。以字节为单位的最短答案将获胜。

有标准漏洞


Answers:


3

木炭,30字节

NδNγFβ¿×γδ«UOγδι¿‹γδA⁻δγδA⁻γδγ

在线尝试!说明:

Nδ      Input d
Nγ      Input g
Fβ      For i In ['a' ... 'z']
 ¿×γδ«   If g * d
  UOγδι   Oblong g, d, i
  ¿‹γδ    If g < d
   A⁻δγδ   d = d - g
   A⁻γδγ   Else g = g - d

烦人的是,木炭的“椭圆形”命令不会占用0一个尺寸,这花了我4个字节。另一种方法是循环while g * d,但是后来我不知道如何迭代b(已预定义为小写字母)。


糟糕,很抱歉,这是一个有意识的设计决定,您认为也应该允许使用负输入吗?
ASCII码,仅ASCII

@仅ASCII当前行为是什么(均为0和否定)?我最好的主意是,负数会吸引到左侧/顶部,而不是右侧/底部。(此外,如果我使用W×γδ,如何每次打印不同的字母?)
尼尔(Neil

@Neil哇,我明白你的意思会很烦。
魔术八达通



1

果冻,32字节

Ṁ,ạ/y
³,⁴ÇÐĿp/€Fs2
pµ¢ṣLµ€+95ỌsY

在线尝试!

Ṁ,ạ/y您想要一个解释吗?这里是。

Ṁ,ạ/y          - perform one step of the Euclidean Algorithm, input 2-element list
 ,             - pair of the following two:
Ṁ              -  maximum of the the input list
  ạ/           -  absolute difference of the two elements
    y          - use this as a mapping on the input.

³,⁴ÇÐĿp/€Fs2   - apply Euclidean Algorithm
³,⁴            - start with the pair [input 1, input 2]
   Ç           - apply a step of the Euclidean Algorithm
    ÐĿ         - repetitively until the results repeat
      p/€      - take the Cartesian product of each step
         Fs2   - flatten and split into all coordinate pairs of letters

pµ¢ṣLµ€+95ỌsY
p              - Cartesian product of inputs: provides all possible coordinate pairs.
 µ   µ€       - for each coordinate
   ṣL         - find the number of times it is included in
  ¢           - the above list of covered coordinates.
       +95Ọ   - convert number of times to letters
           s  - split into rows
            Y - join by newlines.

通过使用隐式参数而不是,我可能可以进行更多操作³,⁴


1

Haskell,181个字节

import Data.List
(['!'..'~']&)
a#[]=a
a#b=zipWith(++)a$transpose b
(s&a)b|b<1=[]|b>a=transpose$s&b$a|n<-div a b,(t,u)<-splitAt n s=foldl1(#)((<$[1..b]).(<$[1..b])<$>t)#(u&b$mod a b)

在线尝试!

对于10更多的字节,您会得到一个不错的螺旋形:)

!!!!!!!!!!!!!$$$#####
!!!!!!!!!!!!!$$$#####
!!!!!!!!!!!!!$$$#####
!!!!!!!!!!!!!%%'#####
!!!!!!!!!!!!!%%&#####
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""
!!!!!!!!!!!!!""""""""

在线尝试!

不打高尔夫球

(#)操作员将两个矩阵彼此相邻,但调换的权利之一,例如:

!!!                !!!"
!!! # "#$    ->    !!!#
!!!                !!!$

a # [] = a
a # b  = zipWith (++) a $ transpose b

这基本上是Euclid算法的递归版本,但是它没有忘记除数和余数并返回gcd,而是从中建立平方并用累加(#)。该s变量是剩余的字符,我们可以使用:

(s & a) b
  | b == 0 = []                     -- Base case
  | b > a = transpose $ (s & b) a   -- In this case we can just flip the arguments and rotate the result by 90 degrees
  | n <- div a b                    -- set n to the number of squares we need
  , (t,u) <- splitAt n s =          -- take n characters, ..
               ((<$[1..b]).(<$[1..b]) <$> t)                     -- .. build squares from them and ..
    foldl1 (#)                                                   -- put them next to each other
                                             (u & b $ mod a b)   -- recursively build the smaller squares with the remaining characters..
                                            #                    -- .. flip them and put them next to the previous one(s)

实际函数只是使用所有可打印字符的字符串从上方调用函数:

(['!'..'~']&)

您需要计数import Data.List才能使用transpose
Anders Kaseorg '17

我确实做了,但是(据我所知)当我使用无点功能时无法进行该导入。但我把它的字节数,请参阅TIO其中字节数实际上是164..
ბიმო

1
哦。您可以玩古怪的预处理器游戏,但是从TIO复制后,在某个时候手动编辑帖子中的代码更有意义。
Anders Kaseorg '17
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.