100% found this document useful (1 vote)
19 views2 pages

Cost Calculation Algorithm for Office Lunch

The document outlines a minor assignment for a course on Applied Computational Thinking, focusing on algorithmic thinking and Python fundamentals. It includes various tasks such as defining algorithms, creating truth tables, writing programs for mathematical calculations, and debugging strategies. The assignment also covers practical coding exercises involving string manipulation, conditional statements, and arithmetic operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
19 views2 pages

Cost Calculation Algorithm for Office Lunch

The document outlines a minor assignment for a course on Applied Computational Thinking, focusing on algorithmic thinking and Python fundamentals. It includes various tasks such as defining algorithms, creating truth tables, writing programs for mathematical calculations, and debugging strategies. The assignment also covers practical coding exercises involving string manipulation, conditional statements, and arithmetic operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Centre for Data Science

Institute of Technical Education & Research, SOA, Deemed to be University

Applied Computational Thinking (CSE 1401)


MINOR ASSIGNMENT-2: ALGORITHMIC THINKING & PYTHON
FUNDAMENTALS

1. Define an algorithm. List and explain six characteristics of a good algorithm.

2. Write a mathematical algorithm for calculating the cost of office lunch with sandwiches ($8.50) and
salads ($7.95).

3. Create a truth table for two conditions: “It rains”, “I carry an umbrella”. Convert the truth table into
a Python decision-making algorithm.

4. Write a deductive reasoning program that checks: If a quadrilateral has four right angles, then it is a
rectangle.

5. Analyze the difference between inductive and deductive reasoning with coding examples.

6. Explain and fix whether the code can generate any error:

number = input("Enter a number: ")


print(10 / number)

7. Discuss three strategies for effective debugging in Python.

8. Develop a program that asks for age and prints whether the user is a child, teenager, adult, or senior
citizen.

9. Write an algorithm to calculate the grade of a student given marks in 5 subjects.

10. Create a simple menu-driven calculator program (add, subtract, multiply, divide).

11. Build a Python program to simulate a digital clock that displays time in HH:MM:SS format.

12. Evaluate in Python: (12 + 5) × 3, 50 − 43 , and 25 ÷ 4 (use both / and //).

13. Find the remainder when 987 is divided by 23.

14. Evaluate the following expressions in Python and explain step by step:

(a) 7 + 4 * 32 − 12/6 + 5%2

(a) 10 - 3 + 2 * 5 / 22 + 7%3

(a) 8 + 6 / 3 * 2 - 5 % 3 + 42

(a) 15 - 3 * 2 + 18 / 32 + 7%4

(a) Evaluate each of the following:


i. 5 + 2 * 3 - 4 / 2
ii. 62 − 3 ∗ 4 + 8%3
ii. 10 / 2 + 7 * 2 - 3

1
Centre for Data Science
Institute of Technical Education & Research, SOA, Deemed to be University

iii. 4 + 32 ∗ 2 − 5%2
iii. 12 - 4 * 2 + 18 / 3 % 5

15. Convert the float 45.89 into integer using int(). Explain the result.

16. Demonstrate multiple assignment: assign x, y, z = 5, 10, 15. Print their sum.

17. Print the following exactly as shown using escape characters:

She said, "Python is fun!"

18. Suppose name = "Anita" and marks = 90. Print "Anita scored 90 marks" using an
f-string.

19. Use the format() method to print: "The value of pi is approximately 3.142".

20. Take inputs: student name, roll number, and print: "My name is <student name>,
my roll number is <roll number>".

21. Print the sentence "2 + 3 = 5" using both f-strings and format().

22. Write Python code to print a multiplication table of 7 in the format: "7 x 1 = 7" up to "7 x 10
= 70".

23. Given s = "Hello BTech", write code to find its length, convert it to uppercase, and count how
many times "l" occurs.

24. Slice "PythonProgramming" to print "Python", "Programming", and "nohtyP" (re-


verse).

25. Write a Python script to display:

*****
* *
*****

using string operations.

Common questions

Powered by AI

The code might generate a `TypeError` because `input()` returns a string, and division directly involving a string with an integer is invalid. To fix this, convert the input to a number using `int()` or `float()` depending on whether you expect an integer or a floating-point number. The fixed code would be: ```python number = float(input("Enter a number: ")) print(10 / number) ``` This allows the division to be performed correctly if the input is a valid number .

The six characteristics of a good algorithm are: 1. **Correctness**: The algorithm should correctly solve the problem for all possible inputs. 2. **Efficiency**: It should make optimal use of resources like time and space. 3. **Finiteness**: The algorithm must terminate after a finite number of steps. 4. **Definiteness**: Each step must be clearly and unambiguously defined. 5. **Input**: It should accept external inputs to process. 6. **Output**: The algorithm should produce at least one output. These characteristics are important as they ensure the algorithm is reliable, performs efficiently, and meets the requirements under all circumstances .

The remainder of 987 divided by 23 is calculated using the modulo operator `%` in Python. The expression `987 % 23` will yield `21` as the remainder. ```python remainder = 987 % 23 print(remainder) # Output will be 21 ``` This operation computes the remainder left over when 987 is divided by 23 .

The algorithm to calculate the cost of an office lunch involving sandwiches and salads is outlined as follows: 1. Set the cost of a sandwich to $8.50 and the cost of a salad to $7.95. 2. Accept the number of sandwiches (num_sandwiches) and salads (num_salads) as inputs. 3. Calculate the total cost using the formula: `total_cost = (num_sandwiches * 8.50) + (num_salads * 7.95)`. 4. Output the total cost. This algorithm calculates the total price by multiplying the quantity of each item by its respective price and summing the results, providing a simple yet effective method to determine meal costs .

The logic for the classification involves using `if...elif...else` statements based on age ranges. Here is a Python program that implements this: ```python age = int(input("Enter your age: ")) if age <= 12: category = 'child' elif 13 <= age <= 19: category = 'teenager' elif 20 <= age <= 64: category = 'adult' else: category = 'senior citizen' print(f'You are classified as a {category}.') ``` The classification is based on standard age definitions where ages 0-12 are children, 13-19 are teenagers, 20-64 are adults, and 65 and above are senior citizens .

The operations involve slicing the string indices directly. Here is the Python code: ```python s = "PythonProgramming" python_part = s[:6] # "Python" programming_part = s[6:] # "Programming" reversed_python = s[5::-1] # "nohtyP" print(python_part) print(programming_part) print(reversed_python) ``` This code uses slicing to extract parts of a string and string splicing with negative indices to reverse the "Python" portion .

The `format()` method in Python is used to insert variables in strings. Here is how it is used to print a specific statement about pi: ```python pi = 3.142 print("The value of pi is approximately {:.3f}".format(pi)) ``` The `{:.3f}` indicates formatting the float to three decimal places, resulting in a precise representation of pi in the string .

To convert a truth table into a Python decision-making algorithm, we define Boolean variables for each condition. For example, 'it_rains' and 'carry_umbrella' can represent the conditions. The decision-making can use an 'if' statement to check these conditions: ```python it_rains = True # or False based on the scenario carry_umbrella = False if it_rains and not carry_umbrella: action = 'Get an umbrella.' elif it_rains and carry_umbrella: action = 'You are prepared.' else: action = 'Enjoy the weather!' ``` This structure allows deciding actions based on combinations of the truth values, similar to rows in a truth table .

Inductive reasoning involves creating generalizations based on specific instances or observations. For example, if a programmer notices that all the errors in a software module are due to boundary conditions, they might generalize that boundary conditions often cause errors in similar contexts. Deductive reasoning, on the other hand, involves applying general rules to specific cases. For instance, if it's a known rule that 'All squares have four equal sides,' then deducing that a quadrilateral with four equal sides is a square applies deductive reasoning. In programming, inductive reasoning might help in debugging by noticing patterns in errors, whereas deductive reasoning helps apply known algorithms or methods to solve specific problems .

Three strategies for effective debugging in Python are: 1. **Print Statements**: Use print statements strategically to inspect the values of variables and flow at different points in your code. This helps in pinpointing where things go wrong. 2. **Use a Debugger**: Employ debugging tools that allow you to set breakpoints, step through code, and evaluate expressions. Tools such as Python's pdb module or IDE-integrated debuggers enhance understanding of code execution. 3. **Check and Log Error Messages**: Review both exceptions and error messages carefully. Consider logging errors for further analysis, which helps identify common issues and potential fixes over time. These methods help locate and identify errors efficiently and ensure a systematic approach to resolving issues .

You might also like