Sunday, February 25, 2024

BCA PART A 1. Check if a number belongs to the Fibonacci Sequence

 A Fibonacci number is a member of the Fibonacci sequence, a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence begins:

0,1,1,2,3,5,8,13,21,34,…

Starting with 0 and 1, each subsequent Fibonacci number is obtained by adding the two numbers that precede it. Mathematically, it can be defined by the recurrence relation:

�(�)=�(�−1)+�(�−2)

with initial conditions �(0)=0 and �(1)=1.

The Fibonacci sequence has many interesting mathematical properties and applications, and it often appears in various areas of mathematics and nature, including the arrangement of leaves on a stem, the branching of trees, and the arrangement of seeds in a sunflower.

Wednesday, December 27, 2023

1 : Write a Python program to perform linear search

 Linear search, also known as sequential search, is a simple search algorithm. It sequentially checks each element in a list until a match is found or the entire list has been searched.

Algorithm:

  • Start from the beginning of the list and examine each element one by one.
  • Compare each element with the target value.
  • If a match is found, return the index of the element.
  • If the entire list is searched and no match is found, return -1.

Time Complexity: ^ The time complexity of linear search is O(n), where n is the number of elements in the list.

  • In the worst-case scenario, the algorithm may need to traverse the entire list.

Advantages:

  • Simple and easy to understand.
  • Works well for small lists or unsorted lists.

Disadvantages:

  • Inefficient for large lists or datasets.
  • Not suitable for situations where a quick search is crucial.

Implementation in Python:

  • Typically implemented using a loop (e.g., for or while) to iterate through the list.
  • A function can be created to encapsulate the linear search logic, providing modularity and reusability.

Use Cases:

  • Useful when the list is small, and efficiency is not a primary concern.
  • Applicable when the list is unordered or partially ordered.

*Comparison with Other Search Algorithms:

  • Linear search is less efficient than binary search for large sorted lists.
  • Binary search has a time complexity of O(log n) but requires a sorted list.

Conclusion:

  • Linear search is a straightforward algorithm suitable for basic search requirements.
  • Its simplicity comes at the cost of efficiency, making it less suitable for large datasets or time-sensitive applications.

Program

# Accept a list from the user
my_list = [int(x) for x in input("Enter a list of numbers separated by spaces: ").split()]

# Accept the target value from the user
target_value = int(input("Enter the target value to search for: "))

# Perform linear search
found = False
for i in range(len(my_list)):
    if my_list[i] == target_value:
        found = True
        index = i
        break

# Display the result
if found:
    print(f"Target {target_value} found at index {index}.")
else:
    print(f"Target {target_value} not found in the list.")


Explanation:

Accepting User Input:

  • The program starts by prompting the user to enter a list of numbers separated by spaces using the input function.
  • The entered string is then split into individual numbers, converted to integers, and stored in the my_list variable.

Accepting Target Value:

  • The program prompts the user to enter the target value to search for using the input function.
  • The entered value is converted to an integer and stored in the target_value variable.

Linear Search:

  • The program uses a for loop to iterate through each element in the list (my_list).
  • Inside the loop, it checks if the current element is equal to the target value (target_value).
  • If a match is found, it sets the found flag to True, records the index (i), and breaks out of the loop.

Displaying Result:

Finally, the program checks the found flag.

  • If found is True, it prints a message indicating that the target value was found and displays the index.
  • If found is False, it prints a message indicating that the target value was not found in the list.
my_list = [int(x) for x in input("Enter a list of numbers separated by spaces: ").split()]

  • input("Enter a list of numbers separated by spaces: "): This part of the code prompts the user to enter a string of numbers separated by spaces. The input() function takes user input as a string.
  • .split(): The split() method is used to split the entered string into a list of substrings. By default, it splits the string based on spaces.
  • int(x) for x in ...: This is a list comprehension. It iterates over each substring (x) obtained after the split and converts it to an integer using int(x).
  • my_list = [...]: The result of the list comprehension is assigned to the variable my_list. This variable now holds a list of integers created from the user's input.

Example

Suppose the user enters: 10 20 30 40

  • The input() function captures the string "10 20 30 40".
  • The .split() method splits this string into a list of substrings: ["10", "20", "30", "40"].
  • The list comprehension iterates over each substring, converting it to an integer: [10, 20, 30, 40].
  • The final list [10, 20, 30, 40] is assigned to the variable my_list.

Saturday, July 26, 2014

NS tools installation on ubuntu14.04

Download the ns-allinone-2.35 from here
Unzip the package to your home directory.
           ex. /home/parimal/

First and foremost we need to update the library

Initial command:    sudo apt-get install build-essential
   
               build-essential is a metapackage (a package that installs many other packages, like g++ and gcc: the GNU C & C++ compilers). It's required if you want to compile anything from source, and if you want to work with almost any programming language.

STEP 1:        ~$ sudo apt-get install gcc-4.4 g++-4.4
STEP 2:        ~$ sudo apt-get install libxt-dev libx11-dev libxmu-dev xorg-dev xgraph
STEP 3:        ~$ cd /home/parimal/ns-allinone-2.35
STEP 4:        ~$ gedit /home/parimal/ns-allinone-2.35/otcl-1.14/configure

          Linux*)
            SHLIB_CFLAGS="-fPIC"
            SHLIB_LD_LIBS=""
            SHLIB_SUFFIX=".so"
            if test "$have_dl" = yes; then
                  #  SHLIB_LD="${CC} -shared"        // comment this line (line number 6576 )
                    SHLIB_LD=”gcc -shared”         // add this line
                    DL_LIBS="-ldl"
                    DL_LD_FLAGS="-rdynamic"

STEP 5:        ~$ gedit /home/parimal/ns-allinone-2.35/tcl8.4.18/unix/Makefile.in
         #CC = @CC@                 // comment this line (line number 232)
            CC = @CC@-4.4                // add this line

STEP 6:        ~$ gedit /home/parimal/ns-allinone-2.35/ns-2.35/Makefile.in
            #CC = @CC@                // comment these two lines (line number 36 & 37 )
            #CPP = @CXX@
            CC = @CC@-4.4                // add these lines
            CPP = @CXX@-4.4

STEP 7:        ~$ gedit /home/parimal/ns-allinone-2.35/ns-2.35/Makefile
            #CC = gcc                // comment these two lines (line number 36 & 37 )
            #CPP = g++
            CC = gcc-4.4                // add these lines
            CPP = g++-4.4   
 
STEP 8:        cd ~/ns-allinone-2.35
                     ./install

STEP 9:        cd ns-2.35
                     sudo make install
                     cd ../nam1.15
                     sudo make install
       
STEP 10:      cd ~/ns-allinone-2.35
                     ./validate