Skip to content

Instantly share code, notes, and snippets.

@miquelvir
Created March 16, 2025 22:02
Show Gist options
  • Save miquelvir/56ab70283a57018d65b1fe1157da91a7 to your computer and use it in GitHub Desktop.
Save miquelvir/56ab70283a57018d65b1fe1157da91a7 to your computer and use it in GitHub Desktop.
devQuest - 16/03/2025 23:02

Exercise 1

Write a program that takes an integer as input and prints whether the number is even or odd.

Teacher solution

number = int(input("Enter a number: "))

if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Test cases

These test cases have been created with AI, cross check anything important.

Even number

Input

4```

#### Expected output

The number is even.```

Odd number

Input

7```

#### Expected output

The number is odd.```

Zero input

Input

0```

#### Expected output

The number is even.```

Explanation

This explanation has been created with AI, cross check anything important.

This line prompts the user to enter a number. input() reads the user's input as text. int() then converts that text into a whole number, which is stored in the variable number.

number = int(input("Enter a number: "))

This line checks if the number is even or odd. The % operator calculates the remainder after division by 2. If the remainder is 0, the number is even.

if number % 2 == 0:

If the condition number % 2 == 0 is true (meaning the number is even), this line prints "The number is even."

print("The number is even.")

If the condition number % 2 == 0 is false (meaning the number is odd), the code inside the else block is executed.

else:

This line prints "The number is odd." when the number is not even.

print("The number is odd.")

Practice exercises

These exercises have been created with AI, cross check anything important.

  • similar Write a program that asks the user for their age. If the age is greater than or equal to 18, print 'You are an adult.' Otherwise, print 'You are a minor.'
  • moderate Write a program that takes two numbers as input. Determine which number is larger and print 'The larger number is: [number]' or if they are equal print 'The numbers are equal.'
  • challenge Write a program that asks the user for a year. Determine if the year is a leap year and print 'Leap year' or 'Not a leap year'. (A leap year is divisible by 4, but not divisible by 100 unless it is also divisible by 400).
  • challenge Write a program that takes an integer as input. Determine if the number is a prime number. Print 'Prime' if it is and 'Not prime' if it is not. (A prime number is only divisible by 1 and itself.)

Exercise 2

Write a program that takes two integer inputs, and then prints whether the second number is equal to, less than, or greater than the square of the first number.

Teacher solution

num1 = int(input("Enter the first value: "))
num2 = int(input("Enter the second value: "))

num1_squared = num1**2  # create a variable to avoid calculating the power twice

if num2 == num1_squared:
    print('The second is the square of the first')
elif num2 < num1_squared:
    print('The second is less than the square of the first')
else:
    print('The second is greater than the square of the first')

Test cases

These test cases have been created with AI, cross check anything important.

Second number is the square of the first.

Input

5
25

Expected output

The second is the square of the first

Second number is less than the square of the first.

Input

3
5

Expected output

The second is less than the square of the first

Second number is greater than the square of the first.

Input

4
17

Expected output

The second is greater than the square of the first

Explanation

This explanation has been created with AI, cross check anything important.

The program starts by asking the user to enter the first number. The input() function reads the user's input as text. int() converts this text into an integer (whole number), and this integer is stored in the variable num1.

num1 = int(input("Enter the first value: "))

Next, the program asks the user to enter the second number. Similar to the first number, the input is read as text, converted to an integer, and stored in the variable num2.

num2 = int(input("Enter the second value: "))

The code calculates the square of the first number (num1) by raising it to the power of 2 using the ** operator. The result is stored in the variable num1_squared. This avoids calculating the square twice in later comparisons.

num1_squared = num1**2

The code then uses an if-elif-else statement to compare num2 with the square of num1 (which is num1_squared).

if num2 == num1_squared:
    print('The second is the square of the first')
elif num2 < num1_squared:
    print('The second is less than the square of the first')
else:
    print('The second is greater than the square of the first')

If num2 is equal to num1_squared, the program prints 'The second is the square of the first'.

if num2 == num1_squared:
    print('The second is the square of the first')

Otherwise, if num2 is less than num1_squared, the program prints 'The second is less than the square of the first'.

elif num2 < num1_squared:
    print('The second is less than the square of the first')

Finally, if neither of the above conditions is true (meaning num2 is greater than num1_squared), the program prints 'The second is greater than the square of the first'.

else:
    print('The second is greater than the square of the first')

Practice exercises

These exercises have been created with AI, cross check anything important.

  • similar Write a program that takes two integer inputs. If the first number is even and the second number is odd, print 'Even Odd'. If the first number is odd and the second number is even, print 'Odd Even'. Otherwise, print 'Same Parity'.
  • moderate Write a program that asks the user for their age and height (in cm). If the age is greater than 18 and the height is greater than 160cm, print 'Eligible'. If the age is less than 18, print 'Too Young'. If the height is less than 160cm, print 'Too Short'. If neither condition is met print 'Not Eligible'.
  • challenge Write a program that takes three integer inputs: a base number, a lower bound, and an upper bound. Calculate the power of the base number for exponents ranging from the lower bound to the upper bound (inclusive). Print 'In range' if all calculated powers fall within the range of 100 to 10000 (inclusive). Print 'Out of range' otherwise.
  • challenge Write a program that takes two string inputs. The first string represents a person's name, and the second string represents their job title. If the name starts with 'A' or 'B' (case-insensitive) AND the job title contains the word 'Engineer' (case-insensitive), print 'Eligible'. If the name starts with 'C' or 'D' AND the job title contains the word 'Manager' print 'Eligible'. Otherwise, print 'Not Eligible'.

Exercise 3

Write a program that takes three side lengths as input and determines whether they form a valid triangle. If so, classify the triangle as equilateral, isosceles, or scalene.

Teacher solution

side1 = float(input("Side 1: "))
side2 = float(input("Side 2: "))
side3 = float(input("Side 3: "))

if side1 < side2 + side3 and side2 < side1 + side3 and side3 < side1 + side2:  # triangle inequality theorem
# (any side of a triangle must be less than the sum of the other sides)
    print("It is a triangle")
    if side1 == side2 == side3:
        print("Equilateral Triangle")
    elif side1 != side2 and side1 != side3 and side2 != side3:
        print("Scalene Triangle")
    else:
        print("Isosceles Triangle")
else:
    print("It is not a triangle")

Test cases

These test cases have been created with AI, cross check anything important.

Scalene triangle

Input

3
4
5

Expected output

It is a triangle
Scalene Triangle

Equilateral triangle

Input

2
2
2

Expected output

It is a triangle
Equilateral Triangle

Isosceles triangle

Input

2
2
3

Expected output

It is a triangle
Isosceles Triangle

Not a triangle

Input

1
2
5

Expected output

It is not a triangle

Explanation

This explanation has been created with AI, cross check anything important.

The code first prompts the user to enter the lengths of three sides and stores them as floating-point numbers in the variables side1, side2, and side3.

side1 = float(input("Side 1: "))
side2 = float(input("Side 2: "))
side3 = float(input("Side 3: "))

It then checks if the given side lengths can form a valid triangle using the triangle inequality theorem. This theorem states that the sum of any two sides of a triangle must be greater than the third side. The if condition checks if all three combinations satisfy this rule.

if side1 < side2 + side3 and side2 < side1 + side3 and side3 < side1 + side2:

If the sides form a valid triangle, the code prints "It is a triangle" and proceeds to classify the triangle based on its side lengths.

print("It is a triangle")

The code checks if all three sides are equal. If they are, it prints "Equilateral Triangle".

if side1 == side2 == side3:
        print("Equilateral Triangle")

If the triangle is not equilateral, the code checks if all three sides are different. If they are, it prints "Scalene Triangle".

elif side1 != side2 and side1 != side3 and side2 != side3:
        print("Scalene Triangle")

If the triangle is not equilateral or scalene, it must be isosceles (meaning two sides are equal). So, it prints "Isosceles Triangle".

else:
        print("Isosceles Triangle")

If the initial if condition (triangle inequality) is false, it means the given side lengths cannot form a valid triangle. In this case, the code prints "It is not a triangle".

else:
    print("It is not a triangle")

Practice exercises

These exercises have been created with AI, cross check anything important.

  • similar Write a program that takes three angles (in degrees) as input and determines if they form a valid triangle. If so, classify the triangle as acute, obtuse, or right-angled.
  • moderate Develop a program that accepts the coordinates (x, y) of three points. Determine if these points form a triangle. If they do, calculate the area of the triangle using Heron's formula.
  • challenge Create a program that takes an array of numbers as input. Determine the number of possible unique triangles that can be formed using any three numbers from the array as side lengths. Optimize for efficiency when the array is large.
  • challenge Write a program that determines if a point (x, y) lies inside a given triangle defined by three vertices (x1, y1), (x2, y2), and (x3, y3). Implement the solution using barycentric coordinates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment