Monday, February 11, 2019

Python: BMI calculation using dict of lists

bmi=dict()

while True:
   name=input('Enter a name:')
   bmi[name]=[float(input('Enter weight in kgs:')),float(input('Height in feet:'))*0.3048]    
   bmi[name].insert(0,(bmi[name][0]/(bmi[name][1]**2)))
   opt=input('Do you want to add more names(input y/n):')
   if opt in ('N','n'):
      break   print()

for k,v in bmi.items():
   print(f"The BMI of {k} is {v[0]} and falls under category of:",end=' ')
   if (0 < v[0] <15):
      print("Very serverely underweight")
   elif (15 <= v[0] <16):
      print("Severely underweight")
   elif (16 <= v[0] < 18.5):
      print("Underweight")
   elif (18.5 <= v[0] < 25):
      print("Normal (Healthy Weight)")
   elif (25 <= v[0] < 30):
      print("Overweight")
   elif (30 <= v[0] < 35):
      print("Obese Class I (Moderately obese)")
   elif (35 <= v[0] < 40):
      print("Obese Class II (Severely obese)")
   elif (40 <= v[0]):
      print("Obese Class III (Very Severely Obese)")
   else:
      print("Done")

print()
print('\t'*2,'*'*10,'SUMMARY','*'*10)
print()

print("Names with lower BMI:",[l for l in bmi.keys() if (bmi[l][0]<18.5)])
print("Names with Normal BMI:",[l for l in bmi.keys() if (18.5<=bmi[l][0]<=25)])
print("Names with higher BMI:",[l for l in bmi.keys() if (bmi[l][0]>25)])

print() 

print("The number of individuals with lower BMI :",len([l for l in bmi.keys() if (bmi[l][0]<18.5)]))
print("The number of individuals with normal BMI :",len([l for l in bmi.keys() if (18.5<=bmi[l][0]<=25)]))
print("The number of individuals with higher BMI :",len([l for l in bmi.keys() if (bmi[l][0]>25)]))

print() 

print("The lowest BMI is:",min([bmi[l][0] for l in bmi.keys()]))
print("The Average BMI is:",sum([bmi[l][0] for l in bmi.keys()])/len([l for l in bmi.keys()]))
print("The maximun BMI is:",max([bmi[l][0] for l in bmi.keys()]))




Example:

% python funcbmi.py

Enter a name:Allen
Enter weight in kgs:78
Height in feet:5.6
Do you want to add more names(input y/n):y
Enter a name:Helen
Enter weight in kgs:45
Height in feet:5.
Do you want to add more names(input y/n):y
Enter a name:Alice
Enter weight in kgs:85
Height in feet:5.8
Do you want to add more names(input y/n):y
Enter a name:Steve
Enter weight in kgs:40
Height in feet:4
Do you want to add more names(input y/n):n

The BMI of Allen is 26.772481266050967 and falls under category of: Overweight
The BMI of Helen is 19.375038750077497 and falls under category of: Normal (Healthy Weight)
The BMI of Alice is 27.19775224198354 and falls under category of: Overweight
The BMI of Steve is 26.909776041774304 and falls under category of: Overweight

                 ********** SUMMARY **********

Names with lower BMI: []
Names with Normal BMI: ['Helen']
Names with higher BMI: ['Allen', 'Alice', 'Steve']

The number of individuals with lower BMI : 0
The number of individuals with normal BMI : 1
The number of individuals with higher BMI : 3

The lowest BMI is: 19.375038750077497
The Average BMI is: 25.063762074971578
The maximun BMI is: 27.19775224198354

Thursday, March 22, 2018

Check commit status

This summary is not available. Please click here to view the post.

Get Release Notes

#!/bin/sh -x

export SVNROOT=svn+ssh://svn.xxx.com/svn

if [ $1 == "--help" ]
then
   echo -e "This script fetches the NEWS file updates between two release tags \n";
   echo -e "Checkout the release workspace with make-toad-x.y.x-workspace.sh script";
   echo -e "Execute release_notes.sh script in the directory";
   echo -e "Usage: release_notes.sh TAG1 TAG2 \n";
   echo -e "Example:\n mkdir temp_dir1 \n cd temp_dir1 \n ./make-toad-3.12.2-workspace.sh\n release_notes.sh  3.12.2.6 3.12.2.0 \n NEWS_update.log has the NEWS file updates \n";
   exit;
fi

if [ $# == 0 ]  || [ $# -ne 2 ]
then
   echo -e "Please input the valid release tags as arguments.";
   exit;
fi

build_version1=`echo $1 | awk -F. '{print $4}' `;
build_version2=`echo $2 | awk -F. '{print $4}' `;

if [ -f NEWS_update.log ]
then
   rm -f NEWS_update.log
fi

for i in `find . -maxdepth 1 -type d | sed 's/\.\///g' | sed 's/\.//g'`
do
  cd $i;
  rel_branch=`svn info | head -2 | tail -1 | awk -F/ '{print $8}' | sed -e 's/VERSION_//g' | sed -s 's/_/./g'`;
  TAG1=$rel_branch.$build_version1;
  TAG2=$rel_branch.$build_version2;
  echo -e "$i:" >> ../NEWS_update.log;
  echo -e "---------------- \n" >> ../NEWS_update.log;
  sed -n '/Shipped in '$TAG1'/,/Shipped in '$TAG2'/{ /Shipped in '$TAG1'/d; /Shipped in '$TAG2'/d; p; }' NEWS >> ../NEWS_update.log;
  echo -e "\n" >> ../NEWS_update.log;
  cd ../;
done
echo -e "NEWS_update.log has release notes";

Wednesday, July 20, 2016

Difference between awk and cut command?

cut:  takes a single character in -d as the field delimiter (the default being TAB), and every single occurrence of that character starts a new field. 



awk:  however, is more flexible. The separator is in the FS variable and can be an empty string (every input character makes a separate field), a single character, or a regular expression. The special case of a single space character (the default) means to split on any sequence of whitespace. Also, awk suppresses leading whitespace by default.

Examples: 

% echo "All is well" | cut -f 2 -d ' '
is

%echo "All  is  well" | cut -f 2 -d ' '


% echo " All is well" | cut -f 2 -d ' '
All


% echo "All is well" | awk '{print $2}'
is

% echo "All  is  well" |  awk '{print $2}'
is

% echo " All is well" | awk '{print $2}'
is





How to check if a port is occupied or not?


Monday, July 18, 2016

Difference between the perl functions: chop and chomp?

chomp: Deletes only new line character "$/   (or) \n" (input record separator or new line character) at                the end of the line.


chop: Deletes the last character irrespective what it is.

Tuesday, May 12, 2015

Write a program to compare two files A and B and report the following: lines present in both files A and B = A U B, lines common to both A and B = A & B, lines present in A but not B = A - B, lines present in B but not in A = B - A.

#!/usr/local/bin/perl

##Script to compare 2 NEWS files
## Generates 3 files
## FILE_1_only: This file contains lines present only in first file
## FILE_2_only: This file contains lines present only in second file
## FILE1_2: This file contains lines common to both files

use strict;
use warnings;

my %hash1;
my %hash2;

##Use Tie module to process the hash in the insertion order ###
use Tie::IxHash;
tie %hash1, "Tie::IxHash";
tie %hash2, "Tie::IxHash";

###trim any leading or trailing spaces####
#sub rtrim { my $s = shift; $s =~ s/\s+$//; return $s };
sub trim { my $s = shift; $s =~ s/^\s+//; $s =~ s/\s+$//; return $s };

if(@ARGV != 2) {
        print "Please pass the files to be compared as command line arguments \n";
        print "Usage: $0 file1 file2 \n";
        print "Ex: $0 NEWS_5_0_1 NEWS_3_15_0 \n";
        print "O/P: FILE_1_only, FILE_2_only and FILE1_2 \n";
}

open (FH1, "<$ARGV[0]") or die "Couldnot open file $ARGV[0], $!";
open (FH2, "<$ARGV[1]") or die "Couldnot open file $ARGV[1], $!";
open (FH3, ">FILE1_2");
open (FH4, ">FILE_1_only");
open (FH5, ">FILE_2_only");

my @arr1=<FH1>;
my @arr2=<FH2>;

foreach my $l (@arr1) {
      chomp($l);
      $hash1{trim($l)}++;
}


foreach my $m (@arr2) {
      chomp($m);
      $hash2{trim($m)}++;
}

foreach my $k1 (keys %hash1) {
        if($hash2{$k1}) {
                print FH3 $k1;
                print FH3 "\n";
        } else {
                print FH4 $k1;
                print FH4 "\n";
        }
}

foreach my $k2 (keys %hash2) {
        if (!$hash1{$k2}) {
                print FH5 $k2;
                print FH5 "\n";
        }
}




Usage: ./compare_NEWS.pl NEWS_5.0.1 NEWS_3.15.0

O/P:
FILE_1_only: This file contains lines present only in first file
FILE_2_only: This file contains lines present only in second file
FILE1_2: This file contains lines common to both files