I'm getting infinite loop and array is not printing as well - while-loop

noOfStudent = int(input("Enter Number of Student : "))
while range(noOfStudent):
subjectArray = []
markArray = []
studentName = input("Enter Student Name : ")
noOfSubject = int(input("Eneter Number of Subjects : "))
totalMark = 0
while range(noOfSubject):
subjectName = input("Enter Subject Name : ")
subjectArray.append(subjectName)
mark = int(input("Enter Mark :"))
markArray.append(mark)
break
totalMark = totalMark + mark
average = totalMark / noOfSubject
print(studentName,subjectArray,markArray)
subjectArray.reverse()
markArray.reverse()
print(studentName,subjectArray,markArray)
subjectArray.clear()
markArray.clear()
print(studentName,subjectArray,markArray)
Array is taking entries of the data but studentName is running infinitely
other than that it is not printing the Average as well as marks
it just keep taking entries and nothing else
I want students name with their subjects and marks as well as total of their marks along with avg

Related

Using R to write "Create Table" Commands

I have this dataset over here in R:
my_table = data.frame(id = c(1,2,3), name = c("sam", "smith", "sean"), height = c(156, 175, 191), address = c("123 first street", "234 second street", "345 third street"))
id name height address
1 1 sam 156 123 first street
2 2 smith 175 234 second street
3 3 sean 191 345 third street
I can get the summary of this table as follows:
st = capture.output(str(my_table))
>st
[1] "'data.frame':\t3 obs. of 4 variables:"
[2] " $ id : num 1 2 3"
[3] " $ name : chr \"sam\" \"smith\" \"sean\""
[4] " $ height : num 156 175 191"
[5] " $ address: chr \"123 first street\" \"234 second street\" \"345 third street\""
Further manipulation:
types = substr(st[c(2,3,4,5)], 13,15)
[1] "num" "chr" "num" "chr"
type_frame = data.frame(id = 1,2,3,4, types)
type_frame = type_frame[,c(1,5)]
type_frame$sql = ifelse(type_frame$types == "num", "int", "varchar(255)")
type_frame$name = colnames(my_table)
id types sql name
1 1 num int id
2 1 chr varchar(255) name
3 1 num int height
4 1 chr varchar(255) address
Based on this information, I would like to generate the following string output:
CREATE TABLE my_table (
id int,
name varchar(255),
height int,
address varchar(255),
);
I thought of the following approach:
first_part = "CREATE TABLE my_table ( "
second_part = c(type_frame[1,4], type_frame[1,3])
second_part = paste(second_part , collapse = " ")
third_part = c(type_frame[2,4], type_frame[2,3])
third_part = paste(third_part , collapse = " ")
fourth_part = c(type_frame[3,4], type_frame[3,3])
fourth_part = paste(fourth_part , collapse = " ")
fifth_part = c(type_frame[4,4], type_frame[4,3])
fifth_part = paste(fifth_part , collapse = " ")
final = paste0(first_part, second_part, " , ", third_part, " , ", fourth_part, " , ", fifth_part, " ,);")
The end result looks something like this:
>final
[1] "CREATE TABLE my_table ( id int , name varchar(255) , height int , address varchar(255) ,);"
In the end, I would like to take the above string and enter it into an SQL software.
The code I have written is very inefficient - can someone please show me a faster way to do this?
Thank you!

Filtering with sqldf in R when fields have quotation marks

I have a large sql db (7gbs), where the fields appear to have quotation marks in them.
For example:
res <- dbSendQuery(con, "
SELECT *
FROM master")
dbf2 <- fetch(res, n = 3)
dbClearResult(res)
Yields
NPI EntityTypeCode ReplacementNPI EmployerIdentificationNumber.EIN.
1 "1679576722" "1" "" ""
2 "1588667638" "1" "" ""
3 "1497758544" "2" "" "<UNAVAIL>"
ProviderOrganizationName.LegalBusinessName. ProviderLastName.LegalName. ProviderFirstName
1 "" "WIEBE" "DAVID"
2 "" "PILCHER" "WILLIAM"
3 "CUMBERLAND COUNTY HOSPITAL SYSTEM, INC" "" ""
I've been trying to get a smaller table by filtering on, say EntityTypeCode but I'm not getting any results. Here's an example of a query not getting anything, any advice? I think the issue is use of double quotes in the fields.
# Filter on State
res <- dbSendQuery(npi2, "
SELECT *
FROM master
WHERE (ProviderBusinessPracticeLocationAddressStateName = 'PA')
")
# Filter on State and type
res <- dbSendQuery(npi2, "
SELECT *
FROM master
WHERE (ProviderBusinessPracticeLocationAddressStateName = 'PA') AND
(EntityTypeCode = '1')
")
Escape the inner double quotes (ie, the ones in the cell) with a \.
res <- dbSendQuery(npi2, "
SELECT *
FROM master
WHERE (ProviderBusinessPracticeLocationAddressStateName = '\"PA\"') AND
(EntityTypeCode = '1')
")
This produces the following string:
SELECT *
FROM master
WHERE (ProviderBusinessPracticeLocationAddressStateName = '"PA"')

calculate rank from student marks stored in datatable in vb. net

I am working on examination result system in vb.net which requires to calculate student ranks based on marks obtained. Subject marks data is stored in database. I am loading the subject marks in a datatable
da.Fill(dt) 'added to a datagridview.
DataGridView1.DataSource = dt
then Add New columns in dt to show result:
dt.Columns.Add("Obtained Marks", GetType(String))
dt.Columns.Add("Percent", GetType(String))
dt.Columns.Add("Result", GetType(String))
dt.Columns.Add("Rank", GetType(Integer))
Then calculated total of all the subjects & added in obtained marks columns by looping through rows & columns of datatable.
For s As Integer = 0 To dt.Rows.Count - 1
For t As Integer = 0 To dt.Columns.Count - 1
obtmarks += CDbl(dt.Rows(s).Item(t))
Next
dt.Rows(s)("Obtained Marks") = obtmarks
dt.Rows(s)("Result") = "PASS"
dt.Rows(s)("Rank") = 'RANK OF STUDENT
Next
How can i calculate rank/position of students on the basis of total marks contained in datatable column "Obtained Marks".
i.e.
Student with marks 436 Rank should be 1
Student with marks 429.5 Rank should be 2
Student with marks 412 Rank should be 3 ....
so on until all the rows in record. (Image atttached)
if there is any function for datatable which can help here or how can i add the logic in the loop to calculate rank of students and add the value in rank column. Thanks
P.S. I dnt want to sort the rows on obtained marks, but want to Add rank of each student in front of his/her marks, which is already order by their Roll No.
You could use this code to set the Rank column in your table
DataView dv = new DataView(dt, "", "ObtainedMarks desc", DataViewRowState.CurrentRows);
for(int x = 0; x < dv.Count; x++)
dv[x].Row["Rank"] = x+1;
This could be done only after you have completed the code that calculates the column ObtainedMarks
Also, I suggest to execute all before setting the DataSource of the DataGridView to avoid unnecessary delays in the grid repainting itself when you have not yet finished with it
EDIT
To have the same rank for persons with the same marks you could try something like this
int lastMark = -1;
int currentRank = 0;
int atSameRank = 1;
DataView dv = new DataView(dt, "", "ObtainedMarks desc", DataViewRowState.CurrentRows);
for(int x = 0; x < dv.Count; x++)
{
int currentMark = Convert.ToInt32(dv["ObtainedMarks"]);
if(currentMark != lastMark)
{
lastMark = currentMark;
currentRank = currentRank + atSameRank;
atSameRank = 0;
}
else
atSameRank++;
dv[x].Row["Rank"] = currentRank;
}
WARNING, I am not at a PC where I could test it.

Can sb help me to correct my vb code?

Dim age As Integer = 1
Do While 1980 + age <> age * age
lblResult.Text = "The solution is " & age & " years old."
age = age + 1
Loop
When I run this program, it will give 44 not 45. Is there anything with it?
The problem is that the code updates lblResult.Text before it increments age, so when age is incremented from 44 to 45, the loop exits without updating the label to show the final value of age.
To fix the code, update lblResult.Text after incrementing age. Although you could do so inside the loop ...
Do While 1980 + age <> age * age
age = age + 1
lblResult.Text = "The solution is " & age & " years old."
Loop
... it suffices to update the label just once, after the loop finishes:
Do While 1980 + age <> age * age
age = age + 1
Loop
lblResult.Text = "The solution is " & age & " years old."

Rails : Calling a function in a loop returns same value for each iteration

I am trying to calculate Leave taken by a particular student "UG10001" in a particular month from a leaves table in mysql database. I am using following snippets of code--
def calculate(student_id)
leave = Leave.find_by_sql("SELECT leave_duration FROM leaves WHERE MONTH(leave_from) = 2
AND YEAR(leave_from) = 2013 AND MONTH(leave_to) = 2 AND YEAR(leave_to) = 2013 AND
academic_status = 'APPROVED' AND warden_status = 'APPROVED' AND student_id = student_id
AND status = 'ACTIVE'")
sum = leave.sum(&:leave_duration)
return sum
end
----------Update----------
def calculate(student_id)
sum = Leave.find_by_sql("SELECT leave_duration FROM leaves WHERE MONTH(leave_from) = 2
AND YEAR(leave_from) = 2013 AND MONTH(leave_to) = 2 AND YEAR(leave_to) = 2013 AND
academic_status = 'APPROVED' AND warden_status = 'APPROVED' AND student_id = student_id
AND status = 'ACTIVE'").sum(&:leave_duration)
return sum
#it did the trick for me.
end
The above method will calculate the leave taken by a particular student in the month of February '13 and returns the sum of the leaves to the calling function.
def calcuateLeaveForEachStudent
array = Leave.find_by_sql("select student_id from students")
c = Array.new
for i in 0...array.length
student_id = array[i].student_id
c[i] = calculate(student_id)
end
end
But when I call the above method, 'calculate' method returns the same value on each iteration. What could be the possible solution for this..? Thanks in advance..!
PS -
Code runs perfectly on Rails Console without any syntactical errors, however there is some error I can't figure out.
calculate(operation, column_name, options = {}).
or you can refer the link