Underrated step for logic building in programming.

Image
Logic building is a crucial and complex skill in programming. In essence, it is ability to come-up with solution of coding problem and write precise instructions ( or code) that a computer can execute autonomously. This skill requires aligning your thought process with computer and its capabilities. And running through code some-what abstractly to know and predict the behavior of code before it is executed. To be able to do this, one essential step that many beginner programmers overlook is performing dry runs. Understanding Dry Runs The concept of a dry run in programming is straightforward: can you mentally execute your code and predict its output without actually running it on a computer? While this seems simple, it is a challenging task. Typically, we are taught to write code, run it, and observe the output. This cycle is essential because code needs to run to be validated. However, if you rely solely on running your code to understand its behavior, you may struggle with building

Python Code for beginner's #10

def fibonacci(n):

    if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

# Get user input for the number of terms in the sequence
num_terms = int(input("Enter the number of terms in the Fibonacci sequence: "))

if num_terms <= 0:
print("Please enter a positive integer.")
else:
print("Fibonacci sequence:")
for i in range(num_terms):
print(fibonacci(i), end=" ")
---------------------------------------------------------------------------------------------
def sum_of_digits(number):
# Base case: if the number is a single digit, return the number
if number < 10:
return number

# Recursive case: sum the last digit and call the function with the remaining digits
return number % 10 + sum_of_digits(number // 10)


# Example usage:
number = 12345
result = sum_of_digits(number)
print(f"The sum of digits in {number} is {result}")
---------------------------------------------------------------------------------------------
def linear_search(arr, target):
"""
Perform linear search on the given list to find the target value.

Parameters:
- arr (list): The list to be searched.
- target: The value to be searched for.

Returns:
- int: Index of the target value if found, otherwise -1.
"""
for i in range(len(arr)):
if arr[i] == target:
return i # Target found, return its index
return -1 # Target not found

# Example usage:
my_list = [2, 5, 8, 12, 16, 23, 38, 45, 50]
target_value = int(input('enter value: '))

result = linear_search(my_list, target_value)

if result != -1:
print(f'Target {target_value} found at index {result}.')
else:
print(f'Target {target_value} not found in the list.')
---------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners # 1

Build eisenhower matrix with React, firebase and neumorphism ui . (Part one)

Creating a Dynamic Search Bar in React: A Step-by-Step Tutorial