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