HackerRank 设计师门垫

问题描述 投票:0回答:11

我正在尝试解决 HackerRank 中的一个问题,但陷入了困境。 帮我写一下这个问题的Python代码

先生。文森特在一家门垫制造公司工作。有一天,他设计了一种新的门垫,规格如下:

垫子尺寸必须为 X。(为奇数自然数,为 倍。) 设计的中心应写有“欢迎”。 设计模式应该只使用 |, 。和 - 字符。 示例设计

Size: 7 x 21 
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
python-3.x
11个回答
2
投票

我认为 python3 中最简洁的答案如下:

    N, M = map(int,input().split())
    for i in range(1,N,2): 
        print((i * ".|.").center(M, "-"))
    print("WELCOME".center(M,"-"))
    for i in range(N-2,-1,-2): 
        print((i * ".|.").center(M, "-"))

0
投票

只是一个简化版本--


n,m = input().split()
n = int(n)
m = int(m)
#printing first half
for i in range(n//2):
    t = int((2*i)+1)
    print(('.|.'*t).center(m, '-'))
#printing middle line
print('WELCOME'.center(m,'-'))
#printing last half
for i in reversed(range(n//2)):
    t = int((2*i)+1)
    print(('.|.'*t).center(m, '-'))

0
投票

此解决方案使用列表理解。有一个关于这个的教程这里

# Using list comprehension
n, m = map(int, input().split())
pattern = [('.|.'*(2 * i + 1)).center(m,'-') for i in range(n//2)]
print('\n'.join(pattern + ['WELCOME'.center(m, '-')] + pattern[::-1]))

0
投票

每个人似乎都错过了第一个提示。输入应该来自 stdin,输出应该读取到 stdout 。


0
投票
n, m = map(int, input().split()) 
str1 = ".|."
half_thickness = n-2

for i in range(1,half_thickness+1,2):
    print((str1*i).center(m,"-"))

print("WELCOME".center(m,"-"))

for i in reversed(range(1,half_thickness+1,2)):
    print((str1*i).center(m,"-"))

0
投票

这是执行此操作的另一种方法

# Read input values
n, m = map(int, input().split())

# Function to generate the pattern
def generate_pattern(n, m):
    # Generate the top half
    top_half = [('.|.' * (2 * i + 1)).center(m, '-') for i in range(n // 2)]
    
    # Generate the middle line
    middle_line = 'WELCOME'.center(m, '-')
    
    # Generate the bottom half
    bottom_half = [('.|.' * (2 * i + 1)).center(m, '-') for i in range(n // 2 - 1, -1, -1)]
    
    # Combine all parts
    pattern = top_half + [middle_line] + bottom_half
    return pattern

# Generate and print the pattern
pattern = generate_pattern(n, m)
print('\n'.join(pattern))

-1
投票
# Enter your code here. Read input from STDIN. Print output to STDOUT

length, breadth = map(int, input().split())

def out(n,string):
    for i in range(n):
        print ("{}".format(string), end='')

def print_out(hyphen_count,polka_count):
    out(hyphen_count, '-')
    out(polka_count, '.|.')
    out(hyphen_count, '-')
    print ('')

hyphen_count = (breadth - 3) // 2
polka_count = 1
for i in range(length):
    if i < (length // 2):
        print_out(hyphen_count,polka_count)        
        hyphen_count = hyphen_count - 3
        polka_count = polka_count + 2
    elif i == (length // 2 + 1):
        out((breadth - 7)//2, '-')
        print ("WELCOME", end='')
        out((breadth - 7)//2, '-')
        print ('')
        hyphen_count = hyphen_count + 3
        polka_count = polka_count - 2        
    elif (length // 2) < i < length:
        print_out(hyphen_count,polka_count)        
        hyphen_count = hyphen_count + 3
        polka_count = polka_count - 2 
print_out(hyphen_count,polka_count)



-1
投票
N, M = map(int, input().split())
d = ".|."
for i in range(N//2):
    print((d*i).rjust(M//2-1,'-') + d + (d*i).ljust(M//2-1,'-'))
print("WELCOME".center(M,'-'))
for j in range(N//2+2,N+1):
    print((d*(N-j)).rjust(M//2-1,'-') + d + (d*(N-j)).ljust(M//2-1,'-'))

-1
投票

我很好奇是否有比我的更好的解决方案来解决这个黑客排名问题,所以我来到了这里。

我的解决方案具有单个
for
循环,成功通过了所有测试用例:

# Enter your code here. Read input from STDIN. Print output to 

if __name__ == "__main__":
    row_num, column_num = map(int, input().split())
    
    
    num_list = []
    
    # take user input N and M as stated in the problem: https://www.hackerrank.com/challenges/designer-door-mat
    # where N are the number of rows and M are the number of coulmns
    # For this explanation we assume N(row_num)=7, M(column_num)=21 for simplicity, 
    # the code below works on all the test cases provided by hackerrank and has cleared the submission
    
    
    # this for loop is for iterating through the number of rows: 0th row to (n-1)th row
    for i in range(0, row_num):
        
        # steps to be done at each row
        if i < row_num//2:
            # BEFORE REACHING THE MIDDLE OF THE DOOR MAT
            
            # we need to generate a pattern of ".|." aligned with '-' in the following manner: 
            # steps below will generate ".|." pattern 1, 3, 5 times 
            # On i=0, times = 2*(0+1)-1 = 1 => ---------.|.---------
            # On i=1, times = 2*(1+1)-1 = 3 => ------.|..|..|.------
            # On i=2, times = 2*(2+1)-1 = 5 => ---.|..|..|..|..|.---
            times = 2*(i+1)-1
            
            # record these numbers since we need to do the reverse when we reach the middle of the "door mat"
            num_list.append(times)
            
            # recall the assignment on Text Alignment: https://www.hackerrank.com/challenges/text-alignment/problem
            # since that idea will be used below - at least this is how I look.
            
            # Essentially, this part of code helps to eliminate the need of a for loop for iterating through each columns to generate '-'
            # which would otherwise have to be used to print '-' surrounding the pattern .|. in each row
            # instead we can use the column number "M" from the user input to determine how much alignment is to be done
            print(('.|.'*times).center(column_num, '-'))
        
        
        elif i == (row_num//2):
            # UPON REACHING EXACTLY IN THE MIDDLE OF THE DOOR MAT
            # once middle of the row is reached, all we need to print is "WELCOME" aligned with '-' : -------WELCOME-------
            print('WELCOME'.center(column_num, '-'))
        else:
        # AFTER CROSSING THE MIDDLE OF THE DOOR MAT
        # as soon as we cross the middle of the row, we need to print the same pattern as done above but this time in reverse order
        # thankfully we have already stored the generated numbers in a list which is num_list = [1, 3, 5]
        # all we need to do is just fetch them one by one in reverse order
        # which is what is done by: num_list[(row_num-1)-i]
        # row_num = 7, i = 4, num_list[(7-1)-4] --> num_list[2] --> 5 => ---.|..|..|..|..|.---
        # row_num = 7, i = 5, num_list[(7-1)-5] --> num_list[1] --> 3 => ------.|..|..|.------
        # row_num = 7, i = 6, num_list[(7-1)-6] --> num_list[0] --> 1 => ---------.|.---------
        print(('.|.'*num_list[(row_num-1)-i]).center(column_num, '-'))
        
        # DONE!


-1
投票
if you do not want to use any align keyword then. It is complicate to understand but diff. approch.

n, m = map(int,input().split())

i = m
i = int(i-3)
f = 1

for j in range(-i,n-3,6):
    j = abs(j)
    
    if j > 0:
        j = int(j/2)
        print(('-'*j)+('.|.'*f)+('-'*j))
        f = f+2
    else:
        f = f-2
        break

wel = int(m-7)
wel = int(wel/2)
print(('-'*wel)+'WELCOME'+('-'*wel))


for j in range(3,i,3):
    j = abs(j)
    
    if f > 0:
        print(('-'*j)+('.|.'*f)+('-'*j))
        f = f-2
    else:
        break

-3
投票
N,M = map(int,raw_input().split())
count = 1
for _ in range(1,N/2+1):
    print ((".|."*count).center(M,"-"))
    count = count+2
print("WELCOME").center(M,"-")
count = count-2
for _ in range(1,N/2+1):
    print ((".|."*count).center(M,"-"))
    count = count-2
© www.soinside.com 2019 - 2024. All rights reserved.