#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Some help with a simple programming task.


    Hi all..
    i need help

    IVE WROTE THE CODE BUT I JUST CANT MAKE IT RUN.

    This is the task:
    The squares in the chessboard are denoted by pairs consisting
    of a letter and a digit ,e.g. b4.We say that such a pair is a position .Letters are in range from a till h ,digits - from 1 till 8.We consider two text files ("white.txt" and "black.txt")containing lines
    with positions of some white and black figures ,respectively.
    Each line contains one position.A square in the chessboard is either empty , or may contain a white figure,a black figure or a white and a black figure together.There exist squares of all kinds.
    1. Whrite a Python 2.7.3 script that generates the described files with random data.Each file should contain at least
    3 lines at most 6 lines ,randomly chosen.

    2. Write another Python 2.7.3 script that reads the above files and output on the screen positions that are occupied by exactly one figure .The output should contain a position on a line and each position should be presented as a letter and a digit,separated by one space .Lines shoule be sorted lexicographically

    example

    white.txt
    c 8
    b 3
    b 2

    black.txt
    b 7
    b 2
    c 8
    a 1

    Output
    a 1
    b 3
    b 7

    like i said,i have written the code ,but it is showing me some error.Can someone tell me whats my mistake
    heres the code for the task one and two:
    Python Task 1:
    Code:
    from sets import Set
    import random
    
    white = "white.txt"
    black = "black.txt"
    
    ws = Set()
    bs = Set()
    
    chars = ['a','b','c','d','e','f','g','h']
    numbers = [1,2,3,4,5,6,7,8]
    
    #write whites
    t = random.randint(3,6)
    while t > 0:
    ws.add(random.choice(chars)+' '+str(random.choice(numbers)) + '\n')
    t -= 1
    
    f = open(white,'w+')
    for i in ws:
    f.write(i)
    f.close()
    
    #write blacks
    t = random.randint(3,6)
    while t > 0:
    bs.add(random.choice(chars)+' '+str(random.choice(numbers)) + '\n')
    t -= 1
    
    f = open(black,'w+')
    for i in bs:
    f.write(i)
    f.close()
    Python Task 2:
    Code:
    from sets import Set
    
    white = "white.txt"
    black = "black.txt"
    
    ws = Set()
    bs = Set()
    
    file = open(white,'r+')
    lines = file.readlines()
    
    for line in lines:
    line = line.rstrip('\n')
    ws.add(line);
    file.close()
    
    file = open(black,'r+')
    lines = file.readlines()
    
    for line in lines:
    line = line.rstrip('\n')
    bs.add(line)
    file.close()
    
    #Set operations are great !
    uniL = bs - ws
    uniR = ws - bs
    uniques = uniL | uniR
    
    #lambda sorting
    sor = sorted(uniques, key=lambda item: (int(item.partition(' ')[0])
    if item[0].isdigit() else float('inf'), item))
    
    #now print
    for i in sor:
    print i
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    1. Please clarify what you mean by "can't make it run". Are you getting an error message when you try to run it? If so, what does the error message say?

    2. Please use CODE tags to wrap your Python code; they will preserve whitespace, which is critical for Python code.

    3. For future reference, a forum is not a good place to receive urgent help. Saying that you need a response urgently makes it less likely that people will actually help you.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by E-Oreo
    1. Please clarify what you mean by "can't make it run". Are you getting an error message when you try to run it? If so, what does the error message say?

    2. Please use CODE tags to wrap your Python code; they will preserve whitespace, which is critical for Python code.

    3. For future reference, a forum is not a good place to receive urgent help. Saying that you need a response urgently makes it less likely that people will actually help you.
    It says IndentationError:expected an indented block on line 14 but its not telling me whats the error like in othere languages -thats for the second code
    And the firs one just disappears before i see the result,i tried to pause it but its just not working
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Ranchos de Taos, NM
    Posts
    3
    Rep Power
    0
    IndentationError means your indentation is wrong on line 14. If the code you included in your original post actually looks like that on file it's no wonder it doesn't run! Python can get away without semicolons and braces {} because indentation is everything. You should indent after each control statement ending in a colon ':' ie, like this,

    Code:
    while t > 0:
        ws.add(random.choice(chars)+' '+str(random.choice(numbers)) + '\n')
        t -= 1

IMN logo majestic logo threadwatch logo seochat tools logo