2
\$\begingroup\$

I have an assignment as follows:

Write a program which reads from the keyboard two integers n and m, and a character c from the keyboard. This program should define and call a function: print rectangle(n, m, c) which prints a rectangle of size n x m consisting of the character c

My solution is:

n=int(input("Enter the lenght of the rectangle: "))
m=int(input("Enter the width: "))
c="c"
def print_rect(n, m, c):
 for a in range(m):
 print (n*c)
print_rect(n, m, c)
input("Press enter to close")

I am sure there are alternative ways to do this assignment. How would you code this assignment? Do you think my solution is OK?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Nov 27, 2013 at 7:58
\$\endgroup\$
4
  • 2
    \$\begingroup\$ "[...] and a character c from the keyboard". c should also be input from the user, just like n and m. \$\endgroup\$ Commented Nov 27, 2013 at 8:12
  • \$\begingroup\$ Should the rectangle be solid or should you only print the outline? If it can be solid you would only need to change what I said about the character. \$\endgroup\$ Commented Nov 27, 2013 at 8:23
  • \$\begingroup\$ Thank you. The rectangle should be solid. But you said something important. What should I do if I only want to print the outline? \$\endgroup\$ Commented Nov 27, 2013 at 8:35
  • \$\begingroup\$ I don't have any code of it but I'd made a nested loop over the width and the length of the rectangle and calculate the position of the outline and only print the character when the loop hits that value. Hope I made it clear. \$\endgroup\$ Commented Nov 27, 2013 at 9:00

4 Answers 4

5
\$\begingroup\$

I think the assignment wants you to read the character from input as well.

When you are printing the input you are composing the string m times. More optimal solution is if you do that 1 time and output it m times:

def print_rect(n, m, c):
 row=n*c
 for a in range(m):
 print (row)

I would even do it with this oneliner:

print((n * c + '\n') * m, end="")
answered Nov 27, 2013 at 12:13
\$\endgroup\$
0
4
\$\begingroup\$
  1. It makes little sense to have a variable c that always contains 'c'. Simply replace c by 'c' in your print call.
  2. Learn about PEP8 and about checking that your code conforms to PEP8. A few things to check here:
    1. spaces between = when doing an assignment
    2. new lines between function definitions
  3. no space after print: print(n * c) or print n * c (the former is better since it's Python 3 proof)
  4. Error checking! What happens if I enter anything else than a number? It's possibly not required by your assignment, but be aware that it can cause issues.
Malachi
29k11 gold badges86 silver badges188 bronze badges
answered Nov 27, 2013 at 9:23
\$\endgroup\$
0
2
\$\begingroup\$
def print_rect(n, m, c):
 l = (n * c for a in range(m))
 print '\n'.join(l)

I believe this would be slightly faster. I did a test on my computer:) (I'm using Python 2.7.5 so the code is slightly different with Python3)

import timeit
prep = """
n = 10
m = 7
c = 'D'
"""
old = """
 for a in xrange(m):
 print n * c
 """
new = """
 l = (n * c for a in xrange(m))
 print \'\\n\'.join(l)
 """
print 'Old'
print timeit.timeit(old, setup=prep, number=10)
print 'New'
print timeit.timeit(new, setup=prep, number=10)

The new implementation is like 10 times faster:)

answered Nov 27, 2013 at 9:07
\$\endgroup\$
6
  • \$\begingroup\$ n, m, and c, needs to be inputs (but I guess OP can easily modify that:)) \$\endgroup\$ Commented Nov 27, 2013 at 9:08
  • \$\begingroup\$ Upvoted because it's an interesting find. But it's also kind of a hack: I would argue it's harder to read, which is way more important than speed. \$\endgroup\$ Commented Nov 27, 2013 at 9:19
  • \$\begingroup\$ I recommend python.net/~goodger/projects/pycon/2007/idiomatic/handout.html where I got this idea:) \$\endgroup\$ Commented Nov 27, 2013 at 15:23
  • \$\begingroup\$ I ran your code in IDLE and it doesn't make a rectangle. \$\endgroup\$ Commented Nov 29, 2013 at 5:53
  • \$\begingroup\$ »I ran your code in IDLE and it doesn't make a rectangle« that is then a creative solution ;) \$\endgroup\$ Commented Nov 29, 2013 at 8:13
1
\$\begingroup\$

Here is my implementation for printing hollow rectangles:

import sys
# Python 2/3 compatibility shims
if sys.hexversion >= 0x3000000:
 inp = input
 rng = range
else:
 inp = raw_input
 rng = xrange
def get_int(prompt):
 while True:
 try:
 return int(inp(prompt))
 except ValueError:
 pass
def get_ch(prompt):
 while True:
 res = inp(prompt).strip()
 if res:
 return res[:1]
def make_row(w, edge, center):
 return edge*(w>0) + center*(w-2) + edge*(w>1)
def print_rectangle(h, w, c):
 top_row = make_row(w, c, c)
 mid_row = make_row(w, c, ' ')
 rows = [top_row]*(h>0) + [mid_row]*(h-2) + [top_row]*(h>1)
 print('\n'.join(rows))
def main():
 h = get_int('Rectangle height: ')
 w = get_int('Rectangle width: ')
 c = get_ch('Character to use: ')
 print_rectangle(h, w, c)
if __name__=="__main__":
 main()

which runs like:

Rectangle height: 4
Rectangle width: 6
Character to use: F
FFFFFF
F F
F F
FFFFFF
answered Nov 29, 2013 at 21:15
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.