Python Forum
How would you (as an python expert) make this code more efficient/simple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would you (as an python expert) make this code more efficient/simple
#1
Hi,

my question is quite simple: How would you make this piece of code more simple/efficient?

def draw_pattern_1(p,m):
    print(p, end=' ')
    print(m, end=' ') 
    print(m, end=' ')
    print(m, end=' ')
    print(m, end=' ')
    print(p, end=' ')
    print(m, end=' ')
    print(m, end=' ')
    print(m, end=' ')
    print(m, end=' ')
    print(p)


def draw_pattern_2(l):
    print(l, end='         ')
    print(l, end='         ')
    print(l)
    

    
def draw_grid(f_1,f_2,p,m,l):
    draw_pattern_1(p,m)
    draw_pattern_2(l)
    draw_pattern_2(l)
    draw_pattern_2(l)
    draw_pattern_2(l)
    draw_pattern_1(p,m)
    draw_pattern_2(l)
    draw_pattern_2(l)
    draw_pattern_2(l)
    draw_pattern_2(l)
    draw_pattern_1(p,m)

p=  "+"
m = "-"
l = "|"

draw_grid(draw_pattern_1,draw_pattern_2,p,m,l)
Output:
+ - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - +
Reply
#2
coder_sw99 Wrote:How would you make this piece of code more simple/efficient?
print("""\
+ - - - - + - - - - +
|         |         |
|         |         |
|         |         |
|         |         |
+ - - - - + - - - - +
|         |         |
|         |         |
|         |         |
|         |         |
+ - - - - + - - - - +""")
coder_sw99 likes this post
Reply
#3
from tabulate import tabulate
data = ([' ', ' '*2])
print(tabulate(data, tablefmt='grid'))
Output:
+--+--+ | | | +--+--+ | | | +--+--+
coder_sw99 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
A more flexible version with itertools
import itertools as itt

def sandwich(chain, sep):
    yield from sep
    for c in chain:
        yield from c
        yield from sep

def tablerows(r, c):        
    top = ''.join(sandwich((sandwich('-' * n, ' ') for n in c), '+'))
    mid = ''.join(sandwich((' ' * (2 * n + 1) for n in c), '|'))
    return sandwich((itt.repeat(mid, k) for k in r), (top,))

a = (3, 2, 4)
b = (2, 4, 3, 1)

for r in tablerows(a, b):
    print(r)
Output:
+ - - + - - - - + - - - + - + | | | | | | | | | | | | | | | + - - + - - - - + - - - + - + | | | | | | | | | | + - - + - - - - + - - - + - + | | | | | | | | | | | | | | | | | | | | + - - + - - - - + - - - + - +
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 227 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  hi need help to make this code work correctly atulkul1985 5 700 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 439 Nov-07-2023, 04:32 PM
Last Post: snippsat
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  A more efficient code titanif 2 459 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,273 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  how to make bot that sends instagram auto password reset code kraixx 2 1,270 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,098 Dec-03-2022, 10:07 PM
Last Post: Extra
  help me simple code result min and max number abrahimusmaximus 2 869 Nov-12-2022, 07:52 AM
Last Post: buran
  Making a function more efficient CatorCanulis 9 1,749 Oct-06-2022, 07:47 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020