1
\$\begingroup\$

I have written a program in which user enters total number of conditions (cases) and then that many number of inputs to print chessboard.

For example:

3
21
*
.
12
*.
35
*.*.*
.*.*.
*.*.*

Here is my code:

def main():
    no_of_case = input()
    for i in range(no_of_case): 
        user_input = input()                    # take input

        row = int(str(user_input)[0])
        column = int(str(user_input)[1]) 

        count = 0


        a = "*"
        b = "."

        while True:
            count = count +1 

            if row == 1 and column == 1:
                print "*."
                break

            if row == 1 and column%2 == 0:  
                print (a + b) * (column/2)
                break


            if row == 1 and column%2 != 0:  
                print (a + b) * (column/2) + "*"
                break

            if column == 1 and row%2 == 0:
                print a
                print b 
                if count == row/2:
                    break

            if column == 1 and row%2 != 0:
                print a
                print b

                if count == row/2:
                    print "*"
                    break

            if row != 1 and column !=1:


                if (row%2 == 0) and (column%2== 0): 
                    print (a + b) * (column/2)
                    print (b + a) * (column/2)

                elif (row%2 != 0) and (column%2 == 0):
                    print (a + b) * (column/2)
                    print (b + a) * (column/2)


                elif (row%2 == 0) and (column%2 != 0):
                    print (a + b) * (column/2) + "*"
                    print (b + a) * (column/2) + "."

                elif (row%2 != 0) and (column%2 != 0):  
                    print (a + b) * (column/2) + "*"
                    print (b + a) * (column/2) + "."


                if row%2 == 0 and count == row/2:
                    break

                elif row%2 != 0 and count == row/2:
                    if column%2 == 0:
                        print "*." * (column/2)
                    elif column%2 != 0:
                        print "*." * (column/2) + "*"
                    break







main()

Any suggestion in what way I can improve it?

\$\endgroup\$
  • \$\begingroup\$ It sounds like your code doesn't actually solve the challenge quite correctly. You can either fix the code or reframe the question so that it performs a simpler task. \$\endgroup\$ – 200_success Apr 27 '16 at 20:26
  • \$\begingroup\$ @200_success I hope it is better now.(I think I need to start that spoj problem from scratch. so it's better to learn some some technique before doing that) \$\endgroup\$ – Freddy Apr 27 '16 at 20:32
1
\$\begingroup\$

Your current code limits the number of rows/columns to a single integer. Is this desired behavior?

For clarity sake, I would probably generate the even/odd rows and then print each until you have printed enough rows

Function to specify the input

def get_int_digits(l):
    try:
        inp = input()
    except SyntaxError:
        print "You did not enter anything"
        return 0
    if type(inp) != int:
        print "You did not enter an integer."
        return 0
    if l is not None and len(str(inp)) != l:
        print "You did not enter the correct number of digits."
        print "{} digits expected.".format(l)
        return 0
    return inp

Function to output the "board"

def print_board(row, column):
    even_string = ("*." * (column // 2 + 1)) [0:column]
    odd_string = (".*" * (column // 2 + 1)) [0:column]

    for i in xrange(row):
        print odd_string if i % 2 else even_string 

Driver

if __name__ == __main__:
    cases = get_int_digits(None)
    for i in xrange(cases):
        row_col = get_int_digits(2)
        print_board(row_col // 10, row_col % 10)
\$\endgroup\$
  • \$\begingroup\$ yes that is desired behavior. actually I was thinking to add try and except so user can't add input more then 99. \$\endgroup\$ – Freddy Apr 27 '16 at 21:06
  • \$\begingroup\$ The try catch would be more useful to ensure that the user has entered enough characters. I have updated my code with the input \$\endgroup\$ – arewm Apr 27 '16 at 21:35
  • \$\begingroup\$ When I say that the number of rows/columns are limited to a single integer, I meant a single digit. If you are trying to solve the problem on SPOj, this approach will fail. \$\endgroup\$ – arewm Apr 27 '16 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.