In: Computer Science
Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below.
· SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel)
The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e. in the above case, 1st element of names contains “Ali Hussan Butt” and 1st element of rolls contain “SP18-BCS-321”; similarly, 2nd element of names contains “Huma Khalid” and 2nd element of rolls contain “SP19-BCS-154” and so on. Hint: You may use delimiters string “() ,”. Ignore empty strings returned as tokens (i.e. strings with length 0), if any.
In the end, first, display roll numbers and names for only the following students:
· Who are enrolled in BCS program.
· Whose roll number is between 100 and 200. (Hint: you can extract last 3 characters of roll number, convert it into integer and then check range 100-200)
For example, in the above case, only following students should be displayed.
1. Ali Hussan Butt, roll number: SP18-BCS-050
2. Adil Jameel, roll number: SP17-BCS-014
NOTE:USE ONLY C LAUNGAGE AND DONT USE FILE HANDLING.
import os
from itertools import islice
cwd = "/home/dexter/Desktop" # working
directory
path = cwd +
"/student.txt" #
name of the text file
f1 = open(path,
"r")
# open text file in read mode
numLines = sum(1 for line in f1) # counting the
numbr of lines in the text file
f1.close()
# closing the text file
begin = 0 # start reading the text file from index
[0]
end = numLines #read the text file till the last row
tempArray=[] # temporary array
rolls=[] # array to store roll no.
names=[] # array to store name
with open(path, 'r') as infile:
lines_gen = islice(infile, begin, end) # reading
the text file from row[0] till last row
#here it is only 1 row becaue the data is comma (') separated
only
for line in lines_gen:
tempArray=line.split(",") # array to store comma (')
separated values
for i in range(len(tempArray)):
temp=tempArray[i].split("(")[0] #roll no
and names are separated by "("
# first part is the roll no. having a whitespace as the last
character
temp=temp.split("
")[0]
# removing the last character i.e. the whitespace
rolls.append(temp)
# storing the roll no. in array
temp=tempArray[i].split("(")[1] #roll no
and names are separated by "("
# second part is the name having a ")" as the last character
temp=temp.split(")")[0]
# removing the last character i.e. ")"
names.append(temp)
# storing the name in array
for i in range(len(rolls)):
currentRoll=rolls[i] #stores
the current roll no.
if (currentRoll.find("BCS") != -1): # if line
contains substring "BCS"
rollNum=int(currentRoll.split("-")[2]) #the 3rd part
contains the roll number, converting into integer
if
not(rollNum>=100
and rollNum<=200): #if the roll
num lies between 100 & 200
print(names[i],",roll number: ",rolls[i])