Posts

Showing posts from December, 2023

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...

9618_s22_qp_42

Image
  def BinarySearch (SearchArray, Lower, Upper, SearchValue): if Upper >= 0 : Mid = int ((Lower + (Upper - 1 )) / 2 ) if SearchArray[Mid] == SearchValue: return Mid elif SearchArray[Mid] > SearchValue: return BinarySearch(SearchArray, Lower, Mid - 1 , SearchValue) else : return BinarySearch(SearchArray, Mid + 1 , Upper, SearchValue) return - 1

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 { ...

Physics' terms definitions.

-  Electric field strength  at a point is defined as: The electrostatic force per unit positive charge acting on a stationary point charge at that point -  Coulomb’s Law states that: The electrostatic force between two point charges is proportional to the product of the charges and inversely proportional to the square of their separation -  The electric potential at a point is defined as: The work done per unit positive charge in bringing a small test charge from infinity to a defined point -  An electric field can be defined in terms of the variation of  electric potential  at different points in the field**:** The electric field at a particular point is equal to the negative gradient of a potential-distance graph at that point -  The potential gradient in an electric field is defined as: The rate of change of electric potential with respect to displacement in the direction of the field -  The electric potential energy Ep at point i...

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

18.1.5 Electric Force Between Two Point Charges

  Coulomb's Law All charged particles produce an electric field around it This field exerts a force on any other charged particle within range The electrostatic force between two charges is defined by  Coulomb’s Law Recall that the charge of a uniform spherical conductor can be considered as a  point charge  at its centre Coulomb’s Law states that: The electrostatic force between two point charges is proportional to the product of the charges and inversely proportional to the square of their separation The Coulomb equation is defined as: E = Q1Q2 / 4 Ï€ ε0r^2 Where: FE = electrostatic force between two charges (N) Q1and Q2 = two point charges (C) ε0 =  permittivity of free space r = distance between the centre of the charges (m) The 1/r^2 relation is called the  inverse square law This means that when a charge is twice as far as away from another, the electrostatic force between them reduces by (½)^2 = ¼ ...

18.1.4 Motion of Charged Particles

  Motion of Charged Particles A charged particle in an electric field will experience a force on it that will cause it to move If a charged particle remains still in a uniform electric field, it will move parallel to the electric field lines (along or against the field lines depending on its charge) If a charged particle is in  motion  through a uniform electric field (e.g. between two charged parallel plates), it will experience a constant electric force and travel in a  parabolic trajectory The direction of the parabola will depend on the charge of the particle A  positive  charge will be deflected towards the  negative  plate A  negative  charge will be deflected towards the  positive  plate The force on the particle is the same at all points and is always in the same direction Note:  an uncharged particle, such as a neutron experiences no force in an electric field and will therefore travel straight through...

18.1.3 Electric Field Strength

Electric Field Strength The electric field strength of a uniform field between two charged parallel plates is defined as: E = ΔV /Δd Where: E =  electric field strength  (V m-1) ΔV = potential difference between the plates (V) Δd = separation between the plates (m) Note:  the electric field strength is now also defined by the units  V m1 The equation shows: The greater the voltage between the plates, the stronger the field The greater the separation between the plates, the weaker the field Remember this equation cannot be used to find the electric field strength around a  point charge  (since this would be a radial field) The direction of the electric field is from the plate connected to the  positive  terminal of the cell to the plate connected to the  negative  terminal The E field strength between two charged parallel plates is the ratio of the potential difference and separation of the plates Note:  if one of th...

18.1.2 Electric Field Lines

Representing Electric Fields The direction of electric fields is represented by electric field lines Electric field lines are directed from positive to negative Therefore, the field lines must be pointed  away  from the  positive  charge and  towards  the  negative  charge A radial field spreads uniformly to or from the charge in all directions e.g. the field around a  point charge  or sphere Around a  point charge , the electric field lines are directly radially inwards or outwards: If the charge is  positive  (+), the field lines are radially  outwards If the charge is  negative  (-), the field lines are radially  inwards This shares many similarities to radial gravitational field lines around a point mass Since gravity is only an attractive force, the field lines will look similar to the negative point charge, whilst electric field lines can be in either direction A unif...

18.1.1 Electric Fields & Forces on Charges

  Electric Field Definition An electric field is a region of space in which an electric charge “feels” a force Electric field strength  at a point is defined as: The electrostatic force per unit positive charge acting on a stationary point charge at that point Electric field strength can be calculated using the equation: E = F/Q Where: E = electric field strength (N C-1) F = electrostatic force on the charge (N) Q = charge (C) It is important to use a positive  test charge  in this definition, as this determines the direction of the electric field The electric field strength is a  vector  quantity, it is always directed: Away  from a positive charge Towards  a negative charge Recall that  opposite charges  (positive and negative) charges  attract  each other Conversely,  like charges  (positive and positive or negative and negative)  repel  each other Forces on Charges The electric ...

9618/41/M/J/21

Image
  arrayData = [ 10 , 5 , 6 , 7 , 1 , 12 , 13 , 15 , 21 , 8 ] def linearSearch (searchValue): for x in range ( 0 , 10 ): if arrayData[x] == searchValue:      return True return False arrayData = [ 10 , 5 , 6 , 7 , 1 , 12 , 13 , 15 , 21 , 8 ] searchValue = int ( input ( "Enter the number to search for" )) returnValue = linearSearch(searchValue) if returnValue == True : print ( "It was found" ) else : print ( "It was not found" )

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

23.1.2 Nuclear Equations

Image
 R epresenting Simple Nuclear Reactions Imagine each nucleus as a unique character identified by a three-letter code: AZX. This code tells us all about its internal composition: A (mass number): The total number of residents in the nucleus – both protons (positively charged) and neutrons (uncharged). Think of it as the bustling population of the nuclear city. Z (atomic number): The number of mischievous protons, the lively bunch that defines the element's identity. It's like the city's official name, reflecting its unique atomic character.

23.1.1 Energy & Mass Equivalence

Energy & Mass Equivalence Einstein showed in his  theory of relativity  that matter can be considered a form of energy and hence, he proposed: Mass can be converted into energy Energy can be converted into mass This is known as  mass-energy equivalence , and can be summarised by the equation: E  =  mc^2 Where: E  = energy (J) m  = mass (kg) c  =  the speed of light  (m s-1) Some examples of mass-energy equivalence are: The  fusion  of hydrogen into helium in the centre of the sun The  fission  of uranium in nuclear power plants Nuclear  weapons High-energy  particle collisions  in particle accelerators  

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 ...

Popular posts from this blog

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

A better way to learn programming.

Thinking in React: A Guide to Building User Interfaces