Problem Solving Tech
Problem Solving Tech
What is program?
A program is a set of instructions that tell a computer what to do. It's like a
recipe for baking a cake: it tells you the ingredients you need and the steps to
follow to make the cake. Similarly, a computer program tells the computer what
tasks to perform, like opening a file, doing calculations, or displaying a message
on the screen.
Define algorithm?
An algorithm is a set of step-by-step instructions designed to solve a specific
problem or perform a specific task. It's like a recipe that outlines the exact steps
to follow to achieve a desired outcome. Algorithms are used in computer
science and programming to help computers solve problems efficiently and
effectively.
what is array?
An array is a data structure that stores a collection of elements, typically of the
same type, in a contiguous block of memory. Each element in the array is
accessed by its index, which is an integer value indicating its position in the
array.
Documentation: Document your code to make it easier for others (or yourself in
the future) to understand and maintain.
1. Define the Goal: Begin by clearly defining the end goal or solution you
want to achieve. This could be a specific outcome, result, or state.
2. Identify the Final Step: Once you have a clear goal, identify the last step
or action that needs to be taken to achieve that goal. This step should
directly lead to the desired outcome.
3. Work Backwards: From the final step, identify the previous step that
would lead to the final step. Continue this process, working backwards
step by step, until you reach the starting point or initial problem.
4. Verify Steps: As you work backwards, ensure that each step logically
leads to the next and ultimately to the desired outcome. If a step does not
seem to connect, reassess and adjust as needed.
5. Implement the Plan: Once you have identified the steps working
backwards from the solution, you can create an action plan to implement
them in the correct sequence.
What are the problem-solving strategies?
Problem-solving strategies are approaches or methods used to find solutions to
problems. They help individuals or teams navigate through challenges and come
up with effective solutions. Some common problem-solving strategies include:
1. Trial and Error: This strategy involves trying different solutions until
the problem is solved. While simple, it can be time-consuming and may
not always be practical for complex problems.
2. Algorithmic Thinking: This strategy involves following a step-by-step
procedure or algorithm to solve a problem. Algorithms are particularly
useful for problems that can be broken down into smaller, more
manageable steps.
3. Heuristic Methods: Heuristics are rules of thumb or general strategies
that may not guarantee a solution but can help narrow down the search
space. Examples include working backwards from the solution or using
analogies from similar problems.
4. Divide and Conquer: This strategy involves breaking a problem down
into smaller, more manageable parts, solving each part independently,
and then combining the solutions to solve the overall problem.
5. Root Cause Analysis: This strategy involves identifying the underlying
cause of a problem rather than just addressing its symptoms. By
addressing the root cause, you can prevent the problem from recurring.
6. Decision Making: Problem-solving often involves making decisions.
Decision-making strategies such as weighing pros and cons, using
decision trees, or conducting cost-benefit analyses can help in choosing
the best course of action.
7. Creativity Techniques: Sometimes, thinking outside the box is
necessary to find innovative solutions. Creativity techniques such as
brainstorming, mind mapping, or lateral thinking can help generate new
ideas.
8. Collaboration: Problem-solving is often more effective when done
collaboratively. Working with others can provide different perspectives
and expertise that can lead to better solutions.
In top-down design, you begin by identifying the main function or goal of the
system. Then, you break down this main function into smaller sub-functions or
modules. Each sub-function is further broken down into smaller sub-functions,
and so on, until you reach a level where the functions are simple enough to be
easily implemented.
1. Identify the Main Function: Define the overall purpose or goal of the
system.
2. Break Down the Main Function: Divide the main function into smaller,
more manageable sub-functions or modules.
3. Repeat the Process: For each sub-function, repeat the process of
breaking it down into smaller sub-functions until you reach a level where
the functions are simple enough to be implemented.
4. Implement the Functions: Implement each function, starting from the
lowest-level functions and working your way up to the top-level function.
5. Test and Debug: Test each function individually to ensure that it works
correctly, and then test the system as a whole to ensure that all functions
work together as expected.
Using the example set {2, 4, 6, 8, 10}, the steps would look like this:
This process can be applied to any set of numbers to find their total sum.
Factorial computation
Calculating the factorial of a number involves multiplying the number by all
the positive integers less than it. For example, the factorial of 5 (written as 5!) is
5 x 4 x 3 x 2 x 1 = 120. Here's a step-by-step explanation of how to compute the
factorial of a number in Python:
1. Input a Number: Start by inputting the number for which you want to
calculate the factorial. Let's say you want to calculate the factorial of 5.
2. Initialize a Variable: Initialize a variable, let's call it factorial, to 1. This
variable will be used to store the running product.
3. Loop Through Numbers: Create a loop that iterates from 1 to the input
number (inclusive). For each iteration, multiply the factorial variable by
the current number in the loop.
4. Calculate the Factorial: After the loop completes, the factorial variable
will contain the factorial of the input number.
5. Print the Result: Print the value of the factorial variable to display the
factorial of the input number.
# Step 1: Input a number
number = 5
Iteration 2:
num = 12, digit = 2, reversed_num = 3 * 10 + 2 = 32
Iteration 3:
num = 1, digit = 1, reversed_num = 32 * 10 + 1 = 321
Output: 321
Base conversion.
Base conversion involves converting a number from one base to another. The
base of a number system determines the number of digits used to represent
numbers and the value of each digit based on its position. For example, the
decimal (base-10) number system uses digits 0-9, while the binary (base-2)
number system uses digits 0 and 1.
Output: result
Unit -3
Finding the square root of number
Finding the square root of a number involves finding a value that, when
multiplied by itself, gives the original number. For example, the square root of
25 is 5 because 5 * 5 = 25. Here's a step-by-step explanation of how to find the
square root of a number using the Babylonian method, also known as the
Heron's method:
1. Input the Number: Start by inputting the number for which you want to
find the square root. Let's say the number is 25.
2. Initialize Variables: Initialize two variables, guess and prev_guess, to
any value. These variables will be used to approximate the square root.
3. Iterative Calculation: Use the following iterative formula to
approximate the square root:
guess = (guess + number / guess) / 2
1. Repeat this calculation until the difference between guess and
prev_guess is within a desired tolerance level, or until a maximum
number of iterations is reached.
2. Print the Result: Print the value of guess, which will be an
approximation of the square root of the input number.
Output: guess
Using the example input 25, the pseudo-code would find the square root as
follows:
Iteration 1:
prev_guess = 0
guess = (0 + 25 / 0) / 2 = 12.5
Iteration 2:
prev_guess = 12.5
guess = (12.5 + 25 / 12.5) / 2 = 7.25
Iteration 3:
prev_guess = 7.25
guess = (7.25 + 25 / 7.25) / 2 = 5.349
Iteration 4:
prev_guess = 5.349
guess = (5.349 + 25 / 5.349) / 2 = 5.015
Iteration 5:
prev_guess = 5.015
guess = (5.015 + 25 / 5.015) / 2 = 5.000023
1. Input the Integer: Start by inputting the integer for which you want to
find the smallest division. Let's say the integer is 12.
2. Initialize Variables: Initialize a variable divisor to 1. This variable will
be used to find the smallest division.
3. Check Division: Use a loop to iterate through all numbers from 1 to the
input integer. For each number, check if the input integer is divisible by
that number without leaving a remainder.
4. Update Smallest Division: If the input integer is divisible by the current
number (i), update the value of divisor to i.
5. Print the Result: Print the value of divisor, which will be the smallest
division of the input integer.
1. Input the Integers: Start by inputting the two integers for which you
want to find the GCD. Let's say the integers are 12 and 18.
2. Initialize Variables: Initialize two variables, a and b, to the two input
integers.
3. Calculate Remainder: Use the Euclidean algorithm to repeatedly
calculate the remainder of a divided by b until the remainder is 0. This is
done by repeatedly setting a to b and b to the remainder of a divided by
b.
4. GCD is the Last Non-Zero Remainder: The GCD of the two input
integers is the last non-zero remainder calculated in the Euclidean
algorithm.
5. Print the Result: Print the value of b, which is the GCD of the two input
integers.
Here's the pseudo-code implementing these steps:
Input: a (integer), b (integer)
Set temp to 0
while b is not 0:
Set temp to b
Set b to a mod b
Set a to temp
Iteration 2:
a = 18, b = 12, temp = 12
b = 18 % 12 = 6, a = 12
Iteration 3:
a = 12, b = 6, temp = 6
b = 12 % 6 = 0, a = 6
1. Input the Range: Start by inputting the range of numbers within which
you want to find prime numbers. Let's say you want to find prime
numbers up to 50.
2. Initialize Variables: Initialize a list (primes) to store the prime numbers
found. Also, initialize a variable (is_prime) to True.
3. Iterate Through the Range: For each number num in the range from 2
to the maximum number in the range:
a. Set is_prime to True.
b. For each number i in the range from 2 to the square root of num
(rounded up to the nearest integer), check if num is divisible by i. If it is,
set is_prime to False and break out of the loop.
c. If is_prime is still True after checking all possible divisors, add num
to the primes list.
4. Print the Result: Print the list of prime numbers (primes) found within
the specified range.
Output: primes
Function Fibonacci(n):
If n is 0:
Return 0
Else If n is 1:
Return 1
Else:
Return Fibonacci(n-1) + Fibonacci(n-2)
Input: n (integer)
Output: Fibonacci(n)
Unit -4
Array order reversal
Reversing the order of an array involves changing the sequence of its elements
from the last element to the first. For example, if you have an array [1, 2, 3, 4,
5], after reversing, it becomes [5, 4, 3, 2, 1]. Here's a step-by-step explanation of
how to reverse the order of an array:
1. Input the Array: Start by inputting the array you want to reverse. Let's
say the array is [1, 2, 3, 4, 5].
2. Initialize Variables: Initialize two variables, start and end, to the first
and last indices of the array, respectively.
3. Swap Elements: Use a loop to swap the element at index start with the
element at index end, and then increment start and decrement end.
Repeat this process until start is greater than or equal to end.
4. Print the Reversed Array: Print the reversed array.
Here's the pseudo-code implementing these steps:
Function reverse_array(arr):
start = 0
end = length(arr) - 1
Return arr
1. Input the Set: Start by inputting the set (or array) for which you want to
find the maximum number. Let's say the set is {3, 7, 1, 9, 5}.
2. Initialize Variables: Initialize a variable max_num to negative infinity.
This variable will store the maximum number found so far.
3. Iterate Through the Set: For each element num in the set, compare it
with max_num. If num is greater than max_num, update max_num to
num.
4. Print the Result: Print the value of max_num, which will be the
maximum number in the set.
Function find_max(set):
max_num = -infinity
for num in set:
if num > max_num:
max_num = num
Return max_num
1. Input the Ordered Array: Start by inputting the ordered array from
which you want to remove duplicates. Let's say the array is [1, 2, 2, 3, 3,
3, 4, 5, 5, 6].
2. Initialize Variables: Initialize an empty array unique_array to store the
unique elements.
3. Iterate Through the Array: For each element num in the ordered array,
check if num is different from the last element added to unique_array.
If it is, append num to unique_array.
4. Print the Result: Print the unique_array, which will contain only the
unique elements from the original ordered array.
Function remove_duplicates(ordered_array):
unique_array = []
last_added = None
Return unique_array
Partitioning an array
Partitioning an array involves rearranging the elements in such a way that all
elements less than a certain value (the pivot) are placed before the pivot, and all
elements greater than or equal to the pivot are placed after it. This process is
commonly used in sorting algorithms like Quicksort. Here's a step-by-step
explanation of how to partition an array:
1. Input the Array and Pivot: Start by inputting the array you want to
partition and choosing a pivot value. Let's say the array is [4, 7, 2, 9, 5]
and the pivot is 5.
2. Initialize Variables: Initialize two pointers, low and high, to the first
and last indices of the array, respectively.
3. Partitioning Step: Repeat the following steps until low is greater than or
equal to high:
• Increment low until you find an element greater than or equal to
the pivot.
• Decrement high until you find an element less than the pivot.
• Swap the elements at indices low and high.
4. Swap Pivot with Element at High: Swap the pivot with the element at
index high. This places the pivot in its correct sorted position in the
array.
5. Print the Result: Print the partitioned array.
Return high
Unit -5
Text line length adjustment
Text line length adjustment, also known as text justification or text wrapping,
involves adjusting the length of lines of text so that they fit within a specified
width. This is commonly done in typesetting and word processing to improve
the readability and appearance of text. Here's a step-by-step explanation of how
text line length adjustment can be done:
1. Input the Text and Maximum Line Width: Start by inputting the text
you want to adjust and the maximum width of each line. For example,
let's say the text is "Lorem ipsum dolor sit amet, consectetur adipiscing
elit." and the maximum line width is 20 characters.
2. Split the Text into Words: Split the input text into individual words. For
the example text, this would result in the array ["Lorem", "ipsum",
"dolor", "sit", "amet,", "consectetur", "adipiscing", "elit."].
3. Initialize Variables: Initialize a variable current_line to an empty
string, which will be used to store the current line being built. Also,
initialize a list justified_lines to store the final adjusted lines of text.
4. Iterate Through the Words: For each word in the array of words:
• If adding the word to the current line would exceed the maximum
line width, add the current line to the justified_lines list and start a
new line with the current word.
• Otherwise, add the word and a space to the current line.
5. Adjust the Last Line: After iterating through all the words, add the
remaining contents of the current line to the justified_lines list.
6. Print the Result: Print each line in the justified_lines list, which will be
the adjusted lines of text.
justified_lines.append(current_line.strip())
Return justified_lines
Input: text (string), max_width (integer)
Output: justified_lines (array of strings)
1. Input the Text and Keywords: Start by inputting the text in which you
want to search for keywords, as well as the keywords you want to search
for. For example, let's say the text is "Lorem ipsum dolor sit amet,
consectetur adipiscing elit." and the keywords are "ipsum" and
"adipiscing".
2. Split the Text into Words: Split the input text into individual words. For
the example text, this would result in the array ["Lorem", "ipsum",
"dolor", "sit", "amet,", "consectetur", "adipiscing", "elit."].
3. Initialize Variables: Initialize a list found_keywords to store the
keywords found in the text.
4. Search for Keywords: For each word in the array of words:
• Check if the word matches any of the keywords. If it does, add the
keyword to the found_keywords list.
5. Print the Result: Print the found_keywords list, which will contain all
the keywords found in the text.
Return found_keywords
Input: text (string), keywords (array of strings)
Output: found_keywords (array of strings)
1. Input the Text Line: Start by inputting the text line you want to edit. For
example, let's say the text line is "Hello, world!".
2. Choose an Editing Operation: Decide on the editing operation you want
to perform. For example, you may want to insert a character, delete a
character, or replace a character.
3. Perform the Editing Operation: Perform the chosen editing operation
on the text line. For example:
• Insertion: Insert a character at a specific position in the text line.
• Deletion: Delete a character at a specific position in the text line.
• Replacement: Replace a character at a specific position in the text
line with another character.
4. Print the Result: Print the edited text line.
Here's the pseudo-code implementing these steps for each type of editing
operation:
Insertion:
1. Input the Text and Keywords: Start by inputting the text in which you
want to search for keywords, as well as the keywords you want to search
for. For example, let's say the text is "Lorem ipsum dolor sit amet,
consectetur adipiscing elit." and the keywords are "ipsum" and
"adipiscing".
2. Split the Text into Words: Split the input text into individual words. For
the example text, this would result in the array ["Lorem", "ipsum",
"dolor", "sit", "amet,", "consectetur", "adipiscing", "elit."].
3. Initialize Variables: Initialize a list found_keywords to store the
keywords found in the text.
4. Search for Keywords: For each word in the array of words:
• Check if the word matches any of the keywords. If it does, add the
keyword to the found_keywords list.
5. Print the Result: Print the found_keywords list, which will contain all
the keywords found in the text.
Return found_keywords