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

No comments:

Post a Comment