.

Labels

.

Test your mental math skill

How good is your mental maths? Well whether it is good or bad , this script will test your ability and train you in mental calcualtion.

-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
In this script randomly selected numbers and mathematical operators create a problem which is piped through bc and the solution of the problem is compared with input answer from the keyboard.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

clear
questions=10       #the number of questions to be asked,change it to your desire.
X[1]="+"
X[2]="-"
X[3]="*"
X[4]="/"
timeout=0
wrong=0
mark=0
for (( i=1; i<=$questions; i++ ));do 
select=$[ ( $RANDOM % 2) + 1 ]

#selecting random numbers for calculation.Limit is set to numbers 25 and less than 25.Change 25 to any number of your choice if needed.
a=$[ ( $RANDOM % 25 ) + 1 ]
b=$[ ( $RANDOM % 25 ) + 1 ]
c=$[ ( $RANDOM % 25 ) + 1 ]
#random selection of mathematical operations.
w==$[ ( $RANDOM % 4 ) + 1 ]
x==$[ ( $RANDOM % 4 ) + 1 ]

     if [[ $select == 1 ]];then
           math="$a${X[$w]}$b"
           echo "$math"
           read -t 10 solution
           if [[ $solution == $null ]];then
               timeout=$((timeout + 1))
               echo -e "\e[1;31mtime out!!\e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
               echo -e "\n"
           elif [[ $solution = $(echo "scale=1; $math" | bc | sed 's/\.0$//') ]];then
               mark=$((mark+1))
               echo -e "\e[1;32m  \e[0m\n------------"
           else
               echo -e "\e[1;34m  \e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
           fi                  
     else 
           math="$a${X[$w]}$b${X[$x]}$c"
           echo "$math"
           read -t 10 solution
           if [[ $solution == $null ]];then
               timeout=$((timeout + 1))
               echo -e "\e[1;31mtime out!!\e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
               echo -e "\n"
           elif [[ $solution = $(echo "scale=1; $math" | bc | sed 's/\.0$//') ]];then
               mark=$((mark+1))
               echo -e "\e[1;32m  \e[0m\n------------"
           else
               echo -e "\e[1;34m  \e[0m    Ans: $(echo "scale=1; $math" | bc )\n------------"
           fi               
      fi
done

#calculation of your score
wrong=$(($questions-($mark + $timeout)))

echo -e "\e[1;31mscore= \e[1;34m$mark/$questions\e[0m    wrong=$wrong    timeout=$timeout"
            


-------------------------------------------------------------------------------------------

Execute the script and it will ask 10 mathematical problems. You will get 10 seconds to answer each problem and after completing the tenth question, it will display your score.




Now, that score is not bad! 

Please note:
when doing calculation you must do division first,then multiplication , then addition and finally subtraction(  division  multiplication  addition  subtraction ) ; else your answer can get wrong.

Enjoy Linux!!

Dont know what to do with these codes ?? click here

Find factors of a number (factorization)

Linux has got a beautiful factor finding tool called factor. This script does the same job of factor tool.

In mathematicsfactorization (also factorisation in British English) or factoring is the decomposition of an object (for example, a number, a polynomial, or a mtrix) into a product of other objects, or factors, which when multiplied together give the original. For example, the number 15 factors into primes as 3 × 5, and the polynomial x2 − 4 factors as (x − 2)(x+ 2). In all cases, a product of simpler objects is obtained.
                                                                   -Wikipediahttp://en.wikipedia.org/wiki/Factorization )


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
The number provided is divided by natural numbers starting from 2. Each natural number recursively divides the number while reminder for each division operation is zero.If reminder is not zero , the script starts dividing with the next natural number.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to find factors of a number
num=$1
for (( i=2; i<=$1; i++ ));do
    while [ $((num%$i)) == 0 ];do
        echo $i
        num=$((num/$i))
    done
done


-------------------------------------------------------------------------------------------


Script is called myfactor in my system. Figure shows finding factors with factor tool and then with myfactor script of 15, 40 and 13



.. are the results same ?? :)

Enjoy Linux !!

Don't know what to do with these codes ??  click here

Create desktop wallpaper with Imagemagick


Imagemagick is a really awesome tool which few people really utilise mainly because it is a command line application. Creating Random Noise Images is one of the many cool features of Imagemagick.  Imagemagick recognises around 680 colors and we can make beautiful background images with individual or dual combinations of these colors . Check http://www.imagemagick.org/script/color.php to see list of colors or if Imagemagick is installed through the command line convert -list color in terminal.

If Imagemagick is not installed install it via terminal sudo apt-get install imagemagick ( Ubuntu ) or through  Software Centre ( Ubuntu ) or Add/Remove Software ( Fedora ).



-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
Colours are randomly selected from the list in color.txt file and wallpapers are made with convert command using these colors.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#to create wallpaper with Imagemagick

#to get the list of colors and save it in a folder
if [ -e /home/$USER/color.txt ];then
   null=$null1
else
   convert -list color | cut -d "(" -f 1 | sed 's/rgb//g'| sed -n '6,$p' > /home/$USER/color.txt
fi

#for selecting colors randomly 

select=$[ ( $RANDOM % 3 ) + 1 ]
num1=$[ ( $RANDOM % 680 ) + 1 ]
num2=$[ ( $RANDOM % 680 ) + 1 ]

color1=$(sed -n "${num1}p" /home/$USER/color.txt)

color2=$(sed -n "${num2}p" /home/$USER/color.txt)


if [ $select == 1 ];then
   convert -size 2000x1250  plasma:$color1 random.jpg
elif [ $select == 2 ];then
   convert -size 2000x1250  plasma:"$color1-$color2" random.jpg
else
   convert -size 2000x1250 plasma:fractal random.jpg
fi


------------------------------------------------------------------------------------------- 

Running the script ones will create color.txt file ( where the list of colours  recognised my Imagemagick is stored for random selection) and image file random.jpg ( 2000x1250 pixels ) in your home directory.
Make random.jpg your desktop background. If not satisfied with background run the script again till you find background of your taste. As random.jpg file is replaced by the script the background will also change automatically. 

You can also try looping the script through a for loop . The script is named back in my system .So in terminal execute 

for (( i=0; i<35; i++ ));do
back
sleep 3
done

See 35 times your background change with really cool colour combinations !! Try it , it is really cool !!



If you want your background to be new everytime you loggin  place the script in Startup Applications  or by executing  the script through  /etc/rc.local 


click on image to see enlarged


 click on image to see enlarged


 click on image to see enlarged


 click on image to see enlarged


 click on image to see enlarged


To read more on backgrounds with Imagemagick please visit http://www.imagemagick.org/Usage/backgrounds/  and  http://www.imagemagick.org/Usage/canvas/#random


Enjoy Linux !!

Dont know what to do with these codes ?? click here

Opening as root by right-clicking

Script for ' opening as root ' by right-clicking.Place this script inside .gnome2/nautilus-scripts/

(Go to home folder and press "Ctrl h" to see .gnome2 folder; inside this folder nautilus-scripts folder is present; place this script there; script can have any name but naming it 'open as root' make some sense; make script executable)

Make sure that gnome-open is installed else install it by sudo apt-get install libgnome2-0 ( Ubuntu ) in terminal or through Software Centre ( Ubuntu ) or Add/Remove Software ( Fedora ).



HOW THIS SCRIPT WORKS:
Nothing much to explain.
'gksu' is the GTK+ frontend for su and sudo
'$*' represent the complete path of the file you have selected i.e script.txt file in Desktop will have a complete path name  /home/$USER/Desktop/script.txt
'gnome-open' will open files and URLs using the default program.

#!/bin/bash

#to open as root

gksu gnome-open "$*"

[Download script]





This script will save a lot of your time !!

Enjoy Linux !!




Fibonacci numbers

Script to display Fibonacci series.

In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:
0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\; (sequence A000045 in OEIS).
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F_n = F_{n-1} + F_{n-2},\!\,
with seed values
F_0 = 0,\; F_1 = 1.

                                           -Wikipedia http://en.wikipedia.org/wiki/Fibonacci_number )



note: If you want only 5 rows of Fibonacci series , this for loop will do
--------------------------------------------------------
for (( i=0; i<5; i++ ));do
echo $((11**$i))
done
--------------------------------------------------------


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
Proper commends are provided in the script itself to make it understandable.
'tput' is command line tool to change the postion of the prompt.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

#for displaying fibonacci numbers

num=1
zero[1]=1
zero[2]=1

read -p "Please enter number of rows:"
#for exiting script if 0 or no number is entered
if [[ $REPLY == 0 || $REPLY == $null ]];then
exit 0
fi

tput cuf $(($(tput cols)/2))
echo "1"
for (( i=1; i<$REPLY; i++ ));do
   
    for (( k=1; k<=$i; k++ ));do
#for getting each number for addition and assigning to array 'a'         
         a[$k]=$(echo "$num%(10^${zero[$k]})" | bc)
         num=$(echo "$num/(10^${zero[$k]})" | bc)
    done

#adding adjascent numbers assigned to array 'a'   
    for (( j=0; j<=$i; j++ ));do
        l=$((j+1))
        sum=$((a[j]+a[l]))
        wordcont=$(echo $sum | wc -c)
        zero[l]=$((wordcont-1))
        totalsum="$totalsum $sum"
    done
    num=$(echo $totalsum | sed 's/ //g')

#finding position of terminal prompt and printing value
    length=$(echo $totalsum | wc -c)
    postion=$((($(tput cols)-$length)/2))
    tput cuf $postion 2>/dev/null
    echo "$totalsum"
    totalsum=$null
done


-------------------------------------------------------------------------------------------


In my system the script is named fibonacci. If you want ,say 15 rows , of fibanocci series ; type fibonacci and press Enter . When script ask you for number of rows to be displayed , enter 15 and press Enter  and ...




.. Howzzat ?? 



Enjoy Linux !!

Dont know what to do with these codes ?? click here

Solve Crossword Puzzle


Script to solve Crossword Puzzle


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
There are two files named british-english and american-english which contains all words in the dictionary without their meaning i.e just the word only , in /usr/share/dict.
The puzzle  provided is compared with the words in any of the above files. 
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#for solving crossword puzzle
# put '?' in place of missing letter
# if you want british spelling replace 'american-english' with 'british-english'

read -p "please enter the puzzle:"

word=$(echo $REPLY | sed 's/?/./g')
cat /usr/share/dict/american-english | grep --color=always "^$word$"


-------------------------------------------------------------------------------------------

Suppose you want solve  puzzle "_ros__ord" , you should enter as ?ros??ord ( put question mark instead of blank spaces ). The script in this case is named crossword .OK lets solve this puzzle together ..





Ohh!! It is crossword !!

Enjoy Linux !!

Dont know what to do with these codes?? click here

Check for Palindrome

Script to check whether given string is a Palindrome.

palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments topunctuation and word dividers.
                                             -Wikipediahttp://en.wikipedia.org/wiki/Palindrome )


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
The script divides the string to be checked into two parts of equal number of characters i.e it will divide anna into an and na.
'rev' is command line tool which will reverse the string provided.The second part of the string is reversed and then compared with the first part.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

#to check whether the given string is palindrome.String can contain white spaces

#finding the lenght of the given string
length=$(echo $* | wc -c)


#cutting string into two with one in reversed order
if [[ $((length%2)) == 0 ]];then
   length=$((length/2))   
   l=$(echo $* | cut -c 1-$length)
   r=$(echo $* | rev | cut -c 1-$length)
else
   length=$(((length-1)/2))
   l=$(echo $* | cut -c 1-$length)
   r=$(echo $* | rev | cut -c 1-$length)
fi

#checking both cut parts of the string
if [[ "$l" == "$r" ]];then
   echo "Yes.It is a palindrome"
else
   echo "Sorry.It is not a palindrome"
fi


-------------------------------------------------------------------------------------------


In my computer this script is named palindrome . If I need to check palindrome of a string say malayalam, I will type palindrome malayalam in terminal and press Enter ..





..DONE SIR !!!

Enjoy Linux!!

Dont know what to do with these codes ?? click here