Another beautiful day in this miserable world

Archive for the ‘programming’ Category

Python Programming


#A certain CS professor gives 5-point quizzes that are graded on the scale
#5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Wirte a program that accepts a quiz score
#as an input and prints out the corresponding grade.
def main():
    grade = eval(input("What is your quiz score? (Type quit to stop) "))
        if grade == 5:
            letter = 'A'
        if grade == 4:
            letter = 'B'
        if grade == 3:
            letter = 'C'
        if grade == 2:
            letter = 'D'
        if grade == 1 or grade == 0:
            letter = 'F'
        print(" Your grade is a ", letter)
main()

Python Programming. Sum of a Series of Numbers


#Write a program to sum a series of numbers entered by the user.
#The program should first prompt the use for how many numbers are to be summer.
#It should then input each of the numbers and print a total sum.

def main():
    total = 0
    num = eval(input("How many numbers do you have? "))
    for i in range (num):
        ber = eval(input("What is your first number? "))
        total = ber + total

    print("The total value of your ", num, "numbers is ", total)

main()

Facebook status turned into a recursion program — Python Programming

I was lamenting (which I obviously do a lot) about stuff and wrote a facebook status – written below. One of my friends mentioned that it was a recurrence relation that should be tested which I immediately took as a challenge and coded it. This was what I needed after struggling to understand Java syntax which is something I find to be an extreme opposite of Python.

Status:
Completely lost trying to program because I have to miss class because I have to go to work because I have to make money because I have to pay for college because I have to pay for class that I have to miss because I have to go to work because I have to make money because I have to pay for college because I have to pay for class that I have to miss because I have to go to work because I….need a vacation….


def func (var):
    if var == "I have to miss class":
        return 1
    if var == "I have to go to work":
        return 2
    if var == "I have to make money":
        return 3
    if var == "I have to pay for college":
        return 4
    if var == "I have to pay for class":
        return 5
    
def status(variable):
    if variable == 5:
        return 1
    else:
        return (variable + 1)
    
def twice(var):
    if var == 1:
        return "I have to miss class"
    if var == 2:
        return "I have to go to work"
    if var == 3:
        return "I have to make money"
    if var == 4:
        return "I have to pay for college"
    if var == 5:
        return "I have to pay for class"

    
def main():
    variable = 1
    while 1>0:
        variable = input("Completely lost in programming because ")
        because = func(variable)
        recur = status(because)
        reverse = twice(recur)
        print(variable, "because", reverse)
        
main()

My First Attempt at Java (Python > Java)

This was a Comp Sci homework assignment to have a user input 5 grades from their classes and it would calculate the GPA. There were to be no loops, so alot of the code is redundant I know. Took me all of 15 hours to do. Python > Java.

*Netbeans IDE


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;


/**
 *
 * @author Cori
 */

public class Main {

    /**
     * @param args the command line arguments
     */
    public static double gpapts(String grade) {
        double pts = 0.00;
        if ("A".equals(grade)) {
            pts = (4.00);
        } else if ("A-".equals(grade)) {
            pts = 3.67;
        } else if ("B+".equals(grade)) {
            pts = 3.33;
        } else if ("B".equals(grade)) {
            pts = 3.00;
        } else if ("B-".equals(grade)) {
            pts = 2.67;
        } else if ("C+".equals(grade)) {
            pts = 2.33;
        } else if ("C".equals(grade)) {
            pts = 2.00;
        } else if ("C-".equals(grade)) {
            pts = 1.67;
        } else if ("D+".equals(grade)) {
            pts = 1.33;
        } else if ("D".equals(grade)) {
            pts = 1.00;
        } else if ("F".equals(grade)) {
            pts = 0.0;
        } else {
                System.out.println("Error"); System.out.print("-1.0"); System.exit(0);
        } 
        return pts;

    }
    public static void main(String[] args) {
       
        
        java.util.Scanner input = new java.util.Scanner(System.in);

        System.out.print("How many credits was your first class ");
        int class1 = input.nextInt(); input.nextLine();
        System.out.print("What grade did you get? ");
        String ans1 = input.nextLine();
        String grade1 = ans1.toUpperCase();
        double pts1 = gpapts(grade1);
        double cg1 = class1 * pts1;
        System.out.print("How many credits was your second class? ");
        int class2 = input.nextInt(); input.nextLine();
        System.out.print("What grade did you get? ");
        String ans2 = input.nextLine();
        String grade2 = ans2.toUpperCase();
        double pts2 = gpapts(grade2);
        double cg2 = class2 * pts2;
        System.out.print("How many credits was your third class? ");
        int class3 = input.nextInt(); input.nextLine();
        System.out.print("What grade did you get? ");
        String ans3 = input.nextLine();
        String grade3 = ans3.toUpperCase();
        double pts3 = gpapts(grade3);
        double cg3 = class3 * pts3;
        System.out.print("How many credits was your fourth class? ");
        int class4 = input.nextInt(); input.nextLine();
        System.out.print("What grade did you get? ");
        String ans4 = input.nextLine();
        String grade4 = ans4.toUpperCase();
        double pts4 = gpapts(grade4);
        double cg4 = class4 * pts4;
        System.out.print("How many credits was your fifth class? ");
        int class5 = input.nextInt(); input.nextLine();
        System.out.print("What grade did you get? ");
        String ans5 = input.nextLine();
        String grade5 = ans5.toUpperCase();
        double pts5 = gpapts(grade5);
        double cg5 = class5 * pts5;
        int clavg = class1+class2+class3+class4+class5;
        double gravg = cg1+cg2+cg3+cg4+cg5;
        double avg = gravg/clavg;
        if (avg == 4.00) {
            System.out.printf("Your GPA is %.2f \n", avg);
            System.out.print("You have made the President's List!");
        }
        else if (avg < 4.00) {
            if (avg >= 3.50) {
                System.out.printf("Your GPA is %.2f \n", avg);
                System.out.print("You have made the Dean's List!");
            }
                if (avg >= 2.00) {
                    if (avg < 3.50) {
                    System.out.printf("Your GPA is %.2f", avg);
                }
            }

            else if (avg < 2.00) {
                System.out.printf("Your GPA is %.2f \n", avg);
                System.out.print("You are on academic probation :'(");
            
        }
    }

}
}

Python Programming: An Introduction to Computer Science by Zelle Chap 3 Ex 12

12. Write a program to find the sum of the cubes of the first n natural numbers where the value of n is provided by the user


def main():
    print("This program finds the sum of the cubes of the first ")
    print("'n' natural numbers starting with 0")
    n = eval(input("Enter any natural number "))
    add = 0
    for i in range (n):
        add = add + i**3
    print(add)
main()

Python Programming: An Introduction to Computer Science by Zelle Chap 3 Ex 11

11. Write a program to find the sum of the first n natural numbers where the value of n is provided by the user.

def main():
    print("This program finds the sum of the first 'n' natural numbers")
    print("starting with 0")
    n = eval(input("Enter any natural number "))
    add = 0
    for i in range (n):
        add = add + i
    print(add)
main()

Python Programming: An Introduction to Computer Science by Zelle Chap 3 Ex 10

10. Write a program to determine the length of a ladder required to reach a given height when leaned against a house. Prompt for an angle in degrees and convert it to radians.


from math import *
def main():
    height = eval(input("Please enter the height of the ladder "))
    deg = eval(input("Please enter the angle of the ladder in degrees "))
    rad = (pi/180) * deg
    leng = height/(sin(rad))
    print("The length of the ladder must be ", leng)
main()

Python Programming: An Introduction to Computer Science by Zelle Chap 3 Ex 9

9. Write a program to calculate the area of a triangle given the length of its three sides a,b, and c.

We will have to change the formula given in the book a little bit due to Python not recognizing a number outside of a parenthesis as multiplying. We will need to insert the “*” to get the program to work correctly.

from math import *
def main():
    a,b,c = eval(input("Enter the length of each side of a triangle seperated by commas "))
    s = (a+b+c)/2
    a = sqrt(s*(s-a)*(s-b)*(s-c))
    print("The area of the triangle is", a)
main()

Python Programming: An Introduction to Computer Science by Zelle Chap 3 Ex 8

This was actually a pretty interesting subject. I did a little bit of research to find out about it mostly because I noticed the “%” in the formula given and that means that we are talking about modulos. It’s pretty interesting. Each year is associated with an Epact. The Epact is a measure of the age of the moon (i.e. the number of days that have passed since an “official” new moon) on a particular date. The number 8 is a constant that calibrates the starting point of the Gregorian Epact so that it matches the actual age of the moon on new year’s day. Actually, this constant should have been 9, but 8 was probably chosen as a safety precaution; the calculation was known to be inaccurate, and the sentiment was that it was better to celebrate Easter too late than too early. In the Gregorian calendar, the Epact can have any value from 1 to 30

Example: What was the Epact for 1992?

GoldenNumber = 1992 mod 19 + 1 = 17
1) JulianEpact = (11 * (17-1)) mod 30 = 26
2) S = (3*20)/4 = 15
3) L = (8*20 + 5)/25 = 6
4) GregorianEpact = 26 – 15 + 6 + 8 = 25
5) No adjustment is necessary

The Epact for 1992 was 25.

8. The Gregorian epact is the number of days between January 1st and the previous new moon. This value is used to figure out the date of Easter.

There is a code given in the book, but we will have to modify it slightly to allow Python to read it correctly. The original program in the book is
c = year//100
epact = (8+(c//4) – c + (8c + 13)//25) + 11(year%19))%30
The reason we have to change this, is because python doesn’t recognize multiplication with the “*”. If you have a number next to a variable, it will come back as invalid syntax. As well, in :11(year%19)” You cannot put a number or variable before a parenthesis to multiply it because python reads that as a “function” instead of reading it as a multiplication problem. There will be more info on functions later. Be sure to keep this in mind with whatever programs you might write.

from math import *
def main():
    year = eval(input("Please enter the 4-digit year "))
    c = year//100
    epact = (8+(c//4) - c + (((8*c) + 13)//25) + 11*(year%19))%30
    print("The Gregorian epact is", epact)
main()

Python Programming: An Introduction to Computer Science by Zelle Chap 3 Ex 7

7. Write a program that accepts two points and determines the distance between them.

We must import the math library to use the “sqrt” function.


from math import *
def main():
    pt1x, pt1y = eval(input("Input the first point seperated by a comma "))
    pt2x, pt2y = eval(input("Input the second point seperated by a comma "))
    dis = sqrt(((pt2x-pt1x)**2) + ((pt2y - pt1y)**2))
    print("The distance between the two points is", dis)
main()