Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 2nd, 2013, 09:45 AM
kaputin kaputin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 kaputin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 32 sec
Reputation 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

Reply With Quote
  #2  
Old February 2nd, 2013, 10:08 AM
E-Oreo's Avatar
E-Oreo E-Oreo is online now
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,057 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 6 h 4 m 32 sec
Reputation Power: 7104
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
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
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

Reply With Quote
  #3  
Old February 2nd, 2013, 10:29 AM
kaputin kaputin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 kaputin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 32 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old February 2nd, 2013, 03:06 PM
Joseph8th Joseph8th is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: Ranchos de Taos, NM
Posts: 3 Joseph8th User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 30 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Some help with a simple programming task.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap