Merge branch 'master' of gitlab.com:shockrahwow/csnotes

This commit is contained in:
Medium Fries 2019-05-07 14:03:41 -07:00
commit 1d08a743e4
11 changed files with 597 additions and 3 deletions

32
338/homework4/Course.java Normal file
View File

@ -0,0 +1,32 @@
/*
Class:
Course
Description:
Structure to represent a course in the school system
*/
public class Course {
public int id;
public String title;
public int instructorID;
public String location;
public int studentCount;
public Course (int id, String title, int instructorID, String location) {
this.id = id;
this.title = title;
this.instructorID = instructorID;
this.location = location;
this.studentCount = 0;
}
// updates the room location of a given course ex. BIT-104 to BIT-110
public void updateLocation(String newLocation) {
this.location = newLocation;
}
public String toString() {
return this.id + " " + this.title;
}
}

View File

@ -0,0 +1,22 @@
/*
Class:
Instructor
Description:
Structure to represent a instructor in the school system
*/
public class Instructor {
public int id;
public String name;
public String email;
public String phone;
public Instructor(int id , String name, String email, String phone) {
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
}
public String toString() {
return this.name;
}
}

300
338/homework4/School.java Normal file
View File

@ -0,0 +1,300 @@
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
// NOTE: there will probably only be one instance of a school object ever
public class School {
private ArrayList<Instructor> instructorList;
private ArrayList<Course> courseList;
private ArrayList<Student> studentList;
private String name;
public School(/*String file,*/ String schoolName) {
this.instructorList = new ArrayList<Instructor>();
this.courseList = new ArrayList<Course>();
this.studentList = new ArrayList<Student>();
this.name = schoolName;
//readData(file);
}
public void readData(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
// first line is how many instructors we want to add
int inst_count = Integer.parseInt(reader.readLine());
for(int i =0;i<inst_count;i++) {
// now we shold begin to populate out instructor list
String[] data = reader.readLine().split(",");
int tmp_id = Integer.parseInt(data[0]);
// ignoring the return value as those custom messages are in the method itself
this.addInstructor(tmp_id, data[1], data[2],data[3]);
}
// next line should be number of courses to add
int course_count = Integer.parseInt(reader.readLine());
for(int i =0;i<course_count;i++) {
String[] data = reader.readLine().split(",");
int tmp_id = Integer.parseInt(data[0]);
int inst_id = Integer.parseInt(data[2]);
this.addCourse(tmp_id, data[1], inst_id, data[3]);
}
// finally the amount of students we want to add is next
int student_count = Integer.parseInt(reader.readLine());
for(int i =0;i<student_count;i++) {
// parse items from the current line
String[] data = reader.readLine().split(",");
int stud_id = Integer.parseInt(data[0]);
int cour_id = Integer.parseInt(data[2]);
float fl_grade = Float.parseFloat(data[3]);
// attempt to add the student
this.addStudent(stud_id, data[1], cour_id, fl_grade, data[4]);
}
System.out.println("Done.");
}
catch(IOException e) {
e.printStackTrace();
}
}
public boolean addInstructor(int id, String name, String email, String phone) {
// make sure that the new instructor isn't already in the system
for(Instructor i : this.instructorList) {
if(id == i.id) {
System.out.println("Instructor ID already in use");
return false;
}
}
Instructor new_inst = new Instructor(id, name, email, phone);
instructorList.add(new_inst);
return true;
}
public boolean addCourse(int id, String title, int instructorID, String location) {
// make sure we don't the same course twice
for(Course c : this.courseList) {
if(c.id == id) {
System.out.println("Course addition failed Course number already used.");
return false;
}
}
// check if the instructor exists
boolean instructorExists = false;
for(Instructor i : this.instructorList) {
if(i.id == instructorID) {
instructorExists = true;
}
}
if(!instructorExists) {System.out.println("Course addition failed Non-existing instructor.");}
// check the instructor
Course new_course = new Course(id, title, instructorID, location);
courseList.add(new_course);
return true;
}
public int courseEnrolled(int id) {
// Helper function which simply counts up how many people are enrolled in a given cuorse
int count = 0;
for(Student s: this.studentList) {
if(s.courseID == id) {
count++;
}
}
return count;
}
public String getInstructor(int id) {
// find the instructor name given an id
for(Instructor i : this.instructorList) {
if(i.id == id) {
return i.name;
}
}
return "Not Found";
}
public float courseAverage(int id) {
// gets average of all the students grades for a given course by id
float sum = 0;
int count = 0;
for(Student s : this.studentList) {
if(s.courseID == id) {
sum += s.floatGrade;
count++;
}
}
if(count == 0) {
return 0.00f;
}
return sum / (float)count;
}
public void courseInfo(int id) {
boolean found = false;
// look for the course given by the id
for(Course i : this.courseList) {
found = true;
if(i.id == id) {
System.out.println("Course number: " + i.id);
System.out.println("Instructor: "+ this.getInstructor(i.instructorID));
System.out.println("Course title: " + i.title);
System.out.println("Room: " + i.location);
System.out.println("Total enrolled: " + this.courseEnrolled(id));
System.out.println("Course average: " + this.courseAverage(id));
}
}
if(!found) {
System.out.println("Course not found: [id = " + id + "]");
}
}
public void courseInfo() {
System.out.println("Number of courses: " + this.courseList.size());
for(Course c : this.courseList) {
System.out.println(c.id + ": " + this.courseEnrolled(c.id) + " enrolled");
}
}
public Course getCourse(int id) {
for(int i =0;i<this.courseList.size();i++) {
if(this.courseList.get(i).id == id) {
return this.courseList.get(i);
}
}
return null;
}
public boolean courseEmpty(int courseID) {
for(Student s : this.studentList) {
if(s.courseID == courseID) {
return false;
}
}
return true;
}
public boolean deleteCourse(int id) {
for(int i =0;i<this.courseList.size();i++) {
if(this.courseEmpty(this.courseList.get(i).id)) {
if(this.courseList.get(i).id == id) {
this.courseList.remove(i);
return true;
}
}
}
return false;
}
public boolean addStudent(int id, String name, int courseID, float floatGrade, String letterGrade) {
for(Student i : this.studentList) {
if(i.id == id) {
// attempt to regitser the student in case they're trying to add a new course
// this method should also give us proper error msgs about registering
if(this.register(id, courseID, floatGrade, letterGrade)) {
return true;
}
return false;
}
}
// check if the course even exists at all
boolean cFound = false;
for(Course c : this.courseList) {
if(c.id == courseID) {
cFound = true;
}
}
if(!cFound) {
System.out.println("Course does not exist");
return false;
}
// finally we can add the student into the system
// this is assuming we didn't register them earlier
Student new_student = new Student(id, name, courseID, floatGrade, letterGrade);
studentList.add(new_student);
return true;
}
private float studentAverage(int id) {
float sum = 0.0f;
int count = 0;
for(Student s : this.studentList) {
if(s.id == id) {
sum += s.floatGrade;
count++;
}
}
return sum/(float)count;
}
private String studentCourses(int id) {
String info = "";
for(Student s : studentList) {
if(s.id == id) {
info += s.courseID + ": " + s.floatGrade +
" " + s.letterGrade;
}
}
return info;
}
public Student getStudentInfo(int id) {
for(int i =0; i<this.studentList.size(); i++) {
if(this.studentList.get(i).id == id) {
this.studentList.get(i).average = studentAverage(id);
this.studentList.get(i).courseInfo = studentCourses(id);
return this.studentList.get(i);
}
}
return null;
}
public boolean graduateStudent(int id) {
// remove the student from all courses and set their only course id to graduated=0
Student tmp = getStudentInfo(id);
if(tmp == null) {
System.out.println("Student not found");
return false;
}
tmp.courseID = 0;
tmp.status = "Graduated";
return true;
}
public boolean register(int studentID, int courseID, float floatGrade, String letterGrade) {
Student tmp = this.getStudentInfo(studentID);
if(tmp.courseID == courseID) {
System.out.println("Student already enrolled");
return false;
}
// make sure we don't try to regitster for a course that doesn't exist
if(this.getCourse(courseID) == null) {
return false;
}
// if the student is there but not enrolled in that course then just create a new student with new data
Student new_student = new Student(tmp.id, tmp.name, courseID, floatGrade, letterGrade);
studentList.add(new_student);
return true;
}
public void drop(int studentID, int courseID) {
}
public void assign(int instructorID, int courseID) {
}
public void unassign(int instructorID, int courseID) {
}
// basic printout of school infor
public void schoolInfo() {
System.out.println("School name: " + this.name);
// instructors
System.out.println("Instructor info");
for(Instructor i : this.instructorList) {
System.out.println(i.name);
}
// courses
System.out.println("Course Information");
for(Course c : this.courseList) {
System.out.println(c.title);
}
// students
System.out.println("Student Information");
for(Student s : this.studentList) {
// grab the course which the student is enrolled in
Course c_title = this.getCourse(s.courseID);
System.out.println(s.name + ": " + c_title.title);
}
}
}

View File

@ -0,0 +1,37 @@
/*
Class:
Student
Description:
Structure to represent students in the school system
*/
public class Student {
public int id;
public String name;
public int courseID;
public float floatGrade;
// helper attributes
public float average;
public String courseInfo;
// using a string to allow for + and - grades
public String letterGrade;
public String status;
public Student(int id, String name, int courseId, float floatGrade, String letterGrade) {
this.name = name;
this.id = id;
this.courseID = courseId;
this.floatGrade = floatGrade;
this.letterGrade = letterGrade;
// default student status
this.status = "Enrolled";
}
public String toString() {
return "Student number: " + id + "\n" +
"Name: " + name + "\n" +
"Status: " + status + "\n" +
"Courses Enrolled: " + courseInfo + "\n" +
"Average: " + average;
}
}

85
338/homework4/T1.java Normal file
View File

@ -0,0 +1,85 @@
/*
Author: Alejandro Santillana
Date: May 6, 2019
Description: Builds a basic school data tracker
keeps track of students and what course they are enrolled in as well as what courses
are offered by the school.
Course information is also available
Input is read from file
*/
public class T1 {
public static void main(String[] args) {
String dataFile1 = "data1.txt";
String dataFile2 = "data2.txt";
// course reference for demo purposes
Course course1;
Student student1;
// first we'll read in some data
System.out.println("===== Read Data 1 =====");
School SCD = new School("CSUMB");
// get all basic info about the SCD
SCD.readData(dataFile1);
System.out.println("\n===== School Info 1 =====");
SCD.schoolInfo();
// round 2 of adding info to the SCD
System.out.println("\n===== Read Data 2 =====");
SCD.readData(dataFile2);
System.out.println("\n===== School Info 2 =====");
SCD.schoolInfo();
// we'll now add some new instructors
SCD.addInstructor(700, "E. Tao", "tao@csumb.edu", "777-777-1234");
SCD.addCourse(300, "CST300 ProSem", 700, "BIT110");
SCD.addCourse(231, "CST231 Intro C++", 100, "BIT104");
// examples of bad courses to add
System.out.println("\n===== Failed Course Addition =====");
SCD.addCourse(306, "CST306 GUI Dev", 250, "BIT120");
SCD.addCourse(499, "CST499 iOS Dev", 150, "BIT104");
System.out.println("\n===== Detailed Course Info =====");
SCD.courseInfo(306);
// updateing a courses location
course1 = SCD.getCourse(205);
course1.updateLocation("Library 104");
// checking some courses' information
System.out.println("\n===== Detailed Course Info 2 =====");
SCD.courseInfo(205);
System.out.println("\n===== Detailed Course Info 3 =====");
SCD.courseInfo();
SCD.deleteCourse(231);
SCD.deleteCourse(336);
SCD.deleteCourse(338);
System.out.println("\n===== Detailed Course Info 4 =====");
SCD.courseInfo();
// adding a student (valid this time)
SCD.addStudent(5555, "Chris Watson", 205, 85.50f, "B");
System.out.println("\n===== Detailed Course Info 5 =====");
SCD.courseInfo(205);
student1 = SCD.getStudentInfo(7777);
// student info
System.out.println("\n===== Detailed Student Info =====");
System.out.println(student1);
System.out.println("\n===== Detailed Student Info 2 =====");
System.out.println(SCD.getStudentInfo(7777));
SCD.graduateStudent(5555);
System.out.println("\n===== Detailed Course Info 6 =====");
SCD.courseInfo(205);
SCD.graduateStudent(5555);
System.out.println("\n===== Good Job! Bye! =====");
}
}

56
338/homework4/Test.java Normal file
View File

@ -0,0 +1,56 @@
import static org.junit.jupiter.api.Assertion.assertFalse;
import static org.junit.jupiter.api.Assertion.assertSame;
import static org.junit.jupiter.api.Assertion.assertNull;
import static org.junit.jupiter.api.Assertion.assertNotNull;
import static org.junit.jupiter.api.Assertion.assertEquals;
import org.junit.jupiter.api.Test;
class Tester {
public static School tester;
Tester() {
testSchool = new School("Test School");
}
@Test
public void addInstructor() {
assertEquals(false, tester.addInstructor(123, "name", "asdf@mail.org", "1123-12-312"));
}
@Test
public void addCourse() {
assertEquals(true, tester.addInstructor(999, "School of what", 100, "New build 7"));
}
@Test
public void getCourse() {
assertNull(test.getCourse(205));
}
@Test
public void deleteCourse() {
int len = tester.courseList.size();
tester.deleteCourse(205);
int newLen = test.courseList.size();
assertSame(len, newLen);
}
@Test
public void addStudent() {
// because the course doesn't exist this should return false
assertSame(false, test.addStudent(456, "Billy", 717, 89.0f, "B"));
}
@Test
public void getStudentInfo() {
assertNotNull(test.getStudentInfo(8888));
}
@Test
public void graduateStudent() {
// no student should have this id, ergo this should fail
assertFalse(tester.graduateStudent(9090));
}
}

13
338/homework4/data1.txt Normal file
View File

@ -0,0 +1,13 @@
4
100,Y. Byun,ybyun@csumb.edu,111-111-1111
200,S. Narayanan,sathya@csumb.edu,222-222-2222
300,M. Lara,lara@csumb.edu,333-333-3333
250,S. Bude,bude@csumb.edu,444-123-4567
3
338,CST338 - Software Design,100,BIT 104
205,CST205 - Multimedia Design and Programming,200,BIT 118
306,CST306 - Game Engine Programming,100,BIT 104
3
7777,Alice Otter,338,89.50,B
8888,Bob Otter,205,75.00,C
7777,Alice Otter,306,98.00,A

10
338/homework4/data2.txt Normal file
View File

@ -0,0 +1,10 @@
2
500,G. Bruns,bruns@csumb.edu,555-222-2222
300,O. Doe,doe@csumb.edu,444-333-3333
1
336,CST336 - Internet Programming,300,Library 1010
4
7777,Alice Otter,205,71.00,C
7777,Unknown Otter,205,71.00,C
7777,Alice Otter,310,81.55,B
9999,John Doe,338,89.50,B

8
338/homework4/makefile Normal file
View File

@ -0,0 +1,8 @@
all:
javac T1.java Course.java Instructor.java School.java Student.java
run: all
@java T1
clean:
rm -f *class

31
370/homework/huff.py Normal file
View File

@ -0,0 +1,31 @@
class Node():
def __init__(self, c, weight):
self.c = c
self.freq = weight
self.left = None
self.right = None
def __repr__(self):
return f'{self.c}|{self.freq}'
class Huffman:
def __init__(self, string):
self.heap = []
self.charMap = self.charFrequencies(string)
def charFrequencies(self, string):
ret = []
for i in string:
if i in ret:
ret[i] += 1
else:
ret[i] = 1
return ret.sort()
if __name__ == '__main__':
text = input()
binary = input()
# encode the first given string to build a tree from it
huff = Huffman(text)
print(huff.charMap)

View File

@ -4,14 +4,14 @@ import heapq
class Node:
def __init__(self, c, weight):
self.c = c
self.weight = weight
self.freq = weight
self.left = None
self.right = None
self.code = ''
def __repr__(self):
return f'{self.c}|{self.weight}'
return f'{self.c}|{self.freq}'
def __lt__(self, other):
return self.weight < other.weight
@ -27,7 +27,7 @@ def frequencyMap(string):
# otherwise increment the frequency of that node
for k in ret:
if k.c == i:
k.weight += 1
k.freq += 1
# Sort the charmap alphabetically
ret.sort(key=lambda x: x.c)