Thursday 31 December 2020

Lucky alive person in a circle sword puzzle - Python Datastructures Algorithm

Hi,The program I provided is an implementation of the famous Josephus problem, which is a theoretical problem named after the Jewish historian Josephus Flavius. The problem involves a circle of people numbered from 1 to n, and a count to skip, k. Starting from person 1, every k-th person is eliminated, and the process continues until only one person is left, who is declared the winner.

The program I provided implements a solution to this problem in Python. It uses a list to represent the circle of people, and iteratively removes people from the list until only one person is left. The program uses the modulo operator to ensure that the index of the person to remove wraps around to the beginning of the list if it goes past the end.

The program prompts the user to input the number of people in the circle and the count to skip, and then outputs the number of the lucky alive person who wins the game. This program can be used to play the Josephus game for any number of people and count to skip

Read more »

Labels:

Thursday 21 October 2021

write 1000 uniq perl interview questions and answers to master in perl part 2

 251. Q: In Perl, how can you find the position of a substring within a string?

1A: In Perl, you can find the position of a substring within a string by using the `index` function. Here is an example: 2 3```perl 4use strict; 5use warnings; 6 7my $string = 'Hello, World! World is beautiful.'; 8my $substring = 'World'; 9 10my $position = index $string, $substring; 11 12if ($position == -1) { 13 print "The substring was not found in the string.\n"; 14} else { 15 print "The substring was found at position: $position\n"; 16} 17``` 18 19In this example, the script finds the position of the substring `'World'` within the string `'Hello, World! World is beautiful.'` and prints the resulting position.

Read more »

Labels:

Tuesday 16 January 2024

Login System using perl catalyst MVC



1.install the required Below dependencies:

cpanm --installdeps .

Read more »

Labels:

Friday 18 June 2021

Top 5000 python programs examples with solutions - Part 3

Program 21:

How to write  a program to calculate negative numbers in a list?

list=[1,2,3,4,5,-1,-2,-3,-4,-8]
if 0 not in list:
    list.append(0)
list=sorted(list)
pos=list.index(0)
print(pos)

Read more »

Labels:

Friday 19 March 2021

perl data structures tutorial - part1

Perl provides a rich set of data structures that can be used to store and manipulate data. In this tutorial, I'll cover the most commonly used data structures in Perl, along with code examples.

Arrays

Arrays are ordered lists of scalar values. To declare an array, you use the @ symbol followed by the array name. Here's an example:

@my_array = (1, 2, 3, 4, 5);


You can access individual elements of the array using the array name followed by the index of the element in square brackets. Here's an example:

print $my_array[0]; # prints 1


You can also use the scalar function to get the number of elements in an array:

print scalar(@my_array); # prints 5


Read more »

Labels:

Wednesday 6 November 2019

perl - array and hash common differences

     S.No
                             ARRAYS
                           HASHES

         1.

         2.


         3.

          
          4.

     
          


          5.

          



         6.





           7.


           
         8.


















          

        9.






      10.









      11.




     12.



      13.






       14.






       15.

It is ordered data collection

Data Can be accessed based on index value

Stack operations performed using push,pop,shift,unshift

Duplicate Elements will be exist in Array




For Large data, Arrays will not be in speed as getting values from hash keys.


Arrays can be created using () brackets @array=(1,3,5,6,2);
For More methods refer this link.

for accessing array index
$array[1];


Array can be accessed in loops as below

1.For(@array)
{
}

2.foreach(@array1,@array2)
   {
       Print $_;
   }

3.print grep{},@array;

4. print map{},@array;







Array slices can be used as below
@array[2];





To find length of an array


 $#array+1;
       or
scalar(@array);
      or
$a=@array;


For Emptying Array
$#array = -1;
     or
@array = ( );

To reverse an array
print reverse @array;


To sort array Elements
ascending order
@a=sort{$a <=> $b }@array;
desending order
@a=sort{$b <=> $a }@array;


To add array elements
$array[0]='55';




To Delete array elements
delete $array[1];
 
It is unordered Data Collection

Data Can be accessed based on Key value

Stack operations cannot be performed


Keys will not have Duplicate elements but Values would have Duplicate elements.

Based on hash Keys Data retrival would be fast


Hash is also possible to create using () bracket %hash=(1,3,5,6,2);
For More methods refer this link.
https://kaavannan-perl.blogspot.com/2019/10/perl-hashes-what-is.html


For accessing hash index use the key name
$hash{1};


Hash can be accessed based on keys and values


While(($k,$vals) = each %h)
{
    Print “$k,$vals\n”;
}

Foreach $k (keys %h)
{
Print “$k => $hash{$k}\n”;
}

Foreach $values (values %hash)
{
Print “$values\n”;
}



Hash slices can be used as below,
$hash{'1'}







To find length of hash

@array=keys %hash;
scalar(@array);
     or
$#array+1;
     or
$a=@array;



For Emptying hash
%hash=( );




To reverse a hash
print reverse %hash;



To sort hash elements

 foreach $k (sort keys %hash)
{
   print "$k => $hash{$k}\n";
}



To add hash Elements
$hash{1}='one';





To Delete hash elements
delete($hash{'1'});


Labels:

Wednesday 15 September 2021

Top 5000 python programs examples with solutions - Part 7

Program 71:

Write a program to form below phrases? using print() function.

I miss you in the morning;

    I miss you late at night.

        Just to think about you

        Is my joy and my delight.

I miss you in the morning;

    I miss you late at night.

print("I miss you in the morning; \n\tI miss you late at night. \n\t\tJust to think about you \n\t\tIs my joy and my delight. \nI miss you in the morning; \n\tI miss you late at night.")

Read more »

Labels:

Thursday 31 October 2019

Perl In Line Hack - Perform Variable Assigning and Deletion in one Line

Method 1:


($_ = 'hellio') =~ s/i//;

print $_;


Method 2:


s/i// for (my $_ = 'hellio');

print $_;


Method 3:


substr(($_ = 'helloi'), index ($_, "i"), length($_),"")

print $_;


Method 4:


map $_ =~ s/i//,$_='hellio';

print $_;


Method 5:


grep $_ =~ s/i//,$_='hellio';

print $_;


Method 6:


print grep $_=~/[^i]+/,split('',$_='hellio');



Method 7:


map $_=~tr/i//d,$_="hellio";

print $_;


Method 8:


$_=~ s/i// if $_='hellio';

print $_;


Method 9:


$_ =~ tr/i//d if $_ = 'hellio';

print $_;


Method 10:


print grep $_=~/[^i]+/, @_, split '', 'hellio';


Method 11:


print grep $_=~/[^i]+/, "hellio"=~ /./g;


Method 12:


($_='hellio')=~tr/i//d;

print $_;


Method 13:


$_="hellio",$_=~tr/i//d;

print $_;


Method 14:


print @_,grep $_=~/[^i]+/, (split '', 'hellio');


Method 15:


grep $_=~tr/i//d,$_="hellio";

print $_;


kaavannan perl blogspot

Labels: , , , ,