Linked is my project that I am working on.
Link to what this project is about:https://i.stack.imgur.com/dnuHD.png, https://i.stack.imgur.com/HuFBe.png, https://i.stack.imgur.com/12CcT.png
I have already completed some of the program. However, I am having trouble on doing part b, which is asking me to create an output file for each major and add the students that are in that major.
Here is my current code:
student_major_file = 'StudentsMajorsList.csv'
gpa_file = 'GPAList.csv'
graduation_file = 'GraduationDatesList.csv'
with open(student_major_file) as file1:
list1 = file1.read().splitlines()
with open(gpa_file) as file2:
list2 = file2.read().splitlines()
with open(graduation_file) as file3:
list3 = file3.read().splitlines()
output1 = open('FullRoster.csv', 'w')
output3 = open('ScholarshipCandidates.csv', 'w')
output4 = open('DisciplinedStudents.csv', 'w')
#part a
for a, b, c in zip(list1, list2, list3):
x = a.split(',')
y = b.split(',')
z = c.split(',')
output1.write(x[0] + ', ' + x[2] + ', ' + y[-1] + ', ' +
x[1] + ', ' + z[-1] + ', ' + x[4])
output1.write('\n')
#part b
#part c
for a, b, c in zip(list1, list2, list3):
x = a.split(',')
y = b.split(',')
z = c.split(',')
if float(b[-1]) > 3.8:
output3.write(x[0] + ', ' + x[1] + ', ' + x[2] + ', ' +
z[-1])
output3.write('\n')
#part d
for a, b, c in zip(list1, list2, list3):
x = a.split(',')
y = b.split(',')
z = c.split(',')
if a[-1] == 'Y':
output4.write(x[0] + ', ' + x[1] + ', ' + x[2] + ', ' +
z[-1])
output4.write('\n')
output1.close()
output3.close()
output4.close()
Thanks in advance!
Related
I have a degree 6 multivariate equation in x and y written in Sympy, e.g.
eqn = a*x**6 + b*x**5*y + c*x**4*y + d*x**3*y + e*x**3*y**2 + ...
Is there a way to collect (x**2+y**2) and rearrange them into the following format?
eqn2 = A*(x**2+y**2)**3 + B*(x**2+y**2)**2 + C*(x**2+y**2) + D
A, B, C, D can be in x, y.
So far I have only tried collect(eqn, x**2 + y**2) and it returned the original equation.
Thank you!
Consider using a temporary symbol z = x**2 + y**2 and replace x**2 with z - y**2, then expand and restore:
>>> ex
A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6 + B*x**4 + 2*B*x**2*y**2 +
B*y**4 + C*x**2 + C*y**2 + D
>>> ex.subs(x**2, z - y**2).expand().subs(z, x**2 + y**2)
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D
Although that works, perhaps a more direct thing to do is separate the expression by coefficients A-D and then factor those collections of terms:
def separatevars_additively(expr, symbols=[]):
free = set(symbols) or expr.free_symbols
d = {}
while free:
f = free.pop()
expr, dep = expr.as_independent(f, as_Add=True)
if dep.has(*free):
return None
d[f] = dep
if expr:
d[0] = expr
return d
>>> coeff = var("A:D")
>>> separatevars_additively(ex, coeff)
{B: B*x**4 + 2*B*x**2*y**2 + B*y**4, A: A*x**6 + 3*A*x**4*y**2 + 3*A*x**2*y**4 + A*y**6, D: D, C: C*x**2 + C*y**2}
>>> Add(*[factor(i) for i in _.values()])
A*(x**2 + y**2)**3 + B*(x**2 + y**2)**2 + C*(x**2 + y**2) + D
Input: 3
gandhi
mahatma gandhI
Mohndas KaramChand gandhi
Output:
Gandhi
M. Gandhi
M. K. Gandhi
And I wrote the following code:
n = int(input())
while n > 0 :
k = raw_input()
if k.find(" ") != -1:
final = str(k[0].upper() + '.')
for i in range(len(k)) :
if (k[i] == ' ') & (k[i:].find(" ")):
final += k[i+1].upper() + ". "
else:
if(k[i] == ' '):
final += k[i+1].upper() + k[i+2:].lower()
n -= 1
print final[0:len(final)]
else :
print k[0].upper() + k[1:].lower()
I'm getting a runtime error for this. Can someone explain why?
try this code.
str = "gandhi mahatma gandhi Mohndas KaramChand gandhi"
temp = ''
for st in str.split():
if st == 'gandhi':
temp = temp + ' ' + st.title()
else:
temp = temp + ' ' + st[0].upper() + '.'
print temp
Output as per your expectation - Gandhi M. Gandhi M. K. Gandhi
I am having trouble iterating a process in Python 2.7.
I have simplified it as much as possible to debug the first steps, and want to build on this.
If I specify the data
data = ("-4.916409,36.629535,3.721236,255,232,242")
And then get it to split
X,Y,Z,R,G,B = data.split(",")
I can recombine some of the elements to create new files and/or folders with the name of those elements:
RGB = (R + "+" + G + "+" + B)
os.makedirs(inputFolder + os.sep + RGB)
fo = open("Z:\\temp\\output" + os.sep + RGB + os.sep + RGB + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
But when I try to do that from a longer text file, I can no longer combine the elements into this "RGB" as a file and/or folder name (only "R" or "G" or "B"), and only get a return for the first line.
inputFolder = ("Z:\\temp\\output")
coordinates = open("Z:\\temp\\accident2.txt", "r")
for line in coordinates:
X,Y,Z,R,G,B = line.split(",")
RGB = (R + "+" + G + "+" + B)
os.makedirs(inputFolder + os.sep + R)
fo = open("Z:\\temp\\output" + os.sep + R + os.sep + R + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
But it works if I switch from the whole numbers to the decimals:
RGB = (X + "," + Y + "," + Z)
Then I can write:
fo = open("Z:\\temp\\output" + os.sep + RGB + os.sep + RGB + ".txt", "w")
Which is not quite right, but closer to what I want.
Why are numbers with decimals easier to "read" than those without?
How do I fix it so the whole numbers are treated like those with decimals?
well your coordinates is a file object, you need to read from this object. You can actually read line-by-line using readlines, function of file object. Check documentation
inputFolder = ("Z:\\temp\\output")
with open("Z:\\temp\\accident2.txt", "r") as coordinates:
for linenumber,line in enumerate( coordinates.readlines() ):
X,Y,Z,R,G,B = line[:-1].split(",")
RGB = (R + "+" + G + "+" + B)
try:
os.makedirs(inputFolder + os.sep + R)
fo = open("Z:\\temp\\output" + os.sep + R + os.sep + R + ".txt", "w")
fo.write(X + "," + Y + "," + Z + "\n")
except:
print "problem in line %d"%linenumber
I'm trying to print the board of my game with this funcion:
boardis a list with the coordinates or cells occupated in the game
lst is a list with all the coordinates just to refer to.
def board_to_string(lst,board):
for n in lst:
for m in range(3):
# it should be board[i]
if n[m] in board[0]:
n[m] = "o"
else:
n[m] = " "
for lst2 in lst:
print("---------------")
print(str(lst2))
a = [['a1','b1','c1'],['a2','b2','c2'],['a3','b3','c3']]
b = [['b1','a3','a2'],['a2','g1']]
board_to_string(a,b)
Output is:
---------------
[' ', 'o', ' ']
---------------
['o', ' ', ' ']
---------------
['o', ' ', ' ']
There are two problems, I want it to search the coordinate in every sublist board has. I tried adding for i in range(3)but it prints twice and it doesn't work either.
The other problem is that I'm supposed to print a string but I can't think of a way to this, only with lists.
Replace your code with this:
def board_to_string(lst,board):
print("------")
for n in lst:
for m in range(3):
# it should be board[i]
if n[m] in board[0]:
print("o", end="")
elif n[m] in board[1]:
print("x", end="")
else:
print(" ", end="")
print("|", end="")
print("\n------")
a = [['a1','b1','c1'],['a2','b2','c2'],['a3','b3','c3']]
b = [['b1','a3','a2'],['a1','c1']]
board_to_string(a,b)
Note that I corrected last but one row (list b), too
Hi please how can I add other elements ie z[i], in z in the last line of this code
z = [2,3,4,5]
my_file.write('%s' % j + 'a' + ':' + str(z[0]) + ',')#z[0] = first element in z
Thanks
smth like that?
z = [2,3,4,5]
with open("output.txt", 'w') as my_file:
my_file.write('a: %s' % ', '.join([str(i) for i in z]))
output.txt:
a: 2, 3, 4, 5