Sangram keshari's Oracle Blog

The Fusion Middleware Administration & Database Administration Blog

Python : HackerRank: Find the Runner-Up Score!

Posted by SangramKeshari on October 17, 2019

https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list/problem

def bubbleSort(arr):
    n=len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] < arr[j+1]:
                swap= arr[j]
                arr[j]=arr[j+1]
                arr[j+1]=swap

secondHighest=0
no = int(input())
arr = list(map(int, input().split()))            
bubbleSort(arr)
#print(arr)
#print ("Sorted array is:")
#for i in range(len(arr)):
#    print ("%d" %arr[i]),

#print("|", len(arr))
#print ("%d" %arr[len(arr)])

n=len(arr)
for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                secondHighest=arr[j+1]
                break

print(secondHighest)

Posted in Uncategorized | Leave a Comment »

BASH : HackerRank : Read in an Array

Posted by SangramKeshari on October 4, 2019

Read in an Array

Given a list of countries, each on a new line, your task is to read them into an array and then display the entire array, with a space between each of the countries’ names.

Recommended References
Here’s a great tutorial tutorial with useful examples related to arrays in Bash.

Input Format

A list of country names. The only characters present in the country names will be upper or lower case characters and hyphens.

Output Format

Display the entire array of country names, with a space between each of them.

Sample Input

Namibia
Nauru
Nepal
Netherlands
NewZealand
Nicaragua
Niger
Nigeria
NorthKorea
Norway

Sample Output

Namibia Nauru Nepal Netherlands NewZealand Nicaragua Niger Nigeria NorthKorea Norway

#!/bin/bash

i=0
n=10

#read a[@]
while [[ $i -lt $n ]]
do
    read a[$i]

#   i=`expr $i + 1`

((i++))
done

echo ${a[*]}

A more descriptive way

=====================

#!/bin/bash

#echo -n "Enter the Total numbers :"
read n
#echo "Enter numbers :"
i=0
while [ $i -lt $n ]
do
    read a[$i]

#   i=`expr $i + 1`

((i++))
done

i=0

<<COMMENT1

while [ $i -lt $n ]
do
    echo ${a[$i]}

    # To increment index
    # by 1, i=i+1
    i=`expr $i + 1`
done

echo "==========================================="

for i in "${a[@]}"
do
    echo "$i"
    #echo
done

echo "==========================================="

COMMENT1


echo ${a[*]}

Posted in Uncategorized | Leave a Comment »

BASH : HackerRank : More on Conditionals

Posted by SangramKeshari on September 25, 2019

Given three integers (, , and ) representing the three sides of a triangle, identify whether the triangle is Scalene, Isosceles, or Equilateral.

Input Format

Three integers, each on a new line.

Constraints


Sum of any two sides will be greater than the third.

Output Format

One word: either "SCALENE" or "EQUILATERAL" or "ISOSCELES" (quotation marks excluded).

Sample Input

Sample Input 1

2
3
4

Sample Input 2

6
6
6 

Sample Output

Sample Output 1

SCALENE

Sample Output 2

EQUILATERAL  
#!/bin/bash
# By Sangram Keshari Dash
########################################################
read x
read y
read z

if [[ $x -eq $y && $x -eq $z  ]];then 
echo EQUILATERAL
elif [[ $x -eq $y || $x -eq $z || $y -eq $z ]];then 
echo ISOSCELES
else
echo SCALENE
fi

Posted in Uncategorized | Leave a Comment »

BASH : HackerRank : Getting started with conditionals

Posted by SangramKeshari on September 25, 2019

Read in one character from the user (this may be ‘Y’, ‘y’, ‘N’, ‘n’). If the character is ‘Y’ or ‘y’ display “YES”. If the character is ‘N’ or ‘n’ display “NO”. No other character will be provided as input.

Input Format

One character (this may be ‘Y’, ‘y’, ‘N’, ‘n’).

Constraints

Output Format

One word: either “YES” or “NO” (quotation marks excluded).

Sample Input

Sample Input 1

y  

Sample Output

Sample Output 1

YES

#!/bin/bash
# By Sangram Keshari Dash
########################################################
read c
case $c in

'y')

echo "YES"
;;

'Y')
echo "YES"
;;

'n')
echo "NO"
;;

'N')
echo "NO"
;;

*)

echo "ERROR"
;;
esac

Posted in Uncategorized | Leave a Comment »

BASH : Char Check

Posted by SangramKeshari on September 24, 2019

#!/bin/bash

read -p " Enter a single Charcter : " chr
echo "chr =: " $chr

if [[ $chr == [A-Z] ]]; then

        echo " MATCH FOUND [A-Z]"

elif [[ $chr == [a-z]  ]]; then

        echo "  MATCH FOUND [a-z]"

elif [[ $chr == [a-z]  ]]; then

        echo "  MATCH FOUND [a-z]"

elif [[ $chr == [a-z]  ]]; then

        echo "  MATCH FOUND [a-z]"


else
        echo "  MATCH NOT FOUND [A-Z] or [a-z]"
fi


if [[ $chr == [AEIOUaeiou] ]]; then
echo " VOWEL"
fi

if [[ $chr > 96 && $chr < 123 ]]; then

      echo      " ASCII MATCH FOUND [a-z]"
fi

if [[ $chr > 96 && $chr < 123 ]]; then

      echo      " ASCII MATCH FOUND [a-z]"
fi


if [[ $chr > 32 && $chr < 123 ]]; then

      echo      " ASCII MATCH FOUND [a-z]"
fi



if [[ $chr > 96 && $chr < 123 ]]; then

      echo      " ASCII MATCH FOUND [a-z]"
fi

Posted in Uncategorized | Leave a Comment »

BASH : Factorial

Posted by SangramKeshari on September 24, 2019

BASH IDE: https://repl.it/languages/bash


1> 


#!/bin/bash 
# By Sangram Keshari Dash

#function
factorial() {
  n=$1
  #echo "::::"$n
 fact=1
 for i in `seq 1 $n`;do
let fact=fact*i
done
}

#main
read -p "Enter Number to find Factorial:" num 

if [[ $num -le 0 ]]; then
echo "ERROR: -ve NOT DEFINED"
exit 
fi

factorial $num
echo "factorial of $num is $fact"
2>


#!/bin/bash 
# By Sangram Keshari Dash
#using while loop

#function
factorial() {
  n=$1
  #echo "::::"$n
 fact=1
#for i in `seq 1 $n`;do
#let fact=fact*i
#done


while [[ $n > 0 ]];
do
let fact=fact*n
((n--))
done
}


#main
read -p "Enter Number to find Factorial:" num 

if [[ $num -le 0 ]]; then
echo "ERROR: -ve NOT DEFINED"
exit 
fi

factorial $num
echo "factorial of $num is $fact"

Posted in Uncategorized | Leave a Comment »

BASH : CASE Statement 1

Posted by SangramKeshari on September 24, 2019

#!/bin/bash

read -p " Enter char:" c
case $c in

[A-Z]*)
 echo " char [A-Z]"
  ;;
[a-z]*)

echo " char [a-z]"
  ;;

[0-9]*)
 echo " [0-9] Number"
  ;;

*)
  echo "OTHER char"
  ;;
esac

Posted in Uncategorized | Leave a Comment »

BASH : Reverse a String or Number

Posted by SangramKeshari on September 24, 2019

#!/bin/bash 
# By Sangram Keshari Dash

read -p "Enter string or number:" string 
len=${#string} 

#echo $len
i=$len
#echo $i

while  [[ $len -ge 0 ]];
do

#echo ${string:$len:1}
onechar=${string:$len:1}
#echo $onechar
rev=$rev$onechar
#echo "rev is " $rev

((len--))

done 

echo "Reverse of $string is " $rev

Posted in Uncategorized | Leave a Comment »

BASH : Reverse a Number and Pallindrome check

Posted by SangramKeshari on September 23, 2019

#!/bin/bash


read -p "Enter number  :" num

#num=12345
orignum=$num

echo $num

len=${#num}
echo "$len"
m=0
revnum=0

while [ $len -gt 0 ];
do

m=`expr $num % 10`
#evnum=`expr 10 \\* $m`

let revnum=revnum*10+m
let num=num/10

echo "m=:" $m "revnum=:" $revnum

((len--))
done

echo "Original Number"
echo "Reverse Number"

if [ $orignum -eq $revnum ]; then
echo "This is Palindrome"
else
echo "Not Palindrome"
fi

Posted in Uncategorized | Leave a Comment »

BASH : While Loop

Posted by SangramKeshari on September 23, 2019

c=10
while [ $c -gt 0 ];
do
echo "c=:" $c
#c=`expr $c - 1`
((c--))
done

BASH IDE: https://repl.it/languages/bash

Posted in Uncategorized | Leave a Comment »