Posts

Showing posts with the label python

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 # 15 'classes'

  class Student: def __init__ ( self , Roll: int , name: str , faculty: str ): self .__Roll = Roll self .__name = name self .__faculty = faculty def display ( self ): print ( f"Student( { self .__Roll } , ' { self .__name } ', ' { self .__faculty } ')" ) def set_Roll ( self ,Roll): self .__Roll=Roll def set_name ( self ,name): self .__name= name def set_faculty ( self ,faculty): self .__faculty = faculty def get_Roll ( self ): return self .__Roll def get_name ( self ): return self .__name def get_faculty ( self ): return self .__faculty c = Student( 2 , 'nn' , 'lk' ) c.display() # Output: Student(2, 'nn', 'lk') print (c.get_name()) c.set_Roll( 42 ) c.display() # Output: Student(42, 'John', 'lk') --------------------------------------------------------------------------------------------- class Studen...

Python code #14

 1. Insertion sort: def InsertionSort (TheData): for Count in range ( 0 , len (TheData)): DataToInsert = TheData[Count] Inserted = 0 NextValue = Count - 1 while (NextValue >= 0 and Inserted != 1 ): if (DataToInsert < TheData[NextValue]): TheData[NextValue + 1 ] = TheData[NextValue] NextValue = NextValue - 1 TheData[NextValue + 1 ] = DataToInsert else : Inserted = 1 return TheData print (InsertionSort([ 3 , 2 , 1 , 7 , 5 , 9 , 0 ]))

Python code #13

1. 2D array bubble sort of array with 3 columns def bubble_sort_2d (arr): rows = len (arr) for i in range (rows): for j in range (rows - i - 1 ): if arr[j][ 0 ] > arr[j + 1 ][ 0 ]: temp1 = arr[j][ 0 ] arr[j][ 0 ] = arr[j + 1 ][ 0 ] arr[j + 1 ][ 0 ] = temp1 temp2 = arr[j][ 1 ] arr[j][ 1 ] = arr[j + 1 ][ 1 ] arr[j + 1 ][ 1 ] = temp2 temp3 = arr[j][ 2 ] arr[j][ 2 ] = arr[j + 1 ][ 2 ] arr[j + 1 ][ 2 ] = temp3 return arr two_d_array = [ [ 4 , 2 , 3 ], [ 1 , 5 , 6 ], [ 7 , 8 , 9 ], [ 10 , 11 , 12 ], [ 13 , 14 , 15 ], [ 16 , 17 , 18 ], [ 19 , 20 , 21 ], [ 8 , 23 , 24 ], [ 28 , 26 , 27 ], [ 21 , 29 , 30 ] ] sorted_array = bubble_sort_2d(two_d_array) for row in sorted_array: print (row) ------------------------------------------------------------------------------------------...

Python Code # 12

  def binary_search (arr, target): """ Perform binary search on the given sorted list to find the target value. Parameters: - arr (list): The sorted list to be searched. - target: The value to be searched for. Returns: - int: Index of the target value if found, otherwise -1. """ low, high = 0 , len (arr) - 1 while low <= high: mid = (low + high) // 2 # Calculate mid index if arr[mid] == target: return mid # Target found, return its index elif arr[mid] < target: low = mid + 1 # Adjust the search range to the right half else : high = mid - 1 # Adjust the search range to the left half return - 1 # Target not found # Example usage: my_sorted_list = [ 2 , 5 , 8 , 12 , 16 , 23 , 38 , 45 , 50 ] target_value = 16 result = binary_search(my_sorted_list, target_value) if result != - 1 : print ( f'Target { target_value } found at index { ...

Python Code for beginner's #11

  ArrayData = [ 7 , 13 , 118 ,- 3 , 7 , 4 , 13 ,- 3 ,- 3 , 7 , 118 ,- 3 ] def LinearSearch (searchValue): founded_times= 0 for i in range ( len (ArrayData)): if ArrayData[i]==searchValue: founded_times += 1 return founded_times num = int ( input ( 'enter num: ' )) result = LinearSearch(num) print ( f"Searched item found { result } times" ) --------------------------------------------------------------------------------------------- ArrayData = [ 7 , 13 , 118 ,- 3 , 7 , 4 , 13 ,- 3 ,- 3 , 7 , 118 ,- 3 ] def LinearSearch (ArrayData): num = int ( input ( 'enter num: ' )) for i in range ( len (ArrayData)): if ArrayData[i] == num: return i # Target found, return its index return - 1 result = LinearSearch(ArrayData) if result == - 1 : print ( "searched item is not found" ) else : print ( f"Searched item found at { result } th index " )

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 } " ) ...

Python code for beginner's # 9

  def find_length_of_number (number): # Convert the number to a string num_str = str (number) # Initialize a counter for the length length = 0 # Iterate through each character in the string for digit in num_str: length += 1 return length # Example usage user_input = int ( input ( "Enter a number: " )) length_of_number = find_length_of_number(user_input) print ( f"The length of the number { user_input } is: { length_of_number } " ) --------------------------------------------------------------------------------------------- def factorial (n): # Base case: factorial of 0 or 1 is 1 if n == 0 or n == 1 : return 1 else : # Recursive case: n! = n * (n-1)! return n * factorial(n- 1 ) # Example usage: number = int ( input ( 'enter num; ' )) result = factorial(number) print ( f"The factorial of { number } is: { result } " )

Python Code for begineer's #8

  # function with argument def isArmstrong (number): original_number = number num_digits = len ( str (number)) sum_of_digits = 0 while number > 0 : digit = number % 10 sum_of_digits += digit ** num_digits number //= 10 if sum_of_digits == original_number: return True else : return False result = isArmstrong( 45 ) if result: print ( 'it is armstrong' ) else : print ( 'it is not armstrong' ) # function without argument def isArmstrong ( number ): number = int ( input ( 'enter number: ' )) original_number = number num_digits = len ( str (number)) sum_of_digits = 0 while number > 0 : digit = number % 10 sum_of_digits += digit ** num_digits number //= 10 if sum_of_digits == original_number: return True else : return False result = isArmstrong( 45 ) if result: print ( 'it is armstrong' ) else : print ( 'i...

Python Code for beginner's #7

1. Write code for to print multiplication table with given input.  # function with argument def multiplication_table (num, up_to): result_list = [] for i in range (up_to + 1 ): result = num * i result_list.append( f" { num } x { i } = { result } " ) return result_list # Example usage: num = 5 up_to = 10 table_results = multiplication_table(num, up_to) # Print the results for result in table_results: print (result) # function with argument def multiplication_table (): num = int ( input ( 'enter num: ' )) up_to = int ( input ( 'upto which value you want: ' )) result_list = [] for i in range (up_to + 1 ): result = num * i result_list.append( f" { num } x { i } = { result } " ) return result_list table_results = multiplication_table() # Print the results for result in table_results: print (result) # procedure with argument def multiplication_table (num, up_to): for i in range...

Python Code for beginner's #6

# Print greatest number amoung 500 number and their average excluding greatest number import random numbers = [random.randint( 1 , 1000 ) for _ in range ( 500 )] max_number = max (numbers) filtered_numbers = [] index = 0 while index < len (numbers): num = numbers[index] if num != max_number: filtered_numbers.append(num) index += 1 average = sum (filtered_numbers) / len (filtered_numbers) print ( "Maximum Number:" , max_number) print ( "Average (excluding maximum):" , average) --------------------------------------------------------------------------------------------- def factorial (n): result = 1 for i in range ( 1 , n + 1 ): result *= i return result def isStrong (num): original_num = num sum_of_factorials = 0 while num > 0 : digit = num % 10 sum_of_factorials += factorial(digit) num //= 10 if sum_of_factorials == original_num: print ( f" { original_num } is a stron...

Python Code for beginner's #5

1. Sum of first n natural number:   # func without argrument def sumOfFirst_n_natural_number (): num = int ( input ( 'enter num: ' )) sum= 0 for i in range (num + 1 ): sum += i return sum print (sumOfFirst_n_natural_number()) # func with argrument def sumOfFirst_n_natural_number (num): sum= 0 for i in range (num + 1 ): sum += i return sum print (sumOfFirst_n_natural_number( 3 )) # procedure with argument def sumOfFirst_n_natural_number (num): sum= 0 for i in range (num + 1 ): sum += i print (sum) sumOfFirst_n_natural_number( 3 ) # procedure without argument def sumOfFirst_n_natural_number (): num = int ( input ( 'enter num: ' )) sum= 0 for i in range (num + 1 ): sum += i print (sum) sumOfFirst_n_natural_number() 2. Factorial of given number: # procedure without argument def factorial (): num = int ( input ( "Enter a number: " )) factorial = 1 i = 1 while i ...

Python Code for beginner's #3

 1. # Get input from the user num = int ( input ( "Enter a number: " )) # Initialize variables for factorial calculation factorial = 1 i = 1 # Calculate factorial using a while loop while i <= num: factorial *= i # Multiply the current factorial by the current value of i i += 1 # Increment i to move to the next number in the sequence # Display the result print ( "Factorial:" , factorial) **Summary:** 1. The program takes an integer input from the user. 2. It initializes variables `factorial` and `i` to 1. 3. It then uses a `while` loop to calculate the factorial of the entered number. 4. The loop multiplies the current value of `factorial` by the current value of `i` and increments `i` until it reaches the entered number. 5. The final factorial value is displayed. 2. # Function to calculate the volume of a rectangular prism def calculate_volume (length, breadth, height): # Formula for the volume of a rectangular prism: length * breadth * hei...

Popular posts from this blog

Thinking in React: Relatable Scenarios for Building User Interfaces

Understanding Closures in JS (with Examples and Analogies)

Python code # 15 'classes'