Ad

Quadratic eq. in python

They need:

  • more tests
  • imaginary roots solving (?)
  • to be shorter

tests working 👍 and i couldn't translate the VN comments (T_T)

Code
Diff
  • import math 
    
    def quadratic(a, b, c):
        l = []
        if a == 0:
            return 'error! a cannot be 0!'
        else:
            D = (b**2) - (4*a*c)
            if D >= 0:
                alp = (-b + math.sqrt(D))/(2*a)
                bet = (-b - math.sqrt(D))/(2*a)
                l.append(D)
                l.append(round(alp, 3))
                l.append(round(bet,3))
                return l
            else: 
                return "nope can do, roots are imaginary or do not exist and I'm not dealing with either"
    
    
    
    • //IamQuan :3
    • import math
    • //Libraly
    • #include<iostream>
    • #include <math.h>
    • using namespace std;
    • //Code
    • //Ham Phu de goi an cho a b c
    • double GoiAn()
    • {
    • double ABC{};
    • std::cin >> ABC;
    • return ABC;
    • }
    • //Code
    • int main()
    • {
    • //Khai bao cho DK
    • int A{};
    • //Khai bao cho tinh toan
    • double x1, x2;
    • //--------------------------------------------------
    • // User nhap a b c
    • cout << "Chuong trinh tinh phuong trinh bac 2 mot an\n";
    • cout << "Vui long nhap cac he so a , b , c\n";
    • cout << "---------------------------------------------" << endl;
    • //Nhap he so a
    • cout << "Nhap he so a: " ;
    • double a{GoiAn()};
    • def quadratic(a, b, c):
    • l = []
    • if a == 0:
    • return 'error! a cannot be 0!'
    • else:
    • D = (b**2) - (4*a*c)
    • if D >= 0:
    • alp = (-b + math.sqrt(D))/(2*a)
    • bet = (-b - math.sqrt(D))/(2*a)
    • l.append(D)
    • l.append(round(alp, 3))
    • l.append(round(bet,3))
    • return l
    • else:
    • return "nope can do, roots are imaginary or do not exist and I'm not dealing with either"
    • //Dieu kien
    • while (a == 0 && A < 1000)
    • {
    • cout << "Phuong trinh da chuyen ve \nphuong trinh bac nhat mot an" << endl;
    • cout << "Ban co muon nhap lai ham so a ?\nNeu muon thi nhap lai.\na:" << endl;
    • cin >> a;
    • A++;
    • }
    • //Nhap he so b
    • cout << "Nhap he so b: ";
    • double b{ GoiAn() };
    • //Nhap he so c
    • cout << "Nhap he so c: ";
    • double c{ GoiAn() };
    • // Thong bao cho user
    • cout << "Dang tinh delta va nghiem ......." << endl;
    • //Khai bao Delta
    • double delta{ (b * b) - 4 * a * c };
    • //Tinh delta
    • if (delta < 0)
    • {
    • cout << "Delta = " << delta << "\n";
    • cout << " => Phuong trinh vo nghiem " << endl;
    • }
    • else if (delta == 0)
    • {
    • cout << "Delta = " << delta << "\n";
    • cout << " => Phuong trinh co nghiem kep " << endl;
    • }
    • else if (delta > 0)
    • {
    • cout << "Delta = " << delta << "\n";
    • cout << " => Phuong trinh co 2 nghiem phan biet " << endl;
    • }
    • cout << "\n\n";
    • //Tinh nghiem
    • if (delta > 0)
    • {
    • x1 = (-b - sqrt(delta)) / (2 * a);
    • x2 = (-b + sqrt(delta)) / (2 * a);
    • cout << "Nghiem cua phuong trinh la: \n";
    • cout << "x1 = " << x1 << endl;
    • cout << "x2 = " << x2 << endl;
    • }
    • else if (delta == 0) {
    • x1 = x2 = -b / (2 * a);
    • cout << "Nghiem cua phuong trinh la: \n";
    • cout << "x1 = x2 = " << x1 << endl;
    • }
    • else
    • {
    • cout << "Vi phuong trinh vo nghiem nen khong co nghiem" << endl;
    • }
    • system("pause");
    • return 0;
    • }

Triangles but it's a class now..It solves the following problems:

  1. Determines third angle (with 2 angles)
  2. Determines the type of triangle with angles (Acute, Obtuse and Right)
  3. Determines the type of triangle with sides (Scalene, Isoceles, Equilateral)

I destroyed it once by reloading it (without saving) 🤦

Code
Diff
  • class Triangle:
        def __init__(a, b, c, s1, s2, s3):
            self.a = a
            self.b = b
            self.c = c 
            self.s1 = s1
            self.s2 = s2
            self.s3 = s3
    
        def other_angle(a, b): 
            return 180 - (a + b)
        
        def triangle_type_angle(a, b, c):
            if all( angle < 90 for angle in (a, b ,c)):
                return "Acute Triangle"
            if any(angle == 90 for angle in (a, b, c)):
                return "Right Triangle"
            return "Obtuse Triangle"
        
        def triangle_type_sides(s1, s2, s3):
            if s1 == s2 == s3:
                return "Equilateral Triangle"
            elif s1 == s2 or s2 == s3 or s1 == s3:
                return "Isoceles Triangle"
            return "Scalene Triangle"
            
    
    • def other_angle(a, b): return 180 - (a + b)
    • class Triangle:
    • def __init__(a, b, c, s1, s2, s3):
    • self.a = a
    • self.b = b
    • self.c = c
    • self.s1 = s1
    • self.s2 = s2
    • self.s3 = s3
    • def other_angle(a, b):
    • return 180 - (a + b)
    • def triangle_type_angle(a, b, c):
    • if all( angle < 90 for angle in (a, b ,c)):
    • return "Acute Triangle"
    • if any(angle == 90 for angle in (a, b, c)):
    • return "Right Triangle"
    • return "Obtuse Triangle"
    • def triangle_type_sides(s1, s2, s3):
    • if s1 == s2 == s3:
    • return "Equilateral Triangle"
    • elif s1 == s2 or s2 == s3 or s1 == s3:
    • return "Isoceles Triangle"
    • return "Scalene Triangle"

Translation to Python??

Code
Diff
  • def other_angle(a, b):
        req_angle = 180 - (a + b)
        return req_angle
    
    
    • fn other_angle(a: u32, b: u32) -> u32 {
    • 180 - (a + b)
    • }
    • def other_angle(a, b):
    • req_angle = 180 - (a + b)
    • return req_angle